1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.service.impl; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.apache.log4j.Logger; |
20 | |
import org.kuali.rice.core.api.component.Component; |
21 | |
import org.kuali.rice.core.api.component.ComponentService; |
22 | |
import org.kuali.rice.core.api.config.ConfigurationException; |
23 | |
import org.kuali.rice.core.framework.parameter.ParameterConstants; |
24 | |
import org.kuali.rice.krad.bo.BusinessObject; |
25 | |
import org.kuali.rice.krad.datadictionary.BusinessObjectEntry; |
26 | |
import org.kuali.rice.krad.datadictionary.DocumentEntry; |
27 | |
import org.kuali.rice.krad.datadictionary.TransactionalDocumentEntry; |
28 | |
import org.kuali.rice.krad.document.TransactionalDocument; |
29 | |
import org.kuali.rice.krad.service.DataDictionaryComponentPublisherService; |
30 | |
import org.kuali.rice.krad.service.DataDictionaryService; |
31 | |
import org.kuali.rice.krad.service.KualiModuleService; |
32 | |
import org.kuali.rice.krad.util.KRADUtils; |
33 | |
|
34 | |
import java.util.ArrayList; |
35 | |
import java.util.HashMap; |
36 | |
import java.util.List; |
37 | |
import java.util.Map; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 0 | public class DataDictionaryComponentPublisherServiceImpl implements DataDictionaryComponentPublisherService { |
47 | |
|
48 | 0 | private static final Logger LOG = Logger.getLogger(DataDictionaryComponentPublisherServiceImpl.class); |
49 | |
|
50 | |
private static final String DEFAULT_COMPONENT_SET_ID_PREFIX = "DD:"; |
51 | |
|
52 | |
private DataDictionaryService dataDictionaryService; |
53 | |
private KualiModuleService kualiModuleService; |
54 | |
private ComponentService componentService; |
55 | |
private String applicationId; |
56 | |
|
57 | |
@Override |
58 | |
public void publishAllComponents() { |
59 | 0 | List<Component> componentsToPublish = getComponentsToPublish(); |
60 | 0 | getComponentService().publishDerivedComponents(generateComponentSetId(), componentsToPublish); |
61 | 0 | } |
62 | |
|
63 | |
protected String generateComponentSetId() { |
64 | 0 | if (StringUtils.isBlank(getApplicationId())) { |
65 | 0 | throw new ConfigurationException("A valid non-null, non-blank application id was not injected into " + getClass().getName()); |
66 | |
} |
67 | 0 | return DEFAULT_COMPONENT_SET_ID_PREFIX + getApplicationId(); |
68 | |
} |
69 | |
|
70 | |
protected List<Component> getComponentsToPublish() { |
71 | 0 | List<Component> components = new ArrayList<Component>(); |
72 | |
|
73 | 0 | Map<String, Component> uniqueComponentMap = new HashMap<String, Component>(); |
74 | 0 | for (BusinessObjectEntry businessObjectEntry : getDataDictionaryService().getDataDictionary().getBusinessObjectEntries().values()) { |
75 | |
try { |
76 | 0 | Component component = deriveComponentFromBusinessObjectEntry(businessObjectEntry); |
77 | 0 | uniqueComponentMap.put(component.getCode(), component); |
78 | |
} |
79 | 0 | catch (Exception e) { |
80 | 0 | LOG.error("An exception was encountered when attempting to publish all components for business object class: " + businessObjectEntry.getBusinessObjectClass(), e); |
81 | 0 | } |
82 | |
} |
83 | 0 | for (DocumentEntry documentEntry : getDataDictionaryService().getDataDictionary().getDocumentEntries().values()) { |
84 | 0 | if (documentEntry instanceof TransactionalDocumentEntry) { |
85 | |
try { |
86 | 0 | Component component = deriveComponentFromDocumentEntry(documentEntry); |
87 | 0 | uniqueComponentMap.put(component.getCode(), component); |
88 | |
} |
89 | 0 | catch (Exception e) { |
90 | 0 | LOG.error("An exception was encountered when attempting to publish all components for transactional document class: " + documentEntry.getDocumentClass(), e); |
91 | 0 | } |
92 | |
} |
93 | |
} |
94 | 0 | components.addAll(uniqueComponentMap.values()); |
95 | 0 | return components; |
96 | |
} |
97 | |
|
98 | |
protected Component deriveComponentFromClass(Class<?> componentSourceClass) { |
99 | 0 | String componentCode = getKualiModuleService().getComponentCode(componentSourceClass); |
100 | 0 | String componentName = deriveComponentName(componentSourceClass); |
101 | 0 | String namespace = getKualiModuleService().getNamespaceCode(componentSourceClass); |
102 | 0 | if (StringUtils.isBlank(componentName)) { |
103 | 0 | componentName = componentCode; |
104 | |
} |
105 | 0 | Component.Builder detailType = Component.Builder.create(namespace, componentCode, componentName); |
106 | 0 | return detailType.build(); |
107 | |
} |
108 | |
|
109 | |
protected Component deriveComponentFromBusinessObjectEntry(BusinessObjectEntry businessObjectEntry) { |
110 | 0 | Class<?> businessObjectClass = businessObjectEntry.getBaseBusinessObjectClass(); |
111 | 0 | if (businessObjectClass == null) { |
112 | 0 | businessObjectClass = businessObjectEntry.getBusinessObjectClass(); |
113 | |
} |
114 | 0 | return deriveComponentFromClass(businessObjectClass); |
115 | |
} |
116 | |
|
117 | |
protected Component deriveComponentFromDocumentEntry(DocumentEntry documentEntry) { |
118 | 0 | Class<?> documentClass = documentEntry.getBaseDocumentClass(); |
119 | 0 | if (documentClass == null) { |
120 | 0 | documentClass = documentEntry.getDocumentClass(); |
121 | |
} |
122 | 0 | return deriveComponentFromClass(documentClass); |
123 | |
} |
124 | |
|
125 | |
protected String deriveComponentName(Class<?> componentSourceClass) { |
126 | 0 | if (componentSourceClass == null) { |
127 | 0 | throw new IllegalArgumentException("The deriveComponentName method requires non-null componentSourceClass"); |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | 0 | if (componentSourceClass.isAnnotationPresent(ParameterConstants.COMPONENT.class)) { |
137 | 0 | BusinessObjectEntry boe = getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(componentSourceClass.getName()); |
138 | 0 | if (boe != null) { |
139 | 0 | return boe.getObjectLabel(); |
140 | |
} |
141 | |
else { |
142 | 0 | return ((ParameterConstants.COMPONENT) componentSourceClass.getAnnotation(ParameterConstants.COMPONENT.class)).component(); |
143 | |
} |
144 | |
} |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | 0 | if (TransactionalDocument.class.isAssignableFrom(componentSourceClass)) { |
153 | 0 | return getDataDictionaryService().getDocumentLabelByClass(componentSourceClass); |
154 | |
} |
155 | 0 | else if (BusinessObject.class.isAssignableFrom(componentSourceClass) ) { |
156 | 0 | BusinessObjectEntry boe = getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(componentSourceClass.getName()); |
157 | 0 | if (boe != null) { |
158 | 0 | return boe.getObjectLabel(); |
159 | |
} |
160 | |
else { |
161 | 0 | return KRADUtils.getBusinessTitleForClass(componentSourceClass); |
162 | |
} |
163 | |
} |
164 | 0 | throw new IllegalArgumentException("The deriveComponentName method of requires TransactionalDocument or BusinessObject class. Was: " + componentSourceClass.getName() ); |
165 | |
} |
166 | |
|
167 | |
public DataDictionaryService getDataDictionaryService() { |
168 | 0 | return dataDictionaryService; |
169 | |
} |
170 | |
|
171 | |
public void setDataDictionaryService(DataDictionaryService dataDictionaryService) { |
172 | 0 | this.dataDictionaryService = dataDictionaryService; |
173 | 0 | } |
174 | |
|
175 | |
public KualiModuleService getKualiModuleService() { |
176 | 0 | return kualiModuleService; |
177 | |
} |
178 | |
|
179 | |
public void setKualiModuleService(KualiModuleService kualiModuleService) { |
180 | 0 | this.kualiModuleService = kualiModuleService; |
181 | 0 | } |
182 | |
|
183 | |
public ComponentService getComponentService() { |
184 | 0 | return componentService; |
185 | |
} |
186 | |
|
187 | |
public void setComponentService(ComponentService componentService) { |
188 | 0 | this.componentService = componentService; |
189 | 0 | } |
190 | |
|
191 | |
public String getApplicationId() { |
192 | 0 | return applicationId; |
193 | |
} |
194 | |
|
195 | |
public void setApplicationId(String applicationId) { |
196 | 0 | this.applicationId = applicationId; |
197 | 0 | } |
198 | |
|
199 | |
} |