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.krad.data.PersistenceOption;
21  import org.kuali.rice.krms.api.repository.action.ActionDefinition;
22  
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  import static org.kuali.rice.krms.impl.repository.BusinessObjectServiceMigrationUtils.*;
31  
32  /**
33   * Implementation of the interface for accessing KRMS repository Action related
34   * business objects.
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   *
38   */
39  public final class ActionBoServiceImpl implements ActionBoService {
40  
41      private DataObjectService dataObjectService;
42  
43      /**
44       * This overridden method creates a KRMS Action in the repository.
45       */
46      @Override
47      public ActionDefinition createAction(ActionDefinition action) {
48          if (action == null){
49              throw new IllegalArgumentException("action is null");
50          }
51  
52          final String actionNameKey = action.getName();
53          final String actionNamespaceKey = action.getNamespace();
54          final ActionDefinition existing = getActionByNameAndNamespace(actionNameKey, actionNamespaceKey);
55  
56          if (existing != null){
57              throw new IllegalStateException("the action to create already exists: " + action);
58          }
59  
60          ActionBo bo = ActionBo.from(action);
61          RuleBo rule = new RuleBo();
62          rule.setId(action.getRuleId());
63          rule.getActions().add(bo);
64          bo.setRule(rule);
65  
66          bo = dataObjectService.save(bo, PersistenceOption.FLUSH);
67  
68          return ActionBo.to(bo);
69      }
70  
71      /**
72       * This overridden method updates an existing Action in the repository.
73       */
74      @Override
75      public void updateAction(ActionDefinition action) {
76          if (action == null){
77              throw new IllegalArgumentException("action is null");
78          }
79  
80          // must already exist to be able to update
81          final String actionIdKey = action.getId();
82          final ActionBo existing = dataObjectService.find(ActionBo.class, actionIdKey);
83  
84          if (existing == null) {
85              throw new IllegalStateException("the action does not exist: " + action);
86          }
87  
88          final ActionDefinition toUpdate;
89  
90          if (existing.getId().equals(action.getId())) {
91              toUpdate = action;
92          } else {
93              // if passed in id does not match existing id, correct it
94              final ActionDefinition.Builder builder = ActionDefinition.Builder.create(action);
95              builder.setId(existing.getId());
96              toUpdate = builder.build();
97          }
98  
99          // copy all updateable fields to bo
100         ActionBo boToUpdate = ActionBo.from(toUpdate);
101 
102         // delete any old, existing attributes
103         deleteMatching(dataObjectService, ActionAttributeBo.class, Collections.singletonMap("action.id", toUpdate.getId()));
104 
105         // update the action and create new attributes
106         dataObjectService.save(boToUpdate, PersistenceOption.FLUSH);
107     }
108 
109     /**
110      * This overridden method retrieves an Action from the repository.
111      */
112     @Override
113     public ActionDefinition getActionByActionId(String actionId) {
114         if (StringUtils.isBlank(actionId)){
115             throw new IllegalArgumentException("action ID is null or blank");
116         }
117 
118         ActionBo bo = dataObjectService.find(ActionBo.class, actionId);
119 
120         return ActionBo.to(bo);
121     }
122 
123     /**
124      * This overridden method retrieves an Action from the repository.
125      */
126     @Override
127     public ActionDefinition getActionByNameAndNamespace(String name, String namespace) {
128         if (StringUtils.isBlank(name)) {
129             throw new IllegalArgumentException("name is blank");
130         }
131         if (StringUtils.isBlank(namespace)) {
132             throw new IllegalArgumentException("namespace is blank");
133         }
134 
135         final Map<String, Object> map = new HashMap<String, Object>();
136         map.put("name", name);
137         map.put("namespace", namespace);
138 
139         ActionBo myAction = findSingleMatching(dataObjectService, ActionBo.class, map);
140 
141         return ActionBo.to(myAction);
142     }
143 
144     /**
145      * This overridden method retrieves a List of Actions associated with a Rule.
146      */
147     @Override
148     public List<ActionDefinition> getActionsByRuleId(String ruleId) {
149         if (StringUtils.isBlank(ruleId)){
150             throw new IllegalArgumentException("ruleId is null or blank");
151         }
152 
153         List<ActionBo> bos = findMatchingOrderBy(dataObjectService, ActionBo.class,
154                 Collections.singletonMap("ruleId", ruleId), "sequenceNumber", true);
155 
156         return convertListOfBosToImmutables(bos);
157     }
158 
159     /**
160      * This overridden method retrieves a specific Action associated with a Rule.
161      */
162     @Override
163     public ActionDefinition getActionByRuleIdAndSequenceNumber(String ruleId, Integer sequenceNumber) {
164         if (StringUtils.isBlank(ruleId)) {
165             throw new IllegalArgumentException("ruleId is null or blank");
166         }
167         if (sequenceNumber == null) {
168             throw new IllegalArgumentException("sequenceNumber is null");
169         }
170 
171         final Map<String, Object> map = new HashMap<String, Object>();
172         map.put("ruleId", ruleId);
173         map.put("sequenceNumber", sequenceNumber);
174         ActionBo bo = dataObjectService.find(ActionBo.class, map);
175 
176         return ActionBo.to(bo);
177     }
178 
179     /**
180      * This method retrieves an ActionAttributeBo by id
181      *
182      * @see org.kuali.rice.krms.impl.repository.ActionBoService#getActionsByRuleId(java.lang.String)
183      */
184     public ActionAttributeBo getActionAttributeById(String attrId) {
185         if (StringUtils.isBlank(attrId)){
186             return null;
187         }
188 
189         return dataObjectService.find(ActionAttributeBo.class, attrId);
190     }
191 
192     /**
193      * Sets the dataObjectService attribute value.
194      *
195      * @param dataObjectService The dataObjectService to set.
196      */
197     public void setDataObjectService(final DataObjectService dataObjectService) {
198         this.dataObjectService = dataObjectService;
199     }
200 
201     /**
202      * Converts a List<ActionBo> to an Unmodifiable List<Action>
203      *
204      * @param actionBos a mutable List<ActionBo> to made completely immutable.
205      * @return An unmodifiable List<Action>
206      */
207     List<ActionDefinition> convertListOfBosToImmutables(final Collection<ActionBo> actionBos) {
208         if (actionBos == null) { return Collections.emptyList(); }
209 
210         ArrayList<ActionDefinition> actions = new ArrayList<ActionDefinition>();
211 
212         for (ActionBo bo : actionBos) {
213             ActionDefinition action = ActionBo.to(bo);
214             actions.add(action);
215         }
216 
217         return Collections.unmodifiableList(actions);
218     }
219 
220 
221 }