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