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.collections.CollectionUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
21 import org.kuali.rice.core.api.mo.ModelObjectUtils;
22 import org.kuali.rice.krad.service.BusinessObjectService;
23 import org.kuali.rice.krms.api.repository.function.FunctionDefinition;
24 import org.kuali.rice.krms.api.repository.function.FunctionRepositoryService;
25
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33
34
35
36
37
38
39 public class FunctionBoServiceImpl implements FunctionRepositoryService, FunctionBoService {
40
41 private BusinessObjectService businessObjectService;
42
43
44 private static final ModelObjectUtils.Transformer<FunctionBo, FunctionDefinition> toFunctionDefinition =
45 new ModelObjectUtils.Transformer<FunctionBo, FunctionDefinition>() {
46 public FunctionDefinition transform(FunctionBo input) {
47 return FunctionBo.to(input);
48 };
49 };
50
51 @Override
52 public FunctionDefinition getFunction(String functionId) {
53 return getFunctionById(functionId);
54 }
55
56 @Override
57 public List<FunctionDefinition> getFunctions(List<String> functionIds) {
58
59 if (functionIds == null) throw new RiceIllegalArgumentException();
60
61 List<FunctionDefinition> functionDefinitions = new ArrayList<FunctionDefinition>();
62 for (String functionId : functionIds){
63 if (!StringUtils.isBlank(functionId)) {
64 FunctionDefinition functionDefinition = getFunctionById(functionId);
65 if (functionDefinition != null) {
66 functionDefinitions.add(functionDefinition);
67 }
68 }
69 }
70 return Collections.unmodifiableList(functionDefinitions);
71 }
72
73
74
75
76
77
78
79 @Override
80 public FunctionDefinition createFunction(FunctionDefinition function) {
81 if (function == null){
82 throw new IllegalArgumentException("function is null");
83 }
84
85 final String nameKey = function.getName();
86 final String namespaceKey = function.getNamespace();
87 final FunctionDefinition existing = getFunctionByNameAndNamespace(nameKey, namespaceKey);
88 if (existing != null && existing.getName().equals(nameKey) && existing.getNamespace().equals(namespaceKey)){
89 throw new IllegalStateException("the function to create already exists: " + function);
90 }
91
92 FunctionBo functionBo = FunctionBo.from(function);
93 businessObjectService.save(functionBo);
94 return FunctionBo.to(functionBo);
95 }
96
97
98
99
100
101
102 @Override
103 public void updateFunction(FunctionDefinition function) {
104 if (function == null){
105 throw new IllegalArgumentException("function is null");
106 }
107
108 final String functionIdKey = function.getId();
109 final FunctionDefinition existing = getFunctionById(functionIdKey);
110 if (existing == null) {
111 throw new IllegalStateException("the function does not exist: " + function);
112 }
113 final FunctionDefinition toUpdate;
114 if (!existing.getId().equals(function.getId())){
115 final FunctionDefinition.Builder builder = FunctionDefinition.Builder.create(function);
116 builder.setId(existing.getId());
117 toUpdate = builder.build();
118 } else {
119 toUpdate = function;
120 }
121
122 businessObjectService.save(FunctionBo.from(toUpdate));
123 }
124
125
126
127
128
129
130
131 @Override
132 public FunctionDefinition getFunctionById(String functionId) {
133 if (StringUtils.isBlank(functionId)){
134 throw new RiceIllegalArgumentException("functionId is null or blank");
135 }
136 FunctionBo functionBo = businessObjectService.findBySinglePrimaryKey(FunctionBo.class, functionId);
137 return FunctionBo.to(functionBo);
138 }
139
140
141
142
143
144
145
146 public FunctionDefinition getFunctionByNameAndNamespace( String name, String namespace ){
147 if (StringUtils.isBlank(name)){
148 throw new IllegalArgumentException("name is null or blank");
149 }
150 if (StringUtils.isBlank(namespace)){
151 throw new IllegalArgumentException("namespace is null or blank");
152 }
153
154 final Map<String, Object> map = new HashMap<String, Object>();
155 map.put("name", name);
156 map.put("namespace", namespace);
157 FunctionBo functionBo = businessObjectService.findByPrimaryKey(FunctionBo.class, Collections.unmodifiableMap(map));
158 return FunctionBo.to(functionBo);
159 }
160
161
162
163
164
165
166
167 public List<FunctionDefinition> getFunctionsByNamespace(String namespace) {
168 if (StringUtils.isBlank(namespace)){
169 throw new IllegalArgumentException("namespace is null or blank");
170 }
171
172 final Map<String, Object> map = new HashMap<String, Object>();
173 map.put("namespace", namespace);
174 Collection<FunctionBo> functionBos = businessObjectService.findMatching(FunctionBo.class, map);
175
176 return convertFunctionBosToImmutables(functionBos);
177 }
178
179
180
181
182
183
184
185 private List<FunctionDefinition> convertFunctionBosToImmutables(final Collection<FunctionBo> functionBos) {
186 if (CollectionUtils.isEmpty(functionBos)) {
187 return Collections.emptyList();
188 }
189 return Collections.unmodifiableList(ModelObjectUtils.transform(functionBos, toFunctionDefinition));
190 }
191
192 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
193 this.businessObjectService = businessObjectService;
194 }
195 }