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 */
023package dk.netarkivet.harvester.datamodel;
024
025import java.util.Date;
026
027import dk.netarkivet.common.exceptions.ArgumentNotValid;
028
029/**
030 * Class encapsulating domain alias information. The information is used to prevent harvesting the domains which are
031 * aliases of other domains.
032 */
033public class AliasInfo {
034
035    /** the domain. */
036    private final String domain;
037    /** the domain which this domain is an alias of. */
038    private final String aliasOf;
039    /** the domain was (re)registered as an alias on this date. */
040    private final Date lastChange;
041
042    /**
043     * Constructor for the AliasInfo class.
044     *
045     * @param domain a given domain
046     * @param aliasOf the given domain is an alias of this domain
047     * @param lastChange the alias was (re-)registered on this date.
048     * @throws ArgumentNotValid in the following cases: 1. domain is null or empty 2. aliasOf is null or empty 3.
049     * lastChange is null 4. domain equals aliasOf
050     */
051    public AliasInfo(String domain, String aliasOf, Date lastChange) throws ArgumentNotValid {
052        ArgumentNotValid.checkNotNullOrEmpty(domain, "domain");
053        ArgumentNotValid.checkNotNullOrEmpty(aliasOf, "aliasOf");
054        ArgumentNotValid.checkNotNull(lastChange, "lastChange");
055        if (domain.equals(aliasOf)) {
056            throw new ArgumentNotValid("the aliasOf argument must not be equal to the domain");
057        }
058        this.domain = domain;
059        this.aliasOf = aliasOf;
060        this.lastChange = (Date) lastChange.clone();
061    }
062
063    /**
064     * @return Returns the aliasOf.
065     */
066    public String getAliasOf() {
067        return aliasOf;
068    }
069
070    /**
071     * @return Returns the domain.
072     */
073    public String getDomain() {
074        return domain;
075    }
076
077    /**
078     * @return Returns the lastChange.
079     */
080    public Date getLastChange() {
081        return (Date) lastChange.clone();
082    }
083
084    /**
085     * Is this alias expired? This method depends upon the Constant:
086     * dk.netarkivet.harvester.webinterface.Constants.ALIAS_TIMEOUT_IN_MILLISECONDS Note that this constant is now read
087     * from settings.
088     *
089     * @return true, if alias is expired
090     */
091    public boolean isExpired() {
092        Date aliasTimeoutDate = getExpirationDate();
093        Date now = new Date();
094        return aliasTimeoutDate.before(now);
095    }
096
097    /**
098     * The date when this alias will expire (or has expired).
099     *
100     * @return The expiration date for this alias. May be in the past or in the future.
101     */
102    public Date getExpirationDate() {
103        Date aliasTimeoutDate = new Date(this.lastChange.getTime() + Constants.ALIAS_TIMEOUT_IN_MILLISECONDS);
104        return aliasTimeoutDate;
105    }
106
107    /**
108     * @return String representation of this AliasInfo object.
109     * @see java.lang.Object#toString
110     */
111    public String toString() {
112        return "Domain '" + getDomain() + "' is an alias of '" + getAliasOf() + "', last updated " + getLastChange();
113    }
114
115}