001/**
002 * Copyright 2005-2018 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.demo.uif.controller;
017
018import org.kuali.rice.krad.demo.uif.form.KradSampleAppForm;
019import org.kuali.rice.krad.demo.uif.form.UITestObject;
020import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
021import org.kuali.rice.krad.uif.UifConstants;
022import org.kuali.rice.krad.uif.view.ViewTheme;
023import org.kuali.rice.krad.util.GlobalVariables;
024import org.kuali.rice.krad.web.controller.UifControllerBase;
025import org.kuali.rice.krad.web.form.UifFormBase;
026import org.kuali.rice.krad.web.service.FileControllerService;
027import org.springframework.beans.factory.annotation.Autowired;
028import org.springframework.beans.factory.annotation.Qualifier;
029import org.springframework.beans.propertyeditors.CustomCollectionEditor;
030import org.springframework.stereotype.Controller;
031import org.springframework.validation.BindingResult;
032import org.springframework.web.bind.WebDataBinder;
033import org.springframework.web.bind.annotation.InitBinder;
034import org.springframework.web.bind.annotation.ModelAttribute;
035import org.springframework.web.bind.annotation.RequestMapping;
036import org.springframework.web.bind.annotation.RequestMethod;
037import org.springframework.web.servlet.ModelAndView;
038
039import javax.servlet.http.HttpServletRequest;
040import javax.servlet.http.HttpServletResponse;
041import java.util.List;
042import java.util.Set;
043
044/**
045 * Basic controller for the KRAD sample application
046 *
047 * @author Kuali Rice Team (rice.collab@kuali.org)
048 */
049@Controller
050@RequestMapping(value = "/kradsampleapp")
051public class KradSampleAppController extends UifControllerBase {
052
053    @Override
054    protected KradSampleAppForm createInitialForm() {
055        return new KradSampleAppForm();
056    }
057
058    @RequestMapping(method = RequestMethod.GET, params = "methodToCall=changeTheme")
059    public ModelAndView changeTheme(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
060            HttpServletRequest request, HttpServletResponse response) {
061        changeTheme(form);
062        return getModelAndView(form);
063    }
064
065    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=validateView")
066    public ModelAndView validateView(@ModelAttribute("KualiForm") UifFormBase uiTestForm, BindingResult result,
067            HttpServletRequest request, HttpServletResponse response) {
068        KRADServiceLocatorWeb.getViewValidationService().validateView(uiTestForm);
069        return getModelAndView(uiTestForm);
070    }
071
072    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addGrowl")
073    public ModelAndView addGrowl(@ModelAttribute("KualiForm") UifFormBase uiTestForm, BindingResult result,
074            HttpServletRequest request, HttpServletResponse response) {
075        String extraInfo = (String) request.getParameter("extraInfo");
076        if (extraInfo == null) {
077            extraInfo = "none";
078        }
079        GlobalVariables.getMessageMap().addGrowlMessage("Growl Message", "demo.fakeGrowl", extraInfo);
080        return getModelAndView(uiTestForm);
081    }
082
083    private void changeTheme(UifFormBase form) {
084        String theme = ((KradSampleAppForm) form).getThemeName();
085        if (theme != null) {
086            ViewTheme newTheme = (ViewTheme) (KRADServiceLocatorWeb.getDataDictionaryService().getDictionaryBean(
087                    theme));
088            if (newTheme != null) {
089                form.getView().setTheme(newTheme);
090            }
091        }
092    }
093
094    /**
095     * Changes the view to readOnly and returns.
096     *
097     * @param uifForm
098     * @param result
099     * @param request
100     * @param response
101     * @return readOnly View
102     */
103    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=makeReadOnly")
104    public ModelAndView makeReadOnly(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
105            HttpServletRequest request, HttpServletResponse response) {
106        //set View to readOnly
107        uifForm.getView().setReadOnly(true);
108        return getModelAndView(uifForm);
109    }
110
111    /**
112     * Adds errors to fields defined in the validationMessageFields array
113     */
114    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addStandardSectionsErrors")
115    public ModelAndView addStandardSectionsErrors(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
116            HttpServletRequest request, HttpServletResponse response) {
117        GlobalVariables.getMessageMap().putError("Demo-ValidationMessages-Section1", "errorSectionTest");
118        GlobalVariables.getMessageMap().putError("Demo-ValidationMessages-Section2", "errorSectionTest");
119
120        Set<String> inputFieldIds = form.getViewPostMetadata().getInputFieldIds();
121        for (String id : inputFieldIds) {
122            if (form.getViewPostMetadata().getComponentPostData(id, UifConstants.PostMetadata.PATH) != null) {
123                String key = (String) form.getViewPostMetadata().getComponentPostData(id, UifConstants.PostMetadata.PATH);
124                GlobalVariables.getMessageMap().putError(key, "error1Test");
125            }
126        }
127
128        return getModelAndView(form);
129    }
130
131    /**
132     * Add a message and refresh the group
133     *
134     * @param form
135     * @param result
136     * @param request
137     * @param response
138     * @return
139     */
140    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=customEditLine")
141    public ModelAndView customEditLine(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
142            HttpServletRequest request, HttpServletResponse response) {
143        KradSampleAppForm kradSampleAppForm = (KradSampleAppForm) form;
144        List<UITestObject> testObjects = kradSampleAppForm.getCollection6();
145        int index = 1;
146        for(UITestObject testObject : testObjects) {
147            testObject.setField1("Custom Edit Line " + Integer.toString(index));
148            index++;
149        }
150        return getModelAndView(form);
151    }
152
153    /**
154     * Refreshes the group
155     *
156     * @param form
157     * @param result
158     * @param request
159     * @param response
160     * @return
161     */
162    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=refreshProgGroup")
163    public ModelAndView refreshProgGroup(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
164            HttpServletRequest request, HttpServletResponse response) {
165        return getModelAndView(form);
166    }
167
168    /**
169     * Refresh and set server messages
170     *
171     * @param form
172     * @param result
173     * @param request
174     * @param response
175     * @return
176     */
177    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=refreshWithServerMessages")
178    public ModelAndView refreshWithServerMessages(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
179            HttpServletRequest request, HttpServletResponse response) {
180        GlobalVariables.getMessageMap().putError("inputField4", "serverTestError");
181        GlobalVariables.getMessageMap().putWarning("inputField4", "serverTestWarning");
182        GlobalVariables.getMessageMap().putInfo("inputField4", "serverTestInfo");
183
184        return getModelAndView(form);
185    }
186
187    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=customRefresh")
188    public ModelAndView customRefresh(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
189            HttpServletRequest request, HttpServletResponse response) {
190        GlobalVariables.getMessageMap().addGrowlMessage("Test", "serverTestInfo");
191
192        return getModelAndView(form);
193    }
194
195    @Autowired
196    @Qualifier("demoFileControllerService")
197    @Override
198    public void setFileControllerService(FileControllerService fileControllerService) {
199        super.setFileControllerService(fileControllerService);
200    }
201
202    @InitBinder
203    protected void initBinder(WebDataBinder binder) throws Exception {
204        binder.registerCustomEditor(List.class, "names", new UITestObjectEditor());
205    }
206
207    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=submitMultiSelect")
208    public ModelAndView submitMultiSelect(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
209            HttpServletRequest request, HttpServletResponse response) {
210        Object obj = request.getParameter("names");
211        return getModelAndView(form);
212    }
213
214    protected class UITestObjectEditor extends CustomCollectionEditor {
215        public UITestObjectEditor() {
216            super(List.class);
217        }
218
219        @Override
220        protected Object convertElement(Object element) {
221            // not a very good example but shows converting string selected to UITestObject
222            KradSampleAppForm form = new KradSampleAppForm();
223            for (UITestObject to : form.getNames()) {
224                if (to.getInputField1().equals(element)) {
225                    return to;
226                }
227            }
228
229            return null;
230        }
231
232        @Override
233        public String getAsText() {
234            Object obj = this.getValue();
235
236            if (obj == null) {
237                return null;
238            }
239
240            StringBuffer buf = new StringBuffer();
241            List<UITestObject> l = (List)obj;
242            for (UITestObject to : l) {
243                buf.append(to.getInputField1().charAt(0)).append(",");
244            }
245
246            if (buf.toString().length() > 0) {
247                return buf.toString().substring(0, buf.toString().length()-1);
248            }
249            return buf.toString();
250        }
251    }
252}