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