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