| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.krad.dao.proxy; |
| 18 | |
|
| 19 | |
import org.apache.commons.lang.StringUtils; |
| 20 | |
import org.kuali.rice.core.api.CoreApiServiceLocator; |
| 21 | |
import org.kuali.rice.core.api.config.ConfigurationException; |
| 22 | |
import org.kuali.rice.core.framework.persistence.jpa.OrmUtils; |
| 23 | |
import org.kuali.rice.krad.bo.ModuleConfiguration; |
| 24 | |
import org.kuali.rice.krad.dao.LookupDao; |
| 25 | |
import org.kuali.rice.krad.dao.impl.LookupDaoJpa; |
| 26 | |
import org.kuali.rice.krad.dao.impl.LookupDaoOjb; |
| 27 | |
import org.kuali.rice.krad.service.KRADServiceLocator; |
| 28 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
| 29 | |
import org.kuali.rice.krad.service.KualiModuleService; |
| 30 | |
import org.kuali.rice.krad.service.ModuleService; |
| 31 | |
import org.springframework.transaction.annotation.Transactional; |
| 32 | |
|
| 33 | |
import javax.persistence.EntityManager; |
| 34 | |
import java.util.Collection; |
| 35 | |
import java.util.Collections; |
| 36 | |
import java.util.HashMap; |
| 37 | |
import java.util.Map; |
| 38 | |
|
| 39 | |
@Transactional |
| 40 | 0 | public class LookupDaoProxy implements LookupDao { |
| 41 | |
|
| 42 | |
private LookupDao lookupDaoJpa; |
| 43 | |
private LookupDao lookupDaoOjb; |
| 44 | |
private static KualiModuleService kualiModuleService; |
| 45 | 0 | private static Map<String, LookupDao> lookupDaoValues = Collections.synchronizedMap(new HashMap<String, LookupDao>()); |
| 46 | |
|
| 47 | |
public void setLookupDaoJpa(LookupDao lookupDaoJpa) { |
| 48 | 0 | this.lookupDaoJpa = lookupDaoJpa; |
| 49 | 0 | } |
| 50 | |
|
| 51 | |
public void setLookupDaoOjb(LookupDao lookupDaoOjb) { |
| 52 | 0 | this.lookupDaoOjb = lookupDaoOjb; |
| 53 | 0 | } |
| 54 | |
|
| 55 | |
private LookupDao getDao(Class clazz) { |
| 56 | 0 | ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz); |
| 57 | 0 | if (moduleService != null) { |
| 58 | 0 | ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration(); |
| 59 | 0 | String dataSourceName = ""; |
| 60 | 0 | EntityManager entityManager = null; |
| 61 | 0 | if (moduleConfig != null) { |
| 62 | 0 | dataSourceName = moduleConfig.getDataSourceName(); |
| 63 | 0 | entityManager = moduleConfig.getEntityManager(); |
| 64 | |
} |
| 65 | |
|
| 66 | 0 | if (StringUtils.isNotEmpty(dataSourceName)) { |
| 67 | 0 | if (lookupDaoValues.get(dataSourceName) != null) { |
| 68 | 0 | return lookupDaoValues.get(dataSourceName); |
| 69 | |
} else { |
| 70 | 0 | if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) { |
| 71 | |
|
| 72 | 0 | LookupDaoJpa classSpecificLookupDaoJpa = new LookupDaoJpa(); |
| 73 | 0 | if (entityManager != null) { |
| 74 | 0 | classSpecificLookupDaoJpa.setEntityManager(entityManager); |
| 75 | 0 | classSpecificLookupDaoJpa.setPersistenceStructureService( |
| 76 | |
KRADServiceLocator.getPersistenceStructureService()); |
| 77 | 0 | classSpecificLookupDaoJpa.setDateTimeService(CoreApiServiceLocator.getDateTimeService()); |
| 78 | 0 | lookupDaoValues.put(dataSourceName, classSpecificLookupDaoJpa); |
| 79 | 0 | return classSpecificLookupDaoJpa; |
| 80 | |
} else { |
| 81 | 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)."); |
| 82 | |
} |
| 83 | |
} else { |
| 84 | 0 | LookupDaoOjb classSpecificLookupDaoOjb = new LookupDaoOjb(); |
| 85 | 0 | classSpecificLookupDaoOjb.setJcdAlias(dataSourceName); |
| 86 | 0 | classSpecificLookupDaoOjb.setPersistenceStructureService( |
| 87 | |
KRADServiceLocator.getPersistenceStructureService()); |
| 88 | 0 | classSpecificLookupDaoOjb.setDateTimeService(CoreApiServiceLocator.getDateTimeService()); |
| 89 | 0 | classSpecificLookupDaoOjb.setDataDictionaryService( |
| 90 | |
KRADServiceLocatorWeb.getDataDictionaryService()); |
| 91 | 0 | lookupDaoValues.put(dataSourceName, classSpecificLookupDaoOjb); |
| 92 | 0 | return classSpecificLookupDaoOjb; |
| 93 | |
} |
| 94 | |
} |
| 95 | |
|
| 96 | |
} |
| 97 | |
} |
| 98 | |
|
| 99 | 0 | return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? lookupDaoJpa : lookupDaoOjb; |
| 100 | |
} |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public boolean createCriteria(Object example, String searchValue, String propertyName, Object criteria) { |
| 106 | 0 | return getDao(example.getClass()).createCriteria(example, searchValue, propertyName, criteria); |
| 107 | |
} |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public boolean createCriteria(Object example, String searchValue, String propertyName, boolean caseInsensitive, boolean treatWildcardsAndOperatorsAsLiteral, Object criteria) { |
| 113 | 0 | return getDao(example.getClass()).createCriteria(example, searchValue, propertyName, caseInsensitive, |
| 114 | |
treatWildcardsAndOperatorsAsLiteral, criteria); |
| 115 | |
} |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
public Collection findCollectionBySearchHelper(Class example, Map formProps, boolean unbounded, boolean usePrimaryKeyValuesOnly) { |
| 121 | 0 | return getDao(example).findCollectionBySearchHelper(example, formProps, unbounded, usePrimaryKeyValuesOnly); |
| 122 | |
} |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
public Long findCountByMap(Object example, Map formProps) { |
| 128 | 0 | return getDao(example.getClass()).findCountByMap(example, formProps); |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
public Object findObjectByMap(Object example, Map formProps) { |
| 135 | 0 | return getDao(example.getClass()).findObjectByMap(example, formProps); |
| 136 | |
} |
| 137 | |
|
| 138 | |
private static KualiModuleService getKualiModuleService() { |
| 139 | 0 | if (kualiModuleService == null) { |
| 140 | 0 | kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService(); |
| 141 | |
} |
| 142 | 0 | return kualiModuleService; |
| 143 | |
} |
| 144 | |
} |