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);
}
}
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);
}
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);
}
}
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);
}
}
}
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;
}
Aggregations