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