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.criteria.CriteriaLookupService;
21 import org.kuali.rice.core.api.criteria.QueryByCriteria;
22 import org.kuali.rice.core.api.criteria.QueryResults;
23 import org.kuali.rice.krad.data.DataObjectService;
24 import org.kuali.rice.krad.data.PersistenceOption;
25 import org.kuali.rice.krad.service.KRADServiceLocator;
26 import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
27
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34
35 import static org.kuali.rice.core.api.criteria.PredicateFactory.in;
36 import static org.kuali.rice.krms.impl.repository.BusinessObjectServiceMigrationUtils.findMatching;
37
38 public final class KrmsAttributeDefinitionServiceImpl implements KrmsAttributeDefinitionService {
39 private CriteriaLookupService criteriaLookupService;
40
41 private DataObjectService dataObjectService;
42
43
44
45
46 @Override
47 public Map<String,String> convertAttributeKeys(Map<String,String> attributesByName, String namespace) {
48 Map<String,String> attributesById = new HashMap<String,String>();
49
50 if(attributesByName != null) {
51 for(Map.Entry<String, String> attr : attributesByName.entrySet()) {
52 String newKey = getKrmsAttributeId(attr.getKey(), namespace);
53 if(StringUtils.isNotEmpty(newKey)) {
54 attributesById.put(newKey, attr.getValue());
55 }
56 }
57 }
58
59 return attributesById;
60 }
61
62
63
64
65 @Override
66 public String getKrmsAttributeId( String attributeName, String namespace) {
67 String returnId = null;
68
69 KrmsAttributeDefinitionBo bo = getKrmsAttributeBo(attributeName, namespace);
70 if (bo != null){
71 returnId = bo.getId();
72 }
73
74 return returnId;
75 }
76
77
78
79
80 @Override
81 public KrmsAttributeDefinitionBo getKrmsAttributeBo( String attributeName, String namespace) {
82 KrmsAttributeDefinitionBo result = null;
83 Map<String,Object> criteria = new HashMap<String,Object>( 3 );
84 criteria.put( "name", attributeName );
85 criteria.put( "namespace", namespace );
86 criteria.put( "active", Boolean.TRUE );
87 Collection<KrmsAttributeDefinitionBo> defs =
88 findMatching(getDataObjectService(), KrmsAttributeDefinitionBo.class, criteria );
89
90 if(CollectionUtils.isNotEmpty(defs)) {
91 if (defs.size() > 1){
92 throw new IllegalStateException("Multiple KrmsAttributeDefinitions found with same name and namespace");
93 }
94
95 result = defs.iterator().next();
96 }
97
98 return result;
99 }
100
101
102
103
104 @Override
105 public KrmsAttributeDefinition createAttributeDefinition(KrmsAttributeDefinition attributeDefinition) {
106 if (attributeDefinition == null){
107 throw new IllegalArgumentException("attributeDefinition is null");
108 }
109
110 final String nameKey = attributeDefinition.getName();
111 final String namespaceKey = attributeDefinition.getNamespace();
112 final KrmsAttributeDefinition existing = getAttributeDefinitionByNameAndNamespace(nameKey, namespaceKey);
113
114 if (existing != null && existing.getName().equals(nameKey) && existing.getNamespace().equals(namespaceKey)){
115 throw new IllegalStateException("the krms attribute definition to create already exists: " + attributeDefinition);
116 }
117
118 KrmsAttributeDefinitionBo bo = KrmsAttributeDefinitionBo.from(attributeDefinition);
119 bo = getDataObjectService().save(bo, PersistenceOption.FLUSH);
120
121 return KrmsAttributeDefinitionBo.to(bo);
122 }
123
124
125
126
127 @Override
128 public void updateAttributeDefinition(KrmsAttributeDefinition attributeDefinition) {
129 if (attributeDefinition == null){
130 throw new IllegalArgumentException("attributeDefinition is null");
131 }
132
133 final String idKey = attributeDefinition.getId();
134 final KrmsAttributeDefinitionBo existing = getDataObjectService().find(KrmsAttributeDefinitionBo.class, idKey);
135
136 if (existing == null){
137 throw new IllegalStateException("the krms attribute definition does not exist: " + attributeDefinition);
138 }
139
140 final KrmsAttributeDefinition toUpdate;
141
142 if (!existing.getId().equals(attributeDefinition.getId())){
143 final KrmsAttributeDefinition.Builder builder = KrmsAttributeDefinition.Builder.create(attributeDefinition);
144 builder.setId(existing.getId());
145 toUpdate = builder.build();
146 } else {
147 toUpdate = attributeDefinition;
148 }
149
150 KrmsAttributeDefinitionBo bo = KrmsAttributeDefinitionBo.from(toUpdate);
151
152 getDataObjectService().save(bo, PersistenceOption.FLUSH);
153 }
154
155
156
157
158 @Override
159 public KrmsAttributeDefinition getAttributeDefinitionById(final String id) {
160 if (StringUtils.isBlank(id)) {
161 throw new IllegalArgumentException("id is blank");
162 }
163
164 KrmsAttributeDefinitionBo bo = getDataObjectService().find(KrmsAttributeDefinitionBo.class, id);
165
166 return KrmsAttributeDefinitionBo.to(bo);
167 }
168
169
170
171
172 @Override
173 public KrmsAttributeDefinition getAttributeDefinitionByNameAndNamespace(final String name, final String namespace) {
174 if (StringUtils.isBlank(name)) {
175 throw new IllegalArgumentException("name is blank");
176 }
177 if (StringUtils.isBlank(namespace)) {
178 throw new IllegalArgumentException("namespace is blank");
179 }
180
181 KrmsAttributeDefinitionBo bo = getKrmsAttributeBo(name, namespace);
182
183 if (bo == null) {
184 return null;
185 }
186
187 return KrmsAttributeDefinitionBo.to(bo);
188 }
189
190
191
192
193 @Override
194 public List<KrmsAttributeDefinition> findAttributeDefinitionsByNamespace(final String namespace) {
195 final Map<String, Object> map = new HashMap<String, Object>();
196 map.put("namespace", namespace);
197 map.put("active", Boolean.TRUE);
198 Collection<KrmsAttributeDefinitionBo> krmsAttributeDefinitionBos = findMatching(getDataObjectService(),
199 KrmsAttributeDefinitionBo.class, Collections.unmodifiableMap(map));
200
201 return convertListOfBosToImmutables(krmsAttributeDefinitionBos);
202 }
203
204 @Override
205
206
207
208 public List<KrmsAttributeDefinition> findAttributeDefinitionsByType(final String typeId) {
209
210 List<KrmsAttributeDefinition> results = Collections.emptyList();
211
212 final Map<String, Object> map = new HashMap<String, Object>();
213 map.put("type.id", typeId);
214 map.put("active", Boolean.TRUE);
215 Collection<KrmsTypeAttributeBo> krmsTypeAttributeBos = findMatching(getDataObjectService(),
216 KrmsTypeAttributeBo.class, Collections.unmodifiableMap(map));
217
218 if (!CollectionUtils.isEmpty(krmsTypeAttributeBos)) {
219 String [] inList = new String[krmsTypeAttributeBos.size()];
220 int inListIdex = 0;
221 for (KrmsTypeAttributeBo krmsTypeAttributeBo : krmsTypeAttributeBos) {
222 inList[inListIdex] = krmsTypeAttributeBo.getAttributeDefinitionId();
223 ++inListIdex;
224 }
225
226 QueryByCriteria.Builder qBuilder = QueryByCriteria.Builder.create();
227 qBuilder.setPredicates(in("id", inList));
228 QueryResults<KrmsAttributeDefinitionBo> queryResults =
229 getDataObjectService().findMatching(KrmsAttributeDefinitionBo.class, qBuilder.build());
230 results = convertListOfBosToImmutables(queryResults.getResults());
231 }
232
233 return results;
234 }
235
236
237
238
239
240 @Override
241 public List<KrmsAttributeDefinition> findAllAttributeDefinitions() {
242 final Map<String, Object> map = new HashMap<String, Object>();
243 map.put("active", Boolean.TRUE);
244
245 Collection<KrmsAttributeDefinitionBo> krmsAttributeDefinitionBos = findMatching(getDataObjectService(),
246 KrmsAttributeDefinitionBo.class, Collections.unmodifiableMap(map));
247
248 return convertListOfBosToImmutables(krmsAttributeDefinitionBos);
249 }
250
251
252
253
254
255
256
257 public List<KrmsAttributeDefinition> convertListOfBosToImmutables(final Collection<KrmsAttributeDefinitionBo> krmsAttributeDefinitionBos) {
258 ArrayList<KrmsAttributeDefinition> krmsAttributeDefinitions = new ArrayList<KrmsAttributeDefinition>();
259
260 for (KrmsAttributeDefinitionBo bo : krmsAttributeDefinitionBos) {
261 KrmsAttributeDefinition krmsAttributeDefinition = KrmsAttributeDefinitionBo.to(bo);
262 krmsAttributeDefinitions.add(krmsAttributeDefinition);
263 }
264
265 return Collections.unmodifiableList(krmsAttributeDefinitions);
266 }
267
268
269
270
271
272
273 public void setDataObjectService(final DataObjectService dataObjectService) {
274 this.dataObjectService = dataObjectService;
275 }
276
277
278
279
280
281
282 protected DataObjectService getDataObjectService() {
283 if ( dataObjectService == null ) {
284 dataObjectService = KRADServiceLocator.getDataObjectService();
285 }
286
287 return dataObjectService;
288 }
289
290 }