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