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 java.util.ArrayList; |
19 | |
import java.util.Collections; |
20 | |
import java.util.HashMap; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
|
24 | |
import org.apache.commons.lang.StringUtils; |
25 | |
import org.kuali.rice.krad.service.BusinessObjectService; |
26 | |
import org.kuali.rice.krms.api.repository.function.FunctionDefinition; |
27 | |
import org.kuali.rice.krms.api.repository.function.FunctionRepositoryService; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | public class FunctionBoServiceImpl implements FunctionRepositoryService, FunctionBoService { |
36 | |
|
37 | |
private BusinessObjectService businessObjectService; |
38 | |
|
39 | |
@Override |
40 | |
public FunctionDefinition getFunction(String functionId) { |
41 | 0 | return getFunctionById(functionId); |
42 | |
} |
43 | |
|
44 | |
@Override |
45 | |
public List<FunctionDefinition> getFunctions(List<String> functionIds) { |
46 | |
|
47 | 0 | List<FunctionDefinition> functionDefinitions = new ArrayList<FunctionDefinition>(); |
48 | 0 | for (String functionId : functionIds){ |
49 | 0 | functionDefinitions.add( getFunctionById(functionId) ); |
50 | |
} |
51 | 0 | return Collections.unmodifiableList(functionDefinitions); |
52 | |
} |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
@Override |
61 | |
public FunctionDefinition createFunction(FunctionDefinition function) { |
62 | 0 | if (function == null){ |
63 | 0 | throw new IllegalArgumentException("function is null"); |
64 | |
} |
65 | |
|
66 | 0 | final String nameKey = function.getName(); |
67 | 0 | final String namespaceKey = function.getNamespace(); |
68 | 0 | final FunctionDefinition existing = getFunctionByNameAndNamespace(nameKey, namespaceKey); |
69 | 0 | if (existing != null && existing.getName().equals(nameKey) && existing.getNamespace().equals(namespaceKey)){ |
70 | 0 | throw new IllegalStateException("the function to create already exists: " + function); |
71 | |
} |
72 | |
|
73 | 0 | FunctionBo functionBo = FunctionBo.from(function); |
74 | 0 | businessObjectService.save(functionBo); |
75 | 0 | return FunctionBo.to(functionBo); |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
@Override |
84 | |
public void updateFunction(FunctionDefinition function) { |
85 | 0 | if (function == null){ |
86 | 0 | throw new IllegalArgumentException("function is null"); |
87 | |
} |
88 | |
|
89 | 0 | final String functionIdKey = function.getId(); |
90 | 0 | final FunctionDefinition existing = getFunctionById(functionIdKey); |
91 | 0 | if (existing == null) { |
92 | 0 | throw new IllegalStateException("the function does not exist: " + function); |
93 | |
} |
94 | |
final FunctionDefinition toUpdate; |
95 | 0 | if (!existing.getId().equals(function.getId())){ |
96 | 0 | final FunctionDefinition.Builder builder = FunctionDefinition.Builder.create(function); |
97 | 0 | builder.setId(existing.getId()); |
98 | 0 | toUpdate = builder.build(); |
99 | 0 | } else { |
100 | 0 | toUpdate = function; |
101 | |
} |
102 | |
|
103 | 0 | businessObjectService.save(FunctionBo.from(toUpdate)); |
104 | 0 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
@Override |
113 | |
public FunctionDefinition getFunctionById(String functionId) { |
114 | 0 | if (StringUtils.isBlank(functionId)){ |
115 | 0 | throw new IllegalArgumentException("functionId is null or blank"); |
116 | |
} |
117 | 0 | FunctionBo functionBo = businessObjectService.findBySinglePrimaryKey(FunctionBo.class, functionId); |
118 | 0 | return FunctionBo.to(functionBo); |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public FunctionDefinition getFunctionByNameAndNamespace( String name, String namespace ){ |
128 | 0 | if (StringUtils.isBlank(name)){ |
129 | 0 | throw new IllegalArgumentException("name is null or blank"); |
130 | |
} |
131 | 0 | if (StringUtils.isBlank(namespace)){ |
132 | 0 | throw new IllegalArgumentException("namespace is null or blank"); |
133 | |
} |
134 | |
|
135 | 0 | final Map<String, Object> map = new HashMap<String, Object>(); |
136 | 0 | map.put("name", name); |
137 | 0 | map.put("namespace", namespace); |
138 | 0 | FunctionBo functionBo = businessObjectService.findByPrimaryKey(FunctionBo.class, Collections.unmodifiableMap(map)); |
139 | 0 | return FunctionBo.to(functionBo); |
140 | |
} |
141 | |
|
142 | |
public void setBusinessObjectService(BusinessObjectService businessObjectService) { |
143 | 0 | this.businessObjectService = businessObjectService; |
144 | 0 | } |
145 | |
} |