View Javadoc

1   /*
2    * #%L
3    * Bitrepository Audit Trail 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.audittrails.store;
23  
24  import static org.bitrepository.audittrails.store.AuditDatabaseConstants.ACTOR_TABLE;
25  import static org.bitrepository.audittrails.store.AuditDatabaseConstants.AUDITTRAIL_TABLE;
26  import static org.bitrepository.audittrails.store.AuditDatabaseConstants.CONTRIBUTOR_TABLE;
27  import static org.bitrepository.audittrails.store.AuditDatabaseConstants.DATABASE_VERSION_ENTRY;
28  import static org.bitrepository.audittrails.store.AuditDatabaseConstants.FILE_TABLE;
29  
30  import java.util.Map;
31  
32  import org.bitrepository.service.database.DBConnector;
33  import org.bitrepository.service.database.DatabaseMigrator;
34  import org.bitrepository.service.database.DatabaseUtils;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  /**
39   * Migration class for the AuditTrailDatabase of the AuditTrailService.
40   * Will only try to perform the migration on an embedded derby database.
41   */
42  public class AuditTrailServiceDatabaseMigrator extends DatabaseMigrator {
43      /** The log.*/
44      private static Logger log = LoggerFactory.getLogger(DatabaseUtils.class);
45      
46      /** The name of the update script for version 1 to 2.*/
47      private static final String UPDATE_SCRIPT_VERSION_1_TO_2 = "sql/derby/auditTrailServiceDBUpdate1to2.sql";
48      /** The name of the update script for version 2 to 3.*/
49      private static final String UPDATE_SCRIPT_VERSION_2_TO_3 = "sql/derby/auditTrailServiceDBUpdate2to3.sql";
50      /** The name of the update script for version 3 to 4.*/
51      private static final String UPDATE_SCRIPT_VERSION_3_TO_4 = "sql/derby/auditTrailServiceDBUpdate3to4.sql";
52      /** The current version of the database. */
53      private final Integer currentVersion = 4;
54      
55      /**
56       * Constructor.
57       * @param connector connection to the database.
58       */
59      public AuditTrailServiceDatabaseMigrator(DBConnector connector) {
60          super(connector);
61      }
62      
63      @Override
64      public void migrate() {
65          Map<String, Integer> versions = getTableVersions();
66          
67          if(!versions.containsKey(FILE_TABLE)) {
68              throw new IllegalStateException("The database does not contain '" + FILE_TABLE 
69                      + "' table as required.");
70          }
71          if(!versions.containsKey(AUDITTRAIL_TABLE)) {
72              throw new IllegalStateException("The database does not contain '" + AUDITTRAIL_TABLE 
73                      + "' table as required.");
74          }
75          if(!versions.containsKey(CONTRIBUTOR_TABLE)) {
76              throw new IllegalStateException("The database does not contain '" + CONTRIBUTOR_TABLE 
77                      + "' table as required.");
78          }
79          if(!versions.containsKey(ACTOR_TABLE)) {
80              throw new IllegalStateException("The database does not contain '" + ACTOR_TABLE 
81                      + "' table as required.");
82          }
83          
84          if(!versions.containsKey(DATABASE_VERSION_ENTRY) || versions.get(DATABASE_VERSION_ENTRY) < 2) {
85              log.warn("Migrating AuditServiceDB from version 1 to 2.");
86              migrateDerbyDatabase(UPDATE_SCRIPT_VERSION_1_TO_2);
87          }
88          if(!versions.containsKey(DATABASE_VERSION_ENTRY) || versions.get(DATABASE_VERSION_ENTRY) < 3) {
89              log.warn("Migrating AuditServiceDB from version 2 to 3.");
90              migrateDerbyDatabase(UPDATE_SCRIPT_VERSION_2_TO_3);
91          }
92          if(!versions.containsKey(DATABASE_VERSION_ENTRY) || versions.get(DATABASE_VERSION_ENTRY) < 4) {
93              log.warn("Migrating AuditServiceDB from version 3 to 4.");
94              migrateDerbyDatabase(UPDATE_SCRIPT_VERSION_3_TO_4);
95          }
96      }
97  
98      @Override
99      public boolean needsMigration() {
100         Map<String, Integer> versions = getTableVersions();
101         
102         if(!versions.containsKey(DATABASE_VERSION_ENTRY)) {
103             throw new IllegalStateException("The database does not contain '" + DATABASE_VERSION_ENTRY 
104                     + "' table as required.");
105         }
106         
107         if(versions.get(DATABASE_VERSION_ENTRY) < currentVersion) {
108             return true;
109         } else {
110             return false;
111         }
112     }
113 }