View Javadoc
1   /**
2    * Copyright 2005-2014 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 final 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 void 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          dataObjectService.save(boToUpdate);
100     }
101 
102     @Override
103     public ContextDefinition getContextByContextId(String contextId) {
104         if (StringUtils.isBlank(contextId)){
105             return null;
106         }
107         ContextBo bo = dataObjectService.find(ContextBo.class, contextId);
108         return ContextBo.to(bo);
109     }
110 
111     @Override
112     public ContextDefinition getContextByNameAndNamespace( String name, String namespace ){
113         if (StringUtils.isBlank(name)){
114             throw new IllegalArgumentException("name is null or blank");
115         }
116         if (StringUtils.isBlank(namespace)){
117             throw new IllegalArgumentException("namespace is null or blank");
118         }
119 
120         final Map<String, Object> map = new HashMap<String, Object>();
121         map.put("name", name);
122         map.put("namespace", namespace);
123         ContextBo bo = findSingleMatching(dataObjectService, ContextBo.class, map);
124 
125         return ContextBo.to(bo);
126     }
127 
128     /**
129      * Sets the dataObjectService attribute value.
130      *
131      * @param dataObjectService The dataObjectService to set.
132      */
133     public void setDataObjectService(final DataObjectService dataObjectService) {
134         this.dataObjectService = dataObjectService;
135     }
136 }