Search in sources :

Example 31 with ModelEntity

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);
            }
        }
    }
}
Also used : ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) ModelRelation(org.apache.ofbiz.entity.model.ModelRelation) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 32 with ModelEntity

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;
}
Also used : TreeSet(java.util.TreeSet) ExecutorService(java.util.concurrent.ExecutorService) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) HashMap(java.util.HashMap) Map(java.util.Map) ModelKeyMap(org.apache.ofbiz.entity.model.ModelKeyMap) LinkedList(java.util.LinkedList)

Example 33 with ModelEntity

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);
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) Test(org.junit.Test)

Example 34 with ModelEntity

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;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) LinkedList(java.util.LinkedList)

Example 35 with ModelEntity

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;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) HashSet(java.util.HashSet)

Aggregations

ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)102 GenericValue (org.apache.ofbiz.entity.GenericValue)37 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)29 ModelField (org.apache.ofbiz.entity.model.ModelField)28 HashMap (java.util.HashMap)22 Delegator (org.apache.ofbiz.entity.Delegator)17 ModelViewEntity (org.apache.ofbiz.entity.model.ModelViewEntity)16 LinkedList (java.util.LinkedList)14 Locale (java.util.Locale)12 ModelKeyMap (org.apache.ofbiz.entity.model.ModelKeyMap)11 ArrayList (java.util.ArrayList)10 ModelRelation (org.apache.ofbiz.entity.model.ModelRelation)10 IOException (java.io.IOException)8 TreeSet (java.util.TreeSet)8 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)8 Map (java.util.Map)7 GeneralRuntimeException (org.apache.ofbiz.base.util.GeneralRuntimeException)7 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)7 ModelFieldType (org.apache.ofbiz.entity.model.ModelFieldType)7 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)7