1 package fr.ifremer.tutti.persistence.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 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
46
47
48
49
50 @Service("technicalPersistenceService")
51 public class TechnicalPersistenceServiceImpl extends AbstractPersistenceService implements TechnicalPersistenceService {
52
53
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 }