001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.labs.parameter;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
020import org.kuali.rice.coreservice.api.parameter.Parameter;
021import org.kuali.rice.coreservice.api.parameter.ParameterKey;
022import org.kuali.rice.krad.util.KRADConstants;
023import org.kuali.rice.krad.web.controller.UifControllerBase;
024import org.kuali.rice.krad.web.form.UifFormBase;
025import org.springframework.stereotype.Controller;
026import org.springframework.web.bind.annotation.ModelAttribute;
027import org.springframework.web.bind.annotation.RequestMapping;
028import org.springframework.web.servlet.ModelAndView;
029
030/**
031 * Controller for the Parameter utility.
032 *
033 * @author Kuali Rice Team (rice.collab@kuali.org)
034 */
035@Controller
036@RequestMapping(value = "/parameter")
037public class ParameterController extends UifControllerBase {
038
039    @Override
040    protected ParameterForm createInitialForm() {
041        return new ParameterForm();
042    }
043
044    @RequestMapping(params = "methodToCall=update")
045    public ModelAndView update(@ModelAttribute("KualiForm") UifFormBase form) {
046        ParameterForm parameterForm = (ParameterForm) form;
047
048        String applicationId = KRADConstants.DEFAULT_PARAMETER_APPLICATION_ID;
049        String namespaceCode = parameterForm.getNamespaceCode();
050        String componentCode = parameterForm.getComponentCode();
051        String parameterName = parameterForm.getParameterName();
052
053        Parameter parameter = null;
054
055        if (StringUtils.isNotBlank(applicationId) && StringUtils.isNotBlank(namespaceCode)
056            && StringUtils.isNotBlank(componentCode) && StringUtils.isNotBlank(parameterName)) {
057            ParameterKey key = ParameterKey.create(applicationId, namespaceCode, componentCode, parameterName);
058            parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(key);
059        }
060
061        if (parameter != null) {
062            Parameter.Builder builder = Parameter.Builder.create(parameter);
063            builder.setValue(parameterForm.getParameterValue());
064
065            CoreServiceApiServiceLocator.getParameterRepositoryService().updateParameter(builder.build());
066        }
067
068        return getModelAndView(form);
069    }
070}