View Javadoc

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