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