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