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.UifConstants; |
21 | |
import org.kuali.rice.krad.uif.UifParameters; |
22 | |
import org.kuali.rice.krad.uif.view.View; |
23 | |
import org.kuali.rice.krad.uif.UifConstants.ViewType; |
24 | |
import org.kuali.rice.krad.uif.service.ViewService; |
25 | |
import org.kuali.rice.krad.util.KRADUtils; |
26 | |
import org.kuali.rice.krad.web.form.UifFormBase; |
27 | |
import org.springframework.core.convert.ConversionService; |
28 | |
import org.springframework.util.Assert; |
29 | |
import org.springframework.validation.AbstractPropertyBindingResult; |
30 | |
import org.springframework.web.bind.ServletRequestDataBinder; |
31 | |
|
32 | |
import javax.servlet.ServletRequest; |
33 | |
import javax.servlet.http.HttpServletRequest; |
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( |
44 | |
UifServletRequestDataBinder.class); |
45 | |
|
46 | |
private UifBeanPropertyBindingResult bindingResult; |
47 | |
private ConversionService conversionService; |
48 | |
|
49 | |
public UifServletRequestDataBinder(Object target) { |
50 | 0 | super(target); |
51 | 0 | setBindingErrorProcessor(new UifBindingErrorProcessor()); |
52 | 0 | } |
53 | |
|
54 | |
public UifServletRequestDataBinder(Object target, String name) { |
55 | 0 | super(target, name); |
56 | 0 | setBindingErrorProcessor(new UifBindingErrorProcessor()); |
57 | 0 | } |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
@Override |
65 | |
public void initBeanPropertyAccess() { |
66 | 0 | Assert.state(this.bindingResult == null, |
67 | |
"DataBinder is already initialized - call initBeanPropertyAccess before other configuration methods"); |
68 | 0 | this.bindingResult = new UifBeanPropertyBindingResult(getTarget(), getObjectName(), isAutoGrowNestedPaths(), |
69 | |
getAutoGrowCollectionLimit()); |
70 | 0 | if (this.conversionService != null) { |
71 | 0 | this.bindingResult.initConversion(this.conversionService); |
72 | |
} |
73 | 0 | } |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
@Override |
81 | |
protected AbstractPropertyBindingResult getInternalBindingResult() { |
82 | 0 | if (this.bindingResult == null) { |
83 | 0 | initBeanPropertyAccess(); |
84 | |
} |
85 | 0 | return this.bindingResult; |
86 | |
} |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
@Override |
94 | |
public void initDirectFieldAccess() { |
95 | 0 | LOG.error("Direct Field access is not allowed in UifServletRequestDataBinder."); |
96 | 0 | throw new RuntimeException("Direct Field access is not allowed in Kuali"); |
97 | |
} |
98 | |
|
99 | |
@Override |
100 | |
@SuppressWarnings("unchecked") |
101 | |
public void bind(ServletRequest request) { |
102 | 0 | super.bind(request); |
103 | |
|
104 | 0 | UifFormBase form = (UifFormBase) this.getTarget(); |
105 | |
|
106 | |
|
107 | 0 | View previousView = form.getView(); |
108 | 0 | form.setPreviousView(previousView); |
109 | |
|
110 | |
|
111 | 0 | Boolean skipViewInit = KRADUtils.getRequestParameterAsBoolean(request, UifParameters.SKIP_VIEW_INIT); |
112 | 0 | if ((skipViewInit == null) || !skipViewInit.booleanValue()) { |
113 | |
|
114 | 0 | View view = null; |
115 | |
|
116 | 0 | String viewId = request.getParameter(UifParameters.VIEW_ID); |
117 | 0 | if (viewId != null) { |
118 | 0 | view = getViewService().getViewById(viewId); |
119 | |
} else { |
120 | |
|
121 | 0 | ViewType viewType = null; |
122 | |
|
123 | 0 | String viewTypeName = request.getParameter(UifParameters.VIEW_TYPE_NAME); |
124 | 0 | viewType = StringUtils.isBlank(viewTypeName) ? form.getViewTypeName() : ViewType.valueOf(viewTypeName); |
125 | |
|
126 | 0 | if (viewType == null) { |
127 | 0 | view = getViewFromPreviousModel(form); |
128 | 0 | if (view == null) { |
129 | 0 | throw new RuntimeException("Could not find enough information to fetch the required view. " |
130 | |
+ " Checked the model retrieved from session for both viewTypeName and viewId"); |
131 | |
} |
132 | |
} else { |
133 | 0 | Map<String, String> parameterMap = KRADUtils.translateRequestParameterMap( |
134 | |
request.getParameterMap()); |
135 | |
try { |
136 | 0 | view = getViewService().getViewByType(viewType, parameterMap); |
137 | 0 | } catch (RuntimeException rtex) { |
138 | 0 | view = getViewFromPreviousModel(form); |
139 | |
|
140 | 0 | if (view == null) { |
141 | 0 | throw rtex; |
142 | |
} |
143 | 0 | LOG.warn("Obtained viewId from cached form, this may not be safe!"); |
144 | 0 | } |
145 | |
} |
146 | |
} |
147 | |
|
148 | 0 | form.setViewId(view.getId()); |
149 | 0 | form.setView(view); |
150 | |
} |
151 | |
|
152 | 0 | form.postBind((HttpServletRequest) request); |
153 | |
|
154 | |
|
155 | 0 | request.setAttribute(UifParameters.FORM_KEY, form.getFormKey()); |
156 | |
|
157 | |
|
158 | 0 | ((HttpServletRequest) request).getSession().setAttribute(form.getFormKey(), form); |
159 | 0 | } |
160 | |
|
161 | |
protected View getViewFromPreviousModel(UifFormBase form) { |
162 | |
|
163 | 0 | if (form.getViewId() != null) { |
164 | 0 | return getViewService().getViewById(form.getViewId()); |
165 | |
} |
166 | |
|
167 | 0 | return null; |
168 | |
} |
169 | |
|
170 | |
public ViewService getViewService() { |
171 | 0 | return KRADServiceLocatorWeb.getViewService(); |
172 | |
} |
173 | |
|
174 | |
} |