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     @Override
264     public View getImmutableViewById(UifDictionaryIndex index, String viewId) {
265         return null;  //To change body of implemented methods use File | Settings | File Templates.
266     }
267 
268     /**
269 	 * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getViewByTypeIndex(UifDictionaryIndex,
270 	 *      java.lang.String, java.util.Map)
271 	 */
272 	@Override
273     public View getViewByTypeIndex(UifDictionaryIndex index, UifConstants.ViewType viewTypeName, Map<String, String> indexKey) {
274 		if (viewTypeName == null) {
275 			throw new IllegalArgumentException("invalid (blank) view type name");
276 		}
277 		if ((indexKey == null) || indexKey.isEmpty()) {
278 			throw new IllegalArgumentException("index key must have at least one entry");
279 		}
280 
281 		return index.getViewByTypeIndex(viewTypeName, indexKey);
282 	}
283 
284     @Override
285     public String getViewIdByTypeIndex(UifDictionaryIndex index, UifConstants.ViewType viewTypeName, Map<String, String> indexKey) {
286         return null;  //To change body of implemented methods use File | Settings | File Templates.
287     }
288 
289     /**
290      * @see org.kuali.rice.krad.datadictionary.DataDictionaryIndexMapper#viewByTypeExist(UifDictionaryIndex,
291      *      java.lang.String, java.util.Map)
292      */
293     @Override
294     public boolean viewByTypeExist(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.viewByTypeExist(viewTypeName, indexKey);
304     }
305 
306     /**
307      * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getViewPropertiesById(org.kuali.rice.krad.datadictionary.view.ViewDictionaryIndex,
308      *      java.lang.String)
309      */
310     @Override
311     public PropertyValues getViewPropertiesById(UifDictionaryIndex index, String viewId) {
312         if (StringUtils.isBlank(viewId)) {
313             throw new IllegalArgumentException("invalid (blank) view id");
314         }
315 
316         return index.getViewPropertiesById(viewId);
317     }
318 
319     /**
320      * @see org.kuali.rice.krad.datadictionary.DataDictionaryIndexMapper#getViewPropertiesByType(UifDictionaryIndex,
321      *      java.lang.String, java.util.Map)
322      */
323     @Override
324     public PropertyValues getViewPropertiesByType(UifDictionaryIndex index, UifConstants.ViewType viewTypeName,
325             Map<String, String> indexKey) {
326         if (viewTypeName == null) {
327             throw new IllegalArgumentException("invalid (blank) view type name");
328         }
329         if ((indexKey == null) || indexKey.isEmpty()) {
330             throw new IllegalArgumentException("index key must have at least one entry");
331         }
332 
333         return index.getViewPropertiesByType(viewTypeName, indexKey);
334     }
335 
336     /**
337 	 * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getViewsForType(UifDictionaryIndex,
338 	 *      java.lang.String)
339 	 */
340 	@Override
341     public List<View> getViewsForType(UifDictionaryIndex index, UifConstants.ViewType viewTypeName) {
342 		if (viewTypeName == null) {
343 			throw new IllegalArgumentException("invalid (blank) view type name");
344 		}
345 
346 		return index.getViewsForType(viewTypeName);
347 	}
348 
349 }