Search in sources :

Example 61 with ModelEntity

use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.

the class EntityDataLoadContainer method loadDataForDelegator.

private void loadDataForDelegator(Map<String, String> loadDataProps, Configuration configuration, Property delegatorNameProp, String overrideDelegator) throws ContainerException {
    // prepare command line properties passed by user
    boolean createPks = isPropertySet(loadDataProps, CREATE_P_KEYS);
    boolean dropPks = isPropertySet(loadDataProps, DROP_P_KEYS);
    boolean createConstraints = isPropertySet(loadDataProps, CREATE_CONSTRAINTS);
    boolean dropConstraints = isPropertySet(loadDataProps, DROP_CONSTRAINTS);
    boolean repairColumns = isPropertySet(loadDataProps, REPAIR_COLUMNS);
    String entityGroup = getEntityGroupNameFromConfig(configuration, loadDataProps.get(DATA_GROUP));
    // prepare objects needed for the data loading logic
    Delegator delegator = getDelegator(delegatorNameProp, overrideDelegator);
    Delegator baseDelegator = getBaseDelegator(delegator);
    GenericHelperInfo helperInfo = getHelperInfo(delegator, entityGroup);
    DatabaseUtil dbUtil = new DatabaseUtil(helperInfo);
    Map<String, ModelEntity> modelEntities = getModelEntities(delegator, entityGroup);
    TreeSet<String> modelEntityNames = new TreeSet<String>(modelEntities.keySet());
    Collection<ComponentConfig> allComponents = ComponentConfig.getAllComponents();
    // data loading logic starts here
    createOrUpdateComponentEntities(baseDelegator, allComponents);
    if (dropConstraints) {
        dropDbConstraints(dbUtil, modelEntities, modelEntityNames);
    }
    if (dropPks) {
        dropPrimaryKeys(dbUtil, modelEntities, modelEntityNames);
    }
    if (repairColumns) {
        repairDbColumns(dbUtil, modelEntities);
    }
    loadData(delegator, baseDelegator, allComponents, helperInfo, loadDataProps);
    if (createPks) {
        createPrimaryKeys(dbUtil, modelEntities, modelEntityNames);
    }
    if (createConstraints) {
        createDbConstraints(dbUtil, modelEntities, modelEntityNames);
    }
}
Also used : Delegator(org.apache.ofbiz.entity.Delegator) TreeSet(java.util.TreeSet) ComponentConfig(org.apache.ofbiz.base.component.ComponentConfig) GenericHelperInfo(org.apache.ofbiz.entity.datasource.GenericHelperInfo) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) DatabaseUtil(org.apache.ofbiz.entity.jdbc.DatabaseUtil)

Example 62 with ModelEntity

use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.

the class EntityDataLoadContainer method createDbConstraints.

private void createDbConstraints(DatabaseUtil dbUtil, Map<String, ModelEntity> modelEntities, TreeSet<String> modelEntityNames) {
    List<String> messages = new ArrayList<String>();
    Debug.logImportant("Creating foreign keys...", module);
    for (String entityName : modelEntityNames) {
        ModelEntity modelEntity = modelEntities.get(entityName);
        if (modelEntity != null) {
            dbUtil.createForeignKeys(modelEntity, modelEntities, messages);
        }
    }
    Debug.logImportant("Creating foreign key indcies...", module);
    for (String entityName : modelEntityNames) {
        ModelEntity modelEntity = modelEntities.get(entityName);
        if (modelEntity != null) {
            dbUtil.createForeignKeyIndices(modelEntity, messages);
        }
    }
    Debug.logImportant("Creating declared indices...", module);
    for (String entityName : modelEntityNames) {
        ModelEntity modelEntity = modelEntities.get(entityName);
        if (modelEntity != null) {
            dbUtil.createDeclaredIndices(modelEntity, messages);
        }
    }
    logMessageList(messages);
}
Also used : ArrayList(java.util.ArrayList) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 63 with ModelEntity

use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.

the class EntityDataAssert method checkSingleValue.

