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.Iterator; 026 027import dk.netarkivet.common.exceptions.ArgumentNotValid; 028import dk.netarkivet.common.exceptions.IOFailure; 029import dk.netarkivet.common.exceptions.PermissionDenied; 030import dk.netarkivet.common.exceptions.UnknownID; 031 032/** 033 * A DAO for reading and writing schedules by name. 034 */ 035public abstract class ScheduleDAO implements DAO, Iterable<Schedule> { 036 037 /** The singleton instance. */ 038 private static ScheduleDAO instance; 039 040 /** 041 * Constructor made private to enforce singleton. 042 */ 043 protected ScheduleDAO() { 044 } 045 046 /** 047 * Gets the singleton instance of the ScheduleDAO. 048 * 049 * @return ScheduleDAO singleton 050 */ 051 public static synchronized ScheduleDAO getInstance() { 052 if (instance == null) { 053 instance = new ScheduleDBDAO(); 054 } 055 return instance; 056 } 057 058 /** 059 * Create a new schedule. 060 * 061 * @param schedule The schedule to create 062 * @throws ArgumentNotValid if schedule is null 063 * @throws PermissionDenied if a schedule already exists 064 */ 065 public abstract void create(Schedule schedule); 066 067 /** 068 * Returns whether a named schedule exists. 069 * 070 * @param scheduleName The name of a schedule 071 * @return True if the schedule exists. 072 * @throws ArgumentNotValid if the schedulename is null or empty 073 */ 074 public abstract boolean exists(String scheduleName); 075 076 /** 077 * Read an existing schedule. 078 * 079 * @param scheduleName the name of the schedule 080 * @return The schedule read 081 * @throws ArgumentNotValid if schedulename is null or empty 082 * @throws UnknownID if the schedule doesn't exist 083 */ 084 public abstract Schedule read(String scheduleName); 085 086 /** 087 * Update a schedule in the DAO. 088 * 089 * @param schedule The schedule to update 090 * @throws ArgumentNotValid If the schedule is null 091 * @throws UnknownID If the schedule doesn't exist in the DAO 092 * @throws IOFailure If the edition of the schedule to update is older than the DAO's 093 */ 094 public abstract void update(Schedule schedule); 095 096 /** 097 * Get iterator to all available schedules. 098 * 099 * @return iterator to all available schedules 100 */ 101 public abstract Iterator<Schedule> getAllSchedules(); 102 103 /** 104 * Get an iterator over the schedules handled by this DAO. Implements the Iterable interface. 105 * 106 * @return Iterator of all current schedules. 107 */ 108 public Iterator<Schedule> iterator() { 109 return getAllSchedules(); 110 } 111 112 /** 113 * Get the number of defined schedules. 114 * 115 * @return The number of defined schedules 116 */ 117 public abstract int getCountSchedules(); 118 119 /** 120 * Reset the DAO. Only for use from within tests. 121 */ 122 static void reset() { 123 instance = null; 124 } 125 126}