View Javadoc
1   package fr.ifremer.tutti.persistence.service;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.nuiton.version.Version;
30  
31  /**
32   * Created on 1/12/15.
33   *
34   * @author Tony Chemit - chemit@codelutin.com
35   * @since 3.12
36   */
37  public abstract class UpdateSchemaContextSupport {
38  
39      /** Logger. */
40      private static final Log log = LogFactory.getLog(UpdateSchemaContextSupport.class);
41  
42      private Version schemaVersion;
43  
44      private Version schemaVersionIfUpdate;
45  
46      private boolean willUpdate;
47  
48      public Version getSchemaVersion() {
49          return schemaVersion;
50      }
51  
52      public Version getSchemaVersionIfUpdate() {
53          return schemaVersionIfUpdate;
54      }
55  
56      public boolean isNeedUpdate() {
57          return schemaVersion.compareTo(schemaVersionIfUpdate) < 0;
58      }
59  
60      public boolean isSchemaVersionTooHigh() {
61          return schemaVersion.compareTo(schemaVersionIfUpdate) > 0;
62      }
63  
64      public boolean isWillUpdate() {
65          return willUpdate;
66      }
67  
68      protected abstract boolean askUserToMigrate(Version schemaVersion, Version schemaVersionIfUpdate);
69  
70      public void init(Version schemaVersion, Version schemaVersionIfUpdate) {
71  
72          this.schemaVersion = schemaVersion;
73  
74          if (log.isInfoEnabled()) {
75              log.info(String.format("Detected schema version: %s", schemaVersion));
76          }
77  
78          this.schemaVersionIfUpdate = schemaVersionIfUpdate;
79  
80          if (log.isInfoEnabled()) {
81              log.info(String.format("Detected schema version if update: %s", schemaVersionIfUpdate));
82          }
83  
84          if (isSchemaVersionTooHigh()) {
85  
86              if (log.isInfoEnabled()) {
87                  log.info(String.format("Schema version %s is higher than incoming schema version %s, can't update!", schemaVersion, schemaVersionIfUpdate));
88              }
89  
90          } else if (isNeedUpdate()) {
91  
92              if (log.isInfoEnabled()) {
93                  log.info(String.format("Schema version %s is smaller than incoming schema version %s, ask to update", schemaVersion, schemaVersionIfUpdate));
94              }
95              willUpdate = askUserToMigrate(schemaVersion, schemaVersionIfUpdate);
96  
97              if (log.isInfoEnabled()) {
98                  log.info("Should we update? " + willUpdate);
99              }
100 
101         } else {
102 
103             if (log.isInfoEnabled()) {
104                 log.info(String.format("Schema version %s is up-to-date, no need to update.", schemaVersion));
105             }
106 
107         }
108 
109     }
110 
111 }