View Javadoc
1   /**
2    * Copyright 2005-2016 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.krms.impl.repository;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.data.DataObjectService;
20  import org.kuali.rice.krms.api.repository.context.ContextDefinition;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import static org.kuali.rice.krms.impl.repository.BusinessObjectServiceMigrationUtils.deleteMatching;
26  import static org.kuali.rice.krms.impl.repository.BusinessObjectServiceMigrationUtils.findSingleMatching;
27  
28  /**
29   * This is the interface for accessing KRMS repository Context related
30   * business objects. 
31   *
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   *
34   */
35  public class ContextBoServiceImpl implements ContextBoService {
36  
37      private DataObjectService dataObjectService;
38  
39      /**
40       * This method will create a {@link ContextDefinition} as described
41       * by the parameter passed in.
42       *
43       * @see org.kuali.rice.krms.impl.repository.ContextBoService#createContext(org.kuali.rice.krms.api.repository.context.ContextDefinition)
44       */
45      @Override
46      public ContextDefinition createContext(ContextDefinition context) {
47          if (context == null) {
48              throw new IllegalArgumentException("context is null");
49          }
50  
51          final String contextIdKey = context.getId();
52          final ContextDefinition existing = getContextByContextId(contextIdKey);
53  
54          if (existing != null) {
55              throw new IllegalStateException("the context to create already exists: " + context);
56          }
57  
58          ContextBo bo = dataObjectService.save(ContextBo.from(context));
59          return ContextBo.to(bo);
60      }
61  
62      /**
63       * This method updates an existing Context in the repository.
64       */
65      @Override
66      public ContextDefinition updateContext(ContextDefinition context) {
67          if (context == null){
68              throw new IllegalArgumentException("context is null");
69          }
70  
71          // must already exist to be able to update
72          final String contextIdKey = context.getId();
73          final ContextBo existing = dataObjectService.find(ContextBo.class, contextIdKey);
74  
75          if (existing == null) {
76              throw new IllegalStateException("the context does not exist: " + context);
77          }
78  
79          final ContextDefinition toUpdate;
80  
81          if (!existing.getId().equals(context.getId())){
82              // if passed in id does not match existing id, correct it
83              final ContextDefinition.Builder builder = ContextDefinition.Builder.create(context);
84              builder.setId(existing.getId());
85              toUpdate = builder.build();
86          } else {
87              toUpdate = context;
88          }
89  
90          // copy all updateable fields to bo
91          ContextBo boToUpdate = ContextBo.from(toUpdate);
92  
93          // delete any old, existing attributes
94          Map<String,String> fields = new HashMap<String,String>(1);
95          fields.put("context.id", toUpdate.getId());
96          deleteMatching(dataObjectService, ContextAttributeBo.class, fields);
97  
98          // update the rule and create new attributes
99          final ContextBo updatedData = dataObjectService.save(boToUpdate);
100 
101         return ContextBo.to(updatedData);
102     }
103 
104     @Override
105     public ContextDefinition getContextByContextId(String contextId) {
106         if (StringUtils.isBlank(contextId)){
107             return null;
108         }
109         ContextBo bo = dataObjectService.find(ContextBo.class, contextId);
110         return ContextBo.to(bo);
111     }
112 
113     @Override
114     public ContextDefinition getContextByNameAndNamespace( String name, String namespace ){
115         if (StringUtils.isBlank(name)){
116             throw new IllegalArgumentException("name is null or blank");
117         }
118         if (StringUtils.isBlank(namespace)){
119             throw new IllegalArgumentException("namespace is null or blank");
120         }
121 
122         final Map<String, Object> map = new HashMap<String, Object>();
123         map.put("name", name);
124         map.put("namespace", namespace);
125         ContextBo bo = findSingleMatching(dataObjectService, ContextBo.class, map);
126 
127         return ContextBo.to(bo);
128     }
129 
130     /**
131      * Sets the dataObjectService attribute value.
132      *
133      * @param dataObjectService The dataObjectService to set.
134      */
135     public void setDataObjectService(final DataObjectService dataObjectService) {
136         this.dataObjectService = dataObjectService;
137     }
138 }