Search in sources :

Example 36 with ModelEntity

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

the class EntityDataLoadContainer method dropDbConstraints.

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

Example 37 with ModelEntity

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

the class EntityDataLoadContainer method createPrimaryKeys.

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

Example 38 with ModelEntity

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

the class EntityDataLoadContainer method dropPrimaryKeys.

private void dropPrimaryKeys(DatabaseUtil dbUtil, Map<String, ModelEntity> modelEntities, TreeSet<String> modelEntityNames) {
    List<String> messages = new ArrayList<String>();
    Debug.logImportant("Dropping primary keys...", module);
    for (String entityName : modelEntityNames) {
        ModelEntity modelEntity = modelEntities.get(entityName);
        if (modelEntity != null) {
            dbUtil.deletePrimaryKey(modelEntity, messages);
        }
    }
    logMessageList(messages);
}
Also used : ArrayList(java.util.ArrayList) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 39 with ModelEntity

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

the class UpgradeServices method generateMySqlFileWithAlterTableForTimestamps.

/**
 * Generate sql file for data migration from mySql.5 and earlier version to mySql.6 to later version
 * mySql added support in 5.6 to support microseconds for datetime field.
 * https://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html
 * <ul>
 * <li>Service will take [groupName] as in param,</li>
 * <li>iterate all the entity and check for datetime and time field</li>
 * <li>it will generate alter table sql statement to update the field data type</li>
 * <li>datetime will be altered with DATETIME(3)</li>
 * <li>time will be altered with TIME(3)</li>
 * <li>sql fiel will be created at following location</li>
 * <li>${ofbiz.home}/runtime/tempfiles/[groupName].sql</li>
 * </ul>
 * @param dctx
 * @param context
 * @return Map with the success result of the service,
 */
public static Map<String, Object> generateMySqlFileWithAlterTableForTimestamps(DispatchContext dctx, Map<String, Object> context) {
    Delegator delegator = dctx.getDelegator();
    Security security = dctx.getSecurity();
    Locale locale = (Locale) context.get("locale");
    // check permission
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    if (!security.hasPermission("ENTITY_MAINT", userLogin)) {
        Debug.logError(UtilProperties.getMessage(resource, "EntityExtServicePermissionNotGranted", locale), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtServicePermissionNotGranted", locale));
    }
    String groupName = (String) context.get("groupName");
    Map<String, ModelEntity> modelEntities;
    try (PrintWriter dataWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(System.getProperty("ofbiz.home") + "/runtime/tempfiles/" + groupName + ".sql")), "UTF-8")))) {
        modelEntities = delegator.getModelEntityMapByGroup(groupName);
        /* TODO:
            1) fetch the meta data of the "date-time" field using the JDBC connection and JDBC meta data;
            2) compare it to date-time and only generate the alter statement if they differs;
            */
        dataWriter.println("SET FOREIGN_KEY_CHECKS=0;");
        for (ModelEntity modelEntity : modelEntities.values()) {
            List<ModelField> fields = modelEntity.getFieldsUnmodifiable();
            for (ModelField field : fields) {
                if (modelEntity.getPlainTableName() != null) {
                    if ("date-time".equals(field.getType())) {
                        dataWriter.println("ALTER TABLE " + modelEntity.getPlainTableName() + " MODIFY " + field.getColName() + " DATETIME(3);");
                    }
                    if ("time".equals(field.getType())) {
                        dataWriter.println("ALTER TABLE " + modelEntity.getPlainTableName() + " MODIFY " + field.getColName() + " TIME(3);");
                    }
                }
            }
        }
        dataWriter.println("SET FOREIGN_KEY_CHECKS=1;");
    } catch (GenericEntityException e) {
        Debug.logError(e, "Error getting list of entities in group: " + e.toString(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtErrorGettingListOfEntityInGroup", UtilMisc.toMap("errorString", e.toString()), locale));
    } catch (FileNotFoundException | UnsupportedEncodingException e) {
        Debug.logError(e, e.getMessage(), module);
        return ServiceUtil.returnError(e.getMessage());
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Security(org.apache.ofbiz.security.Security) BufferedWriter(java.io.BufferedWriter) Delegator(org.apache.ofbiz.entity.Delegator) ModelField(org.apache.ofbiz.entity.model.ModelField) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 40 with ModelEntity

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

the class SqlJdbcUtil method makeViewWhereClause.

public static String makeViewWhereClause(ModelEntity modelEntity, String joinStyle) throws GenericEntityException {
    if (modelEntity instanceof ModelViewEntity) {
        StringBuilder whereString = new StringBuilder();
        ModelViewEntity modelViewEntity = (ModelViewEntity) modelEntity;
        if ("ansi".equals(joinStyle) || "ansi-no-parenthesis".equals(joinStyle)) {
        // nothing to do here, all done in the JOIN clauses
        } else if ("theta-oracle".equals(joinStyle) || "theta-mssql".equals(joinStyle)) {
            boolean isOracleStyle = "theta-oracle".equals(joinStyle);
            boolean isMssqlStyle = "theta-mssql".equals(joinStyle);
            for (int i = 0; i < modelViewEntity.getViewLinksSize(); i++) {
                ModelViewEntity.ModelViewLink viewLink = modelViewEntity.getViewLink(i);
                ModelEntity linkEntity = modelViewEntity.getMemberModelEntity(viewLink.getEntityAlias());
                ModelEntity relLinkEntity = modelViewEntity.getMemberModelEntity(viewLink.getRelEntityAlias());
                if (linkEntity == null) {
                    throw new GenericEntityException("Link entity not found with alias: " + viewLink.getEntityAlias() + " for entity: " + modelViewEntity.getEntityName());
                }
                if (relLinkEntity == null) {
                    throw new GenericEntityException("Rel-Link entity not found with alias: " + viewLink.getRelEntityAlias() + " for entity: " + modelViewEntity.getEntityName());
                }
                for (int j = 0; j < viewLink.getKeyMapsSize(); j++) {
                    ModelKeyMap keyMap = viewLink.getKeyMap(j);
                    ModelField linkField = linkEntity.getField(keyMap.getFieldName());
                    ModelField relLinkField = relLinkEntity.getField(keyMap.getRelFieldName());
                    if (whereString.length() > 0) {
                        whereString.append(" AND ");
                    }
                    whereString.append(viewLink.getEntityAlias());
                    whereString.append(".");
                    whereString.append(linkField.getColName());
                    // if (isOracleStyle && linkMemberEntity.getOptional()) whereString.append(" (+) ");
                    if (isMssqlStyle && viewLink.isRelOptional())
                        whereString.append("*");
                    whereString.append("=");
                    // if (isMssqlStyle && linkMemberEntity.getOptional()) whereString.append("*");
                    if (isOracleStyle && viewLink.isRelOptional())
                        whereString.append(" (+) ");
                    whereString.append(viewLink.getRelEntityAlias());
                    whereString.append(".");
                    whereString.append(relLinkField.getColName());
                }
            }
        } else {
            throw new GenericModelException("The join-style " + joinStyle + " is not supported");
        }
        if (whereString.length() > 0) {
            return "(" + whereString.toString() + ")";
        }
    }
    return "";
}
Also used : ModelKeyMap(org.apache.ofbiz.entity.model.ModelKeyMap) GenericModelException(org.apache.ofbiz.entity.GenericModelException) ModelField(org.apache.ofbiz.entity.model.ModelField) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) 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