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.GlobalVariables;
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 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 super(target);
51 setBindingErrorProcessor(new UifBindingErrorProcessor());
52 }
53
54 public UifServletRequestDataBinder(Object target, String name) {
55 super(target, name);
56 setBindingErrorProcessor(new UifBindingErrorProcessor());
57 }
58
59
60
61
62
63
64 @Override
65 public void initBeanPropertyAccess() {
66 Assert.state(this.bindingResult == null,
67 "DataBinder is already initialized - call initBeanPropertyAccess before other configuration methods");
68 this.bindingResult = new UifBeanPropertyBindingResult(getTarget(), getObjectName(), isAutoGrowNestedPaths(),
69 getAutoGrowCollectionLimit());
70 if (this.conversionService != null) {
71 this.bindingResult.initConversion(this.conversionService);
72 }
73 }
74
75
76
77
78
79
80 @Override
81 protected AbstractPropertyBindingResult getInternalBindingResult() {
82 if (this.bindingResult == null) {
83 initBeanPropertyAccess();
84 }
85 return this.bindingResult;
86 }
87
88
89
90
91
92
93 @Override
94 public void initDirectFieldAccess() {
95 LOG.error("Direct Field access is not allowed in UifServletRequestDataBinder.");
96 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 super.bind(request);
103
104 UifFormBase form = (UifFormBase) this.getTarget();
105
106
107 Boolean skipViewInit = KRADUtils.getRequestParameterAsBoolean(request, UifParameters.SKIP_VIEW_INIT);
108 if ((skipViewInit == null) || !skipViewInit.booleanValue()) {
109
110 View view = null;
111
112 String viewId = request.getParameter(UifParameters.VIEW_ID);
113 if (viewId != null) {
114 view = getViewService().getViewById(viewId);
115 } else {
116
117 ViewType viewType = null;
118
119 String viewTypeName = request.getParameter(UifParameters.VIEW_TYPE_NAME);
120 viewType = StringUtils.isBlank(viewTypeName) ? form.getViewTypeName() : ViewType.valueOf(viewTypeName);
121
122 if (viewType != null) {
123 Map<String, String> parameterMap = KRADUtils.translateRequestParameterMap(
124 request.getParameterMap());
125 view = getViewService().getViewByType(viewType, parameterMap);
126 }
127
128
129 if (view == null) {
130 view = getViewFromPreviousModel(form);
131
132 if (view != null) {
133 LOG.warn("Obtained viewId from cached form, this may not be safe!");
134 }
135 }
136 }
137
138 if (view != null) {
139 form.setViewId(view.getId());
140 form.setView(view);
141 } else {
142 form.setViewId(null);
143 form.setView(null);
144 }
145 }
146
147 form.postBind((HttpServletRequest) request);
148
149
150 GlobalVariables.getUifFormManager().addForm(form);
151 }
152
153 protected View getViewFromPreviousModel(UifFormBase form) {
154
155 if (form.getViewId() != null) {
156 return getViewService().getViewById(form.getViewId());
157 }
158
159 return null;
160 }
161
162 public ViewService getViewService() {
163 return KRADServiceLocatorWeb.getViewService();
164 }
165
166 }