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