View Javadoc
1   package fr.ifremer.tutti.persistence.service;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.base.Preconditions;
26  import fr.ifremer.adagio.core.dao.referential.StatusCode;
27  import fr.ifremer.adagio.core.dao.technical.DatabaseSchemaDao;
28  import fr.ifremer.adagio.core.dao.technical.DatabaseSchemaUpdateException;
29  import fr.ifremer.adagio.core.dao.technical.VersionNotFoundException;
30  import fr.ifremer.adagio.core.dao.technical.hibernate.TemporaryDataHelper;
31  import fr.ifremer.adagio.core.service.technical.CacheService;
32  import fr.ifremer.adagio.core.service.technical.sanity.DatabaseSanityService;
33  import fr.ifremer.tutti.persistence.entities.referential.TuttiReferentialEntity;
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.nuiton.jaxx.application.ApplicationTechnicalException;
37  import org.nuiton.version.Version;
38  import org.nuiton.version.Versions;
39  import org.springframework.stereotype.Service;
40  
41  import javax.annotation.Resource;
42  import java.util.concurrent.Callable;
43  
44  /**
45   * Created on 4/20/14.
46   *
47   * @author Tony Chemit - chemit@codelutin.com
48   * @since 3.5
49   */
50  @Service("technicalPersistenceService")
51  public class TechnicalPersistenceServiceImpl extends AbstractPersistenceService implements TechnicalPersistenceService {
52  
53      /** Logger. */
54      private static final Log log = LogFactory.getLog(TechnicalPersistenceServiceImpl.class);
55  
56      @Resource(name = "databaseSanityService")
57      protected DatabaseSanityService databaseSanityService;
58  
59      @Resource(name = "cacheService")
60      protected CacheService cacheService;
61  
62      @Resource(name = "databaseSchemaDao")
63      protected DatabaseSchemaDao databaseSchemaDao;
64  
65      @Override
66      public <V> V invoke(Callable<V> call) {
67  
68          try {
69              return call.call();
70          } catch (RuntimeException e) {
71              throw e;
72          } catch (Exception e) {
73              throw new ApplicationTechnicalException(e);
74          }
75      }
76  
77      @Override
78      public Version getSchemaVersion() {
79          Object version;
80          try {
81              version = databaseSchemaDao.getSchemaVersion().getVersion();
82          } catch (VersionNotFoundException e) {
83              if (log.isErrorEnabled()) {
84                  log.error("Could not find db version", e);
85              }
86              version = null;
87          }
88          return version == null ? null : Versions.valueOf(version.toString());
89      }
90  
91      @Override
92      public Version getSchemaVersionIfUpdate() {
93          Object version = databaseSchemaDao.getSchemaVersionIfUpdate();
94          return Versions.valueOf(version.toString());
95      }
96  
97      @Override
98      public <U extends UpdateSchemaContextSupport> void prepareUpdateSchemaContext(U context) {
99          context.init(getSchemaVersion(), getSchemaVersionIfUpdate());
100     }
101 
102     @Override
103     public void updateSchema() {
104         try {
105             databaseSchemaDao.updateSchema();
106         } catch (DatabaseSchemaUpdateException e) {
107             throw new ApplicationTechnicalException(e.getCause());
108         }
109     }
110 
111     @Override
112     public void sanityDb() {
113         databaseSanityService.sanity();
114     }
115 
116     @Override
117     public void clearAllCaches() {
118         cacheService.clearAllCaches();
119     }
120 
121     @Override
122     public boolean isTemporary(TuttiReferentialEntity entity) {
123         Preconditions.checkNotNull(entity);
124         Preconditions.checkNotNull(entity.getId());
125         Preconditions.checkNotNull(entity.getStatus());
126 
127         return StatusCode.TEMPORARY.getValue().equals(entity.getStatus().getId()) &&
128                (entity.getIdAsInt() != null && entity.getIdAsInt() < 0
129                 || entity.getId() != null && entity.getId().startsWith(TemporaryDataHelper.TEMPORARY_NAME_PREFIX));
130     }
131 
132 }