Boolean fixSizes = (Boolean) context.get("fixColSizes");
if (fixSizes == null) fixSizes = Boolean.FALSE;
List messages = new ArrayList();
String helperName = delegator.getGroupHelperName(groupName);
DatabaseUtil dbUtil = new DatabaseUtil(helperName);
Map modelEntities = delegator.getModelEntityMapByGroup(groupName);
Set modelEntityNames = new TreeSet(modelEntities.keySet());
Iterator modelEntityNameIter = null;
// step 1 - remove FK indices
Debug.logImportant("Removing all foreign key indices", module);
modelEntityNameIter = modelEntityNames.iterator();
while (modelEntityNameIter.hasNext()) {
String modelEntityName = (String) modelEntityNameIter.next();
ModelEntity modelEntity = (ModelEntity) modelEntities.get(modelEntityName);
dbUtil.deleteForeignKeyIndices(modelEntity, messages);
}
modelEntityNameIter = null;
// step 2 - remove FKs
Debug.logImportant("Removing all foreign keys", module);
modelEntityNameIter = modelEntityNames.iterator();
while (modelEntityNameIter.hasNext()) {
String modelEntityName = (String) modelEntityNameIter.next();
ModelEntity modelEntity = (ModelEntity) modelEntities.get(modelEntityName);
dbUtil.deleteForeignKeys(modelEntity, modelEntities, messages);
}
modelEntityNameIter = null;
// step 3 - remove PKs
Debug.logImportant("Removing all primary keys", module);
modelEntityNameIter = modelEntityNames.iterator();
while (modelEntityNameIter.hasNext()) {
String modelEntityName = (String) modelEntityNameIter.next();
ModelEntity modelEntity = (ModelEntity) modelEntities.get(modelEntityName);
dbUtil.deletePrimaryKey(modelEntity, messages);
}
modelEntityNameIter = null;
// step 4 - remove declared indices
Debug.logImportant("Removing all declared indices", module);
modelEntityNameIter = modelEntityNames.iterator();
while (modelEntityNameIter.hasNext()) {
String modelEntityName = (String) modelEntityNameIter.next();
ModelEntity modelEntity = (ModelEntity) modelEntities.get(modelEntityName);
dbUtil.deleteDeclaredIndices(modelEntity, messages);
}
modelEntityNameIter = null;
// step 5 - repair field sizes
if (fixSizes.booleanValue()) {
Debug.logImportant("Updating column field size changes", module);
List fieldsWrongSize = new LinkedList();
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);
modelEntityNameIter = modelEntityNames.iterator();
while (modelEntityNameIter.hasNext()) {
String modelEntityName = (String) modelEntityNameIter.next();
ModelEntity modelEntity = (ModelEntity) modelEntities.get(modelEntityName);
dbUtil.createPrimaryKey(modelEntity, messages);
}
modelEntityNameIter = null;
// step 7 - create FK indices
Debug.logImportant("Creating all foreign key indices", module);
modelEntityNameIter = modelEntityNames.iterator();
while (modelEntityNameIter.hasNext()) {
String modelEntityName = (String) modelEntityNameIter.next();
ModelEntity modelEntity = (ModelEntity) modelEntities.get(modelEntityName);
dbUtil.createForeignKeyIndices(modelEntity, messages);
}
modelEntityNameIter = null;
// step 8 - create FKs
Debug.logImportant("Creating all foreign keys", module);
modelEntityNameIter = modelEntityNames.iterator();
while (modelEntityNameIter.hasNext()) {
String modelEntityName = (String) modelEntityNameIter.next();
ModelEntity modelEntity = (ModelEntity) modelEntities.get(modelEntityName);
dbUtil.createForeignKeys(modelEntity, modelEntities, messages);
}
modelEntityNameIter = null;
// step 8 - create FKs
Debug.logImportant("Creating all declared indices", module);
modelEntityNameIter = modelEntityNames.iterator();
while (modelEntityNameIter.hasNext()) {
String modelEntityName = (String) modelEntityNameIter.next();
ModelEntity modelEntity = (ModelEntity) modelEntities.get(modelEntityName);
dbUtil.createDeclaredIndices(modelEntity, messages);
}
modelEntityNameIter = null;
// step 8 - checkdb
Debug.logImportant("Running DB check with add missing enabled", module);
dbUtil.checkDb(modelEntities, messages, true);
Map result = ServiceUtil.returnSuccess();
result.put("messages", messages);
return result;
}