/**
* This method builds a JPA datamart structure.
* @return DataMartModelStructure
*/
public IModelStructure build() {
ModelStructure modelStructure;
String modelName;
Metamodel jpaMetamodel;
Set<EntityType<?>> jpaEntities;
logger.debug("IN");
try {
modelStructure = new ModelStructure();
modelName = getDataSource().getConfiguration().getModelName();
Assert.assertNotNull(getDataSource(), "datasource cannot be null");
setEntityManager(getDataSource().getEntityManager());
Assert.assertNotNull(getEntityManager(), "Impossible to find the jar file associated to datamart named: [" + modelName + "]");
propertiesInitializer.addProperties(modelStructure);
Map calculatedFields = getDataSource().getConfiguration().loadCalculatedFields();
modelStructure.setCalculatedFields(calculatedFields);
jpaMetamodel = getEntityManager().getMetamodel();
jpaEntities = jpaMetamodel.getEntities();
logger.debug("Jpa metamodel contains ["+ jpaEntities.size() + "] entity types");
for(EntityType<?> entityType: jpaEntities) {
logger.debug("Adding entity type [" + entityType + "] to model structure");
String entityTypeName = entityType.getJavaType().getName();
addEntity(modelStructure, modelName, entityTypeName);
logger.info("Entity type [" + entityType + "] succesfully added to model structure");
}
/*
* 1) Load Views definitions and adds to the model structure (with outbound relations from Business Views)
*/
List<ModelViewEntity> addedViewsEntities = new ArrayList<ModelViewEntity>();
List<IModelViewEntityDescriptor> list = getDataSource().getConfiguration().loadViews();
if(list.size() > 0) {
for (int i=0; i<list.size(); i++){
IModelViewEntityDescriptor viewDescriptor = list.get(i);
ModelViewEntity viewEntity = new ModelViewEntity(viewDescriptor, modelName, modelStructure, null);
addedViewsEntities.add(viewEntity);
propertiesInitializer.addProperties(viewEntity);
modelStructure.addRootEntity(modelName, viewEntity);
}
}
/*
* 2) Re-scan model structure to add nodes referencing view (inbound relations to Business Views)
*/
//visit all entities
List<IModelEntity> allEntities = visitModelStructure(modelStructure,modelName);
for (int i=0; i<list.size(); i++){
IModelViewEntityDescriptor viewDescriptor = list.get(i);
List<IModelViewRelationshipDescriptor> viewRelationshipsDescriptors = viewDescriptor.getRelationshipDescriptors();
for (IModelViewRelationshipDescriptor viewRelationshipDescriptor : viewRelationshipsDescriptors){
if (!viewRelationshipDescriptor.isOutbound()){
String sourceEntityUniqueName = viewRelationshipDescriptor.getSourceEntityUniqueName();
IModelEntity entity = modelStructure.getEntity(sourceEntityUniqueName);
logger.debug("Source Entity Unique name: "+entity.getUniqueName());
//Add node for first level entities (using UniqueName)
ModelViewEntity viewEntity = new ModelViewEntity(viewDescriptor, modelName, modelStructure, entity);
propertiesInitializer.addProperties(viewEntity);