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