| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kns.dao.proxy; |
| 17 | |
|
| 18 | |
import java.util.Collection; |
| 19 | |
import java.util.HashMap; |
| 20 | |
import java.util.List; |
| 21 | |
import java.util.Map; |
| 22 | |
|
| 23 | |
import javax.persistence.EntityManager; |
| 24 | |
|
| 25 | |
import org.apache.commons.lang.StringUtils; |
| 26 | |
import org.kuali.rice.core.config.ConfigurationException; |
| 27 | |
import org.kuali.rice.core.util.OrmUtils; |
| 28 | |
import org.kuali.rice.kns.bo.ModuleConfiguration; |
| 29 | |
import org.kuali.rice.kns.bo.PersistableBusinessObject; |
| 30 | |
import org.kuali.rice.kns.dao.BusinessObjectDao; |
| 31 | |
import org.kuali.rice.kns.dao.impl.BusinessObjectDaoJpa; |
| 32 | |
import org.kuali.rice.kns.dao.impl.BusinessObjectDaoOjb; |
| 33 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
| 34 | |
import org.kuali.rice.kns.service.KualiModuleService; |
| 35 | |
import org.kuali.rice.kns.service.ModuleService; |
| 36 | |
import org.springframework.transaction.annotation.Transactional; |
| 37 | |
|
| 38 | |
@Transactional |
| 39 | 0 | public class BusinessObjectDaoProxy implements BusinessObjectDao { |
| 40 | |
|
| 41 | 0 | private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BusinessObjectDaoProxy.class); |
| 42 | |
|
| 43 | |
private BusinessObjectDao businessObjectDaoJpa; |
| 44 | |
private BusinessObjectDao businessObjectDaoOjb; |
| 45 | |
private static KualiModuleService kualiModuleService; |
| 46 | 0 | private static HashMap<String, BusinessObjectDao> boDaoValues = new HashMap<String, BusinessObjectDao>(); |
| 47 | |
|
| 48 | |
private BusinessObjectDao getDao(Class clazz) { |
| 49 | 0 | ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz); |
| 50 | 0 | if (moduleService != null) { |
| 51 | 0 | ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration(); |
| 52 | 0 | String dataSourceName = ""; |
| 53 | 0 | EntityManager entityManager = null; |
| 54 | 0 | if (moduleConfig != null) { |
| 55 | 0 | dataSourceName = moduleConfig.getDataSourceName(); |
| 56 | 0 | entityManager = moduleConfig.getEntityManager(); |
| 57 | |
} |
| 58 | |
|
| 59 | 0 | if (StringUtils.isNotEmpty(dataSourceName)) { |
| 60 | 0 | if (boDaoValues.get(dataSourceName) != null) { |
| 61 | 0 | return boDaoValues.get(dataSourceName); |
| 62 | |
} else { |
| 63 | 0 | if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) { |
| 64 | |
|
| 65 | 0 | if (entityManager != null) { |
| 66 | 0 | BusinessObjectDaoJpa boDaoJpa = |
| 67 | |
new BusinessObjectDaoJpa(entityManager, KNSServiceLocator.getPersistenceStructureService()); |
| 68 | |
|
| 69 | 0 | boDaoValues.put(dataSourceName, boDaoJpa); |
| 70 | |
|
| 71 | 0 | return boDaoJpa; |
| 72 | |
} else { |
| 73 | 0 | throw new ConfigurationException("EntityManager is null. EntityManager must be set in the Module Configuration bean in the appropriate spring beans xml. (see nested exception for details)."); |
| 74 | |
} |
| 75 | |
|
| 76 | |
} else { |
| 77 | |
|
| 78 | 0 | BusinessObjectDaoOjb boDaoOjb = new BusinessObjectDaoOjb(KNSServiceLocator.getPersistenceStructureService()); |
| 79 | 0 | boDaoOjb.setJcdAlias(dataSourceName); |
| 80 | |
|
| 81 | 0 | boDaoValues.put(dataSourceName, boDaoOjb); |
| 82 | |
|
| 83 | 0 | return boDaoOjb; |
| 84 | |
} |
| 85 | |
|
| 86 | |
} |
| 87 | |
|
| 88 | |
} |
| 89 | |
} |
| 90 | 0 | return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? businessObjectDaoJpa : businessObjectDaoOjb; |
| 91 | |
} |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
public int countMatching(Class clazz, Map fieldValues) { |
| 97 | 0 | return getDao(clazz).countMatching(clazz, fieldValues); |
| 98 | |
} |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
public int countMatching(Class clazz, Map positiveFieldValues, Map negativeFieldValues) { |
| 104 | 0 | return getDao(clazz).countMatching(clazz, positiveFieldValues, negativeFieldValues); |
| 105 | |
} |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
public void delete(PersistableBusinessObject bo) { |
| 111 | 0 | if (bo != null) { |
| 112 | 0 | getDao(bo.getClass()).delete(bo); |
| 113 | |
} |
| 114 | 0 | } |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
public void delete(List<? extends PersistableBusinessObject> boList) { |
| 120 | 0 | if (!boList.isEmpty()) { |
| 121 | 0 | getDao(boList.get(0).getClass()).delete(boList); |
| 122 | |
} |
| 123 | 0 | } |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
public void deleteMatching(Class clazz, Map fieldValues) { |
| 129 | 0 | getDao(clazz).deleteMatching(clazz, fieldValues); |
| 130 | 0 | } |
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
public Collection findAll(Class clazz) { |
| 136 | 0 | return getDao(clazz).findAll(clazz); |
| 137 | |
} |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
public Collection findAllActive(Class clazz) { |
| 143 | 0 | return getDao(clazz).findAllActive(clazz); |
| 144 | |
} |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
public Collection findAllInactive(Class clazz) { |
| 150 | 0 | return getDao(clazz).findAllInactive(clazz); |
| 151 | |
} |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
public Collection findAllActiveOrderBy(Class clazz, String sortField, boolean sortAscending) { |
| 157 | 0 | return getDao(clazz).findAllActiveOrderBy(clazz, sortField, sortAscending); |
| 158 | |
} |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
public Collection findAllOrderBy(Class clazz, String sortField, boolean sortAscending) { |
| 164 | 0 | return getDao(clazz).findAllOrderBy(clazz, sortField, sortAscending); |
| 165 | |
} |
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
public PersistableBusinessObject findBySinglePrimaryKey(Class clazz, Object primaryKey) { |
| 171 | 0 | return getDao(clazz).findBySinglePrimaryKey(clazz, primaryKey); |
| 172 | |
} |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
public PersistableBusinessObject findByPrimaryKey(Class clazz, Map primaryKeys) { |
| 178 | 0 | return getDao(clazz).findByPrimaryKey(clazz, primaryKeys); |
| 179 | |
} |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
public Collection findMatching(Class clazz, Map fieldValues) { |
| 185 | 0 | return getDao(clazz).findMatching(clazz, fieldValues); |
| 186 | |
} |
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
public Collection findMatchingActive(Class clazz, Map fieldValues) { |
| 192 | 0 | return getDao(clazz).findMatchingActive(clazz, fieldValues); |
| 193 | |
} |
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
public Collection findMatchingOrderBy(Class clazz, Map fieldValues, String sortField, boolean sortAscending) { |
| 199 | 0 | return getDao(clazz).findMatchingOrderBy(clazz, fieldValues, sortField, sortAscending); |
| 200 | |
} |
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
public PersistableBusinessObject retrieve(PersistableBusinessObject object) { |
| 206 | 0 | return getDao(object.getClass()).retrieve(object); |
| 207 | |
} |
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
public void save(PersistableBusinessObject bo) { |
| 213 | 0 | getDao(bo.getClass()).save(bo); |
| 214 | 0 | } |
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
public void save(List businessObjects) { |
| 220 | 0 | if (!businessObjects.isEmpty()) { |
| 221 | 0 | getDao(businessObjects.get(0).getClass()).save(businessObjects); |
| 222 | |
} |
| 223 | 0 | } |
| 224 | |
|
| 225 | |
private static KualiModuleService getKualiModuleService() { |
| 226 | 0 | if (kualiModuleService == null) { |
| 227 | 0 | kualiModuleService = KNSServiceLocator.getKualiModuleService(); |
| 228 | |
} |
| 229 | 0 | return kualiModuleService; |
| 230 | |
} |
| 231 | |
|
| 232 | |
public void setBusinessObjectDaoJpa(BusinessObjectDao businessObjectDaoJpa) { |
| 233 | 0 | this.businessObjectDaoJpa = businessObjectDaoJpa; |
| 234 | 0 | } |
| 235 | |
|
| 236 | |
public void setBusinessObjectDaoOjb(BusinessObjectDao businessObjectDaoOjb) { |
| 237 | 0 | this.businessObjectDaoOjb = businessObjectDaoOjb; |
| 238 | 0 | } |
| 239 | |
|
| 240 | |
} |