public void execute(final OperationContext operationContext, ResultHandler resultHandler) throws ResourceNotFoundException, OperationException
{
final String operationName = operationContext.getOperationName();
OperationAttachment attachment = operationContext.getAttachment(true);
if (attachment == null) throw new OperationException(operationContext.getOperationName(), "No attachment available for MOP import.");
InputStream inputStream = attachment.getStream();
if (inputStream == null) throw new OperationException(operationContext.getOperationName(), "No data stream available for import.");
POMSessionManager mgr = operationContext.getRuntimeContext().getRuntimeComponent(POMSessionManager.class);
POMSession session = mgr.getSession();
if (session == null) throw new OperationException(operationName, "MOP session was null");
Workspace workspace = session.getWorkspace();
if (workspace == null) throw new OperationException(operationName, "MOP workspace was null");
DataStorage dataStorage = operationContext.getRuntimeContext().getRuntimeComponent(DataStorage.class);
if (dataStorage == null) throw new OperationException(operationName, "DataStorage was null");
NavigationService navigationService = operationContext.getRuntimeContext().getRuntimeComponent(NavigationService.class);
if (navigationService == null) throw new OperationException(operationName, "Navigation service was null");
DescriptionService descriptionService = operationContext.getRuntimeContext().getRuntimeComponent(DescriptionService.class);
if (descriptionService == null) throw new OperationException(operationName, "Description service was null");
String mode = operationContext.getAttributes().getValue("importMode");
if (mode == null || "".equals(mode)) mode = "merge";
ImportMode importMode;
try
{
importMode = ImportMode.valueOf(mode.trim().toUpperCase());
}
catch (Exception e)
{
throw new OperationException(operationName, "Unknown importMode " + mode);
}
Map<SiteKey, MopImport> importMap = new HashMap<SiteKey, MopImport>();
final NonCloseableZipInputStream zis = new NonCloseableZipInputStream(inputStream);
ZipEntry entry;
boolean empty = false;
try
{
log.info("Preparing data for import.");
while ( (entry = zis.getNextEntry()) != null)
{
// Skip directories
if (entry.isDirectory()) continue;
// Skip empty entries (this allows empty zip files to not cause exceptions).
empty = entry.getName().equals("");
if (empty) continue;
// Parse zip entry
String[] parts = parseEntry(entry);
SiteKey siteKey = Utils.siteKey(parts[0], parts[1]);
String file = parts[2];
MopImport mopImport = importMap.get(siteKey);
if (mopImport == null)
{
mopImport = new MopImport();
importMap.put(siteKey, mopImport);
}
if (SiteLayoutExportTask.FILES.contains(file))
{
// Unmarshal site layout data
Marshaller<PortalConfig> marshaller = operationContext.getBindingProvider().getMarshaller(PortalConfig.class, ContentType.XML);
PortalConfig portalConfig = marshaller.unmarshal(zis);
portalConfig.setType(siteKey.getTypeName());
if (!portalConfig.getName().equals(siteKey.getName()))
{
throw new OperationException(operationName, "Name of site does not match that of the zip entry site name.");
}
// Add import task to run later
mopImport.siteTask = new SiteLayoutImportTask(portalConfig, siteKey, dataStorage);
}
else if (file.equals(PageExportTask.FILE))
{
// Unmarshal page data
Marshaller<Page.PageSet> marshaller = operationContext.getBindingProvider().getMarshaller(Page.PageSet.class, ContentType.XML);
Page.PageSet pages = marshaller.unmarshal(zis);
for (Page page : pages.getPages())
{
page.setOwnerType(siteKey.getTypeName());
page.setOwnerId(siteKey.getName());
}
// Add import task to run later.
mopImport.pageTask = new PageImportTask(pages, siteKey, dataStorage);
}
else if (file.equals(NavigationExportTask.FILE))
{
// Unmarshal navigation data
Marshaller<PageNavigation> marshaller = operationContext.getBindingProvider().getMarshaller(PageNavigation.class, ContentType.XML);
PageNavigation navigation = marshaller.unmarshal(zis);
navigation.setOwnerType(siteKey.getTypeName());
navigation.setOwnerId(siteKey.getName());
// Add import task to run later
mopImport.navigationTask = new NavigationImportTask(navigation, siteKey, navigationService, descriptionService, dataStorage);
}
}
resultHandler.completed(NoResultModel.INSTANCE);
}
catch (Throwable t)
{
throw new OperationException(operationContext.getOperationName(), "Exception reading data for import.", t);
}
finally
{
try
{
zis.reallyClose();
}
catch (IOException e)
{
log.warn("Exception closing underlying data stream from import.");
}
}
if (empty)
{
log.info("Nothing to import, zip file empty.");
return;
}
// Perform import
Map<SiteKey, MopImport> importsRan = new HashMap<SiteKey, MopImport>();
try
{
log.info("Performing import using importMode '" + mode + "'");
for (Map.Entry<SiteKey, MopImport> mopImportEntry : importMap.entrySet())
{
SiteKey siteKey = mopImportEntry.getKey();
MopImport mopImport = mopImportEntry.getValue();
MopImport ran = new MopImport();
if (importsRan.containsKey(siteKey))
{
throw new IllegalStateException("Multiple site imports for same operation.");
}
importsRan.put(siteKey, ran);
log.debug("Importing data for site " + siteKey);
// Site layout import
if (mopImport.siteTask != null)
{
log.debug("Importing site layout data.");
ran.siteTask = mopImport.siteTask;
mopImport.siteTask.importData(importMode);
}
// Page import
if (mopImport.pageTask != null)
{
log.debug("Importing page data.");
ran.pageTask = mopImport.pageTask;
mopImport.pageTask.importData(importMode);
}
// Navigation import
if (mopImport.navigationTask != null)
{
log.debug("Importing navigation data.");
ran.navigationTask = mopImport.navigationTask;
mopImport.navigationTask.importData(importMode);
}
}
log.info("Import successful !");
}
catch (Throwable t)
{
boolean rollbackSuccess = true;
log.error("Exception importing data.", t);
log.info("Attempting to rollback data modified by import.");
for (Map.Entry<SiteKey, MopImport> mopImportEntry : importsRan.entrySet())
{
SiteKey siteKey = mopImportEntry.getKey();
MopImport mopImport = mopImportEntry.getValue();
log.debug("Rolling back imported data for site " + siteKey);
if (mopImport.navigationTask != null)
{
log.debug("Rolling back navigation modified during import...");
try
{
mopImport.navigationTask.rollback();
}
catch (Throwable t1) // Continue rolling back even though there are exceptions.
{
rollbackSuccess = false;
log.error("Error rolling back navigation data for site " + siteKey, t1);
}
}
if (mopImport.pageTask != null)
{
log.debug("Rolling back pages modified during import...");
try
{
mopImport.pageTask.rollback();
}
catch (Throwable t1) // Continue rolling back even though there are exceptions.
{
rollbackSuccess = false;
log.error("Error rolling back page data for site " + siteKey, t1);
}
}
if (mopImport.siteTask != null)
{
log.debug("Rolling back site layout modified during import...");
try
{
mopImport.siteTask.rollback();
}
catch (Throwable t1) // Continue rolling back even though there are exceptions.
{
rollbackSuccess = false;
log.error("Error rolling back site layout for site " + siteKey, t1);
}
}
}
String message = (rollbackSuccess) ?
"Error during import. Tasks successfully rolled back. Portal should be back to consistent state." :
"Error during import. Errors in rollback as well. Portal may be in an inconsistent state.";
throw new OperationException(operationName, message, t);
}
finally
{
importMap.clear();
importsRan.clear();