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