View Javadoc

1   /*
2    * #%L
3    * Bitrepository Service
4    * %%
5    * Copyright (C) 2010 - 2013 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.service.audit;
23  
24  import static org.bitrepository.service.audit.AuditDatabaseConstants.DATABASE_VERSION_ENTRY;
25  import static org.bitrepository.service.audit.AuditDatabaseConstants.FILE_TABLE;
26  
27  import java.util.Map;
28  
29  import org.bitrepository.service.database.DBConnector;
30  import org.bitrepository.service.database.DatabaseMigrator;
31  import org.bitrepository.service.database.DatabaseUtils;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  /**
36   * Migration class for the AuditTrailContributorDatabase of the ReferencePillar, ChecksumPillar 
37   * and the IntegrityService.
38   * Will only try to perform the migration on an embedded derby database.
39   */
40  public class AuditTrailContributorDatabaseMigrator extends DatabaseMigrator {
41      /** The log.*/
42      private static Logger log = LoggerFactory.getLogger(DatabaseUtils.class);
43      
44      /** The name of the script for updating the database from version 1 to 2.*/
45      private static final String UPDATE_SCRIPT_VERSION_1_TO_2 = "sql/derby/auditContributorDBUpdate1to2.sql";
46      /** The name of the script for updating the database from version 2 to 3.*/
47      private static final String UPDATE_SCRIPT_VERSION_2_TO_3 = "sql/derby/auditContributorDBUpdate2to3.sql";
48      /** The current version of the database. */
49      private final Integer currentVersion = 3;
50      
51      /**
52       * Constructor.
53       * @param connector connection to the database.
54       */
55      public AuditTrailContributorDatabaseMigrator(DBConnector connector) {
56          super(connector);
57      }
58      
59      @Override
60      public void migrate() {
61          Map<String, Integer> versions = getTableVersions();
62          
63          if(!versions.containsKey(FILE_TABLE)) {
64              throw new IllegalStateException("The database does not contain '" + FILE_TABLE 
65                      + "' table as required.");
66          }
67          if(versions.get(FILE_TABLE) == 1) {
68              log.warn("Migrating AuditContributorDB from version 1 to 2.");
69              migrateDerbyDatabase(UPDATE_SCRIPT_VERSION_1_TO_2);
70          }
71          
72          if(!versions.containsKey(DATABASE_VERSION_ENTRY) || versions.get(DATABASE_VERSION_ENTRY) < 3) {
73              log.warn("Migrating AuditContributorDB from version 2 to 3.");
74              migrateDerbyDatabase(UPDATE_SCRIPT_VERSION_2_TO_3);
75          }
76      }
77  
78      @Override
79      public boolean needsMigration() {
80          Map<String, Integer> versions = getTableVersions();
81          
82          if(!versions.containsKey(DATABASE_VERSION_ENTRY)) {
83              // Special case, as the first version of the database did not have DATABASE_VERSION_ENTRY!
84              return true;
85          } else if(versions.get(DATABASE_VERSION_ENTRY) < currentVersion) {
86              return true;
87          } else {
88              return false;
89          }
90      }
91  }