1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.pages.app.action;
35
36 import info.magnolia.context.MgnlContext;
37 import info.magnolia.event.EventBus;
38 import info.magnolia.i18nsystem.SimpleTranslator;
39 import info.magnolia.jcr.util.NodeTypes;
40 import info.magnolia.objectfactory.ComponentProvider;
41 import info.magnolia.pages.app.field.TemplateSelectorFieldFactory;
42 import info.magnolia.registry.RegistrationException;
43 import info.magnolia.rendering.template.TemplateDefinition;
44 import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
45 import info.magnolia.ui.admincentral.dialog.action.CancelDialogActionDefinition;
46 import info.magnolia.ui.api.ModelConstants;
47 import info.magnolia.ui.api.action.AbstractAction;
48 import info.magnolia.ui.api.action.ActionExecutionException;
49 import info.magnolia.ui.api.app.SubAppContext;
50 import info.magnolia.ui.api.app.SubAppEventBus;
51 import info.magnolia.ui.api.event.ContentChangedEvent;
52 import info.magnolia.ui.dialog.DialogView;
53 import info.magnolia.ui.dialog.action.CallbackDialogActionDefinition;
54 import info.magnolia.ui.dialog.definition.ConfiguredFormDialogDefinition;
55 import info.magnolia.ui.dialog.definition.FormDialogDefinition;
56 import info.magnolia.ui.dialog.formdialog.FormDialogPresenter;
57 import info.magnolia.ui.dialog.formdialog.FormDialogPresenterFactory;
58 import info.magnolia.ui.form.EditorCallback;
59 import info.magnolia.ui.form.definition.ConfiguredFormDefinition;
60 import info.magnolia.ui.form.definition.ConfiguredTabDefinition;
61 import info.magnolia.ui.form.field.definition.SelectFieldDefinition;
62 import info.magnolia.ui.form.field.definition.SelectFieldOptionDefinition;
63 import info.magnolia.ui.vaadin.gwt.client.shared.AreaElement;
64 import info.magnolia.ui.vaadin.integration.jcr.DefaultProperty;
65 import info.magnolia.ui.vaadin.integration.jcr.JcrNewNodeAdapter;
66 import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
67
68 import javax.inject.Inject;
69 import javax.inject.Named;
70 import javax.jcr.Node;
71 import javax.jcr.RepositoryException;
72 import javax.jcr.Session;
73
74 import org.apache.commons.lang.StringUtils;
75 import org.slf4j.Logger;
76 import org.slf4j.LoggerFactory;
77
78
79
80
81
82 public class CreateComponentAction extends AbstractAction<CreateComponentActionDefinition> {
83
84 private static final Logger log = LoggerFactory.getLogger(CreateComponentAction.class);
85
86 private AreaElement area;
87 private EventBus eventBus;
88 private TemplateDefinitionRegistry templateDefinitionRegistry;
89 private SubAppContext subAppContext;
90 private ComponentProvider componentProvider;
91 private DialogView dialogView;
92 private FormDialogPresenterFactory formDialogPresenterFactory;
93 private final SimpleTranslator i18n;
94
95 @Inject
96 public CreateComponentAction(CreateComponentActionDefinition definition, AreaElement area, @Named(SubAppEventBus.NAME) EventBus eventBus, TemplateDefinitionRegistry templateDefinitionRegistry,
97 SubAppContext subAppContext, ComponentProvider componentProvider, FormDialogPresenterFactory formDialogPresenterFactory, SimpleTranslator i18n) {
98 super(definition);
99 this.area = area;
100 this.eventBus = eventBus;
101 this.templateDefinitionRegistry = templateDefinitionRegistry;
102 this.subAppContext = subAppContext;
103 this.componentProvider = componentProvider;
104 this.formDialogPresenterFactory = formDialogPresenterFactory;
105 this.i18n = i18n;
106 }
107
108 @Override
109 public void execute() throws ActionExecutionException {
110 final FormDialogDefinition dialogDefinition = buildNewComponentDialog(area.getAvailableComponents());
111
112 final FormDialogPresenter formDialogPresenter = componentProvider.newInstance(FormDialogPresenter.class);
113 try {
114 String workspace = area.getWorkspace();
115 String path = area.getPath();
116 Session session = MgnlContext.getJCRSession(area.getWorkspace());
117 if (path == null || !session.itemExists(path)) {
118 path = "/";
119 }
120 session = MgnlContext.getJCRSession(workspace);
121
122 Node areaNode = session.getNode(path);
123
124 final JcrNodeAdapter item = new JcrNewNodeAdapter(areaNode, NodeTypes.Component.NAME);
125 DefaultProperty<String> property = new DefaultProperty<String>(String.class, "0");
126 item.addItemProperty(ModelConstants.JCR_NAME, property);
127
128
129 this.dialogView = formDialogPresenter.start(item, dialogDefinition, subAppContext, new ComponentCreationCallback(item, formDialogPresenter));
130 } catch (RepositoryException e) {
131 throw new ActionExecutionException(e);
132 }
133 }
134
135 private void openDialog(final JcrNodeAdapter item, String dialogId) {
136
137 final FormDialogPresenter dialogPresenter = formDialogPresenterFactory.createFormDialogPresenter(dialogId);
138
139 dialogPresenter.start(item, dialogId, subAppContext, new EditorCallback() {
140
141 @Override
142 public void onSuccess(String actionName) {
143 eventBus.fireEvent(new ContentChangedEvent(item.getWorkspace(), item.getItemId()));
144 dialogPresenter.closeDialog();
145 }
146
147 @Override
148 public void onCancel() {
149 dialogPresenter.closeDialog();
150 }
151 });
152 }
153
154
155
156
157
158 private FormDialogDefinition buildNewComponentDialog(String availableComponents) {
159
160 ConfiguredFormDefinition form = new ConfiguredFormDefinition();
161
162 ConfiguredTabDefinition tab = new ConfiguredTabDefinition();
163 tab.setName("components");
164
165 SelectFieldDefinition select = new SelectFieldDefinition();
166 select.setName("mgnl:template");
167
168 tab.addField(select);
169
170 form.addTab(tab);
171
172 String[] tokens = availableComponents.split(",");
173
174 for (String token : tokens) {
175 try {
176 TemplateDefinition templateDefinition = templateDefinitionRegistry.getTemplateDefinition(token);
177
178 SelectFieldOptionDefinition option = new SelectFieldOptionDefinition();
179 option.setValue(templateDefinition.getId());
180 option.setLabel(TemplateSelectorFieldFactory.getI18nTitle(templateDefinition));
181 select.addOption(option);
182
183 } catch (RegistrationException e) {
184 log.error("Exception caught: {}", e.getMessage(), e);
185 }
186 }
187
188 ConfiguredFormDialogDefinition dialog = new ConfiguredFormDialogDefinition();
189 dialog.setId("pages:newComponent");
190 dialog.setForm(form);
191
192 CallbackDialogActionDefinition callbackAction = new CallbackDialogActionDefinition();
193 callbackAction.setName("commit");
194 dialog.getActions().put(callbackAction.getName(), callbackAction);
195
196 CancelDialogActionDefinition cancelAction = new CancelDialogActionDefinition();
197 cancelAction.setName("cancel");
198 dialog.getActions().put(cancelAction.getName(), cancelAction);
199
200 return dialog;
201 }
202
203 private class ComponentCreationCallback implements EditorCallback {
204
205 private final JcrNodeAdapter item;
206 private final FormDialogPresenter formDialogPresenter;
207
208 public ComponentCreationCallback(JcrNodeAdapter item, FormDialogPresenter formDialogPresenter) {
209 this.item = item;
210 this.formDialogPresenter = formDialogPresenter;
211 }
212
213 @Override
214 public void onSuccess(String actionName) {
215 String templateId = String.valueOf(item.getItemProperty(NodeTypes.Renderable.TEMPLATE).getValue());
216 try {
217 TemplateDefinition templateDef = templateDefinitionRegistry.getTemplateDefinition(templateId);
218 String dialogId = templateDef.getDialog();
219
220 if (StringUtils.isNotEmpty(dialogId)) {
221 openDialog(item, dialogId);
222 } else {
223
224 try {
225 final Node node = item.applyChanges();
226 node.getSession().save();
227
228 } catch (RepositoryException e) {
229 log.error("Exception caught: {}", e.getMessage(), e);
230 }
231
232 eventBus.fireEvent(new ContentChangedEvent(item.getWorkspace(), item.getItemId()));
233 }
234 } catch (RegistrationException e) {
235 log.error("Exception caught: {}", e.getMessage(), e);
236 } finally {
237 dialogView.close();
238 }
239 }
240
241 @Override
242 public void onCancel() {
243 formDialogPresenter.closeDialog();
244 }
245 }
246 }