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;
23  
24  import java.util.Date;
25  
26  import javax.xml.datatype.XMLGregorianCalendar;
27  
28  import org.bitrepository.common.ArgumentValidator;
29  import org.bitrepository.common.settings.Settings;
30  import org.bitrepository.pillar.cache.database.ChecksumExtractor;
31  import org.bitrepository.pillar.cache.database.ChecksumIngestor;
32  import org.bitrepository.pillar.cache.database.ExtractedChecksumResultSet;
33  import org.bitrepository.pillar.cache.database.ExtractedFileIDsResultSet;
34  import org.bitrepository.service.database.DBConnector;
35  import org.bitrepository.service.database.DatabaseManager;
36  
37  /**
38   * The checksum store backed by a database.
39   * This contains one ingestor and one extractor for each collec
40   */
41  public class ChecksumDAO implements ChecksumStore {
42      /** The ingestors for the database.*/
43      private final ChecksumIngestor ingestor;
44      /** The extractor for the database.*/
45      private final ChecksumExtractor extractor;
46      /** The connector for the database.*/
47      private final DBConnector connector;
48      
49      /**
50       * Constructor.
51       * @param settings The settings.
52       */
53      public ChecksumDAO(DatabaseManager databaseManager) {
54          synchronized(this) {
55              connector = databaseManager.getConnector();
56              this.ingestor = new ChecksumIngestor(connector);
57              this.extractor = new ChecksumExtractor(connector);
58          }
59      }
60      
61      @Override
62      public void insertChecksumCalculation(String fileId, String collectionId, String checksum, Date calculationDate) {
63          ArgumentValidator.checkNotNull(fileId, "String fileId");
64          ArgumentValidator.checkNotNull(collectionId, "String collectionId");
65          
66          if(extractor.hasFile(fileId, collectionId)) {
67              ingestor.updateEntry(fileId, collectionId, checksum, calculationDate);
68          } else {
69              ingestor.insertNewEntry(fileId, collectionId, checksum, calculationDate);
70          }
71      }
72  
73      @Override
74      public void deleteEntry(String fileId, String collectionId) {
75          ArgumentValidator.checkNotNull(fileId, "String fileId");
76          ArgumentValidator.checkNotNull(collectionId, "String collectionId");
77  
78          if(!extractor.hasFile(fileId, collectionId)) {
79              throw new IllegalStateException("No entry for file '" + fileId + "' to delete.");
80          }
81          ingestor.removeEntry(fileId, collectionId);
82      }
83  
84      @Override
85      public ChecksumEntry getEntry(String fileId, String collectionId) {
86          ArgumentValidator.checkNotNull(fileId, "String fileId");
87          ArgumentValidator.checkNotNull(collectionId, "String collectionId");
88          
89          return extractor.extractSingleEntry(fileId, collectionId);
90      }
91      
92      @Override
93      public ExtractedChecksumResultSet getEntries(XMLGregorianCalendar minTimeStamp, XMLGregorianCalendar maxTimeStamp, 
94              Long maxNumberOfResults, String collectionId) {
95          ArgumentValidator.checkNotNull(collectionId, "String collectionId");
96          return extractor.extractEntries(minTimeStamp, maxTimeStamp, maxNumberOfResults, collectionId);
97      }
98      
99      @Override
100     public Date getCalculationDate(String fileId, String collectionId) {
101         ArgumentValidator.checkNotNull(fileId, "String fileId");
102         ArgumentValidator.checkNotNull(collectionId, "String collectionId");
103 
104         Date res = extractor.extractDateForFile(fileId, collectionId);
105         if(res == null) {
106             throw new IllegalStateException("No entry for file '" + fileId + "' to delete.");
107         }
108         return res;
109         
110     }
111     
112     @Override
113     public ExtractedFileIDsResultSet getFileIDs(XMLGregorianCalendar minTimeStamp, XMLGregorianCalendar maxTimeStamp, 
114             Long maxNumberOfResults, String collectionId) {
115         ArgumentValidator.checkNotNull(collectionId, "String collectionId");
116         return extractor.getFileIDs(minTimeStamp, maxTimeStamp, maxNumberOfResults, collectionId);
117     }
118 
119     @Override
120     public boolean hasFile(String fileId, String collectionId) {
121         ArgumentValidator.checkNotNull(fileId, "String fileId");
122         ArgumentValidator.checkNotNull(collectionId, "String collectionId");
123 
124         return extractor.hasFile(fileId, collectionId);
125     }
126 
127     @Override
128     public String getChecksum(String fileId, String collectionId) {
129         ArgumentValidator.checkNotNull(fileId, "String fileId");
130         ArgumentValidator.checkNotNull(collectionId, "String collectionId");
131 
132         String res = extractor.extractChecksumForFile(fileId, collectionId);
133         if(res == null) {
134             throw new IllegalStateException("No entry for file '" + fileId + "' to delete.");
135         }
136         return res;
137     }
138 
139     @Override
140     public java.util.Collection<String> getAllFileIDs(String collectionId) {
141         ArgumentValidator.checkNotNull(collectionId, "String collectionId");
142         return extractor.extractAllFileIDs(collectionId);
143     }
144 
145     @Override
146     public void close() {
147         connector.destroy();
148     }
149 }