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.Iterator;
027
028import dk.netarkivet.common.utils.FilterIterator;
029
030/**
031 * DAO methods for reading templates only. Implemented as a Singleton
032 */
033public abstract class TemplateDAO implements DAO {
034
035    /** The singleton TemplateDAO. */
036    private static TemplateDAO instance;
037
038    /**
039     * Constructor for TemplateDAO. The real construction is done inside the getInstance method.
040     */
041    TemplateDAO() {
042    }
043
044    /**
045     * Gets the TemplateDAO singleton.
046     *
047     * @return the singleton.
048     */
049    public static synchronized TemplateDAO getInstance() {
050        if (instance == null) {
051            instance = new TemplateDBDAO();
052        }
053        return instance;
054    }
055
056    /**
057     * Read an orderxml template for the named order XML.
058     *
059     * @param orderXmlName The name of the order.xml document
060     * @return The contents of this order.xml document
061     */
062    public abstract HeritrixTemplate read(String orderXmlName);
063
064    /**
065     * Returns an iterator with all names of order.xml-templates.
066     *
067     * @return Iterator<String> with all names of templates (without .xml).
068     */
069    public abstract Iterator<String> getAll();
070
071
072    /**
073     * Returns an iterator with names of either all active or all inactive order.xml-templates.
074     *
075     * @param active true if active templates are wanted, false otherwise.
076     * @return Iterator<String> with all names of templates (without .xml).
077     */
078    public abstract Iterator<String> getAll(boolean active);
079
080    /**
081     * Returns an iterator of all templates. Note that this is not the most efficient way of getting all names of
082     * templates, for that just use getAll(). Implements the Iterable interface.
083     *
084     * @return A list of all current templates.
085     */
086    public Iterator<HeritrixTemplate> iterator() {
087        return new FilterIterator<String, HeritrixTemplate>(getAll()) {
088            protected HeritrixTemplate filter(String s) {
089                return read(s);
090            }
091        };
092    }
093
094    /**
095     * Check, if there exists a orderxml-template with a given name.
096     *
097     * @param orderXmlName a given orderxml name
098     * @return true, if there exists a orderxml-template with this name
099     */
100    public abstract boolean exists(String orderXmlName);
101
102    /**
103     * Create a orderxml-template with a given name.
104     *
105     * @param orderXmlName the given name
106     * @param orderXml the Document containing the contents of this new orderxml-template
107     */
108    public abstract void create(String orderXmlName, HeritrixTemplate orderXml);
109
110    /**
111     * Update a specific orderxml-template to contain the contents of the orderXml argument.
112     *
113     * @param orderXmlName the name of a specific orderxml-template
114     * @param orderXml the new contents of this template
115     */
116    public abstract void update(String orderXmlName, HeritrixTemplate orderXml);
117
118    /**
119     * Resets the singleton. Only for use from tests.
120     */
121    static void resetSingleton() {
122        instance = null;
123    }
124
125}