View Javadoc
1   package net.sumaris.core.service.schema;
2   
3   /*-
4    * #%L
5    * SUMARiS :: Sumaris Core Shared
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2018 SUMARiS Consortium
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
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   * <p>DatabaseSchemaServiceImpl class.</p>
44   *
45   * @author Lionel Touseau <lionel.touseau@e-is.pro>
46   */
47  @Service("databaseSchemaService")
48  public class DatabaseSchemaServiceImpl implements DatabaseSchemaService {
49  
50      /** Logger. */
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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
79      @Override
80      public Version getApplicationVersion() {
81          return  databaseSchemaDao.getSchemaVersionIfUpdate();
82      }
83  
84      /** {@inheritDoc} */
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      /** {@inheritDoc} */
95      @Override
96      public boolean isDbLoaded() {
97          return databaseSchemaDao.isDbLoaded();
98      }
99      
100     /** {@inheritDoc} */
101     @Override
102     public boolean isDbExists() {    	
103         return databaseSchemaDao.isDbExists();
104     }
105     
106     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
140     @Override
141     public void createSchemaToFile(File outputFile, boolean withDrop) throws IOException {
142         databaseSchemaDao.generateCreateSchemaFile(outputFile.getCanonicalPath(), false, withDrop, true);
143     }
144 
145     /** {@inheritDoc} */
146     @Override
147     public void updateSchemaToFile(File outputFile) throws IOException {
148         databaseSchemaDao.generateUpdateSchemaFile(outputFile.getCanonicalPath());
149     }
150 }