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