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 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.deleteMatching;
31 import static org.kuali.rice.krms.impl.repository.BusinessObjectServiceMigrationUtils.findMatchingOrderBy;
32 import static org.kuali.rice.krms.impl.repository.BusinessObjectServiceMigrationUtils.findSingleMatching;
33
34
35
36
37
38
39
40 public class ActionBoServiceImpl implements ActionBoService {
41
42 private DataObjectService dataObjectService;
43
44
45
46
47 @Override
48 public ActionDefinition createAction(ActionDefinition action) {
49 if (action == null) {
50 throw new IllegalArgumentException("action is null");
51 }
52
53 final String actionNameKey = action.getName();
54 final String actionNamespaceKey = action.getNamespace();
55 final ActionDefinition existing = getActionByNameAndNamespace(actionNameKey, actionNamespaceKey);
56
57 if (existing != null) {
58 throw new IllegalStateException("the action to create already exists: " + action);
59 }
60
61 ActionBo bo = ActionBo.from(action);
62 RuleBo rule = new RuleBo();
63 rule.setId(action.getRuleId());
64 rule.getActions().add(bo);
65 bo.setRule(rule);
66
67 bo = dataObjectService.save(bo, PersistenceOption.FLUSH);
68
69 return ActionBo.to(bo);
70 }
71
72
73
74
75 @Override
76 public ActionDefinition updateAction(ActionDefinition action) {
77 if (action == null) {
78 throw new IllegalArgumentException("action is null");
79 }
80
81
82 final String actionIdKey = action.getId();
83 final ActionBo existing = dataObjectService.find(ActionBo.class, actionIdKey);
84
85 if (existing == null) {
86 throw new IllegalStateException("the action does not exist: " + action);
87 }
88
89 final ActionDefinition toUpdate;
90
91 if (existing.getId().equals(action.getId())) {
92 toUpdate = action;
93 } else {
94
95 final ActionDefinition.Builder builder = ActionDefinition.Builder.create(action);
96 builder.setId(existing.getId());
97 toUpdate = builder.build();
98 }
99
100
101 ActionBo boToUpdate = ActionBo.from(toUpdate);
102
103
104 deleteMatching(dataObjectService, ActionAttributeBo.class, Collections.singletonMap("action.id",
105 toUpdate.getId()));
106
107
108 final ActionBo updatedData = dataObjectService.save(boToUpdate, PersistenceOption.FLUSH);
109
110 return ActionBo.to(updatedData);
111 }
112
113
114
115
116 @Override
117 public ActionDefinition getActionByActionId(String actionId) {
118 if (StringUtils.isBlank(actionId)) {
119 throw new IllegalArgumentException("action ID is null or blank");
120 }
121
122 ActionBo bo = dataObjectService.find(ActionBo.class, actionId);
123
124 return ActionBo.to(bo);
125 }
126
127
128
129
130 @Override
131 public ActionDefinition getActionByNameAndNamespace(String name, String namespace) {
132 if (StringUtils.isBlank(name)) {
133 throw new IllegalArgumentException("name is blank");
134 }
135 if (StringUtils.isBlank(namespace)) {
136 throw new IllegalArgumentException("namespace is blank");
137 }
138
139 final Map<String, Object> map = new HashMap<String, Object>();
140 map.put("name", name);
141 map.put("namespace", namespace);
142
143 ActionBo myAction = findSingleMatching(dataObjectService, ActionBo.class, map);
144
145 return ActionBo.to(myAction);
146 }
147
148
149
150
151 @Override
152 public List<ActionDefinition> getActionsByRuleId(String ruleId) {
153 if (StringUtils.isBlank(ruleId)) {
154 throw new IllegalArgumentException("ruleId is null or blank");
155 }
156
157 List<ActionBo> bos = findMatchingOrderBy(dataObjectService, ActionBo.class, Collections.singletonMap("ruleId",
158 ruleId), "sequenceNumber", true);
159
160 return convertListOfBosToImmutables(bos);
161 }
162
163
164
165
166 @Override
167 public ActionDefinition getActionByRuleIdAndSequenceNumber(String ruleId, Integer sequenceNumber) {
168 if (StringUtils.isBlank(ruleId)) {
169 throw new IllegalArgumentException("ruleId is null or blank");
170 }
171 if (sequenceNumber == null) {
172 throw new IllegalArgumentException("sequenceNumber is null");
173 }
174
175 final Map<String, Object> map = new HashMap<String, Object>();
176 map.put("ruleId", ruleId);
177 map.put("sequenceNumber", sequenceNumber);
178 ActionBo bo = dataObjectService.find(ActionBo.class, map);
179
180 return ActionBo.to(bo);
181 }
182
183
184
185
186
187
188 public ActionAttributeBo getActionAttributeById(String attrId) {
189 if (StringUtils.isBlank(attrId)) {
190 return null;
191 }
192
193 return dataObjectService.find(ActionAttributeBo.class, attrId);
194 }
195
196
197
198
199
200
201 public void setDataObjectService(final DataObjectService dataObjectService) {
202 this.dataObjectService = dataObjectService;
203 }
204
205
206
207
208
209
210
211 List<ActionDefinition> convertListOfBosToImmutables(final Collection<ActionBo> actionBos) {
212 if (actionBos == null) {
213 return Collections.emptyList();
214 }
215
216 ArrayList<ActionDefinition> actions = new ArrayList<ActionDefinition>();
217
218 for (ActionBo bo : actionBos) {
219 ActionDefinition action = ActionBo.to(bo);
220 actions.add(action);
221 }
222
223 return Collections.unmodifiableList(actions);
224 }
225 }