Coverage Report - org.kuali.rice.kns.org.kuali.rice.krad.app.persistence.jpa.RiceLocalContainerEntityManagerFactoryBean
 
Classes in this File Line Coverage Branch Coverage Complexity
RiceLocalContainerEntityManagerFactoryBean
0%
0/54
0%
0/6
1.714
 
 1  
 /*
 2  
  * Copyright 2006-2011 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  
 
 17  
 package org.kuali.rice.kns.org.kuali.rice.krad.app.persistence.jpa;
 18  
 
 19  
 import org.kuali.rice.core.api.config.property.Config;
 20  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 21  
 import org.kuali.rice.core.framework.persistence.jpa.DevHibernateJpaVendorAdapter;
 22  
 import org.springframework.orm.jpa.JpaVendorAdapter;
 23  
 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
 24  
 import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager;
 25  
 import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
 26  
 
 27  
 import javax.sql.DataSource;
 28  
 import java.util.HashMap;
 29  
 import java.util.Map;
 30  
 
 31  
 /**
 32  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 33  
  */
 34  
 public class RiceLocalContainerEntityManagerFactoryBean extends LocalContainerEntityManagerFactoryBean {
 35  
         
 36  0
         public RiceLocalContainerEntityManagerFactoryBean() {
 37  0
                 throw new RuntimeException(getClass().getName() + " can not be constructed without a datasource");
 38  
         }
 39  
 
 40  
         public RiceLocalContainerEntityManagerFactoryBean(DataSource datasource) {
 41  0
                 this("", datasource);
 42  0
         }
 43  
         
 44  0
         public RiceLocalContainerEntityManagerFactoryBean(String prefix, DataSource datasource) {
 45  0
                 if (prefix.equals("")) {                        
 46  0
                         prefix = "rice";
 47  
                 }
 48  0
                 prefix += ".jpa.";
 49  
                 
 50  0
                 Config config = ConfigContext.getCurrentContextConfig();
 51  
         
 52  0
                 setPersistenceUnitManager(preparePersistentUnitManager(config, prefix, datasource));
 53  0
                 setPersistenceXmlLocation(determineConfigProperty(config, prefix, "PersistenceXmlLocation", "META-INF/persistence.xml"));
 54  0
                 setDataSource(datasource);
 55  0
                 setPersistenceUnitName(determineConfigProperty(config, prefix, "PersistenceUnitName", "rice"));
 56  0
                 setJpaDialect(new org.springframework.orm.jpa.vendor.HibernateJpaDialect());
 57  0
                 setJpaPropertyMap(prepareJpaProperties(config, prefix));
 58  0
                 setJpaVendorAdapter(prepareJpaVendorAdapter(config, prefix));
 59  
                 
 60  0
                 RicePersistenceUnitPostProcessor postProcessor = new RicePersistenceUnitPostProcessor();
 61  0
                 postProcessor.setJtaDataSource(datasource);
 62  0
                 setPersistenceUnitPostProcessors(new RicePersistenceUnitPostProcessor[] { postProcessor });
 63  0
         }
 64  
 
 65  
 
 66  
         private PersistenceUnitManager preparePersistentUnitManager(Config config, String prefix, DataSource datasource) {
 67  0
                 DefaultPersistenceUnitManager persistenceUnitManager = new DefaultPersistenceUnitManager();
 68  0
                 persistenceUnitManager.setDefaultDataSource(datasource);
 69  0
                 persistenceUnitManager.setPersistenceXmlLocations(new String[] {determineConfigProperty(config, prefix, "PersistenceXmlLocation", "META-INF/persistence.xml")});
 70  0
                 persistenceUnitManager.setDefaultPersistenceUnitRootLocation(determineConfigProperty(config, prefix, "PersistenceUnitRootLocation", "classpath:"));
 71  0
                 RicePersistenceUnitPostProcessor postProcessor = new RicePersistenceUnitPostProcessor();
 72  0
                 postProcessor.setJtaDataSource(datasource);
 73  0
                 persistenceUnitManager.setPersistenceUnitPostProcessors(new RicePersistenceUnitPostProcessor[] { postProcessor });
 74  0
                 persistenceUnitManager.afterPropertiesSet();
 75  0
                 return persistenceUnitManager;
 76  
         }
 77  
 
 78  
         private JpaVendorAdapter prepareJpaVendorAdapter(Config config, String prefix) {
 79  0
                 DevHibernateJpaVendorAdapter jpaVendorAdapter = new DevHibernateJpaVendorAdapter();
 80  0
                 jpaVendorAdapter.setDatabasePlatform(determineConfigProperty(config, prefix, "DatabasePlatform", "org.hibernate.dialect.MySQLDialect"));
 81  0
                 jpaVendorAdapter.setGenerateDdl(new Boolean(determineConfigProperty(config, prefix, "GenerateDdl", "false")));
 82  0
                 jpaVendorAdapter.setSerializationFilename(determineConfigProperty(config, prefix, "SerializationFilename", "/tmp/Ejb3Configuration.out"));
 83  0
                 jpaVendorAdapter.setUseSerialization(new Boolean(determineConfigProperty(config, prefix, "UseSerialization", "true")));
 84  
                 try {
 85  0
                         jpaVendorAdapter.afterPropertiesSet();
 86  0
                 } catch (Exception e) {}
 87  0
                 return jpaVendorAdapter;
 88  
         }
 89  
 
 90  
         private Map<String, String> prepareJpaProperties(Config config, String prefix) {
 91  0
                 Map<String, String> jpaProperties = new HashMap<String, String>();
 92  
                 
 93  
                 // Load in all user specified "JPAProperties" prefixed properties                
 94  0
                 jpaProperties.putAll(config.getPropertiesWithPrefix(prefix + "JpaProperties.", true));
 95  
                 
 96  
                 // Load in the defaults for a Hibernate JPA Setup. Since the JPA spec states that these properties will be ignored by
 97  
                 // vendors that do not understand them, we can add all of the necessary defaults per supported JPA vendor here.
 98  0
                 jpaProperties.put("hibernate.show_sql", determineConfigProperty(config, prefix, "JpaProperties.hibernate.show_sql", "false"));
 99  0
         jpaProperties.put("hibernate.format_sql", determineConfigProperty(config, prefix, "JpaProperties.hibernate.format_sql", "false"));
 100  0
         jpaProperties.put("hibernate.use_sql_comments", determineConfigProperty(config, prefix, "JpaProperties.hibernate.use_sql_comments", "false"));
 101  
         // Default now JTOM rather than Atomikos. Atomikos can be used by setting (KULRICE-1909)
 102  
         //   JpaProperties.hibernate.transaction.manager_lookup_class=org.kuali.rice.core.jta.AtomikosTransactionManagerLookup
 103  
         // in a configuration file for a JPA persistence unit.
 104  0
         jpaProperties.put("hibernate.transaction.manager_lookup_class", determineConfigProperty(config, prefix, "JpaProperties.hibernate.transaction.manager_lookup_class", "org.hibernate.transaction.JOTMTransactionManagerLookup"));
 105  
         //jpaProperties.put("hibernate.transaction.manager_lookup_class", determineConfigProperty(config, prefix, "JpaProperties.hibernate.transaction.manager_lookup_class", "org.kuali.rice.core.jta.AtomikosTransactionManagerLookup"));
 106  0
         jpaProperties.put("hibernate.current_session_context_class", determineConfigProperty(config, prefix, "JpaProperties.hibernate.current_session_context_class", "org.hibernate.context.JTASessionContext"));
 107  0
         jpaProperties.put("hibernate.connection.release_mode", determineConfigProperty(config, prefix, "JpaProperties.hibernate.connection.release_mode", "auto"));
 108  0
         jpaProperties.put("hibernate.transaction.flush_before_completion", determineConfigProperty(config, prefix, "JpaProperties.hibernate.transaction.flush_before_completion", "true"));
 109  0
         jpaProperties.put("hibernate.bytecode.use_reflection_optimizer", determineConfigProperty(config, prefix, "JpaProperties.hibernate.bytecode.use_reflection_optimizer", "false"));
 110  0
         jpaProperties.put("hibernate.transaction.auto_close_session", determineConfigProperty(config, prefix, "JpaProperties.hibernate.transaction.auto_close_session", "false"));
 111  0
         jpaProperties.put("hibernate.hbm2ddl.auto", determineConfigProperty(config, prefix, "JpaProperties.hibernate.hbm2ddl.auto", ""));
 112  
         
 113  
         // TODO: Add more vendor specific defaults...
 114  
         
 115  0
         return jpaProperties;
 116  
         }
 117  
 
 118  
         private String determineConfigProperty(Config config, String prefix, String key, String defaultValue) {
 119  0
                 String value = config.getProperty(prefix + key);
 120  
                 // fallback on the defaults (non-module based properties)
 121  0
                 if (value == null) {
 122  0
                         value = config.getProperty("rice.jpa." + key);
 123  
                 }
 124  
                 // fallback on the default value passed in if still no value found for key
 125  0
                 return value == null ? defaultValue : value;
 126  
         }
 127  
         
 128  
 }