Logger.debug("Got " + results.size() + " results from "
+ importer.getClass().getSimpleName());
// TODO: parse through the results, and do the special char
// replacements and validation
VociBusinessObject vbo;
ArrayList<MetadataElement> metadataElements;
MetadataElement metadataElement;
String metadataValue;
CommunityManager communityManager = new CommunityManager();
CollectionManager collectionManager = new CollectionManager();
BundleManager bundleManager = new BundleManager();
ItemManager itemManager = new ItemManager();
for (int m = 0; m < results.size(); m++) {
vbo = results.get(m);
Logger.debug("trying to import '" + vbo.getMetadataBasedTitle()
+ "' of type " + ContentType.valueOf(vbo.getType()));
metadataElements = MetadataManager.getMetadataList(
(new ProjectManager()).getProjectID(project), vbo
.getType());
for (int n = 0; n < metadataElements.size(); n++) {
metadataElement = metadataElements.get(n);
metadataValue = vbo.getMetadataData(metadataElement
.getElement(), metadataElement.getQualifier());
if (metadataValue == null) {
metadataValue = "";
}
vbo.addToMetadataRegistry(metadataElement.getMetadataId(),
metadataValue);
try {
vbo.validateMetadata(metadataElement, metadataValue);
} catch (ValidationException e) {
vbo.addToImportErrorHolder("Validation error on "
+ metadataElement
.getFriendlyElementQualifierName()
+ ": " + e.getMessage(), true);
}
}
// check to see if any fatal errors occurred with this
// element
boolean hadFatalErrors = false;
HashMap<String, Boolean> errorMessages = vbo
.getImportErrorMessages();
String[] errorKeys = new String[errorMessages.size()];
errorKeys = errorMessages.keySet().toArray(errorKeys);
for (int n = 0; n < errorKeys.length; n++) {
if (errorMessages.get(errorKeys[n])) {
hadFatalErrors = true;
break;
}
}
if (!hadFatalErrors) {
if (((vbo.getType() == ContentType.ITEM.getValue()) || (vbo
.getType() == ContentType.BUNDLE.getValue()))
&& (vbo.getImportCollectionIDs().size() == 0)
&& vbo.getImportCommunityIDs().size() == 0) {
vbo.addToImportErrorHolder(
"Imported items and bundles "
+ "must have a collection or "
+ "community parent.", true);
} else {
importCounter++;
try {
if (vbo.getType() == ContentType.COMMUNITY
.getValue()) {
communityManager.addCommunity(user,
((Community) vbo), -1);
vbo.setSuccessfullyImported(true);
Logger
.debug("community vbo successfully imported");
for (int n = 0; n < vbo.getImportCommunityIDs()
.size(); n++) {
communityManager.addCommunityMapping(vbo
.getId(), vbo
.getImportCommunityIDs().get(n));
Logger.debug("-> mapped to community "
+ vbo.getImportCommunityIDs()
.get(n));
}
} else if (vbo.getType() == ContentType.COLLECTION
.getValue()) {
collectionManager.addCollection(user,
((Collection) vbo), -1);
vbo.setSuccessfullyImported(true);
Logger
.debug("collection vbo successfully imported");
for (int n = 0; n < vbo.getImportCommunityIDs()
.size(); n++) {
collectionManager.addCollectionMapping(vbo
.getId(), vbo
.getImportCommunityIDs().get(n));
Logger.debug("-> mapped to community "
+ vbo.getImportCommunityIDs()
.get(n));
}
for (int n = 0; n < vbo
.getImportCollectionIDs().size(); n++) {
collectionManager.addCollectionMapping(vbo
.getId(), vbo
.getImportCollectionIDs().get(n));
Logger.debug("-> mapped to collection "
+ vbo.getImportCollectionIDs().get(
n));
}
} else if (vbo.getType() == ContentType.BUNDLE
.getValue()) {
bundleManager.addBundle(user, ((Bundle) vbo), -1);
vbo.setSuccessfullyImported(true);
Logger
.debug("bundle vbo successfully imported");
for (int n = 0; n < vbo.getImportCommunityIDs()
.size(); n++) {
bundleManager.addBundleMapping(vbo.getId(),
vbo.getImportCommunityIDs().get(n));
Logger.debug("-> mapped to community "
+ vbo.getImportCommunityIDs()
.get(n));
}
for (int n = 0; n < vbo
.getImportCollectionIDs().size(); n++) {
bundleManager
.addBundleMapping(vbo.getId(), vbo
.getImportCollectionIDs()
.get(n));
Logger.debug("-> mapped to collection "
+ vbo.getImportCollectionIDs().get(
n));
}
} else if (vbo.getType() == ContentType.ITEM
.getValue()) {
itemManager.addItem(user, ((Item) vbo), -1);
vbo.setSuccessfullyImported(true);
Logger.debug("item vbo successfully imported");
for (int n = 0; n < vbo.getImportCommunityIDs()
.size(); n++) {
itemManager.addItemMapping(vbo.getId(), vbo
.getImportCommunityIDs().get(n));
Logger.debug("-> mapped to community "
+ vbo.getImportCommunityIDs()
.get(n));
}
for (int n = 0; n < vbo
.getImportCollectionIDs().size(); n++) {
itemManager.addItemMapping(vbo.getId(), vbo
.getImportCollectionIDs().get(n));
Logger.debug("-> mapped to collection "
+ vbo.getImportCollectionIDs().get(
n));
}
for (int n = 0; n < vbo.getImportBundleIDs()
.size(); n++) {
itemManager.addItemMapping(vbo.getId(), vbo
.getImportBundleIDs().get(n));
Logger.debug("-> mapped to bundle "
+ vbo.getImportBundleIDs().get(n));
}
}
} catch (SQLException e) {
// this is the only exception that'll mean
// the item itself wasn't imported ... the
// rest are failed mappings that we'll leave
// to the administrator to resolve.
importCounter--;
vbo.addToImportErrorHolder(
"A database problem occurred: "
+ e.getMessage(), false);
Logger.warn("SQLException during import", e);
} catch (NoSuchCommunityException e) {
vbo.addToImportErrorHolder(
"A community couldn't be found for mapping: "
+ e.getMessage(), false);
Logger.debug("mapping to community failed", e);
} catch (NoSuchCollectionException e) {
vbo.addToImportErrorHolder(
"A collection couldn't be found for mapping: "
+ e.getMessage(), false);
Logger.debug("mapping to collection failed", e);
} catch (NoSuchBundleException e) {
vbo.addToImportErrorHolder(
"A bundle couldn't be found for mapping: "
+ e.getMessage(), false);
Logger.debug("mapping to bundle failed", e);
} catch (NoSuchItemException e) {
vbo.addToImportErrorHolder(
"An item couldn't be found for mapping: "
+ e.getMessage(), false);
Logger.debug("mapping to item failed", e);
}
}
} else {
Logger.debug("vbo import had fatal errors");
}
if (vbo.getImportErrorMessages().size() > 0) {
problems.add(vbo);
}
}
} catch (NumberFormatException e) {
Logger.warn("Bad content type (" + contentType + ") entered", e);