Search in sources :

Example 1 with GenericHelperInfo

use of org.apache.ofbiz.entity.datasource.GenericHelperInfo in project ofbiz-framework by apache.

the class GenericDelegator method initializeOneGenericHelper.

private void initializeOneGenericHelper(String groupName) {
    GenericHelperInfo helperInfo = this.getGroupHelperInfo(groupName);
    if (helperInfo == null) {
        if (Debug.infoOn()) {
            Debug.logInfo("Delegator \"" + delegatorFullName + "\" NOT initializing helper for entity group \"" + groupName + "\" because the group is not associated to this delegator.", module);
        }
        return;
    }
    String helperBaseName = helperInfo.getHelperBaseName();
    if (Debug.infoOn()) {
        Debug.logInfo("Delegator \"" + delegatorFullName + "\" initializing helper \"" + helperBaseName + "\" for entity group \"" + groupName + "\".", module);
    }
    if (UtilValidate.isNotEmpty(helperInfo.getHelperFullName())) {
        // pre-load field type defs, the return value is ignored
        ModelFieldTypeReader.getModelFieldTypeReader(helperBaseName);
        // get the helper and if configured, do the datasource check
        GenericHelper helper = GenericHelperFactory.getHelper(helperInfo);
        try {
            Datasource datasource = EntityConfig.getDatasource(helperBaseName);
            if (datasource.getCheckOnStart()) {
                if (Debug.infoOn()) {
                    Debug.logInfo("Doing database check as requested in entityengine.xml with addMissing=" + datasource.getAddMissingOnStart(), module);
                }
                helper.checkDataSource(this.getModelEntityMapByGroup(groupName), null, datasource.getAddMissingOnStart());
            }
        } catch (GenericEntityException e) {
            Debug.logWarning(e, e.getMessage(), module);
        }
    }
}
Also used : Datasource(org.apache.ofbiz.entity.config.model.Datasource) GenericHelperInfo(org.apache.ofbiz.entity.datasource.GenericHelperInfo) GenericHelper(org.apache.ofbiz.entity.datasource.GenericHelper)

Example 2 with GenericHelperInfo

use of org.apache.ofbiz.entity.datasource.GenericHelperInfo 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 GenericHelperInfo

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

Example 4 with GenericHelperInfo

use of org.apache.ofbiz.entity.datasource.GenericHelperInfo in project ofbiz-framework by apache.

the class GenericDelegator method getGroupHelperInfo.

@Override
public GenericHelperInfo getGroupHelperInfo(String entityGroupName) {
    if (entityGroupName == null) {
        return null;
    }
    String helperBaseName = this.getGroupHelperName(entityGroupName);
    if (helperBaseName == null) {
        return null;
    }
    if (UtilValidate.isNotEmpty(this.delegatorTenantId) && "org.apache.ofbiz.tenant".equals(entityGroupName)) {
        Debug.logInfo("Can't access entity of entityGroup = " + entityGroupName + " using tenant delegator " + this.getDelegatorName() + ", use base delegator instead", module);
        return null;
    }
    GenericHelperInfo helperInfo = new GenericHelperInfo(entityGroupName, helperBaseName);
    if (UtilValidate.isNotEmpty(this.delegatorTenantId)) {
        // get the JDBC parameters from the DB for the entityGroupName and tenantId
        try {
            // NOTE: instead of caching the GenericHelpInfo object do a cached query here and create a new object each time, will avoid issues when the database data changes during run time
            // NOTE: always use the base delegator for this to avoid problems when this is being initialized
            Delegator baseDelegator = DelegatorFactory.getDelegator(this.delegatorBaseName);
            GenericValue tenantDataSource = EntityQuery.use(baseDelegator).from("TenantDataSource").where("tenantId", this.delegatorTenantId, "entityGroupName", entityGroupName).cache(true).queryOne();
            if (tenantDataSource != null) {
                helperInfo.setTenantId(this.delegatorTenantId);
                helperInfo.setOverrideJdbcUri(tenantDataSource.getString("jdbcUri"));
                helperInfo.setOverrideUsername(tenantDataSource.getString("jdbcUsername"));
                helperInfo.setOverridePassword(tenantDataSource.getString("jdbcPassword"));
            } else {
                return null;
            }
        } catch (GenericEntityException e) {
            // don't complain about this too much, just log the error if there is one
            Debug.logInfo(e, "Error getting TenantDataSource info for tenantId=" + this.delegatorTenantId + ", entityGroupName=" + entityGroupName, module);
        }
    }
    return helperInfo;
}
Also used : GenericHelperInfo(org.apache.ofbiz.entity.datasource.GenericHelperInfo)

Aggregations

GenericHelperInfo (org.apache.ofbiz.entity.datasource.GenericHelperInfo)4 Delegator (org.apache.ofbiz.entity.Delegator)2 DatabaseUtil (org.apache.ofbiz.entity.jdbc.DatabaseUtil)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 Datasource (org.apache.ofbiz.entity.config.model.Datasource)1 GenericHelper (org.apache.ofbiz.entity.datasource.GenericHelper)1 Security (org.apache.ofbiz.security.Security)1