1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.core.impl.component; |
17 | |
|
18 | |
import org.apache.commons.collections.CollectionUtils; |
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.apache.commons.lang.builder.CompareToBuilder; |
21 | |
import org.apache.log4j.Logger; |
22 | |
import org.kuali.rice.core.api.component.Component; |
23 | |
import org.kuali.rice.core.api.component.ComponentService; |
24 | |
import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; |
25 | |
import org.kuali.rice.core.api.util.ChecksumUtils; |
26 | |
import org.kuali.rice.krad.service.BusinessObjectService; |
27 | |
import org.springframework.transaction.annotation.Transactional; |
28 | |
|
29 | |
import java.sql.Timestamp; |
30 | |
import java.util.ArrayList; |
31 | |
import java.util.Collection; |
32 | |
import java.util.Collections; |
33 | |
import java.util.Comparator; |
34 | |
import java.util.HashMap; |
35 | |
import java.util.List; |
36 | |
import java.util.Map; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
@Transactional |
44 | 0 | public class ComponentServiceImpl implements ComponentService { |
45 | |
|
46 | 0 | private static final Logger LOG = Logger.getLogger(ComponentServiceImpl.class); |
47 | |
|
48 | |
private BusinessObjectService businessObjectService; |
49 | |
private ComponentSetDao componentSetDao; |
50 | |
|
51 | |
@Override |
52 | |
public Component getComponentByCode(String namespaceCode, String componentCode) { |
53 | 0 | if (StringUtils.isBlank(namespaceCode)) { |
54 | 0 | throw new RiceIllegalArgumentException("namespaceCode was a null or blank value"); |
55 | |
} |
56 | 0 | if (StringUtils.isBlank(componentCode)) { |
57 | 0 | throw new RiceIllegalArgumentException("componentCode was a null or blank value"); |
58 | |
} |
59 | 0 | Map<String, String> primaryKeys = new HashMap<String, String>(); |
60 | 0 | primaryKeys.put("namespaceCode", namespaceCode); |
61 | 0 | primaryKeys.put("code", componentCode); |
62 | 0 | ComponentBo componentBo = getBusinessObjectService().findByPrimaryKey(ComponentBo.class, primaryKeys); |
63 | 0 | if (componentBo != null) { |
64 | 0 | return ComponentBo.to(componentBo); |
65 | |
} |
66 | 0 | DerivedComponentBo derivedComponentBo = getBusinessObjectService().findByPrimaryKey(DerivedComponentBo.class, primaryKeys); |
67 | 0 | return derivedComponentBo == null ? null : DerivedComponentBo.to(derivedComponentBo); |
68 | |
} |
69 | |
|
70 | |
@Override |
71 | |
public List<Component> getAllComponentsByNamespaceCode(String namespaceCode) { |
72 | 0 | if (StringUtils.isBlank(namespaceCode)) { |
73 | 0 | throw new RiceIllegalArgumentException("namespaceCode was a null or blank value"); |
74 | |
} |
75 | 0 | Map<String, String> criteria = new HashMap<String, String>(); |
76 | 0 | criteria.put("namespaceCode", namespaceCode); |
77 | |
|
78 | 0 | List<Component> components = new ArrayList<Component>(); |
79 | |
|
80 | 0 | Collection<ComponentBo> componentBos = |
81 | |
getBusinessObjectService().findMatching(ComponentBo.class, criteria); |
82 | 0 | Collection<DerivedComponentBo> derivedComponentBos = |
83 | |
getBusinessObjectService().findMatching(DerivedComponentBo.class, criteria); |
84 | 0 | return translateCollections(componentBos, derivedComponentBos); |
85 | |
} |
86 | |
|
87 | |
@Override |
88 | |
public List<Component> getActiveComponentsByNamespaceCode(String namespaceCode) { |
89 | 0 | if (StringUtils.isBlank(namespaceCode)) { |
90 | 0 | throw new RiceIllegalArgumentException("namespaceCode was a null or blank value"); |
91 | |
} |
92 | 0 | Map<String, Object> criteria = new HashMap<String, Object>(); |
93 | 0 | criteria.put("namespaceCode", namespaceCode); |
94 | 0 | criteria.put("active", Boolean.TRUE); |
95 | 0 | Collection<ComponentBo> componentBos = |
96 | |
getBusinessObjectService().findMatching(ComponentBo.class, criteria); |
97 | 0 | criteria.remove("active"); |
98 | 0 | Collection<DerivedComponentBo> derivedComponentBos = |
99 | |
getBusinessObjectService().findMatching(DerivedComponentBo.class, criteria); |
100 | 0 | return translateCollections(componentBos, derivedComponentBos); |
101 | |
} |
102 | |
|
103 | |
@Override |
104 | |
public List<Component> getDerivedComponentSet(String componentSetId) { |
105 | 0 | if (StringUtils.isBlank(componentSetId)) { |
106 | 0 | throw new RiceIllegalArgumentException("componentSetId was a null or blank value"); |
107 | |
} |
108 | 0 | Map<String, Object> criteria = new HashMap<String, Object>(); |
109 | 0 | criteria.put("componentSetId", componentSetId); |
110 | 0 | Collection<DerivedComponentBo> derivedComponentBos = |
111 | |
getBusinessObjectService().findMatching(DerivedComponentBo.class, criteria); |
112 | 0 | return translateCollections(null, derivedComponentBos); |
113 | |
} |
114 | |
|
115 | |
@Override |
116 | |
public void publishDerivedComponents(String componentSetId, List<Component> components) { |
117 | 0 | if (StringUtils.isBlank(componentSetId)) { |
118 | 0 | throw new RiceIllegalArgumentException("componentSetId was a null or blank value"); |
119 | |
} |
120 | 0 | components = validateAndNormalizeComponents(componentSetId, components); |
121 | 0 | LOG.info("Requesting to publish " + components.size() + " derived components for componentSetId=" + componentSetId); |
122 | 0 | ComponentSetBo componentSet = getComponentSetDao().getComponentSet(componentSetId); |
123 | 0 | if (componentSet == null) { |
124 | 0 | componentSet = new ComponentSetBo(); |
125 | 0 | componentSet.setComponentSetId(componentSetId); |
126 | |
} |
127 | 0 | String checksum = calculateChecksum(components); |
128 | 0 | if (!checksum.equals(componentSet.getChecksum())) { |
129 | 0 | LOG.info("Checksums were different, proceeding with update of derived components for componentSetId=" + componentSetId); |
130 | 0 | componentSet.setChecksum(checksum); |
131 | 0 | componentSet.setLastUpdateTimestamp(new Timestamp(System.currentTimeMillis())); |
132 | 0 | if (getComponentSetDao().saveIgnoreLockingFailure(componentSet)) { |
133 | 0 | updateDerivedComponents(componentSetId, components); |
134 | |
} |
135 | |
} else { |
136 | 0 | LOG.info("Checksums were the same, no derived component update needed for componentSetId=" + componentSetId); |
137 | |
} |
138 | 0 | } |
139 | |
|
140 | |
protected List<Component> validateAndNormalizeComponents(String componentSetId, List<Component> components) { |
141 | 0 | List<Component> processedComponents = new ArrayList<Component>(); |
142 | |
|
143 | |
|
144 | 0 | if (components == null) { |
145 | 0 | components = new ArrayList<Component>(); |
146 | |
} else { |
147 | 0 | components = new ArrayList<Component>(components); |
148 | |
} |
149 | |
|
150 | 0 | for (Component component : components) { |
151 | |
|
152 | 0 | if (component.getComponentSetId() == null) { |
153 | 0 | Component.Builder builder = Component.Builder.create(component); |
154 | 0 | builder.setComponentSetId(componentSetId); |
155 | 0 | component = builder.build(); |
156 | |
} |
157 | 0 | String currentComponentSetId = component.getComponentSetId(); |
158 | 0 | if (!componentSetId.equals(currentComponentSetId)) { |
159 | 0 | throw new RiceIllegalArgumentException("Encountered a component with an invalid componentSetId of '" + |
160 | |
currentComponentSetId + "'. Expected null or '" + componentSetId + "'."); |
161 | |
} |
162 | 0 | processedComponents.add(component); |
163 | 0 | } |
164 | 0 | return processedComponents; |
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
protected String calculateChecksum(List<Component> components) { |
174 | 0 | Collections.sort(components, new Comparator<Component>() { |
175 | |
@Override |
176 | |
public int compare(Component component1, Component component2) { |
177 | 0 | return CompareToBuilder.reflectionCompare(component1, component2); |
178 | |
} |
179 | |
}); |
180 | 0 | return ChecksumUtils.calculateChecksum(components); |
181 | |
} |
182 | |
|
183 | |
protected void updateDerivedComponents(String componentSetId, List<Component> components) { |
184 | 0 | Map<String, Object> deleteCriteria = new HashMap<String, Object>(); |
185 | 0 | deleteCriteria.put("componentSetId", componentSetId); |
186 | 0 | businessObjectService.deleteMatching(DerivedComponentBo.class, deleteCriteria); |
187 | 0 | if (CollectionUtils.isNotEmpty(components)) { |
188 | 0 | List<DerivedComponentBo> derivedComponentBos = new ArrayList<DerivedComponentBo>(); |
189 | 0 | for (Component component : components) { |
190 | 0 | derivedComponentBos.add(DerivedComponentBo.from(component)); |
191 | |
} |
192 | 0 | businessObjectService.save(derivedComponentBos); |
193 | |
} |
194 | 0 | } |
195 | |
|
196 | |
protected List<Component> translateCollections(Collection<ComponentBo> componentBos, Collection<DerivedComponentBo> derivedComponentBos) { |
197 | 0 | List<Component> components = new ArrayList<Component>(); |
198 | 0 | if (CollectionUtils.isNotEmpty(componentBos)) { |
199 | 0 | for (ComponentBo componentBo : componentBos) { |
200 | 0 | components.add(ComponentBo.to(componentBo)); |
201 | |
} |
202 | |
} |
203 | 0 | if (CollectionUtils.isNotEmpty(derivedComponentBos)) { |
204 | 0 | for (DerivedComponentBo derivedComponentBo : derivedComponentBos) { |
205 | 0 | components.add(DerivedComponentBo.to(derivedComponentBo)); |
206 | |
} |
207 | |
} |
208 | 0 | return Collections.unmodifiableList(components); |
209 | |
} |
210 | |
|
211 | |
public BusinessObjectService getBusinessObjectService() { |
212 | 0 | return businessObjectService; |
213 | |
} |
214 | |
|
215 | |
public void setBusinessObjectService(BusinessObjectService businessObjectService) { |
216 | 0 | this.businessObjectService = businessObjectService; |
217 | 0 | } |
218 | |
|
219 | |
public ComponentSetDao getComponentSetDao() { |
220 | 0 | return componentSetDao; |
221 | |
} |
222 | |
|
223 | |
public void setComponentSetDao(ComponentSetDao componentSetDao) { |
224 | 0 | this.componentSetDao = componentSetDao; |
225 | 0 | } |
226 | |
|
227 | |
} |