001/*
002 * #%L
003 * Netarchivesuite - harvester
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.harvester.datamodel;
025
026import java.util.Locale;
027
028import dk.netarkivet.common.exceptions.ArgumentNotValid;
029import dk.netarkivet.common.exceptions.UnknownID;
030import dk.netarkivet.common.utils.I18n;
031
032/**
033 * Class for containing a reason for stopping the harvesting of a domain. There are five possible reasons:<br>
034 * 1) We have now harvested the whole domain (DOWNLOAD_COMPLETE) <br>
035 * 2) We have now harvested the number of objects allowed from this domain in this iteration (OBJECT_LIMIT) <br>
036 * 3) We have now harvested the the number of bytes allowed from this domain in this iteration (SIZE_LIMIT) <br>
037 * 4) We stopped harvesting because we hit the per-configuration limit (CONFIG_SIZE_LIMIT) <br>
038 * 5) We don't know whether or not the harvesting is completed, because the crawler did not finish in an orderly way
039 * (DOWNLOAD_UNFINISHED) <br>
040 * <p>
041 * Note: This enum is serialized to the database using the order in which these are defined. Thus the order of stop
042 * reasons MUST NOT BE CHANGED!
043 */
044public enum StopReason {
045
046    /**
047     * Stop reason is download complete, when all pages within the scope of the harvest template have been downloaded.
048     */
049    DOWNLOAD_COMPLETE,
050    /**
051     * Stop reason is object limit reached, when the domain reached the maximum number of objects allowed by the
052     * harvest.
053     */
054    OBJECT_LIMIT,
055    /**
056     * Stop reason is size limit reached, when the domain reached the maximum number of bytes allowed by the harvest.
057     */
058    SIZE_LIMIT,
059    /**
060     * Stop reason is configuration size limit reached, when the domain reached the maximum number of bytes allowed by
061     * the configuration.
062     */
063    CONFIG_SIZE_LIMIT,
064    /**
065     * Stop reason is download unfinished, when we don't know whether or not the harvesting is completed, because the
066     * crawler did not finish in an orderly way.
067     */
068    DOWNLOAD_UNFINISHED,
069    /**
070     * Stop reason is configuration object limit reached, when the domain reached the maximum number of objects allowed
071     * by the configuration.
072     */
073    CONFIG_OBJECT_LIMIT,
074
075    /**
076     * Stop reason is harvesting time limit reached, when the harvester is not finished with harvesting the domain when
077     * the harvester reaches its time-limit.
078     */
079    TIME_LIMIT;
080
081    /** Internationalisation object. */
082    private static final I18n I18N = new I18n(dk.netarkivet.harvester.Constants.TRANSLATIONS_BUNDLE);
083
084    /**
085     * Get the StopReason corresponding to the given positive integer.
086     *
087     * @param stopreasonNum a given positive integer
088     * @return the StopReason corresponding to the given positive integer
089     * @see StopReason#ordinal()
090     */
091    static StopReason getStopReason(final int stopreasonNum) {
092        switch (stopreasonNum) {
093        case 0:
094            return DOWNLOAD_COMPLETE;
095        case 1:
096            return OBJECT_LIMIT;
097        case 2:
098            return SIZE_LIMIT;
099        case 3:
100            return CONFIG_SIZE_LIMIT;
101        case 4:
102            return DOWNLOAD_UNFINISHED;
103        case 5:
104            return CONFIG_OBJECT_LIMIT;
105        case 6:
106            return TIME_LIMIT;
107        default:
108            throw new UnknownID("No stop reason assigned to " + stopreasonNum);
109        }
110    }
111
112    /**
113     * Return a localized string describing a stopreason.
114     *
115     * @param l the locale
116     * @return a localized string describing a stopreason.
117     */
118    public String getLocalizedString(Locale l) {
119        ArgumentNotValid.checkNotNull(l, "l");
120        switch (this) {
121        case DOWNLOAD_COMPLETE:
122            return I18N.getString(l, "stopreason.complete");
123        case OBJECT_LIMIT:
124            return I18N.getString(l, "stopreason.max.objects.limit.reached");
125        case CONFIG_OBJECT_LIMIT:
126            return I18N.getString(l, "stopreason.max.domainobjects.limit.reached");
127        case SIZE_LIMIT:
128            return I18N.getString(l, "stopreason.max.bytes.limit.reached");
129        case CONFIG_SIZE_LIMIT:
130            return I18N.getString(l, "stopreason.max.domainconfig.limit.reached");
131        case DOWNLOAD_UNFINISHED:
132            return I18N.getString(l, "stopreason.download.unfinished");
133        case TIME_LIMIT:
134            return I18N.getString(l, "stopreason.timelimit.reached");
135        default:
136            return I18N.getString(l, "stopreason.unknown.0", this);
137        }
138    }
139
140}