View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Default implementation of the collection controller service.
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  public class CollectionControllerServiceImpl implements CollectionControllerService {
37  
38      private ModelAndViewService modelAndViewService;
39  
40      /**
41       * {@inheritDoc}
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       * {@inheritDoc}
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       * {@inheritDoc}
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       * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * Helper method to run a {@link java.lang.Runnable} through the view lifecycle.
170      *
171      * @param form form instance containing the model data
172      * @param runnable code to run in the lifecycle
173      * @return ModelAndView instance for rendering the view
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      * {@inheritDoc}
184      */
185     @Override
186     public ModelAndView retrieveCollectionPage(UifFormBase form) {
187         form.setCollectionPagingRequest(true);
188 
189         return getModelAndViewService().getModelAndView(form);
190     }
191 
192     /**
193      * {@inheritDoc}
194      */
195     @Override
196     public ModelAndView tableJsonRetrieval(UifFormBase form) {
197         form.setCollectionPagingRequest(true);
198 
199         // set property to trigger special JSON rendering logic
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      * Helper class for maintaining collection action parameters for a request.
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          * Constructs a new CollectionActionParameters pulling the action parameter values from the give form.
225          *
226          * @param form form instance containing the action parameter values
227          * @param requireIndexParam whether to thrown an exception if the selected line value action parameter
228          * is not present (or valid)
229          * @throws java.lang.RuntimeException if selected collection path is missing, or requireIndexParam is true
230          * and selected line index is missing
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 }