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