View Javadoc

1   /**
2    * Copyright 2013 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   * Created by bobhurt on 5/1/13
16   */
17  package org.kuali.rice.krad.web.controller;
18  
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
21  import org.kuali.rice.krad.uif.UifParameters;
22  import org.kuali.rice.krad.uif.field.AttributeQueryResult;
23  import org.kuali.rice.krad.web.form.UifFormBase;
24  import org.springframework.validation.BindingResult;
25  import org.springframework.web.bind.annotation.ModelAttribute;
26  import org.springframework.web.bind.annotation.RequestMapping;
27  import org.springframework.web.bind.annotation.RequestMethod;
28  import org.springframework.web.bind.annotation.ResponseBody;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  /**
36   * This class overrides one or more methods from Rice's UifControllerBase class
37   *
38   * @author Kuali Student Team
39   */
40  public abstract class KsUifControllerBase extends UifControllerBase {
41  
42  
43      /**
44       * Invoked to provide the options for a suggest widget. The valid options are retrieved by the associated
45       * <code>AttributeQuery</code> for the field containing the suggest widget. The controller method picks
46       * out the query parameters from the request and calls <code>AttributeQueryService</code> to perform the
47       * suggest query and prepare the result object that will be exposed with JSON
48       */
49      @Override
50      @RequestMapping(method = RequestMethod.GET, params = "methodToCall=performFieldSuggest")
51      public
52      @ResponseBody
53      AttributeQueryResult performFieldSuggest(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
54                                               HttpServletRequest request, HttpServletResponse response) {
55  
56          // retrieve query fields from request
57          Map<String, String> queryParameters = new HashMap<String, String>();
58          for (Object parameterName : request.getParameterMap().keySet()) {
59              if (parameterName.toString().startsWith(UifParameters.QUERY_PARAMETER + ".")) {
60                  String fieldName = StringUtils.substringAfter(parameterName.toString(),
61                          UifParameters.QUERY_PARAMETER + ".");
62                  String fieldValue = request.getParameter(parameterName.toString());
63                  queryParameters.put(fieldName, fieldValue);
64              }
65          }
66  
67          // retrieve id for field to perform query for
68          String queryFieldId = request.getParameter(UifParameters.QUERY_FIELD_ID);
69          if (StringUtils.isBlank(queryFieldId)) {
70              throw new RuntimeException("Unable to find id for field to perform query on under request parameter name: "
71                      + UifParameters.QUERY_FIELD_ID);
72          }
73  
74          // get the field term to match
75          String queryTerm = request.getParameter(UifParameters.QUERY_TERM);
76          Boolean isBlankQueryTermAllowed =
77                  "true".equalsIgnoreCase(request.getParameter("blankQueryTermAllowed"));
78          if ( ! isBlankQueryTermAllowed) {
79              if (StringUtils.isBlank(queryTerm)) {
80                  throw new RuntimeException(
81                          "Unable to find id for query term value for attribute query on under request parameter name: "
82                                  + UifParameters.QUERY_TERM);
83              }
84          }
85  
86          // invoke attribute query service to perform the query
87          AttributeQueryResult queryResult = KRADServiceLocatorWeb.getAttributeQueryService().performFieldSuggestQuery(
88                  form.getPostedView(), queryFieldId, queryTerm, queryParameters);
89  
90          return queryResult;
91      }
92  
93  }