/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE file at the root of the source
* tree and available online at
*
* https://github.com/keeps/roda
*/
package org.roda.core.plugins.plugins.base;
import java.util.Arrays;
import java.util.List;
import org.roda.core.data.common.RodaConstants;
import org.roda.core.data.common.RodaConstants.PreservationEventType;
import org.roda.core.data.exceptions.AuthorizationDeniedException;
import org.roda.core.data.exceptions.GenericException;
import org.roda.core.data.exceptions.NotFoundException;
import org.roda.core.data.exceptions.RequestNotValidException;
import org.roda.core.data.v2.LiteOptionalWithCause;
import org.roda.core.data.v2.ip.AIP;
import org.roda.core.data.v2.ip.AIPState;
import org.roda.core.data.v2.ip.IndexedAIP;
import org.roda.core.data.v2.jobs.Job;
import org.roda.core.data.v2.jobs.PluginType;
import org.roda.core.data.v2.jobs.Report;
import org.roda.core.data.v2.jobs.Report.PluginState;
import org.roda.core.index.IndexService;
import org.roda.core.model.ModelService;
import org.roda.core.plugins.AbstractPlugin;
import org.roda.core.plugins.Plugin;
import org.roda.core.plugins.PluginException;
import org.roda.core.plugins.RODAObjectProcessingLogic;
import org.roda.core.plugins.orchestrate.SimpleJobPluginInfo;
import org.roda.core.plugins.plugins.PluginHelper;
import org.roda.core.storage.StorageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RemoveAIPPlugin extends AbstractPlugin<AIP> {
private static final Logger LOGGER = LoggerFactory.getLogger(RemoveAIPPlugin.class);
@Override
public void init() throws PluginException {
// do nothing
}
@Override
public void shutdown() {
// do nothing
}
@Override
public String getName() {
return "Remove AIP(s)";
}
@Override
public String getDescription() {
return "Permanently removes selected AIP(s) from the repository. Data, metadata and event history will be deleted permanently. WARNING: "
+ "This operation cannot be undone. Use with extreme caution.";
}
@Override
public String getVersionImpl() {
return "1.0";
}
@Override
public Report execute(IndexService index, ModelService model, StorageService storage,
List<LiteOptionalWithCause> liteList) throws PluginException {
return PluginHelper.processObjects(this, new RODAObjectProcessingLogic<AIP>() {
@Override
public void process(IndexService index, ModelService model, StorageService storage, Report report, Job cachedJob,
SimpleJobPluginInfo jobPluginInfo, Plugin<AIP> plugin, AIP object) {
processAIP(model, index, report, jobPluginInfo, cachedJob, object);
}
}, index, model, storage, liteList);
}
private void processAIP(ModelService model, IndexService index, Report report, SimpleJobPluginInfo jobPluginInfo,
Job job, AIP aip) {
Report reportItem = PluginHelper.initPluginReportItem(this, aip.getId(), AIP.class, AIPState.ACTIVE);
try {
LOGGER.debug("Removing AIP {}", aip.getId());
IndexedAIP iAIP = index.retrieve(IndexedAIP.class, aip.getId(),
Arrays.asList(RodaConstants.INDEX_UUID, RodaConstants.AIP_TITLE));
model.deleteAIP(aip.getId());
reportItem.setPluginState(PluginState.SUCCESS).setPluginDetails("Removal of AIP " + aip.getId() + " (title: "
+ iAIP.getTitle() + "; level: " + iAIP.getLevel() + ") ended successfully");
jobPluginInfo.incrementObjectsProcessedWithSuccess();
} catch (RequestNotValidException | NotFoundException | GenericException | AuthorizationDeniedException e) {
reportItem.setPluginState(PluginState.FAILURE)
.setPluginDetails("Removal of AIP " + aip.getId() + " did not end successfully: " + e.getMessage());
jobPluginInfo.incrementObjectsProcessedWithFailure();
}
report.addReport(reportItem);
PluginHelper.updatePartialJobReport(this, model, reportItem, true, job);
}
@Override
public Report beforeAllExecute(IndexService index, ModelService model, StorageService storage)
throws PluginException {
return new Report();
}
@Override
public Report afterAllExecute(IndexService index, ModelService model, StorageService storage) throws PluginException {
return new Report();
}
@Override
public Plugin<AIP> cloneMe() {
return new RemoveAIPPlugin();
}
@Override
public PluginType getType() {
return PluginType.MISC;
}
@Override
public boolean areParameterValuesValid() {
return true;
}
@Override
public PreservationEventType getPreservationEventType() {
return PreservationEventType.DELETION;
}
@Override
public String getPreservationEventDescription() {
return "Removes AIP from the system";
}
@Override
public String getPreservationEventSuccessMessage() {
return "The AIPs were successfully removed";
}
@Override
public String getPreservationEventFailureMessage() {
return "The AIPs were not removed";
}
@Override
public List<String> getCategories() {
return Arrays.asList(RodaConstants.PLUGIN_CATEGORY_MANAGEMENT);
}
@Override
public List<Class<AIP>> getObjectClasses() {
return Arrays.asList(AIP.class);
}
}