use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class DatabaseUtil method deleteForeignKeys.
public void deleteForeignKeys(ModelEntity entity, Map<String, ModelEntity> modelEntities, int constraintNameClipLength, List<String> messages) {
if (entity == null) {
String errMsg = "ModelEntity was null and is required to delete foreign keys for a table";
if (messages != null)
messages.add(errMsg);
Debug.logError(errMsg, module);
return;
}
if (entity instanceof ModelViewEntity) {
// Debug.logError(errMsg, module);
return;
}
String message = "Deleting foreign keys for entity [" + entity.getEntityName() + "]";
Debug.logImportant(message, module);
if (messages != null)
messages.add(message);
// go through the relationships to see if any foreign keys need to be added
Iterator<ModelRelation> relationsIter = entity.getRelationsIterator();
while (relationsIter.hasNext()) {
ModelRelation modelRelation = relationsIter.next();
if ("one".equals(modelRelation.getType())) {
ModelEntity relModelEntity = modelEntities.get(modelRelation.getRelEntityName());
if (relModelEntity == null) {
String errMsg = "Error removing foreign key: ModelEntity was null for related entity name " + modelRelation.getRelEntityName();
if (messages != null)
messages.add(errMsg);
Debug.logError(errMsg, module);
continue;
}
if (relModelEntity instanceof ModelViewEntity) {
String errMsg = "Error removing foreign key: related entity is a view entity for related entity name " + modelRelation.getRelEntityName();
if (messages != null)
messages.add(errMsg);
Debug.logError(errMsg, module);
continue;
}
String retMsg = deleteForeignKey(entity, modelRelation, relModelEntity, constraintNameClipLength);
if (UtilValidate.isNotEmpty(retMsg)) {
if (messages != null)
messages.add(retMsg);
Debug.logError(retMsg, module);
}
}
}
}
use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class DatabaseUtil method induceModelFromDb.
/**
* Creates a list of ModelEntity objects based on meta data from the database
*/
public List<ModelEntity> induceModelFromDb(Collection<String> messages) {
ExecutorService executor = Executors.newFixedThreadPool(datasourceInfo.getMaxWorkerPoolSize());
// get ALL tables from this database
TreeSet<String> tableNames = this.getTableNames(messages);
// get ALL column info, put into hashmap by table name
Map<String, Map<String, ColumnCheckInfo>> colInfo = getColumnInfo(tableNames, true, messages, executor);
// go through each table and make a ModelEntity object, add to list
// for each entity make corresponding ModelField objects
// then print out XML for the entities/fields
List<ModelEntity> newEntList = new LinkedList<ModelEntity>();
boolean isCaseSensitive = getIsCaseSensitive(messages);
// iterate over the table names is alphabetical order
for (String tableName : new TreeSet<String>(colInfo.keySet())) {
Map<String, ColumnCheckInfo> colMap = colInfo.get(tableName);
ModelEntity newEntity = new ModelEntity(tableName, colMap, modelFieldTypeReader, isCaseSensitive);
newEntList.add(newEntity);
}
executor.shutdown();
return newEntList;
}
use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class EntitySaxReaderTests method parse.
@Test
public void parse() throws Exception {
Delegator delegator = mock(Delegator.class);
Delegator clonedDelegator = mock(Delegator.class);
GenericValue genericValue = mock(GenericValue.class);
ModelEntity modelEntity = mock(ModelEntity.class);
when(delegator.cloneDelegator()).thenReturn(clonedDelegator);
when(clonedDelegator.makeValue("EntityName")).thenReturn(genericValue);
when(genericValue.getModelEntity()).thenReturn(modelEntity);
when(genericValue.containsPrimaryKey()).thenReturn(true);
when(modelEntity.isField("fieldName")).thenReturn(true);
EntitySaxReader esr = new EntitySaxReader(delegator);
String input = "<entity-engine-xml><EntityName fieldName=\"field value\"/></entity-engine-xml>";
long recordsProcessed = esr.parse(input);
verify(clonedDelegator).makeValue("EntityName");
assertEquals(1, recordsProcessed);
}
use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class EntityGroupUtil method getModelEntitiesFromRecords.
public static List<ModelEntity> getModelEntitiesFromRecords(List<GenericValue> entityGroupEntryValues, Delegator delegator, boolean requireStampFields) throws GenericEntityException {
List<ModelEntity> entityModelToUseList = new LinkedList<ModelEntity>();
for (String entityName : delegator.getModelReader().getEntityNames()) {
ModelEntity modelEntity = delegator.getModelEntity(entityName);
// if view-entity, throw it out
if (modelEntity instanceof ModelViewEntity) {
continue;
}
// if it doesn't have either or both of the two update stamp fields, throw it out
if (requireStampFields && (!modelEntity.isField(ModelEntity.STAMP_FIELD) || !modelEntity.isField(ModelEntity.STAMP_TX_FIELD))) {
continue;
}
// if there are no includes records, always include; otherwise check each one to make sure at least one matches
if (entityGroupEntryValues.size() == 0) {
entityModelToUseList.add(modelEntity);
} else {
// we have different types of include applications: ESIA_INCLUDE, ESIA_EXCLUDE, ESIA_ALWAYS
// if we find an always we can break right there because this will always be include regardless of excludes, etc
// if we find an include or exclude we have to finish going through the rest of them just in case there is something that overrides it (ie an exclude for an include or an always for an exclude)
boolean matchesInclude = false;
boolean matchesExclude = false;
boolean matchesAlways = false;
Iterator<GenericValue> entitySyncIncludeIter = entityGroupEntryValues.iterator();
while (entitySyncIncludeIter.hasNext()) {
GenericValue entitySyncInclude = entitySyncIncludeIter.next();
String entityOrPackage = entitySyncInclude.getString("entityOrPackage");
boolean matches = false;
if (entityName.equals(entityOrPackage)) {
matches = true;
} else if (modelEntity.getPackageName().startsWith(entityOrPackage)) {
matches = true;
}
if (matches) {
if ("ESIA_INCLUDE".equals(entitySyncInclude.getString("applEnumId"))) {
matchesInclude = true;
} else if ("ESIA_EXCLUDE".equals(entitySyncInclude.getString("applEnumId"))) {
matchesExclude = true;
} else if ("ESIA_ALWAYS".equals(entitySyncInclude.getString("applEnumId"))) {
matchesAlways = true;
break;
}
}
}
if (matchesAlways || (matchesInclude && !matchesExclude)) {
// make sure this log message is not checked in uncommented:
// Debug.logInfo("In runEntitySync adding [" + modelEntity.getEntityName() + "] to list of Entities to sync", module);
entityModelToUseList.add(modelEntity);
}
}
}
return entityModelToUseList;
}
use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class EntityGroupUtil method getEntityNamesByGroup.
public static Set<String> getEntityNamesByGroup(String entityGroupId, Delegator delegator, boolean requireStampFields) throws GenericEntityException {
Set<String> entityNames = new HashSet<String>();
List<GenericValue> entitySyncGroupIncludes = EntityQuery.use(delegator).from("EntityGroupEntry").where("entityGroupId", entityGroupId).queryList();
List<ModelEntity> modelEntities = getModelEntitiesFromRecords(entitySyncGroupIncludes, delegator, requireStampFields);
for (ModelEntity modelEntity : modelEntities) {
entityNames.add(modelEntity.getEntityName());
}
return entityNames;
}
Aggregations