001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.krad.app.persistence.jpa;
017
018import org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo;
019import org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor;
020
021import javax.sql.DataSource;
022
023public class RicePersistenceUnitPostProcessor implements PersistenceUnitPostProcessor {
024        static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RicePersistenceUnitPostProcessor.class);
025        
026        public static final String KRAD_APPLICATION_PERSISTENCE_UNIT_NAME = "krad-application-unit";
027        public static final String KRAD_SERVER_PERSISTENCE_UNIT_NAME = "krad-server-unit";
028
029        private DataSource jtaDataSource;
030    
031    public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo mutablePersistenceUnitInfo) {
032        mutablePersistenceUnitInfo.setJtaDataSource(getJtaDataSource());
033        addKRADManagedClassNames(mutablePersistenceUnitInfo);
034        if (mutablePersistenceUnitInfo.getPersistenceUnitName().equals(KRAD_APPLICATION_PERSISTENCE_UNIT_NAME) || mutablePersistenceUnitInfo.getPersistenceUnitName().equals(
035                KRAD_SERVER_PERSISTENCE_UNIT_NAME)) {
036                addRiceManagedClassNamesToKRADPersistenceUnit(mutablePersistenceUnitInfo);
037        }
038    }
039    
040    /**
041     * 
042     * Adds all the KNS Managed entities to the persistence unit - which is important, becuase all
043     * persistence units get the KNS entities to manage
044     * 
045     * @param mutablePersistenceUnitInfo
046     */
047    public void addKRADManagedClassNames(MutablePersistenceUnitInfo mutablePersistenceUnitInfo) {
048        addManagedClassNames(mutablePersistenceUnitInfo, new KRADPersistableBusinessObjectClassExposer());
049    }
050    
051    /**
052     * Adds the class names listed by exposed by the given exposer into the persistence unit
053     * 
054     * @param mutablePersistenceUnitInfo the persistence unit to add managed JPA entity class names to
055     * @param exposer the exposer for class names to manage
056     */
057    public void addManagedClassNames(MutablePersistenceUnitInfo mutablePersistenceUnitInfo, PersistableBusinessObjectClassExposer exposer) {
058        for (String exposedClassName : exposer.exposePersistableBusinessObjectClassNames()) {
059                if (LOG.isDebugEnabled()) {
060                        LOG.debug("JPA will now be managing class: "+exposedClassName);
061                }
062                mutablePersistenceUnitInfo.addManagedClassName(exposedClassName);
063        }
064    }
065    
066    public void addRiceManagedClassNamesToKRADPersistenceUnit(MutablePersistenceUnitInfo mutablePersistenceUnitInfo) {
067        addManagedClassNames(mutablePersistenceUnitInfo, new RiceToNervousSystemBusinessObjectClassExposer());
068    }
069
070    public DataSource getJtaDataSource() {
071        return jtaDataSource;
072    }
073
074    public void setJtaDataSource(DataSource jtaDataSource) {
075        this.jtaDataSource = jtaDataSource;
076    }
077        
078}