public AppModule deploy(AppModule appModule) throws OpenEJBException {
// search for the cmp persistence unit
PersistenceUnit persistenceUnit = null;
for (PersistenceModule persistenceModule : appModule.getPersistenceModules()) {
Persistence persistence = persistenceModule.getPersistence();
for (PersistenceUnit unit : persistence.getPersistenceUnit()) {
if (CMP_PERSISTENCE_UNIT_NAME.equals(unit.getName())) {
persistenceUnit = unit;
break;
}
}
}
// todo scan existing persistence module for all entity mappings and don't generate mappings for them
// create mappings
EntityMappings cmpMappings = appModule.getCmpMappings();
if (cmpMappings == null) {
cmpMappings = new EntityMappings();
cmpMappings.setVersion("1.0");
appModule.setCmpMappings(cmpMappings);
}
for (EjbModule ejbModule : appModule.getEjbModules()) {
generateEntityMappings(ejbModule, cmpMappings);
}
if (!cmpMappings.getEntity().isEmpty()) {
// if not found create one
if (persistenceUnit == null) {
persistenceUnit = new PersistenceUnit();
persistenceUnit.setName(CMP_PERSISTENCE_UNIT_NAME);
persistenceUnit.setTransactionType(TransactionType.JTA);
persistenceUnit.setJtaDataSource("java:openejb/Resource/Default JDBC Database");
persistenceUnit.setNonJtaDataSource("java:openejb/Resource/Default Unmanaged JDBC Database");
// todo paramterize this
Properties properties = new Properties();
properties.setProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true, Indexes=false, IgnoreErrors=true)");
// properties.setProperty("openjpa.DataCache", "false");
// properties.setProperty("openjpa.Log", "DefaultLevel=TRACE");
persistenceUnit.setProperties(properties);
Persistence persistence = new Persistence();
persistence.setVersion("1.0");
persistence.getPersistenceUnit().add(persistenceUnit);
PersistenceModule persistenceModule = new PersistenceModule(appModule.getModuleId(), persistence);
appModule.getPersistenceModules().add(persistenceModule);
}
persistenceUnit.getMappingFile().add("META-INF/openejb-cmp-generated-orm.xml");