1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.web.service.impl;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.lookup.LookupUtils;
20 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
21 import org.kuali.rice.krad.service.ModuleService;
22 import org.kuali.rice.krad.uif.UifConstants;
23 import org.kuali.rice.krad.uif.UifParameters;
24 import org.kuali.rice.krad.uif.UifPropertyPaths;
25 import org.kuali.rice.krad.uif.field.AttributeQueryResult;
26 import org.kuali.rice.krad.uif.service.AttributeQueryService;
27 import org.kuali.rice.krad.util.KRADUtils;
28 import org.kuali.rice.krad.web.form.UifFormBase;
29 import org.kuali.rice.krad.web.service.ModelAndViewService;
30 import org.kuali.rice.krad.web.service.QueryControllerService;
31 import org.springframework.web.servlet.ModelAndView;
32
33 import javax.servlet.http.HttpServletRequest;
34 import java.util.HashMap;
35 import java.util.Map;
36 import java.util.Properties;
37
38
39
40
41
42
43 public class QueryControllerServiceImpl implements QueryControllerService {
44 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(
45 QueryControllerServiceImpl.class);
46
47 private ModelAndViewService modelAndViewService;
48 private AttributeQueryService attributeQueryService;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 @Override
67 public ModelAndView performLookup(UifFormBase form) {
68 Properties urlParameters = form.getActionParametersAsProperties();
69
70 Class<?> lookupDataObjectClass = getLookupDataObjectClass(urlParameters);
71 if (lookupDataObjectClass == null) {
72 throw new RuntimeException("Lookup data object class not found for lookup call");
73 }
74
75
76 urlParameters.put(UifParameters.PERFORM_DIRTY_CHECK, "false");
77
78
79 String baseLookupUrl = getLookupUrlFromModuleService(lookupDataObjectClass, urlParameters);
80 if (StringUtils.isNotBlank(baseLookupUrl)) {
81
82 urlParameters = new Properties();
83 } else {
84 baseLookupUrl = urlParameters.getProperty(UifParameters.BASE_LOOKUP_URL);
85 urlParameters.remove(UifParameters.BASE_LOOKUP_URL);
86
87 buildLookupUrlParameters(form, form.getRequest(), lookupDataObjectClass, urlParameters);
88 }
89
90 return getModelAndViewService().performRedirect(form, baseLookupUrl, urlParameters);
91 }
92
93
94
95
96
97
98
99
100 protected Class<?> getLookupDataObjectClass(Properties urlParameters) {
101 Class<?> lookupDataObjectClass;
102
103 String lookupObjectClassName = urlParameters.getProperty(UifParameters.DATA_OBJECT_CLASS_NAME);
104 try {
105 lookupDataObjectClass = Class.forName(lookupObjectClassName);
106 } catch (ClassNotFoundException e) {
107 LOG.error("Unable to get class for name: " + lookupObjectClassName);
108 throw new RuntimeException("Unable to get class for name: " + lookupObjectClassName, e);
109 }
110
111 return lookupDataObjectClass;
112 }
113
114
115
116
117
118
119
120
121
122 protected String getLookupUrlFromModuleService(Class<?> lookupDataObjectClass, Properties urlParameters) {
123 String lookupUrl = null;
124
125 ModuleService responsibleModuleService =
126 KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(lookupDataObjectClass);
127 if (responsibleModuleService != null && responsibleModuleService.isExternalizable(lookupDataObjectClass)) {
128 lookupUrl = responsibleModuleService.getExternalizableDataObjectLookupUrl(lookupDataObjectClass,
129 urlParameters);
130 }
131
132 return lookupUrl;
133 }
134
135
136
137
138
139
140
141
142
143
144 protected void buildLookupUrlParameters(UifFormBase form, HttpServletRequest request,
145 Class<?> lookupDataObjectClass, Properties urlParameters) {
146 urlParameters.setProperty(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.START);
147
148 String autoSearchString = urlParameters.getProperty(UifParameters.AUTO_SEARCH);
149 if (Boolean.parseBoolean(autoSearchString)) {
150 urlParameters.setProperty(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.SEARCH);
151 }
152
153 buildLookupCriteriaParameters(form, request, lookupDataObjectClass, urlParameters);
154
155 urlParameters.setProperty(UifParameters.RETURN_LOCATION, form.getFormPostUrl());
156 urlParameters.setProperty(UifParameters.RETURN_FORM_KEY, form.getFormKey());
157 }
158
159
160
161
162
163
164
165
166
167
168 protected void buildLookupCriteriaParameters(UifFormBase form, HttpServletRequest request,
169 Class<?> lookupDataObjectClass, Properties urlParameters) {
170 String lookupParameterString = urlParameters.getProperty(UifParameters.LOOKUP_PARAMETERS);
171 if (StringUtils.isBlank(lookupParameterString)) {
172 return;
173 }
174
175 Map<String, String> lookupParameterFields = KRADUtils.getMapFromParameterString(lookupParameterString);
176 for (Map.Entry<String, String> lookupParameter : lookupParameterFields.entrySet()) {
177 String lookupParameterValue = LookupUtils.retrieveLookupParameterValue(form, request, lookupDataObjectClass,
178 lookupParameter.getValue(), lookupParameter.getKey());
179
180 if (StringUtils.isNotBlank(lookupParameterValue)) {
181 urlParameters.setProperty(UifPropertyPaths.LOOKUP_CRITERIA + "['" + lookupParameter.getValue() + "']",
182 lookupParameterValue);
183 }
184 }
185
186 urlParameters.remove(UifParameters.LOOKUP_PARAMETERS);
187 }
188
189
190
191
192
193
194
195
196 @Override
197 public AttributeQueryResult performFieldSuggest(UifFormBase form) {
198 HttpServletRequest request = form.getRequest();
199
200
201 Map<String, String> queryParameters = new HashMap<String, String>();
202 for (Object parameterName : request.getParameterMap().keySet()) {
203 if (parameterName.toString().startsWith(UifParameters.QUERY_PARAMETERS)) {
204 String fieldName = StringUtils.substringBetween(parameterName.toString(),
205 UifParameters.QUERY_PARAMETERS + "[\"", "\"]");
206 String fieldValue = request.getParameter(parameterName.toString());
207 queryParameters.put(fieldName, fieldValue);
208 }
209 }
210
211
212 String queryFieldId = request.getParameter(UifParameters.QUERY_FIELD_ID);
213 if (StringUtils.isBlank(queryFieldId)) {
214 throw new RuntimeException("Unable to find id for field to perform query on under request parameter name: "
215 + UifParameters.QUERY_FIELD_ID);
216 }
217
218
219 String queryTerm = request.getParameter(UifParameters.QUERY_TERM);
220 if (StringUtils.isBlank(queryTerm)) {
221 throw new RuntimeException(
222 "Unable to find id for query term value for attribute query on under request parameter name: "
223 + UifParameters.QUERY_TERM);
224 }
225
226 return getAttributeQueryService().performFieldSuggestQuery(form.getViewPostMetadata(), queryFieldId, queryTerm,
227 queryParameters);
228 }
229
230
231
232
233
234
235
236
237 @Override
238 public AttributeQueryResult performFieldQuery(UifFormBase form) {
239 HttpServletRequest request = form.getRequest();
240
241
242 Map<String, String> queryParameters = new HashMap<String, String>();
243 for (Object parameterName : request.getParameterMap().keySet()) {
244 if (parameterName.toString().startsWith(UifParameters.QUERY_PARAMETERS)) {
245 String fieldName = StringUtils.substringBetween(parameterName.toString(),
246 UifParameters.QUERY_PARAMETERS + "[\"", "\"]");
247 String fieldValue = request.getParameter(parameterName.toString());
248 queryParameters.put(fieldName, fieldValue);
249 }
250 }
251
252
253 String queryFieldId = request.getParameter(UifParameters.QUERY_FIELD_ID);
254 if (StringUtils.isBlank(queryFieldId)) {
255 throw new RuntimeException("Unable to find id for field to perform query on under request parameter name: "
256 + UifParameters.QUERY_FIELD_ID);
257 }
258
259 return getAttributeQueryService().performFieldQuery(form.getViewPostMetadata(), queryFieldId, queryParameters);
260 }
261
262
263
264
265
266
267 protected ModelAndViewService getModelAndViewService() {
268 return modelAndViewService;
269 }
270
271
272
273
274 public void setModelAndViewService(ModelAndViewService modelAndViewService) {
275 this.modelAndViewService = modelAndViewService;
276 }
277
278 public AttributeQueryService getAttributeQueryService() {
279 return attributeQueryService;
280 }
281
282 public void setAttributeQueryService(AttributeQueryService attributeQueryService) {
283 this.attributeQueryService = attributeQueryService;
284 }
285 }