Search in sources :

Example 1 with DatabaseUtil

use of org.apache.ofbiz.entity.jdbc.DatabaseUtil in project ofbiz-framework by apache.

the class GenericDAO method checkDb.

/* ====================================================================== */
public void checkDb(Map<String, ModelEntity> modelEntities, List<String> messages, boolean addMissing) {
    DatabaseUtil dbUtil = new DatabaseUtil(this.helperInfo);
    dbUtil.checkDb(modelEntities, messages, addMissing);
}
Also used : DatabaseUtil(org.apache.ofbiz.entity.jdbc.DatabaseUtil)

Example 2 with DatabaseUtil

use of org.apache.ofbiz.entity.jdbc.DatabaseUtil 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 3 with DatabaseUtil

use of org.apache.ofbiz.entity.jdbc.DatabaseUtil in project ofbiz-framework by apache.

the class EntityDataServices method rebuildAllIndexesAndKeys.

public static Map<String, Object> rebuildAllIndexesAndKeys(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)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtServicePermissionNotGranted", locale));
    }
    String groupName = (String) context.get("groupName");
    Boolean fixSizes = (Boolean) context.get("fixColSizes");
    if (fixSizes == null)
        fixSizes = Boolean.FALSE;
    List<String> messages = new LinkedList<String>();
    GenericHelperInfo helperInfo = delegator.getGroupHelperInfo(groupName);
    DatabaseUtil dbUtil = new DatabaseUtil(helperInfo);
    Map<String, ModelEntity> modelEntities;
    try {
        modelEntities = delegator.getModelEntityMapByGroup(groupName);
    } 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));
    }
    // step 1 - remove FK indices
    Debug.logImportant("Removing all foreign key indices", module);
    for (ModelEntity modelEntity : modelEntities.values()) {
        dbUtil.deleteForeignKeyIndices(modelEntity, messages);
    }
    // step 2 - remove FKs
    Debug.logImportant("Removing all foreign keys", module);
    for (ModelEntity modelEntity : modelEntities.values()) {
        dbUtil.deleteForeignKeys(modelEntity, modelEntities, messages);
    }
    // step 3 - remove PKs
    Debug.logImportant("Removing all primary keys", module);
    for (ModelEntity modelEntity : modelEntities.values()) {
        dbUtil.deletePrimaryKey(modelEntity, messages);
    }
    // step 4 - remove declared indices
    Debug.logImportant("Removing all declared indices", module);
    for (ModelEntity modelEntity : modelEntities.values()) {
        dbUtil.deleteDeclaredIndices(modelEntity, messages);
    }
    // step 5 - repair field sizes
    if (fixSizes.booleanValue()) {
        Debug.logImportant("Updating column field size changes", module);
        List<String> fieldsWrongSize = new LinkedList<String>();
        dbUtil.checkDb(modelEntities, fieldsWrongSize, messages, true, true, true, true);
        if (fieldsWrongSize.size() > 0) {
            dbUtil.repairColumnSizeChanges(modelEntities, fieldsWrongSize, messages);
        } else {
            String thisMsg = "No field sizes to update";
            messages.add(thisMsg);
            Debug.logImportant(thisMsg, module);
        }
    }
    // step 6 - create PKs
    Debug.logImportant("Creating all primary keys", module);
    for (ModelEntity modelEntity : modelEntities.values()) {
        dbUtil.createPrimaryKey(modelEntity, messages);
    }
    // step 7 - create FK indices
    Debug.logImportant("Creating all foreign key indices", module);
    for (ModelEntity modelEntity : modelEntities.values()) {
        dbUtil.createForeignKeyIndices(modelEntity, messages);
    }
    // step 8 - create FKs
    Debug.logImportant("Creating all foreign keys", module);
    for (ModelEntity modelEntity : modelEntities.values()) {
        dbUtil.createForeignKeys(modelEntity, modelEntities, messages);
    }
    // step 8 - create FKs
    Debug.logImportant("Creating all declared indices", module);
    for (ModelEntity modelEntity : modelEntities.values()) {
        dbUtil.createDeclaredIndices(modelEntity, messages);
    }
    // step 8 - checkdb
    Debug.logImportant("Running DB check with add missing enabled", module);
    dbUtil.checkDb(modelEntities, messages, true);
    Map<String, Object> result = ServiceUtil.returnSuccess();
    result.put("messages", messages);
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) GenericHelperInfo(org.apache.ofbiz.entity.datasource.GenericHelperInfo) Security(org.apache.ofbiz.security.Security) DatabaseUtil(org.apache.ofbiz.entity.jdbc.DatabaseUtil) LinkedList(java.util.LinkedList) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Aggregations

DatabaseUtil (org.apache.ofbiz.entity.jdbc.DatabaseUtil)3 Delegator (org.apache.ofbiz.entity.Delegator)2 GenericHelperInfo (org.apache.ofbiz.entity.datasource.GenericHelperInfo)2 ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)2 LinkedList (java.util.LinkedList)1 Locale (java.util.Locale)1 TreeSet (java.util.TreeSet)1 ComponentConfig (org.apache.ofbiz.base.component.ComponentConfig)1 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)1 GenericValue (org.apache.ofbiz.entity.GenericValue)1 Security (org.apache.ofbiz.security.Security)1