1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.web.controller;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.codehaus.jackson.map.ObjectMapper;
21 import org.kuali.rice.core.api.CoreApiServiceLocator;
22 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
23 import org.kuali.rice.krad.uif.UifConstants;
24 import org.kuali.rice.krad.uif.UifParameters;
25 import org.kuali.rice.krad.uif.container.Container;
26 import org.kuali.rice.krad.uif.layout.LayoutManager;
27 import org.kuali.rice.krad.uif.util.ComponentFactory;
28 import org.kuali.rice.krad.uif.util.ComponentUtils;
29 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
30 import org.kuali.rice.krad.uif.view.View;
31 import org.kuali.rice.krad.uif.component.Component;
32 import org.kuali.rice.krad.uif.service.ViewService;
33 import org.kuali.rice.krad.util.KRADConstants;
34 import org.kuali.rice.krad.util.KRADUtils;
35 import org.kuali.rice.krad.web.form.UifFormBase;
36 import org.kuali.rice.krad.web.form.UifFormManager;
37 import org.springframework.web.servlet.ModelAndView;
38
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44
45
46
47
48
49
50
51
52
53
54 public class UifControllerHelper {
55 private static final Logger LOG = Logger.getLogger(UifControllerHelper.class);
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 public static String getViewIdFromRequest(HttpServletRequest request) {
75 String viewId = request.getParameter(UifParameters.VIEW_ID);
76
77 if (StringUtils.isBlank(viewId)) {
78 String viewTypeName = request.getParameter(UifParameters.VIEW_TYPE_NAME);
79
80 UifConstants.ViewType viewType = null;
81 if (StringUtils.isNotBlank(viewTypeName)) {
82 viewType = UifConstants.ViewType.valueOf(viewTypeName);
83 }
84
85 if (viewType != null) {
86 Map<String, String> parameterMap = KRADUtils.translateRequestParameterMap(request.getParameterMap());
87 viewId = getViewService().getViewIdForViewType(viewType, parameterMap);
88 }
89 }
90
91 if (StringUtils.isNotBlank(viewId)) {
92 request.setAttribute(UifParameters.VIEW_ID, viewId);
93 }
94
95 return viewId;
96 }
97
98
99
100
101
102
103
104
105
106
107 public static ModelAndView getUIFModelAndView(UifFormBase form, String pageId) {
108 if (StringUtils.isNotBlank(pageId)) {
109 form.setPageId(pageId);
110 }
111
112
113 ModelAndView modelAndView = new ModelAndView();
114 modelAndView.addObject(UifConstants.DEFAULT_MODEL_NAME, form);
115 modelAndView.setViewName(UifConstants.SPRING_VIEW_ID);
116
117 return modelAndView;
118 }
119
120
121
122
123
124 public static void postControllerHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
125 ModelAndView modelAndView) throws Exception {
126 if (modelAndView == null) {
127 return;
128 }
129
130 Object model = modelAndView.getModelMap().get(UifConstants.DEFAULT_MODEL_NAME);
131 if (!(model instanceof UifFormBase)) {
132 return;
133 }
134
135 UifFormBase form = (UifFormBase) model;
136
137
138 if (!form.isRequestRedirected()) {
139 if (!form.isJsonRequest() && !form.isOriginalComponentRequest()) {
140 prepareViewForRendering(request, form);
141 }
142
143
144 Component component = null;
145 if (StringUtils.isNotBlank(form.getUpdateComponentId())) {
146 component = form.getPostedView().getViewIndex().getComponentById(form.getUpdateComponentId());
147 } else if (form.isUpdatePageRequest()) {
148 component = form.getView().getCurrentPage();
149 }
150
151 if (form.isOriginalComponentRequest()) {
152
153 updateViewTemplates(component, form);
154 }
155
156 String changeProperties = request.getParameter(UifParameters.CHANGE_PROPERTIES);
157
158
159 if (StringUtils.isNotBlank(changeProperties) && component != null){
160 HashMap<String,Object> changePropertiesMap = new ObjectMapper().readValue(changeProperties,
161 HashMap.class);
162
163 for (String changePropertyPath : changePropertiesMap.keySet()){
164 Object value = changePropertiesMap.get(changePropertyPath);
165 ObjectPropertyUtils.setPropertyValue(component, changePropertyPath, value);
166 }
167 }
168
169 if (component != null) {
170 modelAndView.addObject(UifConstants.COMPONENT_MODEL_NAME, component);
171 }
172 }
173
174
175 modelAndView.addObject(UifParameters.REQUEST, request);
176 modelAndView.addObject(KRADConstants.USER_SESSION_KEY, request.getSession().getAttribute(
177 KRADConstants.USER_SESSION_KEY));
178
179 Map<String, String> properties = CoreApiServiceLocator.getKualiConfigurationService().getAllProperties();
180 modelAndView.addObject(UifParameters.CONFIG_PROPERTIES, properties);
181
182
183 }
184
185
186
187
188
189
190
191 protected static void updateViewTemplates(Component currentComponent, UifFormBase form){
192 List<Component> components = ComponentUtils.getAllNestedComponents(currentComponent);
193 components.add(currentComponent);
194 for (Component component : components){
195
196 if (!component.isSelfRendered() && StringUtils.isNotBlank(component.getTemplate()) &&
197 !form.getPostedView().getViewTemplates().contains(component.getTemplate())) {
198 form.getPostedView().getViewTemplates().add(component.getTemplate());
199 }
200
201 if (component instanceof Container) {
202 LayoutManager layoutManager = ((Container) component).getLayoutManager();
203
204 if ((layoutManager != null)
205 && !form.getPostedView().getViewTemplates().contains(layoutManager.getTemplate())) {
206 form.getPostedView().getViewTemplates().add(layoutManager.getTemplate());
207 }
208 }
209 }
210 }
211
212
213
214
215
216
217
218 public static void prepareViewForRendering(HttpServletRequest request, UifFormBase form) {
219
220 if (form.isUpdateComponentRequest() || form.isUpdateDialogRequest()) {
221 String refreshComponentId = form.getUpdateComponentId();
222
223 View postedView = form.getPostedView();
224
225
226 boolean boxLayoutHorizontalItem = false;
227 boolean boxLayoutVerticalItem = false;
228
229 if (form.isUpdateComponentRequest()) {
230 Component postedComponent = ComponentUtils.findNestedComponentById(postedView, refreshComponentId);
231 if (postedComponent != null && postedComponent.getCssClasses() != null &&
232 postedComponent.getCssClasses().contains("uif-boxLayoutHorizontalItem")) {
233 boxLayoutHorizontalItem = true;
234 } else if (postedComponent != null && postedComponent.getCssClasses() != null &&
235 postedComponent.getCssClasses().contains("uif-boxLayoutVerticalItem")) {
236 boxLayoutVerticalItem = true;
237 }
238 }
239
240
241 Component comp = ComponentFactory.getNewInstanceForRefresh(form.getPostedView(), refreshComponentId);
242
243
244 postedView.getViewHelperService().performComponentLifecycle(postedView, form, comp, refreshComponentId);
245
246
247
248
249
250
251 if (boxLayoutHorizontalItem) {
252 comp.addStyleClass("uif-boxLayoutHorizontalItem");
253 } else if (boxLayoutVerticalItem) {
254 comp.addStyleClass("uif-boxLayoutVerticalItem");
255 }
256
257
258 postedView.getCurrentPage().getValidationMessages().generateMessages(false, postedView, form,
259 postedView.getCurrentPage());
260 } else {
261
262 View view = form.getView();
263
264
265 if (StringUtils.isNotBlank(form.getPageId())) {
266 view.setCurrentPageId(form.getPageId());
267 }
268
269 Map<String, String> parameterMap = KRADUtils.translateRequestParameterMap(request.getParameterMap());
270 parameterMap.putAll(form.getViewRequestParameters());
271
272
273 getViewService().buildView(view, form, parameterMap);
274 }
275 }
276
277
278
279
280
281
282
283
284
285
286
287
288
289 public static void removeUnusedBreadcrumbs(UifFormManager uifFormManager, String formKey, String lastFormKey) {
290 if (StringUtils.isBlank(formKey) || StringUtils.isBlank(lastFormKey) || StringUtils.equals(formKey,
291 lastFormKey)) {
292 return;
293 }
294
295 UifFormBase previousForm = uifFormManager.getSessionForm(lastFormKey);
296 if (previousForm == null) {
297 return;
298 }
299
300 uifFormManager.removeSessionFormByKey(lastFormKey);
301 }
302
303 protected static ViewService getViewService() {
304 return KRADServiceLocatorWeb.getViewService();
305 }
306 }