001    /**
002     * Copyright 2005-2014 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.dao.proxy;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.krad.bo.ModuleConfiguration;
020    import org.kuali.rice.krad.dao.BusinessObjectDao;
021    import org.kuali.rice.krad.dao.DocumentDao;
022    import org.kuali.rice.krad.dao.impl.DocumentDaoOjb;
023    import org.kuali.rice.krad.document.Document;
024    import org.kuali.rice.krad.service.DocumentAdHocService;
025    import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
026    import org.kuali.rice.krad.service.KualiModuleService;
027    import org.kuali.rice.krad.service.ModuleService;
028    import org.kuali.rice.krad.util.LegacyUtils;
029    import org.springframework.transaction.annotation.Transactional;
030    
031    import java.util.List;
032    import java.util.Map;
033    import java.util.concurrent.ConcurrentHashMap;
034    
035    @Transactional
036    public class DocumentDaoProxy implements DocumentDao {
037    
038        private DocumentDao documentDaoOjb;
039    
040        private static KualiModuleService kualiModuleService;
041        private static Map<String, DocumentDao> documentDaoValues = new ConcurrentHashMap<String, DocumentDao>();
042    
043        private DocumentDao getDao(Class<? extends Document> clazz) {
044            ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
045            if (moduleService != null) {
046                ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
047                String dataSourceName = "";
048                if (moduleConfig != null) {
049                    dataSourceName = moduleConfig.getDataSourceName();
050                }
051    
052                if (StringUtils.isNotEmpty(dataSourceName)) {
053                    if (documentDaoValues.get(dataSourceName) != null) {
054                        return documentDaoValues.get(dataSourceName);
055                    }
056                    //using OJB
057                    DocumentDaoOjb documentDaoOjbInstance =
058                            new DocumentDaoOjb(
059                                            this.documentDaoOjb.getBusinessObjectDao(),
060                                            this.documentDaoOjb.getDocumentAdHocService());
061    
062                    // set the data source alias
063                    documentDaoOjbInstance.setJcdAlias(dataSourceName);
064    
065                    documentDaoValues.put(dataSourceName, documentDaoOjbInstance);
066                    return documentDaoOjbInstance;
067    
068                    }
069    
070            }
071    
072            return documentDaoOjb;
073        }
074        
075        /**
076             * @see org.kuali.rice.krad.dao.DocumentDao#save(org.kuali.rice.krad.document.Document)
077             */
078        @Override
079            public <T extends Document> T save(T document) {
080                    return getDao(document.getClass()).save(document);
081            }
082        
083            /**
084             * @see org.kuali.rice.krad.dao.DocumentDao#findByDocumentHeaderId(java.lang.Class, java.lang.String)
085             */
086        @Override
087            public <T extends Document> T findByDocumentHeaderId(Class<T> clazz, String id) {
088                    return getDao(clazz).findByDocumentHeaderId(clazz, id);
089            }
090    
091            /**
092             * @see org.kuali.rice.krad.dao.DocumentDao#findByDocumentHeaderIds(java.lang.Class, java.util.List)
093             */
094        @Override
095            public <T extends Document> List<T> findByDocumentHeaderIds(Class<T> clazz, List<String> idList) {
096                    return getDao(clazz).findByDocumentHeaderIds(clazz, idList);
097            }
098    
099            /**
100             * @see org.kuali.rice.krad.dao.DocumentDao#getBusinessObjectDao()
101             */
102        @Override
103            public BusinessObjectDao getBusinessObjectDao() {
104                    return documentDaoOjb.getBusinessObjectDao();
105            }
106    
107            /**
108             * @see org.kuali.rice.krad.dao.DocumentDao#getDocumentAdHocService()
109             */
110        @Override
111            public DocumentAdHocService getDocumentAdHocService() {
112                    return documentDaoOjb.getDocumentAdHocService();
113        }
114    
115            public void setDocumentDaoOjb(DocumentDao documentDaoOjb) {
116                    this.documentDaoOjb = documentDaoOjb;
117            }
118    
119        private synchronized static KualiModuleService getKualiModuleService() {
120            if (kualiModuleService == null) {
121                kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
122            }
123            return kualiModuleService;
124        }
125    
126    }