001/**
002 * Copyright 2013 The Kuali Foundation Licensed under the
003 * Educational Community License, Version 2.0 (the "License"); you may
004 * not use this file except in compliance with the License. You may
005 * obtain a copy of the License at
006 *
007 * http://www.osedu.org/licenses/ECL-2.0
008 *
009 * Unless required by applicable law or agreed to in writing,
010 * software distributed under the License is distributed on an "AS IS"
011 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012 * or implied. See the License for the specific language governing
013 * permissions and limitations under the License.
014 *
015 * Created by bobhurt on 5/1/13
016 */
017package org.kuali.rice.krad.web.controller;
018
019import org.apache.commons.lang.StringUtils;
020import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
021import org.kuali.rice.krad.uif.UifParameters;
022import org.kuali.rice.krad.uif.field.AttributeQueryResult;
023import org.kuali.rice.krad.web.form.UifFormBase;
024import org.springframework.validation.BindingResult;
025import org.springframework.web.bind.annotation.ModelAttribute;
026import org.springframework.web.bind.annotation.RequestMapping;
027import org.springframework.web.bind.annotation.RequestMethod;
028import org.springframework.web.bind.annotation.ResponseBody;
029
030import javax.servlet.http.HttpServletRequest;
031import javax.servlet.http.HttpServletResponse;
032import java.util.HashMap;
033import java.util.Map;
034
035/**
036 * This class overrides one or more methods from Rice's UifControllerBase class
037 *
038 * @author Kuali Student Team
039 */
040public abstract class KsUifControllerBase extends UifControllerBase {
041
042
043    /**
044     * Invoked to provide the options for a suggest widget. The valid options are retrieved by the associated
045     * <code>AttributeQuery</code> for the field containing the suggest widget. The controller method picks
046     * out the query parameters from the request and calls <code>AttributeQueryService</code> to perform the
047     * suggest query and prepare the result object that will be exposed with JSON
048     */
049    @Override
050    @RequestMapping(method = RequestMethod.GET, params = "methodToCall=performFieldSuggest")
051    public
052    @ResponseBody
053    AttributeQueryResult performFieldSuggest(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
054                                             HttpServletRequest request, HttpServletResponse response) {
055
056        // retrieve query fields from request
057        Map<String, String> queryParameters = new HashMap<String, String>();
058        for (Object parameterName : request.getParameterMap().keySet()) {
059            if (parameterName.toString().startsWith(UifParameters.QUERY_PARAMETER + ".")) {
060                String fieldName = StringUtils.substringAfter(parameterName.toString(),
061                        UifParameters.QUERY_PARAMETER + ".");
062                String fieldValue = request.getParameter(parameterName.toString());
063                queryParameters.put(fieldName, fieldValue);
064            }
065        }
066
067        // retrieve id for field to perform query for
068        String queryFieldId = request.getParameter(UifParameters.QUERY_FIELD_ID);
069        if (StringUtils.isBlank(queryFieldId)) {
070            throw new RuntimeException("Unable to find id for field to perform query on under request parameter name: "
071                    + UifParameters.QUERY_FIELD_ID);
072        }
073
074        // get the field term to match
075        String queryTerm = request.getParameter(UifParameters.QUERY_TERM);
076        Boolean isBlankQueryTermAllowed =
077                "true".equalsIgnoreCase(request.getParameter("blankQueryTermAllowed"));
078        if ( ! isBlankQueryTermAllowed) {
079            if (StringUtils.isBlank(queryTerm)) {
080                throw new RuntimeException(
081                        "Unable to find id for query term value for attribute query on under request parameter name: "
082                                + UifParameters.QUERY_TERM);
083            }
084        }
085
086        // invoke attribute query service to perform the query
087        AttributeQueryResult queryResult = KRADServiceLocatorWeb.getAttributeQueryService().performFieldSuggestQuery(
088                form.getPostedView(), queryFieldId, queryTerm, queryParameters);
089
090        return queryResult;
091    }
092
093}