1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.web.service.impl;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.uif.UifConstants;
20 import org.kuali.rice.krad.uif.UifParameters;
21 import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
22 import org.kuali.rice.krad.uif.service.ViewHelperService;
23 import org.kuali.rice.krad.web.form.UifFormBase;
24 import org.kuali.rice.krad.web.service.CollectionControllerService;
25 import org.kuali.rice.krad.web.service.ModelAndViewService;
26 import org.springframework.web.servlet.ModelAndView;
27
28 import javax.servlet.http.HttpServletRequest;
29 import java.util.Map;
30
31
32
33
34
35
36 public class CollectionControllerServiceImpl implements CollectionControllerService {
37
38 private ModelAndViewService modelAndViewService;
39
40
41
42
43 @Override
44 public ModelAndView addLine(final UifFormBase form) {
45 final CollectionActionParameters parameters = new CollectionActionParameters(form, false);
46
47 Runnable runnable = new Runnable() {
48 @Override
49 public void run() {
50 ViewLifecycle.getHelper().processCollectionAddLine(form, parameters.selectedCollectionId,
51 parameters.selectedCollectionPath);
52 }
53 };
54
55 return performHelperLifecycle(form, runnable);
56 }
57
58
59
60
61 @Override
62 public ModelAndView addBlankLine(final UifFormBase form) {
63 final CollectionActionParameters parameters = new CollectionActionParameters(form, false);
64
65 Runnable runnable = new Runnable() {
66 @Override
67 public void run() {
68 ViewLifecycle.getHelper().processCollectionAddBlankLine(form, parameters.selectedCollectionId,
69 parameters.selectedCollectionPath);
70 }
71 };
72
73 return performHelperLifecycle(form, runnable);
74 }
75
76
77
78
79 @Override
80 public ModelAndView retrieveEditLineDialog(final UifFormBase form) {
81 final CollectionActionParameters parameters = new CollectionActionParameters(form, true);
82
83 Runnable runnable = new Runnable() {
84 @Override
85 public void run() {
86 final ViewHelperService viewHelperService = ViewLifecycle.getHelper();
87 viewHelperService.processCollectionRetrieveEditLineDialog(form, parameters.selectedCollectionId,
88 parameters.selectedCollectionPath, parameters.selectedLineIndex);
89 }
90 };
91
92 return performHelperLifecycle(form, runnable);
93 }
94
95
96
97
98 @Override
99 public ModelAndView editLine(final UifFormBase form) {
100 final CollectionActionParameters parameters = new CollectionActionParameters(form, true);
101
102 Runnable runnable = new Runnable() {
103 @Override
104 public void run() {
105 final ViewHelperService viewHelperService = ViewLifecycle.getHelper();
106 viewHelperService.processCollectionEditLine(form, parameters);
107 }
108 };
109
110 return performHelperLifecycle(form, runnable);
111 }
112
113
114
115
116 @Override
117 public ModelAndView closeEditLineDialog(final UifFormBase form) {
118 final CollectionActionParameters parameters = new CollectionActionParameters(form, true);
119
120 Runnable runnable = new Runnable() {
121 @Override
122 public void run() {
123 final ViewHelperService viewHelperService = ViewLifecycle.getHelper();
124 viewHelperService.processCollectionCloseEditLineDialog(form, parameters.selectedCollectionId,
125 parameters.selectedCollectionPath, parameters.selectedLineIndex);
126 }
127 };
128
129 return performHelperLifecycle(form, runnable);
130 }
131
132
133
134
135 @Override
136 public ModelAndView saveLine(final UifFormBase form) {
137 final CollectionActionParameters parameters = new CollectionActionParameters(form, true);
138
139 Runnable runnable = new Runnable() {
140 @Override
141 public void run() {
142 final ViewHelperService viewHelperService = ViewLifecycle.getHelper();
143 viewHelperService.processCollectionSaveLine(form, parameters);
144 }
145 };
146
147 return performHelperLifecycle(form, runnable);
148 }
149
150
151
152
153 @Override
154 public ModelAndView deleteLine(final UifFormBase form) {
155 final CollectionActionParameters parameters = new CollectionActionParameters(form, true);
156
157 Runnable runnable = new Runnable() {
158 @Override
159 public void run() {
160 ViewLifecycle.getHelper().processCollectionDeleteLine(form, parameters.selectedCollectionId,
161 parameters.selectedCollectionPath, parameters.selectedLineIndex);
162 }
163 };
164
165 return performHelperLifecycle(form, runnable);
166 }
167
168
169
170
171
172
173
174
175 protected ModelAndView performHelperLifecycle(final UifFormBase form, Runnable runnable) {
176 ViewLifecycle.encapsulateLifecycle(form.getView(), form, form.getViewPostMetadata(), null, form.getRequest(),
177 runnable);
178
179 return getModelAndViewService().getModelAndView(form);
180 }
181
182
183
184
185 @Override
186 public ModelAndView retrieveCollectionPage(UifFormBase form) {
187 form.setCollectionPagingRequest(true);
188
189 return getModelAndViewService().getModelAndView(form);
190 }
191
192
193
194
195 @Override
196 public ModelAndView tableJsonRetrieval(UifFormBase form) {
197 form.setCollectionPagingRequest(true);
198
199
200 form.setRequestJsonTemplate(UifConstants.TableToolsValues.JSON_TEMPLATE);
201
202 return getModelAndViewService().getModelAndView(form);
203 }
204
205 protected ModelAndViewService getModelAndViewService() {
206 return modelAndViewService;
207 }
208
209 public void setModelAndViewService(ModelAndViewService modelAndViewService) {
210 this.modelAndViewService = modelAndViewService;
211 }
212
213
214
215
216 public static class CollectionActionParameters {
217
218 private final Map<String, String[]> parameters;
219 private final String selectedCollectionPath;
220 private final String selectedCollectionId;
221 private final int selectedLineIndex;
222
223
224
225
226
227
228
229
230
231
232 public CollectionActionParameters(UifFormBase form, boolean requireIndexParam) {
233 selectedCollectionPath = form.getActionParamaterValue(UifParameters.SELECTED_COLLECTION_PATH);
234 if (StringUtils.isBlank(selectedCollectionPath)) {
235 throw new RuntimeException("Selected collection path was not set for collection action");
236 }
237
238 selectedCollectionId = form.getActionParamaterValue(UifParameters.SELECTED_COLLECTION_ID);
239
240 String selectedLine = form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
241 if (StringUtils.isNotBlank(selectedLine)) {
242 selectedLineIndex = Integer.parseInt(selectedLine);
243 } else {
244 selectedLineIndex = -1;
245 }
246
247 if (requireIndexParam && (selectedLineIndex == -1)) {
248 throw new RuntimeException("Selected line index was not set for collection action");
249 }
250
251 HttpServletRequest formRequest = form.getRequest();
252 parameters = formRequest.getParameterMap();
253 }
254
255 public Map<String, String[]> getParameters() {
256 return parameters;
257 }
258
259 public String getSelectedCollectionPath() {
260 return selectedCollectionPath;
261 }
262
263 public String getSelectedCollectionId() {
264 return selectedCollectionId;
265 }
266
267 public int getSelectedLineIndex() {
268 return selectedLineIndex;
269 }
270 }
271 }