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 fr.ifremer.quadrige3.core.dao.technical.Daos;
30 import fr.ifremer.quadrige3.core.service.DatabaseSchemaService;
31 import fr.ifremer.quadrige3.core.service.ServiceLocator;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.nuiton.version.Version;
35
36 import java.io.File;
37 import java.io.IOException;
38
39
40
41
42 public class DatabaseStatusAction {
43
44 private static final Log log = LogFactory.getLog(DatabaseStatusAction.class);
45
46
47
48
49 public void run() {
50 QuadrigeConfiguration config = QuadrigeConfiguration.getInstance();
51
52 if (log.isInfoEnabled()) {
53 log.info("Starting database status report...");
54 }
55 ActionUtils.logConnectionProperties();
56
57 boolean isValidConnection = Daos.isValidConnectionProperties(config.getJdbcDriver(),
58 config.getJdbcURL(),
59 config.getJdbcUsername(),
60 config.getJdbcPassword());
61
62 if (!isValidConnection) {
63 log.warn("Connection error : could not check database status.");
64 return;
65 }
66 DatabaseSchemaService databaseSchemaService = ServiceLocator.instance().getService("databaseSchemaService", DatabaseSchemaService.class);
67
68
69 if (!databaseSchemaService.isDbLoaded()) {
70 log.warn("Database not start ! Could not check database status.");
71 return;
72 }
73
74 try {
75 Version actualDbVersion = databaseSchemaService.getDbVersion();
76 if (actualDbVersion != null) {
77 log.info("Database schema version is: " + actualDbVersion.toString());
78 }
79
80 Version expectedDbVersion = databaseSchemaService.getApplicationVersion();
81 log.info("Database schema version AFTER an update should be: " + expectedDbVersion.toString());
82 } catch (QuadrigeTechnicalException e) {
83 log.error("Error while getting versions.", e);
84 }
85
86 File outputFile = ActionUtils.checkAndGetOutputFile(false, this.getClass());
87
88 try {
89 log.info("Launching status report generation...");
90 databaseSchemaService.generateStatusReport(outputFile);
91 if (outputFile != null) {
92 log.info(String.format("Database status report successfully generated at %s", outputFile));
93 }
94 else {
95 log.info("Database status report successfully generated.");
96 }
97 } catch (QuadrigeTechnicalException | IOException e) {
98 log.error("Error while writing database status report.", e);
99 }
100 }
101
102 }