Search in sources :

Example 16 with ModelViewEntity

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

the class DatabaseUtil method updateCharacterSetAndCollation.

/* ====================================================================== */
/* ====================================================================== */
public void updateCharacterSetAndCollation(ModelEntity entity, List<String> messages) {
    if (entity instanceof ModelViewEntity) {
        return;
    }
    if (UtilValidate.isEmpty(this.datasourceInfo.getCharacterSet()) && UtilValidate.isEmpty(this.datasourceInfo.getCollate())) {
        messages.add("Not setting character-set and collate for entity [" + entity.getEntityName() + "], options not specified in the datasource definition in the entityengine.xml file.");
        return;
    }
    try (Connection connection = getConnectionLogged(messages)) {
        if (connection == null) {
            return;
        }
        StringBuilder sqlTableBuf = new StringBuilder("ALTER TABLE ");
        sqlTableBuf.append(entity.getTableName(this.datasourceInfo));
        // if there is a characterSet, add the CHARACTER SET arg here
        if (UtilValidate.isNotEmpty(this.datasourceInfo.getCharacterSet())) {
            sqlTableBuf.append(" DEFAULT CHARACTER SET ");
            sqlTableBuf.append(this.datasourceInfo.getCharacterSet());
        }
        // if there is a collate, add the COLLATE arg here
        if (UtilValidate.isNotEmpty(this.datasourceInfo.getCollate())) {
            sqlTableBuf.append(" COLLATE ");
            sqlTableBuf.append(this.datasourceInfo.getCollate());
        }
        if (Debug.verboseOn())
            Debug.logVerbose("[updateCharacterSetAndCollation] character-set and collate sql=" + sqlTableBuf, module);
        try (Statement stmt = connection.createStatement()) {
            stmt.executeUpdate(sqlTableBuf.toString());
        } catch (SQLException e) {
            String errMsg = "SQL Exception while executing the following:\n" + sqlTableBuf + "\nError was: " + e.toString();
            messages.add(errMsg);
            Debug.logError(errMsg, module);
        }
        Iterator<ModelField> fieldIter = entity.getFieldsIterator();
        while (fieldIter.hasNext()) {
            ModelField field = fieldIter.next();
            ModelFieldType type = modelFieldTypeReader.getModelFieldType(field.getType());
            if (type == null) {
                messages.add("Field type [" + type + "] not found for field [" + field.getName() + "] of entity [" + entity.getEntityName() + "], not creating table.");
                continue;
            }
            if (!"String".equals(type.getJavaType()) && !"java.lang.String".equals(type.getJavaType())) {
                continue;
            }
            StringBuilder sqlBuf = new StringBuilder("ALTER TABLE ");
            sqlBuf.append(entity.getTableName(this.datasourceInfo));
            sqlBuf.append(" MODIFY COLUMN ");
            sqlBuf.append(field.getColName());
            sqlBuf.append(" ");
            sqlBuf.append(type.getSqlType());
            // if there is a characterSet, add the CHARACTER SET arg here
            if (UtilValidate.isNotEmpty(this.datasourceInfo.getCharacterSet())) {
                sqlBuf.append(" CHARACTER SET ");
                sqlBuf.append(this.datasourceInfo.getCharacterSet());
            }
            // if there is a collate, add the COLLATE arg here
            if (UtilValidate.isNotEmpty(this.datasourceInfo.getCollate())) {
                sqlBuf.append(" COLLATE ");
                sqlBuf.append(this.datasourceInfo.getCollate());
            }
            if (field.getIsPk() || field.getIsNotNull()) {
                if (this.datasourceInfo.getAlwaysUseConstraintKeyword()) {
                    sqlBuf.append(" CONSTRAINT NOT NULL");
                } else {
                    sqlBuf.append(" NOT NULL");
                }
            }
            if (Debug.verboseOn())
                Debug.logVerbose("[updateCharacterSetAndCollation] character-set and collate sql=" + sqlBuf, module);
            try (Statement stmt = connection.createStatement()) {
                stmt.executeUpdate(sqlBuf.toString());
            } catch (SQLException e) {
                String errMsg = "SQL Exception while executing the following:\n" + sqlBuf + "\nError was: " + e.toString();
                messages.add(errMsg);
                Debug.logError(errMsg, module);
            }
        }
    } catch (SQLException e) {
        Debug.logError(e, module);
    }
}
Also used : ModelField(org.apache.ofbiz.entity.model.ModelField) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) ModelFieldType(org.apache.ofbiz.entity.model.ModelFieldType) Connection(java.sql.Connection)

