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