001/**
002 * Copyright 2005-2015 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.dao.proxy;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.kns.service.KNSServiceLocator;
020import org.kuali.rice.krad.bo.BusinessObject;
021import org.kuali.rice.krad.bo.ModuleConfiguration;
022import org.kuali.rice.krad.bo.PersistableBusinessObject;
023import org.kuali.rice.krad.dao.BusinessObjectDao;
024import org.kuali.rice.krad.dao.impl.BusinessObjectDaoOjb;
025import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
026import org.kuali.rice.krad.service.KualiModuleService;
027import org.kuali.rice.krad.service.ModuleService;
028import org.springframework.transaction.annotation.Transactional;
029
030import java.util.ArrayList;
031import java.util.Collection;
032import java.util.HashMap;
033import java.util.List;
034import java.util.Map;
035
036/**
037 * @deprecated use new KRAD Data framework {@link org.kuali.rice.krad.data.DataObjectService}
038 */
039@Deprecated
040@Transactional
041public class BusinessObjectDaoProxy implements BusinessObjectDao {
042
043        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BusinessObjectDaoProxy.class);
044
045        private BusinessObjectDao businessObjectDaoOjb;
046    private static KualiModuleService kualiModuleService;
047    private static HashMap<String, BusinessObjectDao> boDaoValues = new HashMap<String, BusinessObjectDao>();
048
049    private BusinessObjectDao getDao(Class clazz) {
050        ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
051        if (moduleService != null) {
052            ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
053            String dataSourceName = "";
054            if (moduleConfig != null) {
055                dataSourceName = moduleConfig.getDataSourceName();
056            }
057
058            if (StringUtils.isNotEmpty(dataSourceName)) {
059                if (boDaoValues.get(dataSourceName) != null) {
060                    return boDaoValues.get(dataSourceName);
061                } else {
062                    //using OJB
063                    BusinessObjectDaoOjb boDaoOjb = new BusinessObjectDaoOjb(
064                            KNSServiceLocator.getPersistenceStructureService());
065                    boDaoOjb.setJcdAlias(dataSourceName);
066                    // add to our cache of bo daos
067                    boDaoValues.put(dataSourceName, boDaoOjb);
068                    return boDaoOjb;
069                }
070
071            }
072        }
073        return businessObjectDaoOjb;
074    }
075
076        /**
077         * @see org.kuali.rice.krad.dao.BusinessObjectDao#countMatching(java.lang.Class, java.util.Map)
078         */
079        public int countMatching(Class clazz, Map<String, ?> fieldValues) {
080                return getDao(clazz).countMatching(clazz, fieldValues);
081        }
082
083        /**
084         * @see org.kuali.rice.krad.dao.BusinessObjectDao#countMatching(java.lang.Class, java.util.Map, java.util.Map)
085         */
086        public int countMatching(Class clazz, Map<String, ?> positiveFieldValues, Map<String, ?> negativeFieldValues) {
087                return getDao(clazz).countMatching(clazz, positiveFieldValues, negativeFieldValues);
088        }
089
090        /**
091         * @see org.kuali.rice.krad.dao.BusinessObjectDao#delete(org.kuali.rice.krad.bo.PersistableBusinessObject)
092         */
093        public void delete(Object bo) {
094                if (bo != null) {
095                        getDao(bo.getClass()).delete(bo);
096                }
097        }
098
099        /**
100         * @see org.kuali.rice.krad.dao.BusinessObjectDao#delete(java.util.List)
101         */
102        public void delete(List<? extends PersistableBusinessObject> boList) {
103                if (!boList.isEmpty()) {
104                        getDao(boList.get(0).getClass()).delete(boList);
105                }
106        }
107
108        /**
109         * @see org.kuali.rice.krad.dao.BusinessObjectDao#deleteMatching(java.lang.Class, java.util.Map)
110         */
111        public void deleteMatching(Class clazz, Map<String, ?> fieldValues) {
112                getDao(clazz).deleteMatching(clazz, fieldValues);
113        }
114
115        /**
116         * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAll(java.lang.Class)
117         */
118        public <T extends BusinessObject> Collection<T> findAll(Class<T> clazz) {
119                return getDao(clazz).findAll(clazz);
120        }
121
122        /**
123         * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAllActive(java.lang.Class)
124         */
125        public <T extends BusinessObject> Collection<T> findAllActive(Class<T> clazz) {
126                return getDao(clazz).findAllActive(clazz);
127        }
128
129        /**
130         * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAllInactive(java.lang.Class)
131         */
132        public <T extends BusinessObject> Collection<T> findAllInactive(Class<T> clazz) {
133                return getDao(clazz).findAllInactive(clazz);
134        }
135
136        /**
137         * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAllActiveOrderBy(java.lang.Class, java.lang.String, boolean)
138         */
139        public <T extends BusinessObject> Collection<T> findAllActiveOrderBy(Class<T> clazz, String sortField, boolean sortAscending) {
140                return getDao(clazz).findAllActiveOrderBy(clazz, sortField, sortAscending);
141        }
142
143        /**
144         * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAllOrderBy(java.lang.Class, java.lang.String, boolean)
145         */
146        public <T extends BusinessObject> Collection<T> findAllOrderBy(Class<T> clazz, String sortField, boolean sortAscending) {
147                return getDao(clazz).findAllOrderBy(clazz, sortField, sortAscending);
148        }
149
150        /**
151         * @see org.kuali.rice.krad.dao.BusinessObjectDao#findBySinglePrimaryKey(java.lang.Class, java.lang.Object)
152         */
153        public <T extends BusinessObject> T findBySinglePrimaryKey(Class<T> clazz, Object primaryKey) {
154                return getDao(clazz).findBySinglePrimaryKey(clazz, primaryKey);
155        }
156        
157        /**
158         * @see org.kuali.rice.krad.dao.BusinessObjectDao#findByPrimaryKey(java.lang.Class, java.util.Map)
159         */
160        public <T extends BusinessObject> T findByPrimaryKey(Class<T> clazz, Map<String, ?> primaryKeys) {
161                return getDao(clazz).findByPrimaryKey(clazz, primaryKeys);
162        }
163
164        /**
165         * @see org.kuali.rice.krad.dao.BusinessObjectDao#findMatching(java.lang.Class, java.util.Map)
166         */
167        public <T extends BusinessObject> Collection<T> findMatching(Class<T> clazz, Map<String, ?> fieldValues) {
168                return getDao(clazz).findMatching(clazz, fieldValues);
169        }
170
171        /**
172         * @see org.kuali.rice.krad.dao.BusinessObjectDao#findMatchingActive(java.lang.Class, java.util.Map)
173         */
174        public <T extends BusinessObject> Collection<T> findMatchingActive(Class<T> clazz, Map<String, ?> fieldValues) {
175                return getDao(clazz).findMatchingActive(clazz, fieldValues);
176        }
177
178        /**
179         * @see org.kuali.rice.krad.dao.BusinessObjectDao#findMatchingOrderBy(java.lang.Class, java.util.Map, java.lang.String, boolean)
180         */
181        public <T extends BusinessObject> Collection<T> findMatchingOrderBy(Class<T> clazz, Map<String, ?> fieldValues, String sortField, boolean sortAscending) {
182                return getDao(clazz).findMatchingOrderBy(clazz, fieldValues, sortField, sortAscending);
183        }
184
185        /**
186         * @see org.kuali.rice.krad.dao.BusinessObjectDao#retrieve(org.kuali.rice.krad.bo.PersistableBusinessObject)
187         */
188        public Object retrieve(Object object) {
189                return getDao(object.getClass()).retrieve(object);
190        }
191        
192        /**
193
194         * @see org.kuali.rice.krad.dao.BusinessObjectDao#manageReadOnly(org.kuali.rice.krad.bo.PersistableBusinessObject)
195         */
196        public PersistableBusinessObject manageReadOnly(PersistableBusinessObject bo) {
197                return getDao(bo.getClass()).manageReadOnly(bo);
198        }
199
200        /**
201         * Defers to correct DAO for this class
202         * @see org.kuali.rice.krad.dao.BusinessObjectDao#findByPrimaryKeyUsingKeyObject(java.lang.Class, java.lang.Object)
203         */
204        public <T extends BusinessObject> T findByPrimaryKeyUsingKeyObject(Class<T> clazz, Object pkObject) {
205                return getDao(clazz).findByPrimaryKeyUsingKeyObject(clazz, pkObject);
206        }
207
208        /**
209         * @see org.kuali.rice.krad.dao.BusinessObjectDao#save(org.kuali.rice.krad.bo.PersistableBusinessObject)
210         */
211        public PersistableBusinessObject save(PersistableBusinessObject bo) {
212                PersistableBusinessObject savedBo;
213                savedBo = getDao(bo.getClass()).save(bo);
214                return savedBo;
215        }
216
217        /**
218         * @see org.kuali.rice.krad.dao.BusinessObjectDao#save(java.util.List)
219         */
220        public List<? extends PersistableBusinessObject> save(List businessObjects) {
221                if (!businessObjects.isEmpty()) {
222                        return getDao(businessObjects.get(0).getClass()).save(businessObjects);
223                }
224                return new ArrayList<PersistableBusinessObject>();
225        }
226
227    private static KualiModuleService getKualiModuleService() {
228        if (kualiModuleService == null) {
229            kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
230        }
231        return kualiModuleService;
232    }
233
234        public void setBusinessObjectDaoOjb(BusinessObjectDao businessObjectDaoOjb) {
235                this.businessObjectDaoOjb = businessObjectDaoOjb;
236        }
237}