
uk.gov.nationalarchives.droid.gui.report.ReportAction Maven / Gradle / Ivy
/**
* Copyright (c) 2016, The National Archives
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of the The National Archives nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package uk.gov.nationalarchives.droid.gui.report;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.ExecutionException;
import static java.nio.charset.StandardCharsets.UTF_8;
import javax.swing.SwingWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.gov.nationalarchives.droid.core.interfaces.config.DroidGlobalConfig;
import uk.gov.nationalarchives.droid.report.interfaces.CancellableProgressObserver;
import uk.gov.nationalarchives.droid.report.interfaces.Report;
import uk.gov.nationalarchives.droid.report.interfaces.ReportCancelledException;
import uk.gov.nationalarchives.droid.report.interfaces.ReportManager;
import uk.gov.nationalarchives.droid.report.interfaces.ReportRequest;
import uk.gov.nationalarchives.droid.report.interfaces.ReportSpec;
import uk.gov.nationalarchives.droid.report.interfaces.ReportXmlWriter;
/**
* @author Alok Kumar Dash
*
*/
public class ReportAction extends SwingWorker {
private final Logger log = LoggerFactory.getLogger(getClass());
private List profileIds;
private ReportManager reportManager;
private ReportXmlWriter reportXmlWriter;
private ReportSpec reportSpec;
private ReportProgressDialog progressDialog;
private ReportViewFrame viewDialog;
private DroidGlobalConfig config;
private ExportReportAction exportReportAction;
private Path targetFile;
private CancellableProgressObserver backgroundProgressObserver;
/**
* Default constructor.
*/
public ReportAction() {
setBackgroundProgressObserver();
}
//CHECKSTYLE:OFF too many parameters.
/**
*
* @param config The global config to use.
* @param reportManager The report manager.
* @param reportXmlWriter The report Xml writer.
* @param reportSpec The report spec.
* @param progressDialog The progress dialog.
* @param viewDialog The report dialog.
* @param exportReportAction The export report actions.
* @param profileIds The list of profile ids to report on.
*/
public ReportAction(DroidGlobalConfig config, ReportManager reportManager, ReportXmlWriter reportXmlWriter,
ReportSpec reportSpec, ReportProgressDialog progressDialog, ReportViewFrame viewDialog,
ExportReportAction exportReportAction, List profileIds) {
setBackgroundProgressObserver();
setConfig(config);
setReportManager(reportManager);
setReportXmlWriter(reportXmlWriter);
setReportSpec(reportSpec);
setProgressDialog(progressDialog);
setViewDialog(viewDialog);
setExportReportAction(exportReportAction);
setProfileIds(profileIds);
}
//CHECKSTYLE:ON
private void setBackgroundProgressObserver() {
backgroundProgressObserver = new CancellableProgressObserver() {
private boolean cancelled;
@Override
public void onProgress(Integer progress) {
publish(progress);
}
@Override
public void cancel() {
cancelled = true;
}
@Override
public boolean isCancelled() {
return cancelled;
}
};
}
/**
* @see javax.swing.SwingWorker#done()
*/
@Override
protected void done() {
try {
get();
} catch (ExecutionException e) {
log.error(e.getCause().toString(), e);
} catch (InterruptedException e) {
log.debug(e.getMessage(), e);
}
}
/**
* Cancels a report.
*/
public void cancel() {
backgroundProgressObserver.cancel();
progressDialog.setVisible(false);
progressDialog.dispose();
}
/**
* @param reportManager
* the reportManager to set
*/
public void setReportManager(ReportManager reportManager) {
this.reportManager = reportManager;
}
/**
* @return Void void.
* @see javax.swing.SwingWorker#doInBackground()
*/
@Override
protected Void doInBackground() {
final ReportRequest request = new ReportRequest();
request.setReportSpec(reportSpec);
request.setProfileIds(profileIds);
try {
targetFile = Files.createTempFile(config.getTempDir(), "report~", ".xml");
final Report report = reportManager.generateReport(request, null, backgroundProgressObserver);
try (final Writer writer = Files.newBufferedWriter(targetFile, UTF_8)) {
reportXmlWriter.writeReport(report, writer);
}
} catch (final IOException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} catch (ReportCancelledException e) {
cancel(false);
log.info("Report cancelled by user.");
}
return null;
}
/**
* {@inheritDoc}
*/
@Override
protected void process(List chunks) {
for (Integer i : chunks) {
progressDialog.onProgress(i);
}
}
/**
* @param profileIds
* the profileIds to set
*/
public void setProfileIds(List profileIds) {
this.profileIds = profileIds;
}
/**
* @param reportXmlWriter the reportXmlWriter to set
*/
public void setReportXmlWriter(ReportXmlWriter reportXmlWriter) {
this.reportXmlWriter = reportXmlWriter;
}
/**
* @param reportSpec the reportSpec to set
*/
public void setReportSpec(ReportSpec reportSpec) {
this.reportSpec = reportSpec;
}
/**
* @param viewDialog the viewDialog to set
*/
public void setViewDialog(ReportViewFrame viewDialog) {
this.viewDialog = viewDialog;
}
/**
* @param config the config to set
*/
public void setConfig(DroidGlobalConfig config) {
this.config = config;
}
/**
* @param progressDialog the progressDialog to set
*/
public void setProgressDialog(final ReportProgressDialog progressDialog) {
this.progressDialog = progressDialog;
addPropertyChangeListener(new ReportFinishedListener());
}
/**
* @param exportReportAction the exportReportAction to set
*/
public void setExportReportAction(ExportReportAction exportReportAction) {
this.exportReportAction = exportReportAction;
}
private final class ReportFinishedListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("state".equals(evt.getPropertyName())
&& SwingWorker.StateValue.DONE == evt.getNewValue()) {
progressDialog.setVisible(false);
progressDialog.dispose();
// now show the report in all its glory
viewDialog.renderReport(targetFile, reportSpec.getXslTransforms());
viewDialog.setExportAction(exportReportAction);
viewDialog.setVisible(true);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy