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.extendedfield; 025 026import java.sql.Connection; 027import java.sql.PreparedStatement; 028import java.sql.ResultSet; 029import java.sql.SQLException; 030import java.util.LinkedList; 031import java.util.List; 032 033import org.slf4j.Logger; 034import org.slf4j.LoggerFactory; 035 036import dk.netarkivet.common.exceptions.ArgumentNotValid; 037import dk.netarkivet.common.exceptions.IOFailure; 038import dk.netarkivet.common.exceptions.UnknownID; 039import dk.netarkivet.common.utils.DBUtils; 040import dk.netarkivet.harvester.datamodel.HarvestDBConnection; 041import dk.netarkivet.harvester.datamodel.HarvesterDatabaseTables; 042 043/** 044 * Implementation of the ExtendedFieldTypeDAO interface for creating and accessing extended fields in persistent 045 * storage. 046 */ 047public class ExtendedFieldTypeDBDAO extends ExtendedFieldTypeDAO { 048 049 /** The logger for this class. */ 050 private static final Logger log = LoggerFactory.getLogger(ExtendedFieldTypeDBDAO.class); 051 052 /** 053 * Default constructor of this class. Tries to make any necessary migration of the database. 054 */ 055 protected ExtendedFieldTypeDBDAO() { 056 Connection connection = HarvestDBConnection.get(); 057 try { 058 HarvesterDatabaseTables.checkVersion(connection, HarvesterDatabaseTables.EXTENDEDFIELDTYPE); 059 HarvesterDatabaseTables.checkVersion(connection, HarvesterDatabaseTables.EXTENDEDFIELD); 060 HarvesterDatabaseTables.checkVersion(connection, HarvesterDatabaseTables.EXTENDEDFIELDVALUE); 061 } finally { 062 HarvestDBConnection.release(connection); 063 } 064 } 065 066 @Override 067 public boolean exists(Long aExtendedfieldtypeId) { 068 ArgumentNotValid.checkNotNull(aExtendedfieldtypeId, "Long aExtendedfieldtypeId"); 069 070 Connection c = HarvestDBConnection.get(); 071 try { 072 return exists(c, aExtendedfieldtypeId); 073 } finally { 074 HarvestDBConnection.release(c); 075 } 076 077 } 078 079 /** 080 * Tests if exists an ExtendedFieldType with the given ID. 081 * 082 * @param c an open connection to the database 083 * @param aExtendedfieldtypeId an id belonging to a ExtendedFieldType 084 * @return true, if there exists an ExtendedFieldType with the given ID, otherwise returns false. 085 */ 086 private synchronized boolean exists(Connection c, Long aExtendedfieldtypeId) { 087 return 1 == DBUtils.selectLongValue(c, "SELECT COUNT(*) FROM extendedfieldtype " 088 + "WHERE extendedfieldtype_id = ?", aExtendedfieldtypeId); 089 } 090 091 @Override 092 public synchronized ExtendedFieldType read(Long aExtendedfieldtypeId) { 093 ArgumentNotValid.checkNotNull(aExtendedfieldtypeId, "aExtendedfieldtypeId"); 094 Connection connection = HarvestDBConnection.get(); 095 try { 096 return read(connection, aExtendedfieldtypeId); 097 } finally { 098 HarvestDBConnection.release(connection); 099 } 100 } 101 102 /** 103 * Read an ExtendedFieldType from database belonging to the given id. 104 * 105 * @param connection an open connection to the database 106 * @param aExtendedfieldtypeId an id belonging to a ExtendedFieldType 107 * @return an ExtendedFieldType from database belonging to the given id. 108 */ 109 private synchronized ExtendedFieldType read(Connection connection, Long aExtendedfieldtypeId) { 110 if (!exists(connection, aExtendedfieldtypeId)) { 111 throw new UnknownID("Extended FieldType id " + aExtendedfieldtypeId + " is not known in persistent storage"); 112 } 113 114 ExtendedFieldType extendedFieldType = null; 115 PreparedStatement statement = null; 116 try { 117 statement = connection 118 .prepareStatement("SELECT name FROM extendedfieldtype WHERE extendedfieldtype_id = ?"); 119 120 statement.setLong(1, aExtendedfieldtypeId); 121 ResultSet result = statement.executeQuery(); 122 result.next(); 123 124 String name = result.getString(1); 125 126 extendedFieldType = new ExtendedFieldType(aExtendedfieldtypeId, name); 127 128 return extendedFieldType; 129 } catch (SQLException e) { 130 String message = "SQL error reading extended Field " + aExtendedfieldtypeId + " in database" + "\n"; 131 log.warn(message, e); 132 throw new IOFailure(message, e); 133 } 134 } 135 136 @Override 137 public synchronized List<ExtendedFieldType> getAll() { 138 Connection c = HarvestDBConnection.get(); 139 try { 140 List<Long> idList = DBUtils.selectLongList(c, "SELECT extendedfieldtype_id FROM extendedfieldtype"); 141 List<ExtendedFieldType> extendedFieldTypes = new LinkedList<ExtendedFieldType>(); 142 for (Long extendedfieldtypeId : idList) { 143 extendedFieldTypes.add(read(c, extendedfieldtypeId)); 144 } 145 return extendedFieldTypes; 146 } finally { 147 HarvestDBConnection.release(c); 148 } 149 } 150 151}