View Javadoc

1   /*
2    * #%L
3    * Bitrepository Reference Pillar
4    * %%
5    * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 2.1 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  package org.bitrepository.pillar.cache.database;
23  
24  import static org.bitrepository.pillar.cache.database.DatabaseConstants.CHECKSUM_TABLE;
25  import static org.bitrepository.pillar.cache.database.DatabaseConstants.CS_FILE_ID;
26  import static org.bitrepository.pillar.cache.database.DatabaseConstants.CS_CHECKSUM;
27  import static org.bitrepository.pillar.cache.database.DatabaseConstants.CS_DATE;
28  import static org.bitrepository.pillar.cache.database.DatabaseConstants.CS_COLLECTION_ID;
29  
30  import java.util.Date;
31  
32  import org.bitrepository.common.ArgumentValidator;
33  import org.bitrepository.service.database.DBConnector;
34  import org.bitrepository.service.database.DatabaseUtils;
35  
36  /**
37   * Ingests data to the checksum database. And also deals with the deletion of entries.
38   */
39  public class ChecksumIngestor {
40      /** The connector for the database.*/
41      private final DBConnector connector;
42      
43      /**
44       * Constructor.
45       * @param connector The connector for the database.
46       */
47      public ChecksumIngestor(DBConnector connector) {
48          ArgumentValidator.checkNotNull(connector, "DBConnector connector");
49          
50          this.connector = connector;
51      }
52      
53      /**
54       * Inserts a new entry into the database.
55       * @param fileId The id of the file for the entry.
56       * @param collectionId The id of the collection of the file.
57       * @param checksum The checksum of the file.
58       * @param date The calculation timestamp for the file.
59       */
60      public synchronized void insertNewEntry(String fileId, String collectionId, String checksum, Date date) {
61          String sql = "INSERT INTO " + CHECKSUM_TABLE + " ( " + CS_FILE_ID + " , " + CS_CHECKSUM + " , " + CS_DATE 
62                  + " , " + CS_COLLECTION_ID + " ) VALUES ( ? , ? , ? , ? )";
63          DatabaseUtils.executeStatement(connector, sql, fileId, checksum, date, collectionId);
64      }
65      
66      /**
67       * Updates an existing entry in the database.
68       * @param fileId The id of the file to update.
69       * @param collectionId The id of the collection of the file.
70       * @param checksum The new checksum for the file.
71       * @param date The new date for the calculation of the checksum of the file.
72       */
73      public void updateEntry(String fileId, String collectionId, String checksum, Date date) {
74          String sql = "UPDATE " + CHECKSUM_TABLE + " SET " + CS_CHECKSUM + " = ? , " + CS_DATE + " = ? WHERE " 
75                  + CS_FILE_ID + " = ? AND " + CS_COLLECTION_ID + " = ?";
76          DatabaseUtils.executeStatement(connector, sql, checksum, date, fileId, collectionId);
77      }
78      
79      /**
80       * Removes an entry from the database.
81       * @param fileId The id of the file whose entry should be removed.
82       * @param collectionId The id of the collection of the file.
83       */
84      public void removeEntry(String fileId, String collectionId) {
85          String sql = "DELETE FROM " + CHECKSUM_TABLE + " WHERE " + CS_FILE_ID + " = ? AND " + CS_COLLECTION_ID + " = ?";
86          DatabaseUtils.executeStatement(connector, sql, fileId, collectionId);
87      }
88  }