001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.datadictionary;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.commons.lang.StringUtils;
025import org.apache.log4j.Logger;
026import org.kuali.rice.krad.datadictionary.uif.UifDictionaryIndex;
027import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
028import org.kuali.rice.krad.service.ModuleService;
029import org.kuali.rice.krad.uif.UifConstants;
030import org.kuali.rice.krad.uif.view.View;
031import org.springframework.beans.PropertyValues;
032
033/**
034 * A DataDictionaryMapper that simply consults the statically initialized
035 * DataDictionaryIndex mappings
036 *
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 */
039public class DataDictionaryIndexMapper implements DataDictionaryMapper {
040    private static final Logger LOG = Logger.getLogger(DataDictionaryIndexMapper.class);
041
042    /**
043     * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getAllInactivationBlockingMetadatas(org.kuali.rice.krad.datadictionary.DataDictionaryIndex,
044     *      java.lang.Class)
045     */
046    @Override
047    public Set<InactivationBlockingMetadata> getAllInactivationBlockingMetadatas(DataDictionaryIndex index,
048            Class<?> blockedClass) {
049        return index.getInactivationBlockersForClass().get(blockedClass);
050    }
051
052    /**
053     * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getBusinessObjectClassNames(org.kuali.rice.krad.datadictionary.DataDictionaryIndex)
054     */
055    @Override
056    public List<String> getBusinessObjectClassNames(DataDictionaryIndex index) {
057        List classNames = new ArrayList();
058        classNames.addAll(index.getBusinessObjectEntries().keySet());
059
060        return Collections.unmodifiableList(classNames);
061    }
062
063    /**
064     * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getBusinessObjectEntries(org.kuali.rice.krad.datadictionary.DataDictionaryIndex)
065     */
066    @Override
067    public Map<String, BusinessObjectEntry> getBusinessObjectEntries(DataDictionaryIndex index) {
068        return index.getBusinessObjectEntries();
069    }
070
071    /**
072     * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getBusinessObjectEntries(org.kuali.rice.krad.datadictionary.DataDictionaryIndex)
073     */
074    public Map<String, DataObjectEntry> getDataObjectEntries(DataDictionaryIndex index) {
075        return index.getDataObjectEntries();
076    }
077
078    /**
079     * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getDataObjectEntryForConcreteClass(org.kuali.rice.krad.datadictionary.DataDictionaryIndex,
080     *      java.lang.String)
081     */
082    @Override
083    public DataObjectEntry getDataObjectEntryForConcreteClass(DataDictionaryIndex ddIndex, String className) {
084        if (StringUtils.isBlank(className)) {
085            throw new IllegalArgumentException("invalid (blank) className");
086        }
087        if (LOG.isDebugEnabled()) {
088            LOG.debug("calling getDataObjectEntry '" + className + "'");
089        }
090
091        String trimmedClassName = className;
092        int index = className.indexOf("$$");
093        if (index >= 0) {
094            trimmedClassName = className.substring(0, index);
095        }
096        return ddIndex.getDataObjectEntries().get(trimmedClassName);
097    }
098
099    /**
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}