1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.dao.proxy; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.kuali.rice.core.api.config.ConfigurationException; |
20 | |
import org.kuali.rice.core.framework.persistence.jpa.OrmUtils; |
21 | |
import org.kuali.rice.krad.bo.ModuleConfiguration; |
22 | |
import org.kuali.rice.krad.dao.BusinessObjectDao; |
23 | |
import org.kuali.rice.krad.dao.DocumentDao; |
24 | |
import org.kuali.rice.krad.dao.impl.DocumentDaoJpa; |
25 | |
import org.kuali.rice.krad.dao.impl.DocumentDaoOjb; |
26 | |
import org.kuali.rice.krad.document.Document; |
27 | |
import org.kuali.rice.krad.service.DocumentAdHocService; |
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.List; |
35 | |
import java.util.Map; |
36 | |
import java.util.concurrent.ConcurrentHashMap; |
37 | |
|
38 | |
@Transactional |
39 | 0 | public class DocumentDaoProxy implements DocumentDao { |
40 | |
|
41 | |
private DocumentDao documentDaoJpa; |
42 | |
private DocumentDao documentDaoOjb; |
43 | |
|
44 | |
private static KualiModuleService kualiModuleService; |
45 | 0 | private static Map<String, DocumentDao> documentDaoValues = new ConcurrentHashMap<String, DocumentDao>(); |
46 | |
|
47 | |
private DocumentDao getDao(Class<? extends Document> clazz) { |
48 | 0 | ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz); |
49 | 0 | if (moduleService != null) { |
50 | 0 | ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration(); |
51 | 0 | String dataSourceName = ""; |
52 | 0 | EntityManager entityManager = null; |
53 | 0 | if (moduleConfig != null) { |
54 | 0 | dataSourceName = moduleConfig.getDataSourceName(); |
55 | 0 | entityManager = moduleConfig.getEntityManager(); |
56 | |
} |
57 | |
|
58 | 0 | if (StringUtils.isNotEmpty(dataSourceName)) { |
59 | 0 | if (documentDaoValues.get(dataSourceName) != null) { |
60 | 0 | return documentDaoValues.get(dataSourceName); |
61 | |
} |
62 | 0 | if (OrmUtils.isJpaAnnotated(clazz) && (OrmUtils.isJpaEnabled() || OrmUtils.isJpaEnabled("rice.krad"))) { |
63 | |
|
64 | 0 | if (entityManager != null) { |
65 | |
|
66 | 0 | DocumentDaoJpa documentDaoJpaInstance = |
67 | |
new DocumentDaoJpa(entityManager, this.documentDaoJpa.getBusinessObjectDao(), |
68 | |
this.documentDaoJpa.getDocumentAdHocService()); |
69 | |
|
70 | 0 | documentDaoValues.put(dataSourceName, documentDaoJpaInstance); |
71 | 0 | return documentDaoJpaInstance; |
72 | |
} |
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 | 0 | DocumentDaoOjb documentDaoOjbInstance = |
77 | |
new DocumentDaoOjb( |
78 | |
this.documentDaoOjb.getBusinessObjectDao(), |
79 | |
this.documentDaoOjb.getDocumentAdHocService()); |
80 | |
|
81 | |
|
82 | 0 | documentDaoOjbInstance.setJcdAlias(dataSourceName); |
83 | |
|
84 | 0 | documentDaoValues.put(dataSourceName, documentDaoOjbInstance); |
85 | 0 | return documentDaoOjbInstance; |
86 | |
|
87 | |
} |
88 | |
|
89 | |
} |
90 | 0 | return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? documentDaoJpa : documentDaoOjb; |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
@Override |
97 | |
public <T extends Document> T save(T document) { |
98 | 0 | return getDao(document.getClass()).save(document); |
99 | |
} |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
@Override |
105 | |
public <T extends Document> T findByDocumentHeaderId(Class<T> clazz, String id) { |
106 | 0 | return getDao(clazz).findByDocumentHeaderId(clazz, id); |
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
@Override |
113 | |
public <T extends Document> List<T> findByDocumentHeaderIds(Class<T> clazz, List<String> idList) { |
114 | 0 | return getDao(clazz).findByDocumentHeaderIds(clazz, idList); |
115 | |
} |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
@Override |
121 | |
public BusinessObjectDao getBusinessObjectDao() { |
122 | 0 | if (OrmUtils.isJpaEnabled()) { |
123 | 0 | return documentDaoJpa.getBusinessObjectDao(); |
124 | |
} |
125 | 0 | return documentDaoOjb.getBusinessObjectDao(); |
126 | |
} |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
@Override |
132 | |
public DocumentAdHocService getDocumentAdHocService() { |
133 | 0 | if (OrmUtils.isJpaEnabled()) { |
134 | 0 | return documentDaoJpa.getDocumentAdHocService(); |
135 | |
} |
136 | 0 | return documentDaoOjb.getDocumentAdHocService(); |
137 | |
} |
138 | |
|
139 | |
public void setDocumentDaoJpa(DocumentDao documentDaoJpa) { |
140 | 0 | this.documentDaoJpa = documentDaoJpa; |
141 | 0 | } |
142 | |
|
143 | |
public void setDocumentDaoOjb(DocumentDao documentDaoOjb) { |
144 | 0 | this.documentDaoOjb = documentDaoOjb; |
145 | 0 | } |
146 | |
|
147 | |
private synchronized static KualiModuleService getKualiModuleService() { |
148 | 0 | if (kualiModuleService == null) { |
149 | 0 | kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService(); |
150 | |
} |
151 | 0 | return kualiModuleService; |
152 | |
} |
153 | |
|
154 | |
} |