1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
58
59 @Override
60 public void updateAction(ActionDefinition action) {
61 if (action == null){
62 throw new IllegalArgumentException("action is null");
63 }
64
65
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
76 final ActionDefinition.Builder builder = ActionDefinition.Builder.create(action);
77 builder.setId(existing.getId());
78 toUpdate = builder.build();
79 }
80
81
82 ActionBo boToUpdate = ActionBo.from(toUpdate);
83
84
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
90 businessObjectService.save(boToUpdate);
91 }
92
93
94
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
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
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
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
159
160
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
171
172
173
174 public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
175 this.businessObjectService = businessObjectService;
176 }
177
178
179
180
181
182
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 }