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.UifParameters; |
28 | |
import org.kuali.rice.kns.uif.container.View; |
29 | |
import org.kuali.rice.kns.uif.service.ViewService; |
30 | |
import org.kuali.rice.kns.util.WebUtils; |
31 | |
import org.kuali.rice.kns.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 | |
|
44 | |
public class UifServletRequestDataBinder extends ServletRequestDataBinder { |
45 | |
|
46 | 0 | protected static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(UifServletRequestDataBinder.class); |
47 | |
|
48 | |
private UifBeanPropertyBindingResult bindingResult; |
49 | |
private ConversionService conversionService; |
50 | |
|
51 | |
protected ViewService viewService; |
52 | |
|
53 | |
|
54 | |
public UifServletRequestDataBinder(Object target) { |
55 | 0 | super(target); |
56 | 0 | } |
57 | |
|
58 | |
public UifServletRequestDataBinder(Object target, String name) { |
59 | 0 | super(target, name); |
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 | View previousView = form.getView(); |
109 | 0 | form.setPreviousView(previousView); |
110 | |
|
111 | 0 | Map<String, String> viewRequestParameters = new HashMap<String, String>(); |
112 | 0 | if (previousView != null) { |
113 | 0 | viewRequestParameters = previousView.getViewRequestParameters(); |
114 | |
} |
115 | |
|
116 | |
|
117 | 0 | View view = null; |
118 | |
|
119 | |
|
120 | |
|
121 | 0 | Map<String, String> parameterMap = WebUtils.translateRequestParameterMap(request.getParameterMap()); |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | 0 | parameterMap.putAll(viewRequestParameters); |
133 | |
|
134 | 0 | String viewId = request.getParameter(UifParameters.VIEW_ID); |
135 | 0 | if (viewId != null) { |
136 | 0 | view = getViewService().getView(viewId, parameterMap); |
137 | |
} |
138 | |
else { |
139 | 0 | String viewTypeName = request.getParameter(UifParameters.VIEW_TYPE_NAME); |
140 | 0 | if (viewTypeName == null) { |
141 | 0 | viewTypeName = form.getViewTypeName(); |
142 | |
} |
143 | |
|
144 | 0 | if (StringUtils.isBlank(viewTypeName)) { |
145 | 0 | view = getViewFromPreviousModel(form, parameterMap); |
146 | 0 | if (view == null) { |
147 | 0 | throw new RuntimeException("Could not find enough information to fetch the required view. Checked the model retrieved from session for both viewTypeName and viewId"); |
148 | |
} |
149 | |
} else { |
150 | |
try { |
151 | 0 | view = getViewService().getViewByType(viewTypeName, parameterMap); |
152 | |
} |
153 | 0 | catch(DataDictionaryException ddex) { |
154 | 0 | view = getViewFromPreviousModel(form, parameterMap); |
155 | |
|
156 | 0 | if(view == null) { |
157 | 0 | throw ddex; |
158 | |
} |
159 | 0 | LOG.warn("Obtained viewId from cached form, this may not be safe!"); |
160 | 0 | } |
161 | |
} |
162 | |
} |
163 | |
|
164 | 0 | form.setViewRequestParameters(view.getViewRequestParameters()); |
165 | 0 | form.setViewId(view.getId()); |
166 | 0 | form.setView(view); |
167 | |
|
168 | 0 | form.postBind((HttpServletRequest) request); |
169 | 0 | } |
170 | |
|
171 | |
protected View getViewFromPreviousModel(UifFormBase form, Map<String, String> parameterMap) { |
172 | |
|
173 | 0 | if(form.getViewId() != null) { |
174 | 0 | return getViewService().getView(form.getViewId(), parameterMap); |
175 | |
} |
176 | 0 | return null; |
177 | |
} |
178 | |
|
179 | |
public ViewService getViewService() { |
180 | 0 | if(viewService == null) { |
181 | 0 | viewService = KNSServiceLocatorWeb.getViewService(); |
182 | |
} |
183 | 0 | return this.viewService; |
184 | |
} |
185 | |
|
186 | |
public void setViewService(ViewService viewService) { |
187 | 0 | this.viewService = viewService; |
188 | 0 | } |
189 | |
|
190 | |
} |