1 package fr.ifremer.quadrige3.core.action;
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 fr.ifremer.quadrige3.core.config.QuadrigeConfiguration;
28 import fr.ifremer.quadrige3.core.exception.QuadrigeTechnicalException;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.nuiton.version.Version;
32
33 import fr.ifremer.quadrige3.core.dao.technical.Daos;
34 import fr.ifremer.quadrige3.core.service.DatabaseSchemaService;
35 import fr.ifremer.quadrige3.core.service.ServiceLocator;
36
37
38
39
40
41 public class DatabaseUpdateSchemaAction {
42
43 private static final Log log = LogFactory.getLog(DatabaseUpdateSchemaAction.class);
44
45
46
47
48 public void run() {
49 QuadrigeConfiguration config = QuadrigeConfiguration.getInstance();
50
51 if (log.isInfoEnabled()) {
52 log.info("Starting database schema update...");
53 }
54 ActionUtils.logConnectionProperties();
55
56 boolean isValidConnection = Daos.isValidConnectionProperties(config.getJdbcDriver(),
57 config.getJdbcURL(),
58 config.getJdbcUsername(),
59 config.getJdbcPassword());
60
61 if (!isValidConnection) {
62 log.warn("Connection error: could not update the schema.");
63 return;
64 }
65 DatabaseSchemaService databaseSchemaService = ServiceLocator.instance().getService("databaseSchemaService", DatabaseSchemaService.class);
66
67
68 if (!databaseSchemaService.isDbLoaded()) {
69 log.warn("Database not start ! Could not update the schema.");
70 return;
71 }
72
73
74 try {
75 Version actualDbVersion = databaseSchemaService.getDbVersion();
76
77 if (actualDbVersion == null) {
78 log.warn("Could not get database schema version");
79 }
80 else {
81 log.info(String.format("Database schema version is [%s]", actualDbVersion));
82 }
83
84 } catch (QuadrigeTechnicalException e) {
85 log.error("Error while getting database version.", e);
86 }
87
88
89 try {
90 Version expectedDbVersion = databaseSchemaService.getApplicationVersion();
91 if (expectedDbVersion == null) {
92 log.warn("Unable to get the database schema version AFTER the update. Nothing to update !");
93 return;
94 }
95 log.info(String.format("Database schema version AFTER the update should be [%s]", expectedDbVersion));
96 } catch (QuadrigeTechnicalException e) {
97 log.error("Error while getting database version AFTER the update.", e);
98 }
99
100
101 try {
102 log.info("Launching update...");
103 databaseSchemaService.updateSchema();
104 log.info("Database schema successfully updated.");
105 } catch (QuadrigeTechnicalException e) {
106 log.error("Error while updating the database schema.", e);
107 }
108 }
109 }