View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.app.persistence.jpa;
17  
18  import org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo;
19  import org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor;
20  
21  import javax.sql.DataSource;
22  
23  public class RicePersistenceUnitPostProcessor implements PersistenceUnitPostProcessor {
24  	static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RicePersistenceUnitPostProcessor.class);
25  	
26  	public static final String KRAD_APPLICATION_PERSISTENCE_UNIT_NAME = "krad-application-unit";
27  	public static final String KRAD_SERVER_PERSISTENCE_UNIT_NAME = "krad-server-unit";
28  
29  	private DataSource jtaDataSource;
30      
31      public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo mutablePersistenceUnitInfo) {
32          mutablePersistenceUnitInfo.setJtaDataSource(getJtaDataSource());
33          addKRADManagedClassNames(mutablePersistenceUnitInfo);
34          if (mutablePersistenceUnitInfo.getPersistenceUnitName().equals(KRAD_APPLICATION_PERSISTENCE_UNIT_NAME) || mutablePersistenceUnitInfo.getPersistenceUnitName().equals(
35                  KRAD_SERVER_PERSISTENCE_UNIT_NAME)) {
36          	addRiceManagedClassNamesToKRADPersistenceUnit(mutablePersistenceUnitInfo);
37          }
38      }
39      
40      /**
41       * 
42       * Adds all the KNS Managed entities to the persistence unit - which is important, becuase all
43       * persistence units get the KNS entities to manage
44       * 
45       * @param mutablePersistenceUnitInfo
46       */
47      public void addKRADManagedClassNames(MutablePersistenceUnitInfo mutablePersistenceUnitInfo) {
48      	addManagedClassNames(mutablePersistenceUnitInfo, new KRADPersistableBusinessObjectClassExposer());
49      }
50      
51      /**
52       * Adds the class names listed by exposed by the given exposer into the persistence unit
53       * 
54       * @param mutablePersistenceUnitInfo the persistence unit to add managed JPA entity class names to
55       * @param exposer the exposer for class names to manage
56       */
57      public void addManagedClassNames(MutablePersistenceUnitInfo mutablePersistenceUnitInfo, PersistableBusinessObjectClassExposer exposer) {
58      	for (String exposedClassName : exposer.exposePersistableBusinessObjectClassNames()) {
59      		if (LOG.isDebugEnabled()) {
60      			LOG.debug("JPA will now be managing class: "+exposedClassName);
61      		}
62      		mutablePersistenceUnitInfo.addManagedClassName(exposedClassName);
63      	}
64      }
65      
66      public void addRiceManagedClassNamesToKRADPersistenceUnit(MutablePersistenceUnitInfo mutablePersistenceUnitInfo) {
67      	addManagedClassNames(mutablePersistenceUnitInfo, new RiceToNervousSystemBusinessObjectClassExposer());
68      }
69  
70      public DataSource getJtaDataSource() {
71          return jtaDataSource;
72      }
73  
74      public void setJtaDataSource(DataSource jtaDataSource) {
75          this.jtaDataSource = jtaDataSource;
76      }
77  	
78  }