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