View Javadoc
1   package org.kuali.rice.krad.dao.proxy;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.rice.kns.service.KNSServiceLocator;
5   import org.kuali.rice.krad.bo.BusinessObject;
6   import org.kuali.rice.krad.bo.ModuleConfiguration;
7   import org.kuali.rice.krad.bo.PersistableBusinessObject;
8   import org.kuali.rice.krad.dao.BusinessObjectDao;
9   import org.kuali.rice.krad.dao.impl.BusinessObjectDaoOjb;
10  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
11  import org.kuali.rice.krad.service.KualiModuleService;
12  import org.kuali.rice.krad.service.ModuleService;
13  import org.springframework.transaction.annotation.Transactional;
14  
15  import java.util.*;
16  
17  /**
18   * Created by angelind on 11/6/14.
19   *
20   * Overridden by 'Sheik Salahudeen'
21   * Overridden for fixing the document fetching issue in KRAD Transaction document with OJB.
22   * Modified method names: findAll,findAllActive,findAllInactive,findAllActiveOrderBy,findAllOrderBy,findBySinglePrimaryKey,findByPrimaryKey,findMatching,findMatchingActive,findMatchingOrderBy,findByPrimaryKeyUsingKeyObject
23   * Changes description : Modified the return type of method from '<T extends BusinessObject>' to  '<T>'.
24   */
25  @Deprecated
26  @Transactional
27  public class BusinessObjectDaoProxy implements BusinessObjectDao {
28  
29      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BusinessObjectDaoProxy.class);
30  
31      private BusinessObjectDao businessObjectDaoOjb;
32      private static KualiModuleService kualiModuleService;
33      private static HashMap<String, BusinessObjectDao> boDaoValues = new HashMap<String, BusinessObjectDao>();
34  
35      private BusinessObjectDao getDao(Class clazz) {
36          ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
37          if (moduleService != null) {
38              ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
39              String dataSourceName = "";
40              if (moduleConfig != null) {
41                  dataSourceName = moduleConfig.getDataSourceName();
42              }
43  
44              if (StringUtils.isNotEmpty(dataSourceName)) {
45                  if (boDaoValues.get(dataSourceName) != null) {
46                      return boDaoValues.get(dataSourceName);
47                  } else {
48                      //using OJB
49                      BusinessObjectDaoOjb boDaoOjb = new BusinessObjectDaoOjb(
50                              KNSServiceLocator.getPersistenceStructureService());
51                      boDaoOjb.setJcdAlias(dataSourceName);
52                      // add to our cache of bo daos
53                      boDaoValues.put(dataSourceName, boDaoOjb);
54                      return boDaoOjb;
55                  }
56  
57              }
58          }
59          return businessObjectDaoOjb;
60      }
61  
62      /**
63       * @see org.kuali.rice.krad.dao.BusinessObjectDao#countMatching(java.lang.Class, java.util.Map)
64       */
65      public int countMatching(Class clazz, Map<String, ?> fieldValues) {
66          return getDao(clazz).countMatching(clazz, fieldValues);
67      }
68  
69      /**
70       * @see org.kuali.rice.krad.dao.BusinessObjectDao#countMatching(java.lang.Class, java.util.Map, java.util.Map)
71       */
72      public int countMatching(Class clazz, Map<String, ?> positiveFieldValues, Map<String, ?> negativeFieldValues) {
73          return getDao(clazz).countMatching(clazz, positiveFieldValues, negativeFieldValues);
74      }
75  
76      /**
77       * @see org.kuali.rice.krad.dao.BusinessObjectDao#delete(org.kuali.rice.krad.bo.PersistableBusinessObject)
78       */
79      public void delete(Object bo) {
80          if (bo != null) {
81              getDao(bo.getClass()).delete(bo);
82          }
83      }
84  
85      /**
86       * @see org.kuali.rice.krad.dao.BusinessObjectDao#delete(java.util.List)
87       */
88      public void delete(List<? extends PersistableBusinessObject> boList) {
89          if (!boList.isEmpty()) {
90              getDao(boList.get(0).getClass()).delete(boList);
91          }
92      }
93  
94      /**
95       * @see org.kuali.rice.krad.dao.BusinessObjectDao#deleteMatching(java.lang.Class, java.util.Map)
96       */
97      public void deleteMatching(Class clazz, Map<String, ?> fieldValues) {
98          getDao(clazz).deleteMatching(clazz, fieldValues);
99      }
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