public static void checkSingleValue(GenericValue checkValue, Delegator delegator, List<Object> errorMessages) throws GenericEntityException {
    if (checkValue == null) {
        errorMessages.add("Got a value to check was null");
        return;
    }
    // to check get the PK, find by that, compare all fields
    GenericPK checkPK = null;
    try {
        checkPK = checkValue.getPrimaryKey();
        GenericValue currentValue = EntityQuery.use(delegator).from(checkPK.getEntityName()).where(checkPK).queryOne();
        if (currentValue == null) {
            errorMessages.add("Entity [" + checkPK.getEntityName() + "] record not found for pk: " + checkPK);
            return;
        }
        ModelEntity modelEntity = checkValue.getModelEntity();
        for (String nonpkFieldName : modelEntity.getNoPkFieldNames()) {
            // skip the fields the entity engine maintains
            if (ModelEntity.CREATE_STAMP_FIELD.equals(nonpkFieldName) || ModelEntity.CREATE_STAMP_TX_FIELD.equals(nonpkFieldName) || ModelEntity.STAMP_FIELD.equals(nonpkFieldName) || ModelEntity.STAMP_TX_FIELD.equals(nonpkFieldName)) {
                continue;
            }
            Object checkField = checkValue.get(nonpkFieldName);
            Object currentField = currentValue.get(nonpkFieldName);
            if (checkField != null && !checkField.equals(currentField)) {
                errorMessages.add("Field [" + modelEntity.getEntityName() + "." + nonpkFieldName + "] did not match; file value [" + checkField + "], db value [" + currentField + "] pk [" + checkPK + "]");
            }
        }
    } catch (GenericEntityException e) {
        throw e;
    } catch (Throwable t) {
        String errMsg;
        if (checkPK == null) {
            errMsg = "Error checking value [" + checkValue + "]: " + t.toString();
        } else {
            errMsg = "Error checking entity [" + checkPK.getEntityName() + "] with pk [" + checkPK.getAllFields() + "]: " + t.toString();
        }
        errorMessages.add(errMsg);
        Debug.logError(t, errMsg, module);
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericPK(org.apache.ofbiz.entity.GenericPK) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 64 with ModelEntity

use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.

the class PrimaryKeyFinder method runFind.

@Override
public void runFind(Map<String, Object> context, Delegator delegator) throws GeneralException {
    String entityName = this.entityNameExdr.expandString(context);
    String useCacheString = this.useCacheStrExdr.expandString(context);
    // default to false
    boolean useCacheBool = "true".equals(useCacheString);
    String autoFieldMapString = this.autoFieldMapExdr.expandString(context);
    // default to true
    boolean autoFieldMapBool = !"false".equals(autoFieldMapString);
    ModelEntity modelEntity = delegator.getModelEntity(entityName);
    if (modelEntity == null) {
        throw new IllegalArgumentException("No entity definition found for entity name [" + entityName + "]");
    }
    GenericValue valueOut = runFind(modelEntity, context, delegator, useCacheBool, autoFieldMapBool, this.fieldMap, this.selectFieldExpanderList);
    if (!valueNameAcsr.isEmpty()) {
        this.valueNameAcsr.put(context, valueOut);
    } else {
        if (valueOut != null) {
            context.putAll(valueOut);
        }
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 65 with ModelEntity

use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.

the class UtilHelpText method getEntityFieldDescription.

/**
 * Find the help text associated with an entity field.
 *
 * @param entityName the entity name
 * @param fieldName the field name
 * @param delegator the delegator
 * @param locale the locale
 * @return the help text, or the resource propertyName if no help text exists
 */
public static String getEntityFieldDescription(final String entityName, final String fieldName, final Delegator delegator, final Locale locale) {
    if (UtilValidate.isEmpty(entityName)) {
        // Debug.logWarning("entityName [" + entityName + "] is empty", module);
        return "";
    }
    if (UtilValidate.isEmpty(fieldName)) {
        Debug.logWarning("fieldName [" + fieldName + "] is empty", module);
        return "";
    }
    ModelReader reader = delegator.getModelReader();
    ModelEntity entity = null;
    try {
        if (!reader.getEntityNames().contains(entityName)) {
            Debug.logWarning("couldn't find entityName [" + entityName + "]", module);
            return "";
        }
        entity = reader.getModelEntity(entityName);
    } catch (GenericEntityException e) {
        Debug.logError(e, "Error getting help text for entity=" + entityName + " field " + fieldName, module);
        return "";
    }
    String entityResourceName = entity.getDefaultResourceName();
    String messageId = "FieldDescription." + entityName + "." + fieldName;
    String fieldDescription = UtilProperties.getMessage(entityResourceName, messageId, locale);
    if (fieldDescription.equals(messageId)) {
        messageId = "FieldDescription." + fieldName;
        if (Debug.verboseOn()) {
            Debug.logVerbose("No help text found in [" + entityResourceName + "] with key [" + messageId + "], Trying with: " + messageId, module);
        }
        fieldDescription = UtilProperties.getMessage(entityResourceName, messageId, locale);
        if (fieldDescription.equals(messageId)) {
            if (Debug.verboseOn()) {
                Debug.logVerbose("No help text found in [" + entityResourceName + "] with key [" + messageId + "]", module);
            }
            return "";
        }
    }
    return fieldDescription;
}
Also used : ModelReader(org.apache.ofbiz.entity.model.ModelReader) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

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