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     * Returns an iterator of all templates. Note that this is not the most efficient way of getting all names of
073     * templates, for that just use getAll(). Implements the Iterable interface.
074     *
075     * @return A list of all current templates.
076     */
077    public Iterator<HeritrixTemplate> iterator() {
078        return new FilterIterator<String, HeritrixTemplate>(getAll()) {
079            protected HeritrixTemplate filter(String s) {
080                return read(s);
081            }
082        };
083    }
084
085    /**
086     * Check, if there exists a orderxml-template with a given name.
087     *
088     * @param orderXmlName a given orderxml name
089     * @return true, if there exists a orderxml-template with this name
090     */
091    public abstract boolean exists(String orderXmlName);
092
093    /**
094     * Create a orderxml-template with a given name.
095     *
096     * @param orderXmlName the given name
097     * @param orderXml the Document containing the contents of this new orderxml-template
098     */
099    public abstract void create(String orderXmlName, HeritrixTemplate orderXml);
100
101    /**
102     * Update a specific orderxml-template to contain the contents of the orderXml argument.
103     *
104     * @param orderXmlName the name of a specific orderxml-template
105     * @param orderXml the new contents of this template
106     */
107    public abstract void update(String orderXmlName, HeritrixTemplate orderXml);
108
109    /**
110     * Resets the singleton. Only for use from tests.
111     */
112    static void resetSingleton() {
113        instance = null;
114    }
115
116}