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