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.service.impl;
017
018import java.util.Collection;
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.kuali.rice.core.api.CoreConstants;
024import org.kuali.rice.core.api.mo.common.active.Inactivatable;
025import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
026import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
027import org.kuali.rice.krad.service.KeyValuesService;
028import org.kuali.rice.krad.service.ModuleService;
029
030/**
031 * This class provides collection retrievals to populate key value pairs of business objects.
032 */
033@Deprecated
034public class KeyValuesServiceImpl implements KeyValuesService {
035    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KeyValuesServiceImpl.class);
036
037    /**
038     * @see org.kuali.rice.krad.service.KeyValuesService#findAll(java.lang.Class)
039     */
040    @Override
041        public <T> Collection<T> findAll(Class<T> clazz) {
042        ModuleService responsibleModuleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(clazz);
043                if(responsibleModuleService!=null && responsibleModuleService.isExternalizable(clazz)){
044                        return (Collection<T>) responsibleModuleService.getExternalizableBusinessObjectsList((Class<ExternalizableBusinessObject>) clazz, Collections.<String, Object>emptyMap());
045                }
046        if (containsActiveIndicator(clazz)) {
047                return KRADServiceLocatorWeb.getLegacyDataAdapter().findMatching(clazz, Collections.singletonMap(CoreConstants.CommonElements.ACTIVE, true));
048        }
049        if (LOG.isDebugEnabled()) {
050                        LOG.debug("Active indicator not found for class " + clazz.getName());
051                }
052        return KRADServiceLocatorWeb.getLegacyDataAdapter().findAll(clazz);
053    }
054
055    /**
056     * @see org.kuali.rice.krad.service.KeyValuesService#findAllOrderBy(java.lang.Class, java.lang.String, boolean)
057     */
058    @Override
059        public <T> Collection<T> findAllOrderBy(Class<T> clazz, String sortField, boolean sortAscending) {
060        if (containsActiveIndicator(clazz)) {
061            return KRADServiceLocatorWeb.getLegacyDataAdapter().findMatchingOrderBy(clazz, Collections.singletonMap(CoreConstants.CommonElements.ACTIVE, true), sortField, sortAscending);
062        }
063        if (LOG.isDebugEnabled()) {
064                        LOG.debug("Active indicator not found for class " + clazz.getName());
065                }
066        return KRADServiceLocatorWeb.getLegacyDataAdapter().findMatchingOrderBy(clazz, new HashMap<String,Object>(), sortField, sortAscending);
067    }
068
069    /**
070     * @see org.kuali.rice.krad.service.BusinessObjectService#findMatching(java.lang.Class, java.util.Map)
071     */
072    @Override
073        public <T> Collection<T> findMatching(Class<T> clazz, Map<String, Object> fieldValues) {
074        if (containsActiveIndicator(clazz)) {
075                // copying the map since we need to change it and don't know if it is unmodifiable
076                Map<String,Object> criteria = new HashMap<String, Object>( fieldValues );
077                criteria.put(CoreConstants.CommonElements.ACTIVE, true);
078            return KRADServiceLocatorWeb.getLegacyDataAdapter().findMatching(clazz, criteria);
079        }
080        if (LOG.isDebugEnabled()) {
081                        LOG.debug("Active indicator not found for class " + clazz.getName());
082                }
083        return KRADServiceLocatorWeb.getLegacyDataAdapter().findMatching(clazz, fieldValues);
084    }
085
086    /**
087     * Checks whether the class implements the Inactivatable interface.
088     *
089     * NOTE: This is different than earlier checks, as it assumes that the active flag
090     * is persistent.
091     *
092     * @param clazz
093     * @return boolean if active column is mapped for Class
094     */
095    private <T> boolean containsActiveIndicator(Class<T> clazz) {
096        return Inactivatable.class.isAssignableFrom(clazz);
097    }
098
099    @Override
100        public <T> Collection<T> findAllInactive(Class<T> clazz) {
101        if (containsActiveIndicator(clazz)) {
102                return KRADServiceLocatorWeb.getLegacyDataAdapter().findMatching(clazz, Collections.singletonMap(CoreConstants.CommonElements.ACTIVE, false));
103        }
104                LOG.warn("Active indicator not found for class.  Assuming all are active. " + clazz.getName());
105        return Collections.emptyList();
106    }
107
108}