001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.web.service.impl;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.krad.uif.UifConstants;
020import org.kuali.rice.krad.uif.UifParameters;
021import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
022import org.kuali.rice.krad.uif.service.ViewHelperService;
023import org.kuali.rice.krad.web.form.UifFormBase;
024import org.kuali.rice.krad.web.service.CollectionControllerService;
025import org.kuali.rice.krad.web.service.ModelAndViewService;
026import org.springframework.web.servlet.ModelAndView;
027
028import javax.servlet.http.HttpServletRequest;
029import java.util.Map;
030
031/**
032 * Default implementation of the collection controller service.
033 *
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 */
036public class CollectionControllerServiceImpl implements CollectionControllerService {
037
038    private ModelAndViewService modelAndViewService;
039
040    /**
041     * {@inheritDoc}
042     */
043    @Override
044    public ModelAndView addLine(final UifFormBase form) {
045        final CollectionActionParameters parameters = new CollectionActionParameters(form, false);
046
047        Runnable runnable = new Runnable() {
048            @Override
049            public void run() {
050                ViewLifecycle.getHelper().processCollectionAddLine(form, parameters.selectedCollectionId,
051                        parameters.selectedCollectionPath);
052            }
053        };
054
055        return performHelperLifecycle(form, runnable);
056    }
057
058    /**
059     * {@inheritDoc}
060     */
061    @Override
062    public ModelAndView addBlankLine(final UifFormBase form) {
063        final CollectionActionParameters parameters = new CollectionActionParameters(form, false);
064
065        Runnable runnable = new Runnable() {
066            @Override
067            public void run() {
068                ViewLifecycle.getHelper().processCollectionAddBlankLine(form, parameters.selectedCollectionId,
069                        parameters.selectedCollectionPath);
070            }
071        };
072
073        return performHelperLifecycle(form, runnable);
074    }
075
076    /**
077     * {@inheritDoc}
078     */
079    @Override
080    public ModelAndView retrieveEditLineDialog(final UifFormBase form) {
081        final CollectionActionParameters parameters = new CollectionActionParameters(form, true);
082
083        Runnable runnable = new Runnable() {
084            @Override
085            public void run() {
086                final ViewHelperService viewHelperService = ViewLifecycle.getHelper();
087                viewHelperService.processCollectionRetrieveEditLineDialog(form, parameters.selectedCollectionId,
088                        parameters.selectedCollectionPath, parameters.selectedLineIndex);
089            }
090        };
091
092        return performHelperLifecycle(form, runnable);
093    }
094
095    /**
096     * {@inheritDoc}
097     */
098    @Override
099    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}