001/*
002 * #%L
003 * Netarchivesuite - archive
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023
024package dk.netarkivet.archive.bitarchive.distribute;
025
026import java.io.File;
027import java.util.Collection;
028import java.util.HashSet;
029
030import dk.netarkivet.archive.distribute.ArchiveMessage;
031import dk.netarkivet.archive.distribute.ArchiveMessageVisitor;
032import dk.netarkivet.common.distribute.ChannelID;
033import dk.netarkivet.common.distribute.RemoteFile;
034import dk.netarkivet.common.exceptions.ArgumentNotValid;
035
036/**
037 * Message class used by the bit archive monitor to notify the ArcRepository of a completed batch job.
038 */
039@SuppressWarnings({"serial"})
040public class BatchReplyMessage extends ArchiveMessage {
041    /** Number of files processed by the BatchJob. */
042    private int noOfFilesProcessed;
043    /** Set of files that the BatchJob could not process. */
044    private HashSet<File> filesFailed;
045    /** The result of the BatchJob. */
046    private RemoteFile resultFile;
047
048    /**
049     * Message to signal from BitarchiveMonitorServer that the batch job identified by originatingBatchMsgId is
050     * completed. Holds status information: list of files processed and a list of files on which the batch job failed
051     *
052     * @param to The queue to which this message is to be sent. This will normally be the ARCREPOS queue
053     * @param replyTo The queue that should receive replies.
054     * @param originatingBatchMsgId The Id of the BathMessage which gave rise to this reply
055     * @param filesProcessed The total number of file processed in this batch job
056     * @param failedFiles A Collection of strings with the names of files on which this batch job failed. May be null or
057     * empty for no errors.
058     * @param resultFile The RemoteFile containing the output from the batch job, or null if an error occurred that
059     * prevented the creation of the file.
060     * @throws ArgumentNotValid if the input parameters are not meaningful
061     */
062    public BatchReplyMessage(ChannelID to, ChannelID replyTo, String originatingBatchMsgId, int filesProcessed,
063            Collection<File> failedFiles, RemoteFile resultFile) throws ArgumentNotValid {
064        // replyTo must be set here because it is used by AdminData to work
065        // out which bitarchive the batch job operated on
066        super(to, replyTo);
067        ArgumentNotValid.checkNotNullOrEmpty(originatingBatchMsgId, "originatingBatchMsgId");
068        ArgumentNotValid.checkTrue(filesProcessed >= 0, "filesProcessed should not be less than zero");
069
070        this.replyOfId = originatingBatchMsgId;
071        this.noOfFilesProcessed = filesProcessed;
072        if (failedFiles != null) {
073            this.filesFailed = new HashSet<File>(failedFiles);
074        } else {
075            this.filesFailed = new HashSet<File>();
076        }
077        this.resultFile = resultFile;
078    }
079
080    /**
081     * Returns the total number of files processed by this batch job.
082     *
083     * @return the number of files
084     */
085    public int getNoOfFilesProcessed() {
086        return noOfFilesProcessed;
087    }
088
089    /**
090     * Retrieves the collection of files, where this batchjob has failed. (may be null)
091     *
092     * @return The collection of failed files
093     */
094    public Collection<File> getFilesFailed() {
095        return filesFailed;
096    }
097
098    /**
099     * Returns the RemoteFile that contains the output of this batchjob. May be null if the message is not ok.
100     *
101     * @return the RemoteFile mentioned above. May be null, if the message is not ok.
102     */
103    public RemoteFile getResultFile() {
104        return resultFile;
105    }
106
107    /**
108     * Should be implemented as a part of the visitor pattern. fx.: public void accept(ArchiveMessageVisitor v) {
109     * v.visit(this); }
110     *
111     * @param v A message visitor
112     */
113    public void accept(ArchiveMessageVisitor v) {
114        v.visit(this);
115    }
116
117    /**
118     * Retrieval of a string representing the instance.
119     *
120     * @return A string representing this instance.
121     */
122    public String toString() {
123        return "BatchReplyMessage for batch job " + replyOfId + "\nFilesProcessed = " + noOfFilesProcessed
124                + "\nFilesFailed = " + filesFailed.size() + "\n" + super.toString();
125    }
126}