001/**
002 * Copyright 2005-2012 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, java.lang.Class)
044         */
045        @Override
046    public Set<InactivationBlockingMetadata> getAllInactivationBlockingMetadatas(DataDictionaryIndex index, Class<?> blockedClass) {
047        return index.getInactivationBlockersForClass().get(blockedClass);
048        }
049
050        /**
051         * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getBusinessObjectClassNames(org.kuali.rice.krad.datadictionary.DataDictionaryIndex)
052         */
053        @Override
054    public List<String> getBusinessObjectClassNames(DataDictionaryIndex index) {
055                List classNames = new ArrayList();
056                classNames.addAll(index.getBusinessObjectEntries().keySet());
057
058                return Collections.unmodifiableList(classNames);
059        }
060
061        /**
062         * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getBusinessObjectEntries(org.kuali.rice.krad.datadictionary.DataDictionaryIndex)
063         */
064        @Override
065    public Map<String, BusinessObjectEntry> getBusinessObjectEntries(DataDictionaryIndex index) {
066                return index.getBusinessObjectEntries();
067        }
068
069        /**
070     * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getDataObjectEntryForConcreteClass(org.kuali.rice.krad.datadictionary.DataDictionaryIndex, java.lang.String)
071     */
072        @Override
073    public DataObjectEntry getDataObjectEntryForConcreteClass(DataDictionaryIndex ddIndex, String className) {
074            if (StringUtils.isBlank(className)) {
075            throw new IllegalArgumentException("invalid (blank) className");
076        }
077        if ( LOG.isDebugEnabled() ) {
078            LOG.debug("calling getDataObjectEntry '" + className + "'");
079        }
080
081        String trimmedClassName = className;
082        int index = className.indexOf("$$");
083        if (index >= 0) {
084            trimmedClassName = className.substring(0, index);
085        }
086        return ddIndex.getDataObjectEntries().get(trimmedClassName);
087    }
088
089    /**
090         * @see org.kuali.rice.krad.datadictionary.DataDictionaryMapper#getBusinessObjectEntryForConcreteClass(java.lang.String)
091         */
092        @Override
093    public BusinessObjectEntry getBusinessObjectEntryForConcreteClass(DataDictionaryIndex ddIndex, String className) {
094                if (StringUtils.isBlank(className)) {
095                        throw new IllegalArgumentException("invalid (blank) className");
096                }
097                if ( LOG.isDebugEnabled() ) {
098                    LOG.debug("calling getBusinessObjectEntry '" + className + "'");
099                }
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}