1 package fr.ifremer.quadrige3.core.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 fr.ifremer.quadrige3.core.config.QuadrigeConfiguration;
28 import fr.ifremer.quadrige3.core.exception.QuadrigeTechnicalException;
29 import fr.ifremer.quadrige3.core.dao.technical.DatabaseSchemaDao;
30 import fr.ifremer.quadrige3.core.exception.DatabaseSchemaUpdateException;
31 import fr.ifremer.quadrige3.core.exception.VersionNotFoundException;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.nuiton.version.Version;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Service;
37
38 import java.io.File;
39 import java.io.IOException;
40
41
42
43
44
45
46 @Service("databaseSchemaService")
47 public class DatabaseSchemaServiceImpl implements DatabaseSchemaService {
48
49
50 private static final Log log =
51 LogFactory.getLog(DatabaseSchemaServiceImpl.class);
52
53 @Autowired
54 protected QuadrigeConfiguration config;
55
56 @Autowired
57 protected DatabaseSchemaDao databaseSchemaDao;
58
59
60 @Override
61 public Version getDbVersion() {
62 Version version;
63 try {
64 if (!isDbLoaded()) {
65 throw new VersionNotFoundException("db is not open");
66 }
67 version = databaseSchemaDao.getSchemaVersion();
68 } catch (VersionNotFoundException e) {
69 if (log.isWarnEnabled()) {
70 log.warn(e.getMessage());
71 }
72 version = null;
73 }
74 return version;
75 }
76
77
78 @Override
79 public Version getApplicationVersion() {
80 return databaseSchemaDao.getSchemaVersionIfUpdate();
81 }
82
83
84 @Override
85 public void updateSchema() {
86 try {
87 databaseSchemaDao.updateSchema();
88 } catch (DatabaseSchemaUpdateException e) {
89 throw new QuadrigeTechnicalException(e.getCause());
90 }
91 }
92
93
94 @Override
95 public boolean isDbLoaded() {
96 return databaseSchemaDao.isDbLoaded();
97 }
98
99
100 @Override
101 public boolean isDbExists() {
102 return databaseSchemaDao.isDbExists();
103 }
104
105
106 @Override
107 public void generateStatusReport(File outputFile) throws IOException {
108 if (outputFile == null || !outputFile.getParentFile().isDirectory() || !outputFile.canWrite()) {
109 log.error("Could not write into the output file. Please make sure the given path is a valid path.");
110 return;
111 }
112
113 databaseSchemaDao.generateStatusReport(outputFile);
114 }
115
116
117 @Override
118 public void generateDiffReport(File outputFile) {
119 if (outputFile == null || !outputFile.getParentFile().isDirectory() || !outputFile.canWrite()) {
120 log.error("Could not write into the output file. Please make sure the given path is a valid path.");
121 return;
122 }
123 databaseSchemaDao.generateDiffReport(outputFile, config.getLiquibaseDiffTypes());
124 }
125
126
127 @Override
128 public void generateDiffChangeLog(File outputFile) {
129 if (outputFile == null
130 || !outputFile.getParentFile().isDirectory()
131 || (outputFile.exists() && !outputFile.canWrite())) {
132 log.error("Could not write into the output file. Please make sure the given path is a valid path.");
133 return;
134 }
135 databaseSchemaDao.generateDiffChangeLog(outputFile, config.getLiquibaseDiffTypes());
136 }
137
138
139 @Override
140 public void createSchemaToFile(File outputFile, boolean withDrop) throws IOException {
141 databaseSchemaDao.generateCreateSchemaFile(outputFile.getCanonicalPath(), false, withDrop, true);
142 }
143 }