blob: 6d663f7ceecbafd95c7f46d501d5ab91729047cb [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2010-2013 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
* which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* dclarke - EclipseLink 2.4 - MySports Demo Bug 344608
******************************************************************************/
package eclipselink.example.mysports.application.test;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.eclipse.persistence.exceptions.DatabaseException;
import org.eclipse.persistence.jpa.JpaHelper;
import org.eclipse.persistence.sessions.server.Server;
import org.eclipse.persistence.tools.schemaframework.DefaultTableGenerator;
import org.eclipse.persistence.tools.schemaframework.SchemaManager;
import org.eclipse.persistence.tools.schemaframework.TableCreator;
import eclipselink.example.mysports.admin.model.HostedLeague;
import eclipselink.example.mysports.application.MySportsConfig;
import eclipselink.example.mysports.application.services.LeagueRepository;
import eclipselink.example.mysports.application.test.admin.MockAdminServerConnector;
public class PersistenceTesting {
public static TestingLeagueRepository createLeagueRepository(String ddlGeneration) {
Map<String, Object> properties = TestingLeagueRepository.get();
//properties.put(PersistenceUnitProperties.DDL_GENERATION, ddlGeneration);
//properties.put(PersistenceUnitProperties.DDL_GENERATION_MODE, PersistenceUnitProperties.DDL_DATABASE_GENERATION);
properties.put(PersistenceUnitProperties.ALLOW_NATIVE_SQL_QUERIES, "true");
EntityManagerFactory adminEMF = Persistence.createEntityManagerFactory("MySportsAdmin", properties);
//ExampleLeagueDefinition.populateAll(adminEMF);
return createSchemaUsingAll(adminEMF);
}
public static TestingLeagueRepository createSchemaUsingAll(EntityManagerFactory adminEMF) {
EntityManager em = adminEMF.createEntityManager();
em.getTransaction().begin();
// Create default shared league for purposes of schema gen
HostedLeague allLeague = em.find(HostedLeague.class, "ALL");
if (allLeague == null) {
allLeague = new HostedLeague("ALL", "All League", "red");
allLeague.addPlayerExtension("flex1", "java.lang.String", "FLEX_1");
allLeague.addPlayerExtension("flex2", "java.lang.String", "FLEX_2");
allLeague.addPlayerExtension("flex3", "java.lang.String", "FLEX_3");
allLeague.addPlayerExtension("flex4", "java.lang.String", "FLEX_4");
allLeague.addPlayerExtension("flex5", "java.lang.String", "FLEX_5");
allLeague.addTeamExtension("flex1", "java.lang.String", "FLEX_1");
allLeague.addTeamExtension("flex2", "java.lang.String", "FLEX_2");
allLeague.addTeamExtension("flex3", "java.lang.String", "FLEX_3");
allLeague.addTeamExtension("flex4", "java.lang.String", "FLEX_4");
allLeague.addTeamExtension("flex5", "java.lang.String", "FLEX_5");
allLeague.addDivisionExtension("flex1", "java.lang.String", "FLEX_1");
allLeague.addDivisionExtension("flex2", "java.lang.String", "FLEX_2");
allLeague.addDivisionExtension("flex3", "java.lang.String", "FLEX_3");
allLeague.addDivisionExtension("flex4", "java.lang.String", "FLEX_4");
allLeague.addDivisionExtension("flex5", "java.lang.String", "FLEX_5");
em.persist(allLeague);
}
em.getTransaction().commit();
MySportsConfig config = new MySportsConfig();
TestingLeagueRepository repository = new TestingLeagueRepository(config);
((MockAdminServerConnector) config.getAdminConnector()).setEMF(adminEMF);
createSharedMySportsSchema(repository);
return repository;
}
public static void closeRepository(TestingLeagueRepository repository, boolean dropAdminTables) {
MySportsConfig config = repository.getConfig();
EntityManagerFactory adminEMF = ((MockAdminServerConnector) config.getAdminConnector()).getEMF();
if (dropAdminTables) {
dropTables(adminEMF);
} else {
EntityManager em = repository.unwrap(EntityManagerFactory.class).createEntityManager();
try {
em.getTransaction().begin();
em.createNativeQuery("DELETE from MYS_PLAYER").executeUpdate();
em.createNativeQuery("DELETE from MYS_TEAM").executeUpdate();
em.createNativeQuery("DELETE from MYS_DIV").executeUpdate();
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
}
adminEMF.close();
repository.close();
}
public static void dropTables(EntityManagerFactory emf) {
Server session = JpaHelper.getServerSession(emf);
SchemaManager sm = new SchemaManager(session);
TableCreator tc = new DefaultTableGenerator(session.getProject(), true).generateDefaultTableCreator();
tc.setIgnoreDatabaseException(true);
tc.dropTables(session, sm, true);
}
public static void createSharedMySportsSchema(LeagueRepository repository) {
Map<String, Object> properties = TestingLeagueRepository.get("ALL");
properties.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.CREATE_ONLY);
properties.put(PersistenceUnitProperties.DDL_GENERATION_MODE, PersistenceUnitProperties.DDL_DATABASE_GENERATION);
properties.put(PersistenceUnitProperties.ALLOW_NATIVE_SQL_QUERIES, "true");
repository.setLeagueId("ALL", properties);
}
public static void dropSharedMySportsSchema(LeagueRepository repository) {
Server session = repository.unwrap(Server.class);
SchemaManager sm = new SchemaManager(session);
TableCreator tc = new DefaultTableGenerator(session.getProject(), true).generateDefaultTableCreator();
tc.setIgnoreDatabaseException(true);
tc.dropTables(session, sm, true);
}
public static void dropLeagueTables(LeagueRepository repository, String leagueId) {
Map<String, Object> properties = TestingLeagueRepository.get(leagueId);
properties.put(PersistenceUnitProperties.ALLOW_NATIVE_SQL_QUERIES, "true");
repository.setLeagueId(leagueId, properties);
Server session = repository.unwrap(Server.class);
try {
session.executeNonSelectingSQL("DROP TABLE MYS_PLAYER_" + leagueId);
} catch (DatabaseException dbe) {
}
try {
session.executeNonSelectingSQL("DROP TABLE MYS_TEAM_" + leagueId);
} catch (DatabaseException dbe) {
}
try {
session.executeNonSelectingSQL("DROP TABLE MYS_DIV_" + leagueId);
} catch (DatabaseException dbe) {
}
}
public static void createLeagueTables(LeagueRepository repository, String leagueId) {
Map<String, Object> properties = TestingLeagueRepository.get(leagueId);
properties.put(PersistenceUnitProperties.ALLOW_NATIVE_SQL_QUERIES, "true");
repository.setLeagueId(leagueId, properties);
Server session = repository.unwrap(Server.class);
session.executeNonSelectingSQL("CREATE TABLE MYS_DIV_" + leagueId + " (ID INTEGER NOT NULL, DEF_DIV INTEGER default 0, NAME VARCHAR(255), FLEX_1 VARCHAR(255), FLEX_2 VARCHAR(255), FLEX_3 VARCHAR(255), FLEX_4 VARCHAR(255), FLEX_5 VARCHAR(255), VERSION INTEGER, PRIMARY KEY (ID))");
session.executeNonSelectingSQL("CREATE TABLE MYS_PLAYER_" + leagueId + " (ID INTEGER NOT NULL, EMAIL VARCHAR(255), F_NAME VARCHAR(255), L_NAME VARCHAR(255), NUM INTEGER, FLEX_1 VARCHAR(255), FLEX_2 VARCHAR(255), FLEX_3 VARCHAR(255), FLEX_4 VARCHAR(255), FLEX_5 VARCHAR(255), USER_ID VARCHAR(255), VERSION INTEGER, TEAM_ID INTEGER, PRIMARY KEY (ID))");
session.executeNonSelectingSQL("CREATE TABLE MYS_TEAM_" + leagueId + " (ID INTEGER NOT NULL, NAME VARCHAR(255), FLEX_1 VARCHAR(255), FLEX_2 VARCHAR(255), FLEX_3 VARCHAR(255), FLEX_4 VARCHAR(255), FLEX_5 VARCHAR(255), VERSION INTEGER, DIVISION_ID INTEGER, PRIMARY KEY (ID))");
}
}