1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.web.spring; |
17 | |
|
18 | |
import java.util.HashMap; |
19 | |
import java.util.Map; |
20 | |
|
21 | |
import javax.servlet.ServletRequest; |
22 | |
import javax.servlet.http.HttpServletRequest; |
23 | |
|
24 | |
import org.apache.commons.lang.StringUtils; |
25 | |
import org.kuali.rice.krad.datadictionary.DataDictionaryException; |
26 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
27 | |
import org.kuali.rice.krad.uif.UifParameters; |
28 | |
import org.kuali.rice.krad.uif.container.View; |
29 | |
import org.kuali.rice.krad.uif.service.ViewService; |
30 | |
import org.kuali.rice.krad.util.WebUtils; |
31 | |
import org.kuali.rice.krad.web.spring.form.UifFormBase; |
32 | |
import org.springframework.core.convert.ConversionService; |
33 | |
import org.springframework.util.Assert; |
34 | |
import org.springframework.validation.AbstractPropertyBindingResult; |
35 | |
import org.springframework.web.bind.ServletRequestDataBinder; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public class UifServletRequestDataBinder extends ServletRequestDataBinder { |
44 | 0 | protected static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(UifServletRequestDataBinder.class); |
45 | |
|
46 | |
private UifBeanPropertyBindingResult bindingResult; |
47 | |
private ConversionService conversionService; |
48 | |
|
49 | |
protected ViewService viewService; |
50 | |
|
51 | |
|
52 | |
public UifServletRequestDataBinder(Object target) { |
53 | 0 | super(target); |
54 | 0 | setBindingErrorProcessor(new UifBindingErrorProcessor()); |
55 | 0 | } |
56 | |
|
57 | |
public UifServletRequestDataBinder(Object target, String name) { |
58 | 0 | super(target, name); |
59 | 0 | setBindingErrorProcessor(new UifBindingErrorProcessor()); |
60 | 0 | } |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
@Override |
68 | |
public void initBeanPropertyAccess() { |
69 | 0 | Assert.state(this.bindingResult == null, |
70 | |
"DataBinder is already initialized - call initBeanPropertyAccess before other configuration methods"); |
71 | 0 | this.bindingResult = new UifBeanPropertyBindingResult(getTarget(), getObjectName(), isAutoGrowNestedPaths()); |
72 | 0 | if (this.conversionService != null) { |
73 | 0 | this.bindingResult.initConversion(this.conversionService); |
74 | |
} |
75 | 0 | } |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
@Override |
83 | |
protected AbstractPropertyBindingResult getInternalBindingResult() { |
84 | 0 | if (this.bindingResult == null) { |
85 | 0 | initBeanPropertyAccess(); |
86 | |
} |
87 | 0 | return this.bindingResult; |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
@Override |
96 | |
public void initDirectFieldAccess() { |
97 | 0 | LOG.error("Direct Field access is not allowed in UifServletRequestDataBinder."); |
98 | 0 | throw new RuntimeException("Direct Field access is not allowed in Kuali"); |
99 | |
} |
100 | |
|
101 | |
@Override |
102 | |
@SuppressWarnings("unchecked") |
103 | |
public void bind(ServletRequest request) { |
104 | 0 | super.bind(request); |
105 | 0 | UifFormBase form = (UifFormBase) this.getTarget(); |
106 | |
|
107 | |
|
108 | 0 | Boolean skipViewInit = WebUtils.getRequestParameterAsBoolean(request, UifParameters.SKIP_VIEW_INIT); |
109 | 0 | if ((skipViewInit != null) && skipViewInit.booleanValue()) { |
110 | |
|
111 | 0 | form.postBind((HttpServletRequest) request); |
112 | 0 | return; |
113 | |
} |
114 | |
|
115 | |
|
116 | 0 | View previousView = form.getView(); |
117 | 0 | form.setPreviousView(previousView); |
118 | |
|
119 | 0 | Map<String, String> viewRequestParameters = new HashMap<String, String>(); |
120 | |
|
121 | 0 | if (previousView != null) { |
122 | 0 | viewRequestParameters = previousView.getViewRequestParameters(); |
123 | |
} |
124 | |
|
125 | |
|
126 | 0 | View view = null; |
127 | |
|
128 | |
|
129 | |
|
130 | 0 | Map<String, String> parameterMap = WebUtils.translateRequestParameterMap(request.getParameterMap()); |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | 0 | parameterMap.putAll(viewRequestParameters); |
143 | |
|
144 | 0 | String viewId = request.getParameter(UifParameters.VIEW_ID); |
145 | 0 | if (viewId != null) { |
146 | 0 | view = getViewService().getView(viewId, parameterMap); |
147 | |
} else { |
148 | 0 | String viewTypeName = request.getParameter(UifParameters.VIEW_TYPE_NAME); |
149 | 0 | if (viewTypeName == null) { |
150 | 0 | viewTypeName = form.getViewTypeName(); |
151 | |
} |
152 | |
|
153 | 0 | if (StringUtils.isBlank(viewTypeName)) { |
154 | 0 | view = getViewFromPreviousModel(form, parameterMap); |
155 | 0 | if (view == null) { |
156 | 0 | throw new RuntimeException( |
157 | |
"Could not find enough information to fetch the required view. " + |
158 | |
" Checked the model retrieved from session for both viewTypeName and viewId"); |
159 | |
} |
160 | |
} else { |
161 | |
try { |
162 | 0 | view = getViewService().getViewByType(viewTypeName, parameterMap); |
163 | 0 | } catch (DataDictionaryException ddex) { |
164 | 0 | view = getViewFromPreviousModel(form, parameterMap); |
165 | |
|
166 | 0 | if (view == null) { |
167 | 0 | throw ddex; |
168 | |
} |
169 | 0 | LOG.warn("Obtained viewId from cached form, this may not be safe!"); |
170 | 0 | } |
171 | |
} |
172 | |
} |
173 | |
|
174 | |
|
175 | 0 | if (!form.isDefaultsApplied()) { |
176 | 0 | view.getViewHelperService().applyDefaultValues(view, form); |
177 | |
} |
178 | |
|
179 | 0 | form.setViewRequestParameters(view.getViewRequestParameters()); |
180 | 0 | form.setViewId(view.getId()); |
181 | 0 | form.setView(view); |
182 | 0 | form.postBind((HttpServletRequest) request); |
183 | 0 | } |
184 | |
|
185 | |
protected View getViewFromPreviousModel(UifFormBase form, Map<String, String> parameterMap) { |
186 | |
|
187 | 0 | if(form.getViewId() != null) { |
188 | 0 | return getViewService().getView(form.getViewId(), parameterMap); |
189 | |
} |
190 | 0 | return null; |
191 | |
} |
192 | |
|
193 | |
public ViewService getViewService() { |
194 | 0 | if(viewService == null) { |
195 | 0 | viewService = KRADServiceLocatorWeb.getViewService(); |
196 | |
} |
197 | 0 | return this.viewService; |
198 | |
} |
199 | |
|
200 | |
public void setViewService(ViewService viewService) { |
201 | 0 | this.viewService = viewService; |
202 | 0 | } |
203 | |
|
204 | |
} |