001/**
002 * Copyright 2005-2016 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 org.apache.commons.lang.StringUtils;
019import org.apache.log4j.Logger;
020import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
021import org.kuali.rice.krad.service.ModuleService;
022import org.kuali.rice.krad.uif.UifConstants;
023import org.kuali.rice.krad.uif.view.View;
024import org.springframework.beans.PropertyValues;
025
026import java.util.ArrayList;
027import java.util.Collections;
028import java.util.List;
029import java.util.Map;
030import java.util.Set;
031
032/**
033 * A DataDictionaryMapper that simply consults the statically initialized
034 * DataDictionaryIndex mappings
035 * 
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 */
038public class DataDictionaryIndexMapper implements DataDictionaryMapper {
039        private static final Logger LOG = Logger.getLogger(DataDictionaryIndexMapper.class);
040
041        /**
042         * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getAllInactivationBlockingMetadatas(org.kuali.rice.krad.datadictionary.DataDictionaryIndex, java.lang.Class)
043         */
044        public Set<InactivationBlockingMetadata> getAllInactivationBlockingMetadatas(DataDictionaryIndex index, Class<?> blockedClass) {
045        return index.getInactivationBlockersForClass().get(blockedClass);
046        }
047
048        /**
049         * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getBusinessObjectClassNames(org.kuali.rice.krad.datadictionary.DataDictionaryIndex)
050         */
051        public List<String> getBusinessObjectClassNames(DataDictionaryIndex index) {
052                List classNames = new ArrayList();
053                classNames.addAll(index.getBusinessObjectEntries().keySet());
054
055                return Collections.unmodifiableList(classNames);
056        }
057
058        /**
059         * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getBusinessObjectEntries(org.kuali.rice.krad.datadictionary.DataDictionaryIndex)
060         */
061        public Map<String, BusinessObjectEntry> getBusinessObjectEntries(DataDictionaryIndex index) {
062                return index.getBusinessObjectEntries();
063        }
064
065        /**
066     * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getDataObjectEntryForConcreteClass(org.kuali.rice.krad.datadictionary.DataDictionaryIndex, java.lang.String)
067     */
068        @Override
069    public DataObjectEntry getDataObjectEntryForConcreteClass(DataDictionaryIndex ddIndex, String className) {
070            if (StringUtils.isBlank(className)) {
071            throw new IllegalArgumentException("invalid (blank) className");
072        }
073        if ( LOG.isDebugEnabled() ) {
074            LOG.debug("calling getDataObjectEntry '" + className + "'");
075        }
076        
077        String trimmedClassName = className;
078        int index = className.indexOf("$$");
079        if (index >= 0) {
080            trimmedClassName = className.substring(0, index);
081        }
082        return ddIndex.getDataObjectEntries().get(trimmedClassName);
083    }
084
085    /**
086         * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getBusinessObjectEntryForConcreteClass(java.lang.String)
087         */
088        public BusinessObjectEntry getBusinessObjectEntryForConcreteClass(DataDictionaryIndex ddIndex, String className) {
089                if (StringUtils.isBlank(className)) {
090                        throw new IllegalArgumentException("invalid (blank) className");
091                }
092                if ( LOG.isDebugEnabled() ) {
093                    LOG.debug("calling getBusinessObjectEntry '" + className + "'");
094                }
095                int index = className.indexOf("$$");
096                if (index >= 0) {
097                        className = className.substring(0, index);
098                }
099                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        // KULRICE-8005 Breaks when override business object classes
125        // check the BO list
126        if ( entry == null ) {
127            entry = getBusinessObjectEntry(ddIndex, className);
128        }
129        // check the document list
130        if ( entry == null ) {
131            entry = getDocumentEntry(ddIndex, className);
132        }
133
134                return entry;
135        }
136
137        /**
138     * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getDataObjectEntry(org.kuali.rice.krad.datadictionary.DataDictionaryIndex, java.lang.String)
139     */
140        @Override
141    public DataObjectEntry getDataObjectEntry(DataDictionaryIndex index, String className) {
142            DataObjectEntry entry = getDataObjectEntryForConcreteClass(index, className);
143            
144        if (entry == null) {
145            Class<?> boClass = null;
146            try{
147                boClass = Class.forName(className);
148                ModuleService responsibleModuleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(boClass);
149                if(responsibleModuleService!=null && responsibleModuleService.isExternalizable(boClass)) {
150                    entry = responsibleModuleService.getExternalizableBusinessObjectDictionaryEntry(boClass);
151                }
152            } catch(ClassNotFoundException cnfex){
153                // swallow so we can return null
154            }
155        }
156        
157        return entry;
158    }
159
160        public BusinessObjectEntry getBusinessObjectEntry(DataDictionaryIndex index, String className ) {
161                BusinessObjectEntry entry = getBusinessObjectEntryForConcreteClass(index, className);
162                if (entry == null) {
163                        Class boClass = null;
164                        try{
165                                boClass = Class.forName(className);
166                                ModuleService responsibleModuleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(boClass);
167                                if(responsibleModuleService!=null && responsibleModuleService.isExternalizable(boClass)) {
168                                        return responsibleModuleService.getExternalizableBusinessObjectDictionaryEntry(boClass);
169                                }
170                        } catch(ClassNotFoundException cnfex){
171                        }
172                        return null;
173                }
174                else {
175                        return entry;
176                }
177        }
178        
179        /**
180         * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getDocumentEntries(org.kuali.rice.krad.datadictionary.DataDictionaryIndex)
181         */
182        public Map<String, DocumentEntry> getDocumentEntries(DataDictionaryIndex index) {
183                return Collections.unmodifiableMap(index.getDocumentEntries());
184        }
185
186        /**
187         * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getDocumentEntry(org.kuali.rice.krad.datadictionary.DataDictionaryIndex, java.lang.String)
188         */
189        public DocumentEntry getDocumentEntry(DataDictionaryIndex index, String documentTypeDDKey) {
190
191                if (StringUtils.isBlank(documentTypeDDKey)) {
192                        throw new IllegalArgumentException("invalid (blank) documentTypeName");
193                }
194                if ( LOG.isDebugEnabled() ) {
195                    LOG.debug("calling getDocumentEntry by documentTypeName '" + documentTypeDDKey + "'");
196                }
197
198                DocumentEntry de = index.getDocumentEntries().get(documentTypeDDKey);   
199                
200                if ( de == null ) {
201                    try {
202                    Class<?> clazz = Class.forName( documentTypeDDKey );
203                    de = index.getDocumentEntriesByBusinessObjectClass().get(clazz);
204                    if ( de == null ) {
205                        de = index.getDocumentEntriesByMaintainableClass().get(clazz);
206                    }
207                    } catch ( ClassNotFoundException ex ) {
208                        LOG.warn( "Unable to find document entry for key: " + documentTypeDDKey );
209                    }
210                }
211                
212        return de;
213        }
214
215        /**
216         * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getDocumentTypeName(org.kuali.rice.krad.datadictionary.DataDictionaryIndex, java.lang.String)
217         */
218        public String getDocumentTypeName(DataDictionaryIndex index,
219                        String documentTypeName) {
220                // TODO arh14 - THIS METHOD NEEDS JAVADOCS
221                return null;
222        }
223
224        /**
225         * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getMaintenanceDocumentEntryForBusinessObjectClass(org.kuali.rice.krad.datadictionary.DataDictionaryIndex, java.lang.Class)
226         */
227        public MaintenanceDocumentEntry getMaintenanceDocumentEntryForBusinessObjectClass(DataDictionaryIndex index, Class<?> businessObjectClass) {
228                if (businessObjectClass == null) {
229                        throw new IllegalArgumentException("invalid (null) dataObjectClass");
230                }
231                if ( LOG.isDebugEnabled() ) {
232                    LOG.debug("calling getDocumentEntry by dataObjectClass '" + businessObjectClass + "'");
233                }
234
235                return (MaintenanceDocumentEntry) index.getDocumentEntriesByBusinessObjectClass().get(businessObjectClass);
236        }
237        
238        /**
239         * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getViewById(org.kuali.rice.krad.datadictionary.view.ViewDictionaryIndex,
240         *      java.lang.String)
241         */
242        public View getViewById(UifDictionaryIndex index, String viewId) {
243                if (StringUtils.isBlank(viewId)) {
244                        throw new IllegalArgumentException("invalid (blank) view id");
245                }
246                if (LOG.isDebugEnabled()) {
247                        LOG.debug("calling getViewById by id '" + viewId + "'");
248                }
249
250                return index.getViewById(viewId);
251        }
252
253        /**
254         * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getViewByTypeIndex(UifDictionaryIndex,
255         *      java.lang.String, java.util.Map)
256         */
257        public View getViewByTypeIndex(UifDictionaryIndex index, UifConstants.ViewType viewTypeName, Map<String, String> indexKey) {
258                if (viewTypeName == null) {
259                        throw new IllegalArgumentException("invalid (blank) view type name");
260                }
261                if ((indexKey == null) || indexKey.isEmpty()) {
262                        throw new IllegalArgumentException("index key must have at least one entry");
263                }
264
265                return index.getViewByTypeIndex(viewTypeName, indexKey);
266        }
267
268    /**
269     * @see org.kuali.rice.krad.datadictionary.DataDictionaryIndexMapper#viewByTypeExist(UifDictionaryIndex,
270     *      java.lang.String, java.util.Map)
271     */
272    public boolean viewByTypeExist(UifDictionaryIndex index, UifConstants.ViewType viewTypeName,
273            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.viewByTypeExist(viewTypeName, indexKey);
282    }
283
284    /**
285     * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getViewPropertiesById(org.kuali.rice.krad.datadictionary.view.ViewDictionaryIndex,
286     *      java.lang.String)
287     */
288    public PropertyValues getViewPropertiesById(UifDictionaryIndex index, String viewId) {
289        if (StringUtils.isBlank(viewId)) {
290            throw new IllegalArgumentException("invalid (blank) view id");
291        }
292
293        return index.getViewPropertiesById(viewId);
294    }
295
296    /**
297     * @see org.kuali.rice.krad.datadictionary.DataDictionaryIndexMapper#getViewPropertiesByType(UifDictionaryIndex,
298     *      java.lang.String, java.util.Map)
299     */
300    public PropertyValues getViewPropertiesByType(UifDictionaryIndex index, UifConstants.ViewType viewTypeName,
301            Map<String, String> indexKey) {
302        if (viewTypeName == null) {
303            throw new IllegalArgumentException("invalid (blank) view type name");
304        }
305        if ((indexKey == null) || indexKey.isEmpty()) {
306            throw new IllegalArgumentException("index key must have at least one entry");
307        }
308
309        return index.getViewPropertiesByType(viewTypeName, indexKey);
310    }
311
312    /**
313         * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getViewsForType(UifDictionaryIndex,
314         *      java.lang.String)
315         */
316        public List<View> getViewsForType(UifDictionaryIndex index, UifConstants.ViewType viewTypeName) {
317                if (viewTypeName == null) {
318                        throw new IllegalArgumentException("invalid (blank) view type name");
319                }
320
321                return index.getViewsForType(viewTypeName);
322        }
323
324}