View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16     package org.kuali.rice.coreservice.web.parameter;
17  
18  import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
19  import org.kuali.rice.coreservice.api.parameter.Parameter;
20  import org.kuali.rice.coreservice.api.parameter.ParameterKey;
21  import org.kuali.rice.coreservice.api.parameter.ParameterRepositoryService;
22  import org.kuali.rice.coreservice.api.parameter.ParameterType;
23  import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
24  import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
25  
26  public class ParameterMaintainableImpl extends KualiMaintainableImpl {
27      private static final long serialVersionUID = 4914145799502207182L;
28  
29      @Override
30      public void saveDataObject() {
31              if (super.getDataObject() instanceof ParameterBo) {
32                  ParameterBo object  = (ParameterBo)getDataObject();
33                  Parameter param = null;
34  
35                  // construct a ParameterType.Builder for when we need to set the type
36                  // (since object may not have a ParameterType set)
37                  ParameterType.Builder parameterTypeBuilder =
38                          ParameterType.Builder.create(object.getParameterTypeCode());
39  
40                  ParameterRepositoryService parameterRepository = CoreServiceApiServiceLocator.getParameterRepositoryService();
41                  
42                  param = parameterRepository.getParameter(
43                          ParameterKey.create(object.getApplicationId(),
44                          object.getNamespaceCode(), 
45                          object.getComponentCode(), 
46                          object.getName()));
47  
48                  // Note that the parameter repository service will try to get back a parameter with the Rice application
49                  // ID if it can't find it with the give application ID.  If the given application ID is not Rice, and a
50                  // parameter for Rice comes back, we have to be careful and not update it as that would effectively
51                  // erase the Rice parameter.
52                  
53                  if(param == null || !object.getApplicationId().equals(param.getApplicationId())) { // create new
54                      param = parameterRepository.createParameter(Parameter.Builder.create(object.getApplicationId(),
55                              object.getNamespaceCode(), object.getComponentCode(), object.getName(), parameterTypeBuilder
56                      /* set the type using our builder from above */).build());
57  
58                  }
59  
60                  Parameter.Builder b = Parameter.Builder.create(param);
61                  b.setValue(object.getValue());
62                  b.setDescription(object.getDescription());
63                  b.setEvaluationOperator(object.getEvaluationOperator());
64                  b.setParameterType(parameterTypeBuilder); // set the type using our builder from above
65  
66                  parameterRepository.updateParameter(b.build());
67  
68              } else {
69                  throw new RuntimeException(
70                          "Cannot update object of type: " + this.getDataObjectClass() + " with Parameter repository service");
71              }
72          }
73  
74  }