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 org.apache.commons.collections.CollectionUtils; |
20 | |
import org.apache.commons.lang.StringUtils; |
21 | |
import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; |
22 | |
import org.kuali.rice.core.api.exception.RiceIllegalStateException; |
23 | |
import org.kuali.rice.krad.service.BusinessObjectService; |
24 | |
import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition; |
25 | |
import org.kuali.rice.krms.api.repository.type.KrmsTypeAttribute; |
26 | |
import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition; |
27 | |
import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService; |
28 | |
|
29 | |
import javax.jws.WebParam; |
30 | |
import java.util.*; |
31 | |
|
32 | 0 | public final class KrmsTypeBoServiceImpl implements KrmsTypeRepositoryService { |
33 | |
|
34 | |
private BusinessObjectService businessObjectService; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
@Override |
43 | |
public KrmsTypeDefinition createKrmsType(KrmsTypeDefinition krmsType) { |
44 | 0 | if (krmsType == null){ |
45 | 0 | throw new RiceIllegalArgumentException("krmsType is null"); |
46 | |
} |
47 | 0 | final String nameKey = krmsType.getName(); |
48 | 0 | final String namespaceKey = krmsType.getNamespace(); |
49 | 0 | final KrmsTypeDefinition existing = getTypeByName(namespaceKey, nameKey); |
50 | 0 | if (existing != null && existing.getName().equals(nameKey) && existing.getNamespace().equals(namespaceKey)){ |
51 | 0 | throw new RiceIllegalStateException("the KRMS Type to create already exists: " + krmsType); |
52 | |
} |
53 | |
|
54 | 0 | KrmsTypeBo bo = (KrmsTypeBo)businessObjectService.save(KrmsTypeBo.from(krmsType)); |
55 | |
|
56 | 0 | return KrmsTypeBo.to(bo); |
57 | |
} |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
@Override |
65 | |
public void updateKrmsType(KrmsTypeDefinition krmsType) { |
66 | 0 | if (krmsType == null) { |
67 | 0 | throw new RiceIllegalArgumentException("krmsType is null"); |
68 | |
} |
69 | 0 | final String idKey = krmsType.getId(); |
70 | 0 | final KrmsTypeBo existing = businessObjectService.findBySinglePrimaryKey(KrmsTypeBo.class, idKey); |
71 | 0 | if (existing == null) { |
72 | 0 | throw new RiceIllegalStateException("the KRMS type does not exist: " + krmsType); |
73 | |
} |
74 | |
final KrmsTypeDefinition toUpdate; |
75 | 0 | if (!existing.getId().equals(krmsType.getId())){ |
76 | 0 | final KrmsTypeDefinition.Builder builder = KrmsTypeDefinition.Builder.create(krmsType); |
77 | 0 | builder.setId(existing.getId()); |
78 | 0 | toUpdate = builder.build(); |
79 | 0 | } else { |
80 | 0 | toUpdate = krmsType; |
81 | |
} |
82 | |
|
83 | 0 | businessObjectService.save(KrmsTypeBo.from(toUpdate)); |
84 | 0 | } |
85 | |
|
86 | |
@Override |
87 | |
public KrmsTypeDefinition getTypeById(final String id) { |
88 | 0 | if (StringUtils.isBlank(id)) { |
89 | 0 | throw new RiceIllegalArgumentException("id was a null or blank value"); |
90 | |
} |
91 | |
|
92 | 0 | KrmsTypeBo krmsTypeBo = businessObjectService.findBySinglePrimaryKey(KrmsTypeBo.class, id); |
93 | |
|
94 | 0 | return KrmsTypeBo.to(krmsTypeBo); |
95 | |
} |
96 | |
|
97 | |
@Override |
98 | |
public KrmsTypeDefinition getTypeByName(final String namespaceCode, final String name) { |
99 | 0 | if (StringUtils.isBlank(namespaceCode)) { |
100 | 0 | throw new RiceIllegalArgumentException("namespaceCode was a null or blank value"); |
101 | |
} |
102 | 0 | if (StringUtils.isBlank(name)) { |
103 | 0 | throw new RiceIllegalArgumentException("name was a null or blank value"); |
104 | |
} |
105 | 0 | final Map<String, Object> map = new HashMap<String, Object>(); |
106 | 0 | map.put("namespace", namespaceCode); |
107 | 0 | map.put("name", name); |
108 | |
|
109 | 0 | KrmsTypeBo myType = businessObjectService.findByPrimaryKey(KrmsTypeBo.class, Collections.unmodifiableMap(map)); |
110 | 0 | return KrmsTypeBo.to(myType); |
111 | |
} |
112 | |
|
113 | |
@Override |
114 | |
public List<KrmsTypeDefinition> findAllTypesByNamespace(final String namespaceCode) { |
115 | 0 | if (StringUtils.isBlank(namespaceCode)) { |
116 | 0 | throw new RiceIllegalArgumentException("namespaceCode was a null or blank value"); |
117 | |
} |
118 | 0 | final Map<String, Object> map = new HashMap<String, Object>(); |
119 | 0 | map.put("namespace", namespaceCode); |
120 | 0 | map.put("active", Boolean.TRUE); |
121 | |
|
122 | 0 | Collection<KrmsTypeBo> krmsTypeBos = businessObjectService.findMatching(KrmsTypeBo.class, Collections.unmodifiableMap(map)); |
123 | |
|
124 | 0 | return convertListOfBosToImmutables(krmsTypeBos); |
125 | |
} |
126 | |
|
127 | |
@Override |
128 | |
public List<KrmsTypeDefinition> findAllTypes() { |
129 | 0 | final Map<String, Object> map = new HashMap<String, Object>(); |
130 | 0 | map.put("active", Boolean.TRUE); |
131 | |
|
132 | 0 | Collection<KrmsTypeBo> krmsTypeBos = businessObjectService.findMatching(KrmsTypeBo.class, Collections.unmodifiableMap(map)); |
133 | 0 | return convertListOfBosToImmutables(krmsTypeBos); |
134 | |
} |
135 | |
|
136 | |
@Override |
137 | |
public KrmsAttributeDefinition getAttributeDefinitionById(String attributeDefinitionId) { |
138 | 0 | if (StringUtils.isBlank(attributeDefinitionId)) { |
139 | 0 | throw new RiceIllegalArgumentException("attributeDefinitionId was a null or blank value"); |
140 | |
} |
141 | 0 | KrmsAttributeDefinitionBo krmsAttributeDefinitionBo = businessObjectService.findBySinglePrimaryKey(KrmsAttributeDefinitionBo.class, attributeDefinitionId); |
142 | 0 | return KrmsAttributeDefinitionBo.to(krmsAttributeDefinitionBo); |
143 | |
} |
144 | |
|
145 | |
@Override |
146 | |
public KrmsAttributeDefinition getAttributeDefinitionByName(@WebParam(name = "namespaceCode") String namespaceCode, |
147 | |
@WebParam(name = "name") String name) { |
148 | 0 | if (StringUtils.isBlank(namespaceCode)) { |
149 | 0 | throw new RiceIllegalArgumentException("namespaceCode was a null or blank value"); |
150 | |
} |
151 | 0 | if (StringUtils.isBlank(name)) { |
152 | 0 | throw new RiceIllegalArgumentException("name was a null or blank value"); |
153 | |
} |
154 | 0 | final Map<String, Object> criteria = new HashMap<String, Object>(); |
155 | 0 | criteria.put("name", name); |
156 | 0 | criteria.put("namespace", namespaceCode); |
157 | |
|
158 | 0 | Collection<KrmsAttributeDefinitionBo> attributeDefinitionBos = businessObjectService.findMatching(KrmsAttributeDefinitionBo.class, criteria); |
159 | 0 | if (CollectionUtils.isEmpty(attributeDefinitionBos)) { |
160 | 0 | return null; |
161 | |
} |
162 | 0 | return KrmsAttributeDefinitionBo.to(attributeDefinitionBos.iterator().next()); |
163 | |
} |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
public void setBusinessObjectService(final BusinessObjectService businessObjectService) { |
171 | 0 | this.businessObjectService = businessObjectService; |
172 | 0 | } |
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
protected List<KrmsTypeDefinition> convertListOfBosToImmutables(final Collection<KrmsTypeBo> krmsTypeBos) { |
181 | 0 | ArrayList<KrmsTypeDefinition> krmsTypes = new ArrayList<KrmsTypeDefinition>(); |
182 | 0 | for (KrmsTypeBo bo : krmsTypeBos) { |
183 | 0 | KrmsTypeDefinition krmsType = KrmsTypeBo.to(bo); |
184 | 0 | krmsTypes.add(krmsType); |
185 | 0 | } |
186 | 0 | return Collections.unmodifiableList(krmsTypes); |
187 | |
} |
188 | |
|
189 | |
} |