Example 17 with ModelViewEntity

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

the class DatabaseUtil method createPrimaryKey.

public String createPrimaryKey(ModelEntity entity, boolean usePkConstraintNames, int constraintNameClipLength) {
    if (entity == null) {
        return "ModelEntity was null and is required to create the primary key for a table";
    }
    if (entity instanceof ModelViewEntity) {
        return "Ignoring view entity [" + entity.getEntityName() + "]";
    }
    String message;
    if (entity.getPksSize() > 0) {
        message = "Creating primary key for entity [" + entity.getEntityName() + "]";
        // now add constraint clause
        StringBuilder sqlBuf = new StringBuilder("ALTER TABLE ");
        sqlBuf.append(entity.getTableName(datasourceInfo));
        sqlBuf.append(" ADD ");
        String pkName = makePkConstraintName(entity, constraintNameClipLength);
        if (usePkConstraintNames) {
            sqlBuf.append("CONSTRAINT ");
            sqlBuf.append(pkName);
        }
        sqlBuf.append(" PRIMARY KEY (");
        entity.colNameString(entity.getPkFieldsUnmodifiable(), sqlBuf, "");
        sqlBuf.append(")");
        if (Debug.verboseOn())
            Debug.logVerbose("[createPrimaryKey] sql=" + sqlBuf.toString(), module);
        try (Connection connection = getConnection();
            Statement stmt = connection.createStatement()) {
            stmt.executeUpdate(sqlBuf.toString());
        } catch (SQLException e) {
            return "SQL Exception while executing the following:\n" + sqlBuf.toString() + "\nError was: " + e.toString();
        } catch (GenericEntityException e) {
            return "Unable to establish a connection with the database for helperName [" + this.helperInfo.getHelperFullName() + "]... Error was: " + e.toString();
        }
    } else {
        message = "No primary-key defined for table [" + entity.getEntityName() + "]";
    }
    Debug.logImportant(message, module);
    return message;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) Connection(java.sql.Connection)

Example 18 with ModelViewEntity

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

the class DatabaseUtil method deletePrimaryKey.

public String deletePrimaryKey(ModelEntity entity, boolean usePkConstraintNames, int constraintNameClipLength) {
    if (entity == null) {
        return "ModelEntity was null and is required to delete the primary key for a table";
    }
    if (entity instanceof ModelViewEntity) {
        return "Ignoring view entity [" + entity.getEntityName() + "]";
    }
    String message;
    if (entity.getPksSize() > 0) {
        message = "Deleting primary key for entity [" + entity.getEntityName() + "]";
        // now add constraint clause
        StringBuilder sqlBuf = new StringBuilder("ALTER TABLE ");
        sqlBuf.append(entity.getTableName(datasourceInfo));
        sqlBuf.append(" DROP ");
        String pkName = makePkConstraintName(entity, constraintNameClipLength);
        if (usePkConstraintNames) {
            sqlBuf.append("CONSTRAINT ");
            sqlBuf.append(pkName);
            sqlBuf.append(" CASCADE");
        } else {
            sqlBuf.append(" PRIMARY KEY");
        // DEJ20050502 not sure why this is here, shouldn't be needed and some dbs don't support like this, ie when used with PRIMARY KEY: sqlBuf.append(" CASCADE");
        }
        if (Debug.verboseOn())
            Debug.logVerbose("[deletePrimaryKey] sql=" + sqlBuf.toString(), module);
        try (Connection connection = getConnection();
            Statement stmt = connection.createStatement()) {
            stmt.executeUpdate(sqlBuf.toString());
        } catch (SQLException e) {
            String errMsg = "SQL Exception while executing the following:\n" + sqlBuf.toString() + "\nError was: " + e.toString();
            Debug.logError(e, errMsg, module);
            return errMsg;
        } catch (GenericEntityException e) {
            String errMsg = "Unable to establish a connection with the database for helperName [" + this.helperInfo.getHelperFullName() + "]... Error was: " + e.toString();
            Debug.logError(e, errMsg, module);
            return errMsg;
        }
    } else {
        message = "No primary-key defined for table [" + entity.getEntityName() + "]";
    }
    Debug.logImportant(message, module);
    return message;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) Connection(java.sql.Connection)

