1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  package org.kuali.rice.krad.service.impl;
18  
19  import org.kuali.rice.core.api.component.Component;
20  import org.kuali.rice.core.api.config.property.ConfigurationService;
21  import org.kuali.rice.core.framework.parameter.ParameterConstants.COMPONENT;
22  import org.kuali.rice.kns.lookup.LookupUtils;
23  import org.kuali.rice.krad.bo.BusinessObject;
24  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
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.DataDictionaryService;
30  import org.kuali.rice.krad.service.KRADServiceLocator;
31  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
32  import org.kuali.rice.krad.service.KualiModuleService;
33  import org.kuali.rice.krad.service.RiceApplicationConfigurationService;
34  import org.kuali.rice.krad.util.KRADConstants;
35  import org.kuali.rice.krad.util.KRADUtils;
36  
37  import java.util.ArrayList;
38  import java.util.Collections;
39  import java.util.HashMap;
40  import java.util.List;
41  import java.util.Map;
42  
43  
44  public class RiceApplicationConfigurationServiceImpl implements RiceApplicationConfigurationService {
45      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RiceApplicationConfigurationServiceImpl.class);
46      
47      protected List<Component> components = new ArrayList<Component>();
48      protected List<String> packagePrefixes = new ArrayList<String>();
49      private ConfigurationService kualiConfigurationService;
50      private KualiModuleService kualiModuleService;
51      private DataDictionaryService dataDictionaryService;
52      
53      public String getConfigurationParameter( String parameterName ){
54      	return getKualiConfigurationService().getPropertyValueAsString(parameterName);
55      }
56      
57      
58  
59  
60  
61  
62  
63      public List<Component> getNonDatabaseComponents() {
64          if (components.isEmpty()) {
65              Map<String, Component> uniqueParameterDetailTypeMap = new HashMap<String, Component>();
66                          
67              DataDictionaryService dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService();
68              
69              
70              for (BusinessObjectEntry businessObjectEntry : dataDictionaryService.getDataDictionary().getBusinessObjectEntries().values()) {
71                  try {
72                      Component parameterDetailType = getParameterDetailType((businessObjectEntry.getBaseBusinessObjectClass() != null) ? businessObjectEntry.getBaseBusinessObjectClass() : businessObjectEntry.getBusinessObjectClass());
73                      uniqueParameterDetailTypeMap.put(parameterDetailType.getCode(), parameterDetailType);
74                  }
75                  catch (Exception e) {
76                      LOG.error("The getDataDictionaryAndSpringComponents method of ParameterUtils encountered an exception while trying to create the detail type for business object class: " + businessObjectEntry.getBusinessObjectClass(), e);
77                  }
78              }
79              for (DocumentEntry documentEntry : dataDictionaryService.getDataDictionary().getDocumentEntries().values()) {
80                  if (documentEntry instanceof TransactionalDocumentEntry) {
81                      try {
82                          Component parameterDetailType = getParameterDetailType((documentEntry.getBaseDocumentClass() != null) ? documentEntry.getBaseDocumentClass() : documentEntry.getDocumentClass());
83                          uniqueParameterDetailTypeMap.put(parameterDetailType.getCode(), parameterDetailType);
84                      }
85                      catch (Exception e) {
86                          LOG.error("The getNonDatabaseDetailTypes encountered an exception while trying to create the detail type for transactional document class: " +
87                          		((documentEntry.getBaseDocumentClass() != null) ? documentEntry.getBaseDocumentClass() : documentEntry.getDocumentClass()), e);
88                      }
89                  }
90              }
91              components.addAll(uniqueParameterDetailTypeMap.values());
92          }
93          return Collections.unmodifiableList(components);
94      }
95      
96      @SuppressWarnings("unchecked")
97  	protected Component getParameterDetailType(Class documentOrStepClass) {
98          String detailTypeString = getKualiModuleService().getComponentCode(documentOrStepClass);
99  
100         String detailTypeName = getDetailTypeName(documentOrStepClass);
101 
102         String namespace = getKualiModuleService().getNamespaceCode(documentOrStepClass);
103         String name = (detailTypeName == null) ? detailTypeString : detailTypeName;
104         Component.Builder detailType = Component.Builder.create(namespace, detailTypeName, name, false);
105         return detailType.build();
106     }
107 
108     @SuppressWarnings("unchecked")
109     
110 
111 
112 
113 
114 
115 	protected String getDetailTypeName(Class documentOrStepClass) {
116         if (documentOrStepClass == null) {
117             throw new IllegalArgumentException("The getDetailTypeName method of ParameterRepositoryServiceImpl requires non-null documentOrStepClass");
118         }
119         
120         
121 
122 
123 
124 
125 
126         if (documentOrStepClass.isAnnotationPresent(COMPONENT.class)) {
127             BusinessObjectEntry boe = getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(documentOrStepClass.getName());
128             if (boe != null) {
129                 return boe.getObjectLabel();
130             }
131             else {
132                 return ((COMPONENT) documentOrStepClass.getAnnotation(COMPONENT.class)).component();
133             }
134         }
135 
136         
137 
138 
139 
140 
141 
142         if (TransactionalDocument.class.isAssignableFrom(documentOrStepClass)) {
143             return getDataDictionaryService().getDocumentLabelByClass(documentOrStepClass);
144         }
145         else if (BusinessObject.class.isAssignableFrom(documentOrStepClass) ) {
146             BusinessObjectEntry boe = getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(documentOrStepClass.getName());
147             if (boe != null) {
148                 return boe.getObjectLabel();
149             }
150             else {
151                 return KRADUtils.getBusinessTitleForClass(documentOrStepClass);
152             }
153         }
154         throw new IllegalArgumentException("The getDetailTypeName method of ParameterRepositoryServiceImpl requires TransactionalDocument, BusinessObject, or Step class. Was: " + documentOrStepClass.getName() );
155     }
156     
157     protected ConfigurationService getKualiConfigurationService() {
158 		if (kualiConfigurationService == null) {
159 			kualiConfigurationService = KRADServiceLocator.getKualiConfigurationService();
160 		}
161 		return kualiConfigurationService;
162 	}
163     
164     protected KualiModuleService getKualiModuleService() {
165     	if (kualiModuleService == null) {
166     		kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
167     	}
168     	return kualiModuleService;
169     }
170 
171     protected DataDictionaryService getDataDictionaryService() {
172     	if (dataDictionaryService == null) {
173     		dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService();
174     	}
175     	return dataDictionaryService;
176     }
177 
178 	
179 
180 
181 	public String getBaseInquiryUrl(String businessObjectClassName) {
182 		return LookupUtils.getBaseInquiryUrl();
183 	}
184 
185 	
186 
187 
188 	public String getBaseLookupUrl(String businessObjectClassName) {
189 		
190 		return LookupUtils.getBaseLookupUrl(false);
191 	}
192 	
193 	public String getBaseHelpUrl(String businessObjectClassName) {
194 		return KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
195                 KRADConstants.APPLICATION_URL_KEY) + "/kr/help.do";
196 	}
197 
198 	
199 
200 
201 	public boolean isResponsibleForPackage(String packageName) {
202 		if ( LOG.isDebugEnabled() ) {
203 			LOG.debug( "Checking if application ("+packagePrefixes+") is responsible for package: " + packageName );
204 		}
205 		for ( String prefix : packagePrefixes ) {
206 			if ( packageName.startsWith(prefix) ) {
207 				if ( LOG.isDebugEnabled() ) {
208 					LOG.debug("Found match ("+prefix+") - returning true");
209 				}
210 				return true;
211 			}
212 		}
213 		if ( LOG.isDebugEnabled() ) {
214 			LOG.debug("No Match Found: packageName="+packageName+" / prefix list=" + packagePrefixes);
215 		}
216 		return false;
217 	}
218 	
219 	
220 
221 
222 	public boolean supportsBusinessObjectClass(String businessObjectClassName) {
223 		return getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(businessObjectClassName) != null;
224 	}
225 	
226 	
227 
228 
229 	public AttributeDefinition getBusinessObjectAttributeDefinition( String businessObjectClassName, String attributeName) {
230 		if ( LOG.isDebugEnabled() ) {
231 			LOG.debug( "Asking ("+packagePrefixes+") for BO AttributeDefinition: " + businessObjectClassName + " / " + attributeName );
232 		}
233 		BusinessObjectEntry boe = getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(businessObjectClassName);
234 		if ( boe == null ) {
235 			if ( LOG.isInfoEnabled() ) {
236 				LOG.info( "No BusinessObjectEntry found for class name: " + businessObjectClassName );
237 			}
238 			return null;
239 		}
240 		return boe.getAttributeDefinition(attributeName);
241 	}
242 
243 	
244 
245 
246 	public List<String> getPackagePrefixes() {
247 		return this.packagePrefixes;
248 	}
249 
250 	
251 
252 
253 	public void setPackagePrefixes(List<String> packagePrefixes) {
254 		this.packagePrefixes = packagePrefixes;
255 	}
256 }