001 /**
002 * Copyright 2005-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.krms.impl.repository;
017
018
019 import java.util.ArrayList;
020 import java.util.Collection;
021 import java.util.Collections;
022 import java.util.HashMap;
023 import java.util.List;
024 import java.util.Map;
025
026 import org.apache.commons.lang.StringUtils;
027 import org.kuali.rice.krad.service.BusinessObjectService;
028 import org.kuali.rice.krms.api.repository.action.ActionDefinition;
029 import org.kuali.rice.krms.impl.util.KrmsImplConstants.PropertyNames;
030
031 public final class ActionBoServiceImpl implements ActionBoService {
032
033 private BusinessObjectService businessObjectService;
034
035 /**
036 * This overridden method creates a KRMS Action in the repository.
037 */
038 @Override
039 public ActionDefinition createAction(ActionDefinition action) {
040 if (action == null){
041 throw new IllegalArgumentException("action is null");
042 }
043 final String actionNameKey = action.getName();
044 final String actionNamespaceKey = action.getNamespace();
045 final ActionDefinition existing = getActionByNameAndNamespace(actionNameKey, actionNamespaceKey);
046 if (existing != null){
047 throw new IllegalStateException("the action to create already exists: " + action);
048 }
049
050 ActionBo bo = ActionBo.from(action);
051 businessObjectService.save(bo);
052
053 return ActionBo.to(bo);
054 }
055
056 /**
057 * This overridden method updates an existing Action in the repository.
058 */
059 @Override
060 public void updateAction(ActionDefinition action) {
061 if (action == null){
062 throw new IllegalArgumentException("action is null");
063 }
064
065 // must already exist to be able to update
066 final String actionIdKey = action.getId();
067 final ActionBo existing = businessObjectService.findBySinglePrimaryKey(ActionBo.class, actionIdKey);
068 if (existing == null) {
069 throw new IllegalStateException("the action does not exist: " + action);
070 }
071 final ActionDefinition toUpdate;
072 if (existing.getId().equals(action.getId())) {
073 toUpdate = action;
074 } else {
075 // if passed in id does not match existing id, correct it
076 final ActionDefinition.Builder builder = ActionDefinition.Builder.create(action);
077 builder.setId(existing.getId());
078 toUpdate = builder.build();
079 }
080
081 // copy all updateable fields to bo
082 ActionBo boToUpdate = ActionBo.from(toUpdate);
083
084 // delete any old, existing attributes
085 Map<String,String> fields = new HashMap<String,String>(1);
086 fields.put(PropertyNames.Action.ACTION_ID, toUpdate.getId());
087 businessObjectService.deleteMatching(ActionAttributeBo.class, fields);
088
089 // update the action and create new attributes
090 businessObjectService.save(boToUpdate);
091 }
092
093 /**
094 * This overridden method retrieves an Action from the repository.
095 */
096 @Override
097 public ActionDefinition getActionByActionId(String actionId) {
098 if (StringUtils.isBlank(actionId)){
099 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 }