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.module.admininterface.dialogs;
35
36 import info.magnolia.cms.beans.runtime.MultipartForm;
37 import info.magnolia.cms.core.Content;
38 import info.magnolia.cms.core.ItemType;
39 import info.magnolia.module.admininterface.DialogHandlerManager;
40 import info.magnolia.module.admininterface.DialogMVCHandler;
41 import info.magnolia.module.admininterface.SaveHandler;
42 import info.magnolia.objectfactory.Components;
43 import info.magnolia.registry.RegistrationException;
44 import info.magnolia.rendering.template.TemplateDefinition;
45 import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
46
47 import java.io.IOException;
48
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
51
52 import org.apache.commons.lang.StringUtils;
53
54
55
56
57
58
59 public class ParagraphEditDialog extends ConfiguredDialog {
60
61 private DialogMVCHandler dialogHandler;
62
63 public ParagraphEditDialog(String name, HttpServletRequest request, HttpServletResponse response, Content configNode) {
64 super(name, request, response, configNode);
65
66
67 String paragraphName = params.getParameter("mgnlParagraph");
68
69 final String dialogName = getDialogUsedByTemplate(paragraphName);
70
71 if (StringUtils.isEmpty(dialogName)) {
72 dialogHandler = new NoDialogMVCHandler(request, response);
73 } else {
74
75 dialogHandler = DialogHandlerManager.getInstance().getDialogHandler(dialogName, request, response);
76
77
78 dialogHandler.getDialog().setConfig("paragraph", paragraphName);
79 dialogHandler.getDialog().setConfig("collectionNodeCreationItemType", ItemType.AREA.getSystemName());
80 dialogHandler.getDialog().setConfig("creationItemType", ItemType.COMPONENT.getSystemName());
81 }
82 }
83
84 private String getDialogUsedByTemplate(String templateId) {
85 if (StringUtils.isEmpty(templateId)) {
86 throw new IllegalStateException("No paragraph selected.");
87 }
88 TemplateDefinition templateDefinition;
89 try {
90 templateDefinition = Components.getComponent(TemplateDefinitionRegistry.class).getTemplateDefinition(templateId);
91 } catch (RegistrationException e) {
92 throw new IllegalStateException(e);
93 }
94 if (templateDefinition == null) {
95 throw new IllegalStateException("No template registered with name " + templateId);
96 }
97 return templateDefinition.getDialog();
98 }
99
100
101
102
103 @Override
104 public String getCommand() {
105 return dialogHandler.getCommand();
106 }
107
108 @Override
109 public String execute(String command) {
110 return dialogHandler.execute(command);
111 }
112
113 @Override
114 public void renderHtml(String view) throws IOException {
115 dialogHandler.renderHtml(view);
116 }
117
118 public static class NoDialogMVCHandler extends DialogMVCHandler {
119 public NoDialogMVCHandler(HttpServletRequest request, HttpServletResponse response) {
120
121 super("auto-save", request, response);
122
123 }
124
125 @Override
126 public String getCommand() {
127
128 return DialogMVCHandler.COMMAND_SAVE;
129 }
130
131 @Override
132 public String save() {
133
134 final SaveHandler saveHandler = Components.getComponentProvider().newInstance(SaveHandler.class);
135 final MultipartForm dummyForm = new MultipartForm();
136 dummyForm.addparameterValues("mgnlSaveInfo", new String[0]);
137 saveHandler.init(dummyForm);
138
139
140 saveHandler.setPath(path);
141 saveHandler.setNodeCollectionName(params.getParameter("mgnlNodeCollection"));
142 saveHandler.setNodeName(nodeName);
143 saveHandler.setParagraph(params.getParameter("mgnlParagraph"));
144 saveHandler.setRepository(repository);
145 saveHandler.setCollectionNodeCreationItemType(ItemType.AREA);
146 saveHandler.setCreationItemType(ItemType.COMPONENT);
147
148
149 boolean result = saveHandler.save();
150 return result ? DialogMVCHandler.VIEW_CLOSE_WINDOW : DialogMVCHandler.VIEW_ERROR;
151 }
152
153 }
154 }