1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.krms.impl.repository; |
18 | |
|
19 | |
|
20 | |
import org.apache.commons.lang.StringUtils; |
21 | |
import org.kuali.rice.kns.service.BusinessObjectService; |
22 | |
import org.kuali.rice.krms.api.repository.KrmsAttributeDefinition; |
23 | |
import org.kuali.rice.krms.api.repository.KrmsAttributeDefinitionService; |
24 | |
|
25 | |
import java.util.*; |
26 | |
|
27 | 0 | public final class KrmsAttributeDefinitionServiceImpl implements KrmsAttributeDefinitionService { |
28 | |
|
29 | |
private BusinessObjectService businessObjectService; |
30 | |
|
31 | |
@Override |
32 | |
public void createAttributeDefinition(KrmsAttributeDefinition attributeDefinition) { |
33 | 0 | if (attributeDefinition == null){ |
34 | 0 | throw new IllegalArgumentException("attributeDefinition is null"); |
35 | |
} |
36 | 0 | final String idKey = attributeDefinition.getId(); |
37 | 0 | final KrmsAttributeDefinition existing = getAttributeDefinitionById(idKey); |
38 | 0 | if (existing != null && existing.getId().equals(idKey)){ |
39 | 0 | throw new IllegalStateException("the krms attribute definition to create already exists: " + attributeDefinition); |
40 | |
} |
41 | 0 | businessObjectService.save(KrmsAttributeDefinitionBo.from(attributeDefinition)); |
42 | 0 | } |
43 | |
|
44 | |
@Override |
45 | |
public void updateAttributeDefinition(KrmsAttributeDefinition attributeDefinition) { |
46 | 0 | if (attributeDefinition == null){ |
47 | 0 | throw new IllegalArgumentException("attributeDefinition is null"); |
48 | |
} |
49 | 0 | final String nameKey = attributeDefinition.getName(); |
50 | 0 | final String namespaceKey = attributeDefinition.getNamespace(); |
51 | 0 | final KrmsAttributeDefinition existing = getAttributeDefinitionByNameAndNamespace(nameKey, namespaceKey); |
52 | 0 | if (existing == null){ |
53 | 0 | throw new IllegalStateException("the krms attribute definition does not exist: " + attributeDefinition); |
54 | |
} |
55 | |
final KrmsAttributeDefinition toUpdate; |
56 | 0 | if (!existing.getId().equals(attributeDefinition.getId())){ |
57 | 0 | final KrmsAttributeDefinition.Builder builder = KrmsAttributeDefinition.Builder.create(attributeDefinition); |
58 | 0 | builder.setId(existing.getId()); |
59 | 0 | toUpdate = builder.build(); |
60 | 0 | } else { |
61 | 0 | toUpdate = attributeDefinition; |
62 | |
} |
63 | |
|
64 | 0 | businessObjectService.save(KrmsAttributeDefinitionBo.from(toUpdate)); |
65 | 0 | } |
66 | |
|
67 | |
@Override |
68 | |
public KrmsAttributeDefinition getAttributeDefinitionById(final String id) { |
69 | 0 | if (StringUtils.isBlank(id)) { |
70 | 0 | throw new IllegalArgumentException("id is blank"); |
71 | |
} |
72 | 0 | KrmsAttributeDefinitionBo bo = businessObjectService.findBySinglePrimaryKey(KrmsAttributeDefinitionBo.class, id); |
73 | 0 | return KrmsAttributeDefinitionBo.to(bo); |
74 | |
} |
75 | |
|
76 | |
@Override |
77 | |
public KrmsAttributeDefinition getAttributeDefinitionByNameAndNamespace(final String name, final String namespace) { |
78 | 0 | if (StringUtils.isBlank(name)) { |
79 | 0 | throw new IllegalArgumentException("name is blank"); |
80 | |
} |
81 | 0 | if (StringUtils.isBlank(namespace)) { |
82 | 0 | throw new IllegalArgumentException("namespace is blank"); |
83 | |
} |
84 | 0 | final Map<String, Object> map = new HashMap<String, Object>(); |
85 | 0 | map.put("name", name); |
86 | 0 | map.put("namespace", namespace); |
87 | 0 | Collection<KrmsAttributeDefinitionBo> definitionList = businessObjectService.findMatching(KrmsAttributeDefinitionBo.class, Collections.unmodifiableMap(map)); |
88 | 0 | if (definitionList == null || definitionList.isEmpty()) { |
89 | 0 | return null; |
90 | 0 | } else if (definitionList.size() == 1) { |
91 | 0 | return KrmsAttributeDefinitionBo.to(definitionList.iterator().next()); |
92 | 0 | } else throw new IllegalStateException("Multiple KkrmsAttributeDefinitions found with same name and namespace"); |
93 | |
} |
94 | |
|
95 | |
@Override |
96 | |
public List<KrmsAttributeDefinition> findAttributeDefinitionsByNamespace(final String namespace) { |
97 | 0 | final Map<String, Object> map = new HashMap<String, Object>(); |
98 | 0 | map.put("namespace", namespace); |
99 | 0 | map.put("active", Boolean.TRUE); |
100 | 0 | Collection<KrmsAttributeDefinitionBo> krmsAttributeDefinitionBos = businessObjectService.findMatching(KrmsAttributeDefinitionBo.class, Collections.unmodifiableMap(map)); |
101 | 0 | return convertListOfBosToImmutables(krmsAttributeDefinitionBos); |
102 | |
} |
103 | |
|
104 | |
@Override |
105 | |
public List<KrmsAttributeDefinition> findAllAttributeDefinitions() { |
106 | 0 | final Map<String, Object> map = new HashMap<String, Object>(); |
107 | 0 | map.put("active", Boolean.TRUE); |
108 | |
|
109 | 0 | Collection<KrmsAttributeDefinitionBo> krmsAttributeDefinitionBos = businessObjectService.findMatching(KrmsAttributeDefinitionBo.class, Collections.unmodifiableMap(map)); |
110 | 0 | return convertListOfBosToImmutables(krmsAttributeDefinitionBos); |
111 | |
} |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
public void setBusinessObjectService(final BusinessObjectService businessObjectService) { |
119 | 0 | this.businessObjectService = businessObjectService; |
120 | 0 | } |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
List<KrmsAttributeDefinition> convertListOfBosToImmutables(final Collection<KrmsAttributeDefinitionBo> krmsAttributeDefinitionBos) { |
129 | 0 | ArrayList<KrmsAttributeDefinition> krmsAttributeDefinitions = new ArrayList<KrmsAttributeDefinition>(); |
130 | 0 | for (KrmsAttributeDefinitionBo bo : krmsAttributeDefinitionBos) { |
131 | 0 | KrmsAttributeDefinition krmsAttributeDefinition = KrmsAttributeDefinitionBo.to(bo); |
132 | 0 | krmsAttributeDefinitions.add(krmsAttributeDefinition); |
133 | 0 | } |
134 | 0 | return Collections.unmodifiableList(krmsAttributeDefinitions); |
135 | |
} |
136 | |
|
137 | |
} |