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 org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029 030import dk.netarkivet.common.exceptions.ArgumentNotValid; 031import dk.netarkivet.common.exceptions.IOFailure; 032import dk.netarkivet.common.exceptions.PermissionDenied; 033import dk.netarkivet.common.exceptions.UnknownID; 034import dk.netarkivet.common.utils.Settings; 035import dk.netarkivet.harvester.HarvesterSettings; 036 037/** 038 * A DAO for reading and writing schedules by name. 039 */ 040public abstract class ScheduleDAO implements DAO, Iterable<Schedule> { 041 042 private static final Logger log = LoggerFactory.getLogger(ScheduleDAO.class); 043 044 /** The singleton instance. */ 045 private static ScheduleDAO instance; 046 047 /** 048 * Constructor made private to enforce singleton. 049 */ 050 protected ScheduleDAO() { 051 } 052 053 /** 054 * Gets the singleton instance of the ScheduleDAO. 055 * 056 * @return ScheduleDAO singleton 057 */ 058 public static synchronized ScheduleDAO getInstance() { 059 if (instance == null) { 060 instance = new ScheduleDBDAO(); 061 } 062 return instance; 063 } 064 065 /** 066 * Create a new schedule. 067 * 068 * @param schedule The schedule to create 069 * @throws ArgumentNotValid if schedule is null 070 * @throws PermissionDenied if a schedule already exists 071 */ 072 public abstract void create(Schedule schedule); 073 074 /** 075 * Returns whether a named schedule exists. 076 * 077 * @param scheduleName The name of a schedule 078 * @return True if the schedule exists. 079 * @throws ArgumentNotValid if the schedulename is null or empty 080 */ 081 public abstract boolean exists(String scheduleName); 082 083 /** 084 * Read an existing schedule. 085 * 086 * @param scheduleName the name of the schedule 087 * @return The schedule read 088 * @throws ArgumentNotValid if schedulename is null or empty 089 * @throws UnknownID if the schedule doesn't exist 090 */ 091 public abstract Schedule read(String scheduleName); 092 093 /** 094 * Update a schedule in the DAO. 095 * 096 * @param schedule The schedule to update 097 * @throws ArgumentNotValid If the schedule is null 098 * @throws UnknownID If the schedule doesn't exist in the DAO 099 * @throws IOFailure If the edition of the schedule to update is older than the DAO's 100 */ 101 public abstract void update(Schedule schedule); 102 103 /** 104 * Get iterator to all available schedules. 105 * 106 * @return iterator to all available schedules 107 */ 108 public abstract Iterator<Schedule> getAllSchedules(); 109 110 /** 111 * Get an iterator over the schedules handled by this DAO. Implements the Iterable interface. 112 * 113 * @return Iterator of all current schedules. 114 */ 115 public Iterator<Schedule> iterator() { 116 return getAllSchedules(); 117 } 118 119 /** 120 * Get the number of defined schedules. 121 * 122 * @return The number of defined schedules 123 */ 124 public abstract int getCountSchedules(); 125 126 /** 127 * Reset the DAO. Only for use from within tests. 128 */ 129 static void reset() { 130 instance = null; 131 } 132 133 /** 134 * Does the schedule represented by HarvesterSettings.DOMAIN_CONFIG_SCHEDULE exist? 135 * @return false if defaultschedule is undefined (empty) or does not exist in the database. 136 */ 137 public boolean existsDefaultSchedule() { 138 String scheduleName = getDefaultScheduleName(); 139 if (scheduleName.isEmpty()) { 140 return false; 141 } else { 142 boolean exists = instance.exists(scheduleName); 143 if (!exists) { 144 log.warn("The default schedule '{}' defined by your setting '{}' does not exist", scheduleName, HarvesterSettings.DOMAIN_CONFIG_SCHEDULE); 145 } 146 return exists; 147 } 148 } 149 150 /** 151 * @return the default schedule 152 */ 153 public String getDefaultScheduleName() { 154 return Settings.get(HarvesterSettings.DOMAIN_CONFIG_SCHEDULE); 155 } 156}