1 package fr.ifremer.tutti.persistence.service;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
33
34
35
36
37 public abstract class UpdateSchemaContextSupport {
38
39
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 }