Example 19 with ModelViewEntity

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

the class DatabaseUtil method createDeclaredIndices.

/* ====================================================================== */
/* ====================================================================== */
public int createDeclaredIndices(ModelEntity entity, List<String> messages) {
    if (entity == null) {
        String message = "ModelEntity was null and is required to create declared indices for a table";
        Debug.logError(message, module);
        if (messages != null)
            messages.add(message);
        return 0;
    }
    if (entity instanceof ModelViewEntity) {
        String message = "Cannot create declared indices for a view entity";
        Debug.logWarning(message, module);
        if (messages != null)
            messages.add(message);
        return 0;
    }
    int dinsCreated = 0;
    // go through the indexes to see if any need to be added
    Iterator<ModelIndex> indexesIter = entity.getIndexesIterator();
    while (indexesIter.hasNext()) {
        ModelIndex modelIndex = indexesIter.next();
        String retMsg = createDeclaredIndex(entity, modelIndex);
        if (UtilValidate.isNotEmpty(retMsg)) {
            String message = "Could not create declared indices for entity [" + entity.getEntityName() + "]: " + retMsg;
            Debug.logError(message, module);
            if (messages != null)
                messages.add(message);
            continue;
        }
        dinsCreated++;
    }
    if (dinsCreated > 0) {
        String message = "Created " + dinsCreated + " declared indices for entity [" + entity.getEntityName() + "]";
        Debug.logImportant(message, module);
        if (messages != null)
            messages.add(message);
    }
    return dinsCreated;
}
Also used : ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) ModelIndex(org.apache.ofbiz.entity.model.ModelIndex)

Example 20 with ModelViewEntity

use of org.apache.ofbiz.entity.model.ModelViewEntity 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)

Aggregations

ModelViewEntity (org.apache.ofbiz.entity.model.ModelViewEntity)37 ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)16 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)14 LinkedList (java.util.LinkedList)13 ModelField (org.apache.ofbiz.entity.model.ModelField)13 SQLException (java.sql.SQLException)11 Connection (java.sql.Connection)7 Statement (java.sql.Statement)7 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)7 GenericModelException (org.apache.ofbiz.entity.GenericModelException)6 ModelFieldType (org.apache.ofbiz.entity.model.ModelFieldType)6 ModelKeyMap (org.apache.ofbiz.entity.model.ModelKeyMap)6 ModelRelation (org.apache.ofbiz.entity.model.ModelRelation)6 GenericNotImplementedException (org.apache.ofbiz.entity.GenericNotImplementedException)5 HashMap (java.util.HashMap)4 TreeSet (java.util.TreeSet)4 GenericValue (org.apache.ofbiz.entity.GenericValue)4 IOException (java.io.IOException)3 Map (java.util.Map)3 GenericDataSourceException (org.apache.ofbiz.entity.GenericDataSourceException)3