View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.datadictionary;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.krad.datadictionary.uif.UifDictionaryIndex;
21  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
22  import org.kuali.rice.krad.service.ModuleService;
23  import org.kuali.rice.krad.uif.UifConstants;
24  import org.kuali.rice.krad.uif.view.View;
25  import org.springframework.beans.PropertyValues;
26  
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Set;
32  
33  /**
34   * A DataDictionaryMapper that simply consults the statically initialized
35   * DataDictionaryIndex mappings
36   * 
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  public class DataDictionaryIndexMapper implements DataDictionaryMapper {
40  	private static final Logger LOG = Logger.getLogger(DataDictionaryIndexMapper.class);
41  
42  	/**
43  	 * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getAllInactivationBlockingMetadatas(org.kuali.rice.krad.datadictionary.DataDictionaryIndex, java.lang.Class)
44  	 */
45  	public Set<InactivationBlockingMetadata> getAllInactivationBlockingMetadatas(DataDictionaryIndex index, Class<?> blockedClass) {
46          return index.getInactivationBlockersForClass().get(blockedClass);
47  	}
48  
49  	/**
50  	 * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getBusinessObjectClassNames(org.kuali.rice.krad.datadictionary.DataDictionaryIndex)
51  	 */
52  	public List<String> getBusinessObjectClassNames(DataDictionaryIndex index) {
53  		List classNames = new ArrayList();
54  		classNames.addAll(index.getBusinessObjectEntries().keySet());
55  
56  		return Collections.unmodifiableList(classNames);
57  	}
58  
59  	/**
60  	 * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getBusinessObjectEntries(org.kuali.rice.krad.datadictionary.DataDictionaryIndex)
61  	 */
62  	public Map<String, BusinessObjectEntry> getBusinessObjectEntries(DataDictionaryIndex index) {
63  		return index.getBusinessObjectEntries();
64  	}
65  
66  	/**
67       * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getDataObjectEntryForConcreteClass(org.kuali.rice.krad.datadictionary.DataDictionaryIndex, java.lang.String)
68       */
69  	@Override
70      public DataObjectEntry getDataObjectEntryForConcreteClass(DataDictionaryIndex ddIndex, String className) {
71  	    if (StringUtils.isBlank(className)) {
72              throw new IllegalArgumentException("invalid (blank) className");
73          }
74          if ( LOG.isDebugEnabled() ) {
75              LOG.debug("calling getDataObjectEntry '" + className + "'");
76          }
77          
78          String trimmedClassName = className;
79          int index = className.indexOf("$$");
80          if (index >= 0) {
81              trimmedClassName = className.substring(0, index);
82          }
83          return ddIndex.getDataObjectEntries().get(trimmedClassName);
84      }
85  
86      /**
87  	 * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getBusinessObjectEntryForConcreteClass(java.lang.String)
88  	 */
89  	public BusinessObjectEntry getBusinessObjectEntryForConcreteClass(DataDictionaryIndex ddIndex, String className) {
90  		if (StringUtils.isBlank(className)) {
91  			throw new IllegalArgumentException("invalid (blank) className");
92  		}
93  		if ( LOG.isDebugEnabled() ) {
94  		    LOG.debug("calling getBusinessObjectEntry '" + className + "'");
95  		}
96  		int index = className.indexOf("$$");
97  		if (index >= 0) {
98  			className = className.substring(0, index);
99  		}
100 		return ddIndex.getBusinessObjectEntries().get(className);
101 	}
102 
103 	/**
104 	 * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getDictionaryObjectEntry(org.kuali.rice.krad.datadictionary.DataDictionaryIndex, java.lang.String)
105 	 */
106 	public DataDictionaryEntry getDictionaryObjectEntry(DataDictionaryIndex ddIndex, String className) {
107 		if (StringUtils.isBlank(className)) {
108 			throw new IllegalArgumentException("invalid (blank) className");
109 		}
110 		if ( LOG.isDebugEnabled() ) {
111 		    LOG.debug("calling getDictionaryObjectEntry '" + className + "'");
112 		}
113 		int index = className.indexOf("$$");
114 		if (index >= 0) {
115 			className = className.substring(0, index);
116 		}
117 
118 		// look in the JSTL key cache
119 		DataDictionaryEntry entry = null;
120         if (ddIndex.getEntriesByJstlKey() != null) {
121             entry = ddIndex.getEntriesByJstlKey().get(className);
122         }
123 
124         // check the Object list
125         if (entry == null){
126             entry = ddIndex.getDataObjectEntries().get(className);
127         }
128         // KULRICE-8005 Breaks when override business object classes
129         // check the BO list
130         if ( entry == null ) {
131             entry = getBusinessObjectEntry(ddIndex, className);
132         }
133         // check the document list
134         if ( entry == null ) {
135             entry = getDocumentEntry(ddIndex, className);
136         }
137 
138 		return entry;
139 	}
140 
141 	/**
142      * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getDataObjectEntry(org.kuali.rice.krad.datadictionary.DataDictionaryIndex, java.lang.String)
143      */
144 	@Override
145     public DataObjectEntry getDataObjectEntry(DataDictionaryIndex index, String className) {
146 	    DataObjectEntry entry = getDataObjectEntryForConcreteClass(index, className);
147 	    
148         if (entry == null) {
149             Class<?> boClass = null;
150             try{
151                 boClass = Class.forName(className);
152                 ModuleService responsibleModuleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(boClass);
153                 if(responsibleModuleService!=null && responsibleModuleService.isExternalizable(boClass)) {
154                     entry = responsibleModuleService.getExternalizableBusinessObjectDictionaryEntry(boClass);
155                 }
156             } catch(ClassNotFoundException cnfex){
157                 // swallow so we can return null
158             }
159         }
160         
161         return entry;
162     }
163 
164 	public BusinessObjectEntry getBusinessObjectEntry(DataDictionaryIndex index, String className ) {
165 		BusinessObjectEntry entry = getBusinessObjectEntryForConcreteClass(index, className);
166 		if (entry == null) {
167 			Class boClass = null;
168 			try{
169 				boClass = Class.forName(className);
170 				ModuleService responsibleModuleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(boClass);
171 				if(responsibleModuleService!=null && responsibleModuleService.isExternalizable(boClass)) {
172 					return responsibleModuleService.getExternalizableBusinessObjectDictionaryEntry(boClass);
173 				}
174 			} catch(ClassNotFoundException cnfex){
175 			}
176 			return null;
177 		}
178 		else {
179 			return entry;
180 		}
181 	}
182 	
183 	/**
184 	 * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getDocumentEntries(org.kuali.rice.krad.datadictionary.DataDictionaryIndex)
185 	 */
186 	public Map<String, DocumentEntry> getDocumentEntries(DataDictionaryIndex index) {
187 		return Collections.unmodifiableMap(index.getDocumentEntries());
188 	}
189 
190 	/**
191 	 * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getDocumentEntry(org.kuali.rice.krad.datadictionary.DataDictionaryIndex, java.lang.String)
192 	 */
193 	public DocumentEntry getDocumentEntry(DataDictionaryIndex index, String documentTypeDDKey) {
194 
195 		if (StringUtils.isBlank(documentTypeDDKey)) {
196 			throw new IllegalArgumentException("invalid (blank) documentTypeName");
197 		}
198 		if ( LOG.isDebugEnabled() ) {
199 		    LOG.debug("calling getDocumentEntry by documentTypeName '" + documentTypeDDKey + "'");
200 		}
201 
202 		DocumentEntry de = index.getDocumentEntries().get(documentTypeDDKey);	
203 		
204 		if ( de == null ) {
205 		    try {
206     		    Class<?> clazz = Class.forName( documentTypeDDKey );
207     		    de = index.getDocumentEntriesByBusinessObjectClass().get(clazz);
208     		    if ( de == null ) {
209     		        de = index.getDocumentEntriesByMaintainableClass().get(clazz);
210     		    }
211 		    } catch ( ClassNotFoundException ex ) {
212 		        LOG.warn( "Unable to find document entry for key: " + documentTypeDDKey );
213 		    }
214 		}
215 		
216         return de;
217 	}
218 
219 	/**
220 	 * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getDocumentTypeName(org.kuali.rice.krad.datadictionary.DataDictionaryIndex, java.lang.String)
221 	 */
222 	public String getDocumentTypeName(DataDictionaryIndex index,
223 			String documentTypeName) {
224 		// TODO arh14 - THIS METHOD NEEDS JAVADOCS
225 		return null;
226 	}
227 
228 	/**
229 	 * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getMaintenanceDocumentEntryForBusinessObjectClass(org.kuali.rice.krad.datadictionary.DataDictionaryIndex, java.lang.Class)
230 	 */
231 	public MaintenanceDocumentEntry getMaintenanceDocumentEntryForBusinessObjectClass(DataDictionaryIndex index, Class<?> businessObjectClass) {
232 		if (businessObjectClass == null) {
233 			throw new IllegalArgumentException("invalid (null) dataObjectClass");
234 		}
235 		if ( LOG.isDebugEnabled() ) {
236 		    LOG.debug("calling getDocumentEntry by dataObjectClass '" + businessObjectClass + "'");
237 		}
238 
239 		return (MaintenanceDocumentEntry) index.getDocumentEntriesByBusinessObjectClass().get(businessObjectClass);
240 	}
241 	
242 	/**
243 	 * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getViewById(org.kuali.rice.krad.datadictionary.view.ViewDictionaryIndex,
244 	 *      java.lang.String)
245 	 */
246 	public View getViewById(UifDictionaryIndex index, String viewId) {
247 		if (StringUtils.isBlank(viewId)) {
248 			throw new IllegalArgumentException("invalid (blank) view id");
249 		}
250 		if (LOG.isDebugEnabled()) {
251 			LOG.debug("calling getViewById by id '" + viewId + "'");
252 		}
253 
254 		return index.getViewById(viewId);
255 	}
256 
257 	/**
258 	 * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getViewByTypeIndex(UifDictionaryIndex,
259 	 *      java.lang.String, java.util.Map)
260 	 */
261 	public View getViewByTypeIndex(UifDictionaryIndex index, UifConstants.ViewType viewTypeName, Map<String, String> indexKey) {
262 		if (viewTypeName == null) {
263 			throw new IllegalArgumentException("invalid (blank) view type name");
264 		}
265 		if ((indexKey == null) || indexKey.isEmpty()) {
266 			throw new IllegalArgumentException("index key must have at least one entry");
267 		}
268 
269 		return index.getViewByTypeIndex(viewTypeName, indexKey);
270 	}
271 
272     /**
273      * @see org.kuali.rice.krad.datadictionary.DataDictionaryIndexMapper#viewByTypeExist(UifDictionaryIndex,
274      *      java.lang.String, java.util.Map)
275      */
276     public boolean viewByTypeExist(UifDictionaryIndex index, UifConstants.ViewType viewTypeName,
277             Map<String, String> indexKey) {
278         if (viewTypeName == null) {
279             throw new IllegalArgumentException("invalid (blank) view type name");
280         }
281         if ((indexKey == null) || indexKey.isEmpty()) {
282             throw new IllegalArgumentException("index key must have at least one entry");
283         }
284 
285         return index.viewByTypeExist(viewTypeName, indexKey);
286     }
287 
288     /**
289      * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getViewPropertiesById(org.kuali.rice.krad.datadictionary.view.ViewDictionaryIndex,
290      *      java.lang.String)
291      */
292     public PropertyValues getViewPropertiesById(UifDictionaryIndex index, String viewId) {
293         if (StringUtils.isBlank(viewId)) {
294             throw new IllegalArgumentException("invalid (blank) view id");
295         }
296 
297         return index.getViewPropertiesById(viewId);
298     }
299 
300     /**
301      * @see org.kuali.rice.krad.datadictionary.DataDictionaryIndexMapper#getViewPropertiesByType(UifDictionaryIndex,
302      *      java.lang.String, java.util.Map)
303      */
304     public PropertyValues getViewPropertiesByType(UifDictionaryIndex index, UifConstants.ViewType viewTypeName,
305             Map<String, String> indexKey) {
306         if (viewTypeName == null) {
307             throw new IllegalArgumentException("invalid (blank) view type name");
308         }
309         if ((indexKey == null) || indexKey.isEmpty()) {
310             throw new IllegalArgumentException("index key must have at least one entry");
311         }
312 
313         return index.getViewPropertiesByType(viewTypeName, indexKey);
314     }
315 
316     /**
317 	 * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getViewsForType(UifDictionaryIndex,
318 	 *      java.lang.String)
319 	 */
320 	public List<View> getViewsForType(UifDictionaryIndex index, UifConstants.ViewType viewTypeName) {
321 		if (viewTypeName == null) {
322 			throw new IllegalArgumentException("invalid (blank) view type name");
323 		}
324 
325 		return index.getViewsForType(viewTypeName);
326 	}
327 
328 }