View Javadoc
1   package fr.ifremer.quadrige3.core.service;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Core Shared
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (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 Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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   * <p>DatabaseSchemaServiceImpl class.</p>
43   *
44   * @author Lionel Touseau <lionel.touseau@e-is.pro>
45   */
46  @Service("databaseSchemaService")
47  public class DatabaseSchemaServiceImpl implements DatabaseSchemaService {
48  
49      /** Logger. */
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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
78      @Override
79      public Version getApplicationVersion() {
80          return  databaseSchemaDao.getSchemaVersionIfUpdate();
81      }
82  
83      /** {@inheritDoc} */
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      /** {@inheritDoc} */
94      @Override
95      public boolean isDbLoaded() {
96          return databaseSchemaDao.isDbLoaded();
97      }
98      
99      /** {@inheritDoc} */
100     @Override
101     public boolean isDbExists() {    	
102         return databaseSchemaDao.isDbExists();
103     }
104     
105     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
139     @Override
140     public void createSchemaToFile(File outputFile, boolean withDrop) throws IOException {
141         databaseSchemaDao.generateCreateSchemaFile(outputFile.getCanonicalPath(), false, withDrop, true);
142     }
143 }