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