View Javadoc
1   /**
2    * Copyright 2005-2016 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,
44       *      java.lang.Class)
45       */
46      @Override
47      public Set<InactivationBlockingMetadata> getAllInactivationBlockingMetadatas(DataDictionaryIndex index,
48              Class<?> blockedClass) {
49          return index.getInactivationBlockersForClass().get(blockedClass);
50      }
51  
52      /**
53       * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getBusinessObjectClassNames(org.kuali.rice.krad.datadictionary.DataDictionaryIndex)
54       */
55      @Override
56      public List<String> getBusinessObjectClassNames(DataDictionaryIndex index) {
57          List classNames = new ArrayList();
58          classNames.addAll(index.getBusinessObjectEntries().keySet());
59  
60          return Collections.unmodifiableList(classNames);
61      }
62  
63      /**
64       * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getBusinessObjectEntries(org.kuali.rice.krad.datadictionary.DataDictionaryIndex)
65       */
66      @Override
67      public Map<String, BusinessObjectEntry> getBusinessObjectEntries(DataDictionaryIndex index) {
68          return index.getBusinessObjectEntries();
69      }
70  
71      /**
72       * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getBusinessObjectEntries(org.kuali.rice.krad.datadictionary.DataDictionaryIndex)
73       */
74      public Map<String, DataObjectEntry> getDataObjectEntries(DataDictionaryIndex index) {
75          return index.getDataObjectEntries();
76      }
77  
78      /**
79       * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getDataObjectEntryForConcreteClass(org.kuali.rice.krad.datadictionary.DataDictionaryIndex,
80       *      java.lang.String)
81       */
82      @Override
83      public DataObjectEntry getDataObjectEntryForConcreteClass(DataDictionaryIndex ddIndex, String className) {
84          if (StringUtils.isBlank(className)) {
85              throw new IllegalArgumentException("invalid (blank) className");
86          }
87          if (LOG.isDebugEnabled()) {
88              LOG.debug("calling getDataObjectEntry '" + className + "'");
89          }
90  
91          String trimmedClassName = className;
92          int index = className.indexOf("$$");
93          if (index >= 0) {
94              trimmedClassName = className.substring(0, index);
95          }
96          return ddIndex.getDataObjectEntries().get(trimmedClassName);
97      }
98  
99      /**
100      * {@inheritDoc}
101      */
102     @Override
103     public BusinessObjectEntry getBusinessObjectEntryForConcreteClass(DataDictionaryIndex ddIndex, String className) {
104         if (StringUtils.isBlank(className)) {
105             throw new IllegalArgumentException("invalid (blank) className");
106         }
107         if (LOG.isDebugEnabled()) {
108             LOG.debug("calling getBusinessObjectEntry '" + className + "'");
109         }
110         int index = className.indexOf("$$");
111         if (index >= 0) {
112             className = className.substring(0, index);
113         }
114         return ddIndex.getBusinessObjectEntries().get(className);
115     }
116 
117     /**
118      * {@inheritDoc}
119      */
120     @Override
121     public DataDictionaryEntry getDictionaryObjectEntry(DataDictionaryIndex ddIndex, String className) {
122         if (StringUtils.isBlank(className)) {
123             throw new IllegalArgumentException("invalid (blank) className");
124         }
125         if (LOG.isDebugEnabled()) {
126             LOG.debug("calling getDictionaryObjectEntry '" + className + "'");
127         }
128         int index = className.indexOf("$$");
129         if (index >= 0) {
130             className = className.substring(0, index);
131         }
132 
133         // look in the JSTL key cache
134         DataDictionaryEntry entry = null;
135         if (ddIndex.getEntriesByJstlKey() != null) {
136             entry = ddIndex.getEntriesByJstlKey().get(className);
137         }
138 
139         // check the Object list
140         if (entry == null) {
141             entry = ddIndex.getDataObjectEntries().get(className);
142         }
143         // KULRICE-8005 Breaks when override business object classes
144         // check the BO list
145         if (entry == null) {
146             entry = getBusinessObjectEntry(ddIndex, className);
147         }
148         // check the document list
149         if (entry == null) {
150             entry = getDocumentEntry(ddIndex, className);
151         }
152 
153         return entry;
154     }
155 
156     /**
157      * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getDataObjectEntry(org.kuali.rice.krad.datadictionary.DataDictionaryIndex,
158      *      java.lang.String)
159      */
160     @Override
161     public DataObjectEntry getDataObjectEntry(DataDictionaryIndex index, String className) {
162         DataObjectEntry entry = getDataObjectEntryForConcreteClass(index, className);
163 
164         if (entry == null) {
165             Class<?> boClass = null;
166             try {
167                 boClass = Class.forName(className);
168                 ModuleService responsibleModuleService =
169                         KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(boClass);
170                 if (responsibleModuleService != null && responsibleModuleService.isExternalizable(boClass)) {
171                     entry = responsibleModuleService.getExternalizableBusinessObjectDictionaryEntry(boClass);
172                 }
173             } catch (ClassNotFoundException cnfex) {
174                 // swallow so we can return null
175             }
176         }
177 
178         return entry;
179     }
180 
181     @Override
182     public BusinessObjectEntry getBusinessObjectEntry(DataDictionaryIndex index, String className) {
183         BusinessObjectEntry entry = getBusinessObjectEntryForConcreteClass(index, className);
184         if (entry == null) {
185             Class boClass = null;
186             try {
187                 boClass = Class.forName(className);
188                 ModuleService responsibleModuleService =
189                         KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(boClass);
190                 if (responsibleModuleService != null && responsibleModuleService.isExternalizable(boClass)) {
191                     return responsibleModuleService.getExternalizableBusinessObjectDictionaryEntry(boClass);
192                 }
193             } catch (ClassNotFoundException cnfex) {
194             }
195             return null;
196         } else {
197             return entry;
198         }
199     }
200 
201     /**
202      * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getDocumentEntries(org.kuali.rice.krad.datadictionary.DataDictionaryIndex)
203      */
204     @Override
205     public Map<String, DocumentEntry> getDocumentEntries(DataDictionaryIndex index) {
206         return Collections.unmodifiableMap(index.getDocumentEntries());
207     }
208 
209     /**
210      * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getDocumentEntry(org.kuali.rice.krad.datadictionary.DataDictionaryIndex,
211      *      java.lang.String)
212      */
213     @Override
214     public DocumentEntry getDocumentEntry(DataDictionaryIndex index, String documentTypeDDKey) {
215 
216         if (StringUtils.isBlank(documentTypeDDKey)) {
217             throw new IllegalArgumentException("invalid (blank) documentTypeName");
218         }
219         if (LOG.isDebugEnabled()) {
220             LOG.debug("calling getDocumentEntry by documentTypeName '" + documentTypeDDKey + "'");
221         }
222 
223         DocumentEntry de = index.getDocumentEntries().get(documentTypeDDKey);
224 
225         if (de == null) {
226             try {
227                 Class<?> clazz = Class.forName(documentTypeDDKey);
228                 de = index.getDocumentEntriesByBusinessObjectClass().get(clazz);
229                 if (de == null) {
230                     de = index.getDocumentEntriesByMaintainableClass().get(clazz);
231                 }
232             } catch (ClassNotFoundException ex) {
233                 LOG.warn("Unable to find document entry for key: " + documentTypeDDKey);
234             }
235         }
236 
237         return de;
238     }
239 
240     /**
241      * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getDocumentTypeName(org.kuali.rice.krad.datadictionary.DataDictionaryIndex,
242      *      java.lang.String)
243      */
244     @Override
245     public String getDocumentTypeName(DataDictionaryIndex index, String documentTypeName) {
246         // TODO arh14 - THIS METHOD NEEDS JAVADOCS
247         return null;
248     }
249 
250     /**
251      * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getMaintenanceDocumentEntryForBusinessObjectClass(org.kuali.rice.krad.datadictionary.DataDictionaryIndex,
252      *      java.lang.Class)
253      */
254     @Override
255     public MaintenanceDocumentEntry getMaintenanceDocumentEntryForBusinessObjectClass(DataDictionaryIndex index,
256             Class<?> businessObjectClass) {
257         if (businessObjectClass == null) {
258             throw new IllegalArgumentException("invalid (null) dataObjectClass");
259         }
260         if (LOG.isDebugEnabled()) {
261             LOG.debug("calling getDocumentEntry by dataObjectClass '" + businessObjectClass + "'");
262         }
263 
264         return (MaintenanceDocumentEntry) index.getDocumentEntriesByBusinessObjectClass().get(businessObjectClass);
265     }
266 
267     /**
268      * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getViewById(org.kuali.rice.krad.datadictionary.uif.UifDictionaryIndex,
269      *      java.lang.String)
270      */
271     @Override
272     public View getViewById(UifDictionaryIndex index, String viewId) {
273         if (StringUtils.isBlank(viewId)) {
274             throw new IllegalArgumentException("invalid (blank) view id");
275         }
276         if (LOG.isDebugEnabled()) {
277             LOG.debug("calling getViewById by id '" + viewId + "'");
278         }
279 
280         return index.getViewById(viewId);
281     }
282 
283     /**
284      * {@inheritDoc}
285      */
286     @Override
287     public View getImmutableViewById(UifDictionaryIndex index, String viewId) {
288         if (StringUtils.isBlank(viewId)) {
289             throw new IllegalArgumentException("invalid (blank) view id");
290         }
291 
292         return index.getImmutableViewById(viewId);
293     }
294 
295     /**
296      * {@inheritDoc}
297      */
298     @Override
299     public View getViewByTypeIndex(UifDictionaryIndex index, UifConstants.ViewType viewTypeName,
300             Map<String, String> indexKey) {
301         if (viewTypeName == null) {
302             throw new IllegalArgumentException("invalid (blank) view type name");
303         }
304         if ((indexKey == null) || indexKey.isEmpty()) {
305             throw new IllegalArgumentException("index key must have at least one entry");
306         }
307 
308         return index.getViewByTypeIndex(viewTypeName, indexKey);
309     }
310 
311     /**
312      * {@inheritDoc}
313      */
314     @Override
315     public String getViewIdByTypeIndex(UifDictionaryIndex index, UifConstants.ViewType viewTypeName,
316             Map<String, String> indexKey) {
317         if (viewTypeName == null) {
318             throw new IllegalArgumentException("invalid (blank) view type name");
319         }
320 
321         if ((indexKey == null) || indexKey.isEmpty()) {
322             throw new IllegalArgumentException("index key must have at least one entry");
323         }
324 
325         return index.getViewIdByTypeIndex(viewTypeName, indexKey);
326     }
327 
328     /**
329      * {@inheritDoc}
330      */
331     @Override
332     public boolean viewByTypeExist(UifDictionaryIndex index, UifConstants.ViewType viewTypeName,
333             Map<String, String> indexKey) {
334         if (viewTypeName == null) {
335             throw new IllegalArgumentException("invalid (blank) view type name");
336         }
337         if ((indexKey == null) || indexKey.isEmpty()) {
338             throw new IllegalArgumentException("index key must have at least one entry");
339         }
340 
341         return index.viewByTypeExist(viewTypeName, indexKey);
342     }
343 
344     /**
345      * {@inheritDoc}
346      */
347     @Override
348     public PropertyValues getViewPropertiesById(UifDictionaryIndex index, String viewId) {
349         if (StringUtils.isBlank(viewId)) {
350             throw new IllegalArgumentException("invalid (blank) view id");
351         }
352 
353         return index.getViewPropertiesById(viewId);
354     }
355 
356     /**
357      * {@inheritDoc}
358      */
359     @Override
360     public PropertyValues getViewPropertiesByType(UifDictionaryIndex index, UifConstants.ViewType viewTypeName,
361             Map<String, String> indexKey) {
362         if (viewTypeName == null) {
363             throw new IllegalArgumentException("invalid (blank) view type name");
364         }
365         if ((indexKey == null) || indexKey.isEmpty()) {
366             throw new IllegalArgumentException("index key must have at least one entry");
367         }
368 
369         return index.getViewPropertiesByType(viewTypeName, indexKey);
370     }
371 
372     /**
373      * {@inheritDoc}
374      */
375     @Override
376     public List<View> getViewsForType(UifDictionaryIndex index, UifConstants.ViewType viewTypeName) {
377         if (viewTypeName == null) {
378             throw new IllegalArgumentException("invalid (blank) view type name");
379         }
380 
381         return index.getViewsForType(viewTypeName);
382     }
383 
384 }