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.service.KRADServiceLocatorWeb;
20 import org.kuali.rice.krad.uif.UifParameters;
21 import org.kuali.rice.krad.uif.view.View;
22 import org.kuali.rice.krad.uif.UifConstants.ViewType;
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.Map;
34
35
36
37
38
39
40
41 public class UifServletRequestDataBinder extends ServletRequestDataBinder {
42 protected static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(
43 UifServletRequestDataBinder.class);
44
45 private UifBeanPropertyBindingResult bindingResult;
46 private ConversionService conversionService;
47
48 public UifServletRequestDataBinder(Object target) {
49 super(target);
50
51 setBindingErrorProcessor(new UifBindingErrorProcessor());
52 }
53
54 public UifServletRequestDataBinder(Object target, String name) {
55 super(target, name);
56
57 setBindingErrorProcessor(new UifBindingErrorProcessor());
58 }
59
60
61
62
63
64
65 @Override
66 public void initBeanPropertyAccess() {
67 Assert.state(this.bindingResult == null,
68 "DataBinder is already initialized - call initBeanPropertyAccess before other configuration methods");
69
70 this.bindingResult = new UifBeanPropertyBindingResult(getTarget(), getObjectName(), isAutoGrowNestedPaths(),
71 getAutoGrowCollectionLimit());
72
73 if (this.conversionService != null) {
74 this.bindingResult.initConversion(this.conversionService);
75 }
76 }
77
78
79
80
81
82
83 @Override
84 protected AbstractPropertyBindingResult getInternalBindingResult() {
85 if (this.bindingResult == null) {
86 initBeanPropertyAccess();
87 }
88
89 return this.bindingResult;
90 }
91
92
93
94
95
96
97 @Override
98 public void initDirectFieldAccess() {
99 LOG.error("Direct Field access is not allowed in UifServletRequestDataBinder.");
100 throw new RuntimeException("Direct Field access is not allowed in Kuali");
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 @Override
123 public void bind(ServletRequest request) {
124 super.bind(request);
125
126 UifFormBase form = (UifFormBase) this.getTarget();
127
128
129 if (!form.isUpdateComponentRequest() && !form.isUpdateNoneRequest() && !form.isUpdateDialogRequest()) {
130 View view = null;
131
132
133 String viewId = (String) request.getAttribute(UifParameters.VIEW_ID);
134 if (StringUtils.isBlank(viewId)) {
135 viewId = request.getParameter(UifParameters.VIEW_ID);
136 }
137
138 if (StringUtils.isNotBlank(viewId)) {
139 view = getViewService().getViewById(viewId);
140 }
141
142
143 if (view == null) {
144 view = getViewByType(request, form);
145 }
146
147
148 if (view == null) {
149 view = getViewFromPreviousModel(form);
150
151 if (view != null) {
152 LOG.warn("Obtained viewId from cached form, this may not be safe!");
153 }
154 }
155
156 if (view != null) {
157 form.setViewId(view.getId());
158
159
160 } else {
161 form.setViewId(null);
162 }
163
164 form.setView(view);
165 }
166
167
168 form.postBind((HttpServletRequest) request);
169 }
170
171
172
173
174
175
176
177
178
179 protected View getViewByType(ServletRequest request, UifFormBase form) {
180 View view = null;
181
182 String viewTypeName = request.getParameter(UifParameters.VIEW_TYPE_NAME);
183 ViewType viewType = StringUtils.isBlank(viewTypeName) ? form.getViewTypeName() : ViewType.valueOf(viewTypeName);
184
185 if (viewType != null) {
186 Map<String, String> parameterMap = KRADUtils.translateRequestParameterMap(request.getParameterMap());
187 view = getViewService().getViewByType(viewType, parameterMap);
188 }
189
190 return view;
191 }
192
193
194
195
196
197
198
199
200 protected View getViewFromPreviousModel(UifFormBase form) {
201
202 if (form.getViewId() != null) {
203 return getViewService().getViewById(form.getViewId());
204 }
205
206 return null;
207 }
208
209 public ViewService getViewService() {
210 return KRADServiceLocatorWeb.getViewService();
211 }
212
213 }
214
215