if (helperInfo == null) {
throw new ContainerException("Unable to locate the datasource helper for the group [" + groupNameToUse + "]");
}
// get the database util object
DatabaseUtil dbUtil = new DatabaseUtil(helperInfo);
Map<String, ModelEntity> modelEntities;
try {
modelEntities = delegator.getModelEntityMapByGroup(groupNameToUse);
} catch (GenericEntityException e) {
throw new ContainerException(e.getMessage(), e);
}
TreeSet<String> modelEntityNames = new TreeSet<String>(modelEntities.keySet());
// check for drop index/fks
if (dropConstraints) {
List<String> messages = FastList.newInstance();
Debug.logImportant("Dropping foreign key indcies...", 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);
}
}
if (messages.size() > 0) {
if (Debug.infoOn()) {
for (String message : messages) {
Debug.logInfo(message, module);
}
}
}
}
// drop pks
if (dropPks) {
List<String> messages = FastList.newInstance();
Debug.logImportant("Dropping primary keys...", module);
for (String entityName : modelEntityNames) {
ModelEntity modelEntity = modelEntities.get(entityName);
if (modelEntity != null) {
dbUtil.deletePrimaryKey(modelEntity, messages);
}
}
if (messages.size() > 0) {
if (Debug.infoOn()) {
for (String message : messages) {
Debug.logInfo(message, module);
}
}
}
}
// repair columns
if (repairColumns) {
List<String> fieldsToRepair = FastList.newInstance();
List<String> messages = FastList.newInstance();
dbUtil.checkDb(modelEntities, fieldsToRepair, messages, false, false, false, false);
if (fieldsToRepair.size() > 0) {
messages = FastList.newInstance();
dbUtil.repairColumnSizeChanges(modelEntities, fieldsToRepair, messages);
if (messages.size() > 0) {
if (Debug.infoOn()) {
for (String message : messages) {
Debug.logInfo(message, module);
}
}
}
}
}
// get the reader name URLs first
List<URL> urlList = null;
if (readerNames != null) {
urlList = EntityDataLoader.getUrlList(helperInfo.getHelperBaseName(), component, readerNames);
} else if (!"none".equalsIgnoreCase(this.readers)) {
urlList = EntityDataLoader.getUrlList(helperInfo.getHelperBaseName(), component);
}
// need a list if it is empty
if (urlList == null) {
urlList = FastList.newInstance();
}
// add in the defined extra files
for (String fileName: this.files) {
URL fileUrl = UtilURL.fromResource((String) fileName);
if (fileUrl != null) {
urlList.add(fileUrl);
}
}
// next check for a directory of files
if (this.directory != null) {
File dir = new File(this.directory);
if (dir.exists() && dir.isDirectory() && dir.canRead()) {
File[] fileArray = dir.listFiles();
if (fileArray != null && fileArray.length > 0) {
for (File file: fileArray) {
if (file.getName().toLowerCase().endsWith(".xml")) {
try {
urlList.add(file.toURI().toURL());
} catch (MalformedURLException e) {
Debug.logError(e, "Unable to load file (" + file.getName() + "); not a valid URL.", module);
}
}
}
}
}
}
// process the list of files
NumberFormat changedFormat = NumberFormat.getIntegerInstance();
changedFormat.setMinimumIntegerDigits(5);
changedFormat.setGroupingUsed(false);
List<Object> errorMessages = FastList.newInstance();
List<String> infoMessages = FastList.newInstance();
int totalRowsChanged = 0;
if (UtilValidate.isNotEmpty(urlList)) {
Debug.logImportant("=-=-=-=-=-=-= Doing a data load with the following files:", module);
for (URL dataUrl: urlList) {
Debug.logImportant(dataUrl.toExternalForm(), module);
}
Debug.logImportant("=-=-=-=-=-=-= Starting the data load...", module);
for (URL dataUrl: urlList) {
try {
int rowsChanged = EntityDataLoader.loadData(dataUrl, helperInfo.getHelperBaseName(), delegator, errorMessages, txTimeout, useDummyFks, maintainTxs, tryInserts);
totalRowsChanged += rowsChanged;
infoMessages.add(changedFormat.format(rowsChanged) + " of " + changedFormat.format(totalRowsChanged) + " from " + dataUrl.toExternalForm());
} catch (GenericEntityException e) {
Debug.logError(e, "Error loading data file: " + dataUrl.toExternalForm(), module);
}
}
} else {
Debug.logImportant("=-=-=-=-=-=-= No data load files found.", module);
}
if (infoMessages.size() > 0) {
Debug.logImportant("=-=-=-=-=-=-= Here is a summary of the data load:", module);
for (String message: infoMessages) {
Debug.logImportant(message, module);
}
}
if (errorMessages.size() > 0) {
Debug.logImportant("The following errors occured in the data load:", module);
for (Object message: errorMessages) {
Debug.logImportant(message.toString(), module);
}
}
Debug.logImportant("=-=-=-=-=-=-= Finished the data load with " + totalRowsChanged + " rows changed.", module);
// create primary keys
if (createPks) {
List<String> messages = FastList.newInstance();
Debug.logImportant("Creating primary keys...", module);
for (String entityName : modelEntityNames) {
ModelEntity modelEntity = modelEntities.get(entityName);
if (modelEntity != null) {
dbUtil.createPrimaryKey(modelEntity, messages);
}
}
if (messages.size() > 0) {
if (Debug.infoOn()) {
for (String message : messages) {
Debug.logInfo(message, module);
}
}
}
}
// create constraints
if (createConstraints) {
List<String> messages = FastList.newInstance();
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);
}
}
if (messages.size() > 0) {
if (Debug.infoOn()) {