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.service.impl;
017
018import org.kuali.rice.krad.bo.BusinessObject;
019import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
020import org.kuali.rice.krad.dao.BusinessObjectDao;
021import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
022import org.kuali.rice.krad.service.KeyValuesService;
023import org.kuali.rice.krad.service.ModuleService;
024import org.kuali.rice.krad.service.PersistenceStructureService;
025import org.kuali.rice.krad.util.KRADPropertyConstants;
026
027import java.util.ArrayList;
028import java.util.Collection;
029import java.util.Collections;
030import java.util.Map;
031
032/**
033 * This class provides collection retrievals to populate key value pairs of business objects.
034 */
035public class KeyValuesServiceImpl implements KeyValuesService {
036    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KeyValuesServiceImpl.class);
037
038    private BusinessObjectDao businessObjectDao;
039    private PersistenceStructureService persistenceStructureService;
040    
041    /**
042     * @see org.kuali.rice.krad.service.KeyValuesService#findAll(java.lang.Class)
043     */
044    @Override
045        public <T extends BusinessObject> Collection<T> findAll(Class<T> clazz) {
046        ModuleService responsibleModuleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(clazz);
047                if(responsibleModuleService!=null && responsibleModuleService.isExternalizable(clazz)){
048                        return (Collection<T>) responsibleModuleService.getExternalizableBusinessObjectsList((Class<ExternalizableBusinessObject>) clazz, Collections.<String, Object>emptyMap());
049                }
050        if (containsActiveIndicator(clazz)) {
051            return businessObjectDao.findAllActive(clazz);
052        }
053        if (LOG.isDebugEnabled()) {
054                        LOG.debug("Active indicator not found for class " + clazz.getName());
055                }
056        return businessObjectDao.findAll(clazz);
057    }
058    
059        public static <E> Collection<E> createUnmodifiableUpcastList(Collection<? extends E> list, Class<E> type) {
060                return new ArrayList<E>(list);
061        }
062
063    /**
064     * @see org.kuali.rice.krad.service.KeyValuesService#findAllOrderBy(java.lang.Class, java.lang.String, boolean)
065     */
066    @Override
067        public <T extends BusinessObject> Collection<T> findAllOrderBy(Class<T> clazz, String sortField, boolean sortAscending) {
068        if (containsActiveIndicator(clazz)) {
069            return businessObjectDao.findAllActiveOrderBy(clazz, sortField, sortAscending);
070        }
071        if (LOG.isDebugEnabled()) {
072                        LOG.debug("Active indicator not found for class " + clazz.getName());
073                }
074        return businessObjectDao.findAllOrderBy(clazz, sortField, sortAscending);
075    }
076
077    /**
078     * @see org.kuali.rice.krad.service.BusinessObjectService#findMatching(java.lang.Class, java.util.Map)
079     */
080    @Override
081        public <T extends BusinessObject> Collection<T> findMatching(Class<T> clazz, Map<String, Object> fieldValues) {
082        if (containsActiveIndicator(clazz)) {
083            return businessObjectDao.findMatchingActive(clazz, fieldValues);
084        }
085        if (LOG.isDebugEnabled()) {
086                        LOG.debug("Active indicator not found for class " + clazz.getName());
087                }
088        return businessObjectDao.findMatching(clazz, fieldValues);
089    }
090
091
092
093    /**
094     * @return Returns the businessObjectDao.
095     */
096    public BusinessObjectDao getBusinessObjectDao() {
097        return businessObjectDao;
098    }
099
100    /**
101     * @param businessObjectDao The businessObjectDao to set.
102     */
103    public void setBusinessObjectDao(BusinessObjectDao businessObjectDao) {
104        this.businessObjectDao = businessObjectDao;
105    }
106
107    /**
108     * Gets the persistenceStructureService attribute.
109     * 
110     * @return Returns the persistenceStructureService.
111     */
112    public PersistenceStructureService getPersistenceStructureService() {
113        return persistenceStructureService;
114    }
115
116    /**
117     * Sets the persistenceStructureService attribute value.
118     * 
119     * @param persistenceStructureService The persistenceStructureService to set.
120     */
121    public void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) {
122        this.persistenceStructureService = persistenceStructureService;
123    }
124
125    /**
126     * Uses persistence service to determine if the active column is mapped up in ojb.
127     * 
128     * @param clazz
129     * @return boolean if active column is mapped for Class
130     */
131    private <T extends BusinessObject> boolean containsActiveIndicator(Class<T> clazz) {
132        boolean containsActive = false;
133
134        if (persistenceStructureService.listFieldNames(clazz).contains(KRADPropertyConstants.ACTIVE)) {
135            containsActive = true;
136        }
137
138        return containsActive;
139    }
140    
141    /**
142     * @see org.kuali.rice.krad.service.KeyValuesService#findAll(java.lang.Class)
143     */
144    @Override
145        public <T extends BusinessObject> Collection<T> findAllInactive(Class<T> clazz) {
146        if (LOG.isDebugEnabled()) {
147                        LOG.debug("Active indicator not found for class " + clazz.getName());
148                }
149        return businessObjectDao.findAllInactive(clazz);
150    }
151
152}