| ... |
... |
@@ -24,8 +24,24 @@ package fr.ird.observe; |
|
24
|
24
|
|
|
25
|
25
|
import com.google.common.collect.ImmutableSet;
|
|
26
|
26
|
import fr.ird.observe.entities.Entities;
|
|
27
|
|
-import fr.ird.observe.entities.migration.ObserveMigrationConfigurationProvider;
|
|
28
|
27
|
import fr.ird.observe.entities.migration.ObserveMigrationEngine;
|
|
|
28
|
+import java.io.BufferedInputStream;
|
|
|
29
|
+import java.io.File;
|
|
|
30
|
+import java.io.FileInputStream;
|
|
|
31
|
+import java.io.FileNotFoundException;
|
|
|
32
|
+import java.io.IOException;
|
|
|
33
|
+import java.io.InputStream;
|
|
|
34
|
+import java.nio.file.Files;
|
|
|
35
|
+import java.nio.file.Path;
|
|
|
36
|
+import java.util.EnumSet;
|
|
|
37
|
+import java.util.LinkedHashSet;
|
|
|
38
|
+import java.util.Objects;
|
|
|
39
|
+import java.util.Set;
|
|
|
40
|
+import java.util.UUID;
|
|
|
41
|
+import java.util.stream.Collectors;
|
|
|
42
|
+import java.util.stream.Stream;
|
|
|
43
|
+import java.util.zip.GZIPInputStream;
|
|
|
44
|
+import org.apache.commons.io.IOUtils;
|
|
29
|
45
|
import org.apache.commons.logging.Log;
|
|
30
|
46
|
import org.apache.commons.logging.LogFactory;
|
|
31
|
47
|
import org.hibernate.HibernateException;
|
| ... |
... |
@@ -43,17 +59,6 @@ import org.nuiton.topia.service.sql.batch.tables.TopiaSqlTables; |
|
43
|
59
|
import org.nuiton.topia.service.sql.batch.tables.TopiaSqlTablesFactory;
|
|
44
|
60
|
import org.nuiton.util.StringUtil;
|
|
45
|
61
|
|
|
46
|
|
-import java.io.IOException;
|
|
47
|
|
-import java.nio.file.Files;
|
|
48
|
|
-import java.nio.file.Path;
|
|
49
|
|
-import java.util.EnumSet;
|
|
50
|
|
-import java.util.LinkedHashSet;
|
|
51
|
|
-import java.util.Objects;
|
|
52
|
|
-import java.util.Set;
|
|
53
|
|
-import java.util.UUID;
|
|
54
|
|
-import java.util.stream.Collectors;
|
|
55
|
|
-import java.util.stream.Stream;
|
|
56
|
|
-
|
|
57
|
62
|
public class ObserveTopiaApplicationContext extends AbstractObserveTopiaApplicationContext implements TopiaMetadataModelSupport {
|
|
58
|
63
|
|
|
59
|
64
|
/**
|
| ... |
... |
@@ -61,9 +66,9 @@ public class ObserveTopiaApplicationContext extends AbstractObserveTopiaApplicat |
|
61
|
66
|
*/
|
|
62
|
67
|
private static final Log log = LogFactory.getLog(ObserveTopiaApplicationContext.class);
|
|
63
|
68
|
|
|
64
|
|
- public static final String MIGRATION_SERVICE_NAME = "migration";
|
|
|
69
|
+ static final String MIGRATION_SERVICE_NAME = "migration";
|
|
65
|
70
|
|
|
66
|
|
- public static final String SQL_BATCH_SERVICE_NAME = "sqlBatch";
|
|
|
71
|
+ static final String SQL_BATCH_SERVICE_NAME = "sqlBatch";
|
|
67
|
72
|
|
|
68
|
73
|
private static final String CREATE_SCHEMA_SCRIPT
|
|
69
|
74
|
= "CREATE SCHEMA OBSERVE_COMMON;\n"
|
| ... |
... |
@@ -78,19 +83,17 @@ public class ObserveTopiaApplicationContext extends AbstractObserveTopiaApplicat |
|
78
|
83
|
protected boolean open;
|
|
79
|
84
|
|
|
80
|
85
|
protected final String authenticationToken;
|
|
81
|
|
- protected final TopiaMetadataModelSupport topiaMetadataModelSupport;
|
|
82
|
|
- protected final TopiaSqlTablesFactory topiaSqlTablesFactory;
|
|
83
|
|
- protected final ObserveMigrationConfigurationProvider observeMigrationConfigurationProvider;
|
|
84
|
|
- protected TopiaSqlTables tripSeineTables;
|
|
85
|
|
- protected TopiaSqlTables tripLonglineTables;
|
|
86
|
|
- protected TopiaSqlTables referentialTables;
|
|
|
86
|
+ private final TopiaMetadataModelSupport topiaMetadataModelSupport;
|
|
|
87
|
+ private final TopiaSqlTablesFactory topiaSqlTablesFactory;
|
|
|
88
|
+ private TopiaSqlTables tripSeineTables;
|
|
|
89
|
+ private TopiaSqlTables tripLonglineTables;
|
|
|
90
|
+ private TopiaSqlTables referentialTables;
|
|
87
|
91
|
|
|
88
|
92
|
public ObserveTopiaApplicationContext(ObserveTopiaConfiguration topiaConfiguration) {
|
|
89
|
93
|
super(topiaConfiguration);
|
|
90
|
94
|
this.authenticationToken = UUID.randomUUID().toString();
|
|
91
|
95
|
this.topiaMetadataModelSupport = new TopiaMetadataModelSupportImpl("fr.ird.observe", "Observe");
|
|
92
|
96
|
this.topiaSqlTablesFactory = new TopiaSqlTablesFactory(getMetadataModel(), this);
|
|
93
|
|
- this.observeMigrationConfigurationProvider = ObserveMigrationConfigurationProvider.get();
|
|
94
|
97
|
}
|
|
95
|
98
|
|
|
96
|
99
|
@Override
|
| ... |
... |
@@ -146,8 +149,8 @@ public class ObserveTopiaApplicationContext extends AbstractObserveTopiaApplicat |
|
146
|
149
|
try (ObserveTopiaPersistenceContext topiaPersistenceContext = newPersistenceContext()) {
|
|
147
|
150
|
|
|
148
|
151
|
String sql = Stream.concat(Entities.REFERENCE_ENTITIES_LIST.stream(), Entities.DATA_ENTITIES_LIST.stream())
|
|
149
|
|
- .map(entity -> String.format(INSERT_LAST_UPDATE_PATTERN, entity.ordinal(), entity.getContract().getCanonicalName()))
|
|
150
|
|
- .collect(Collectors.joining("\n"));
|
|
|
152
|
+ .map(entity -> String.format(INSERT_LAST_UPDATE_PATTERN, entity.ordinal(), entity.getContract().getCanonicalName()))
|
|
|
153
|
+ .collect(Collectors.joining("\n"));
|
|
151
|
154
|
|
|
152
|
155
|
topiaPersistenceContext.getSqlSupport().executeSql(sql);
|
|
153
|
156
|
topiaPersistenceContext.commit();
|
| ... |
... |
@@ -170,7 +173,7 @@ public class ObserveTopiaApplicationContext extends AbstractObserveTopiaApplicat |
|
170
|
173
|
|
|
171
|
174
|
}
|
|
172
|
175
|
|
|
173
|
|
- protected void executeSqlStatements0(byte... content) throws IOException {
|
|
|
176
|
+ private void executeSqlStatements0(byte... content) throws IOException {
|
|
174
|
177
|
|
|
175
|
178
|
if (getConfiguration().isH2Configuration()) {
|
|
176
|
179
|
|
| ... |
... |
@@ -178,7 +181,32 @@ public class ObserveTopiaApplicationContext extends AbstractObserveTopiaApplicat |
|
178
|
181
|
log.info(String.format("Restore script size: %s to h2 from file : %s", StringUtil.convertMemory(content.length), tempFile.toString()));
|
|
179
|
182
|
try {
|
|
180
|
183
|
Files.write(tempFile, content);
|
|
181
|
|
- JdbcH2Helper jdbcH2Helper = new JdbcH2Helper(configuration);
|
|
|
184
|
+ JdbcH2Helper jdbcH2Helper = new JdbcH2Helper(configuration) {
|
|
|
185
|
+ @Override
|
|
|
186
|
+ public void restore(File file) throws FileNotFoundException {
|
|
|
187
|
+ String options = "";
|
|
|
188
|
+ InputStream in = null;
|
|
|
189
|
+ try {
|
|
|
190
|
+ in = new BufferedInputStream(new FileInputStream(file));
|
|
|
191
|
+ in.mark(2);
|
|
|
192
|
+ // read header to see if is compressed file
|
|
|
193
|
+ int b = in.read();
|
|
|
194
|
+ // redundant cast : int magic = ((int) in.read() << 8) | b;
|
|
|
195
|
+ int magic = in.read() << 8 | b;
|
|
|
196
|
+ in.reset();
|
|
|
197
|
+ if (magic == GZIPInputStream.GZIP_MAGIC) {
|
|
|
198
|
+ options += " COMPRESSION GZIP";
|
|
|
199
|
+ }
|
|
|
200
|
+ in.close();
|
|
|
201
|
+ } catch (IOException e) {
|
|
|
202
|
+ throw new RuntimeException(e);
|
|
|
203
|
+ } finally {
|
|
|
204
|
+ IOUtils.closeQuietly(in);
|
|
|
205
|
+ }
|
|
|
206
|
+ options += " CHARSET 'UTF8'";
|
|
|
207
|
+ runUpdate("RUNSCRIPT FROM '" + file.getAbsolutePath() + "'" + options);
|
|
|
208
|
+ }
|
|
|
209
|
+ };
|
|
182
|
210
|
jdbcH2Helper.restore(tempFile.toFile());
|
|
183
|
211
|
} finally {
|
|
184
|
212
|
Files.delete(tempFile);
|
| ... |
... |
@@ -462,8 +490,8 @@ public class ObserveTopiaApplicationContext extends AbstractObserveTopiaApplicat |
|
462
|
490
|
|
|
463
|
491
|
private static class TripReplicateTablesPredicate implements TopiaSqlTablesFactory.TopiaSqlTablesPredicate {
|
|
464
|
492
|
|
|
465
|
|
- protected final Set<TopiaMetadataEntity> dones = new LinkedHashSet<>();
|
|
466
|
|
- protected final Set<String> sectionsHolders = ImmutableSet.of(
|
|
|
493
|
+ final Set<TopiaMetadataEntity> dones = new LinkedHashSet<>();
|
|
|
494
|
+ final Set<String> sectionsHolders = ImmutableSet.of(
|
|
467
|
495
|
ObserveEntityEnum.CatchLongline.name(),
|
|
468
|
496
|
ObserveEntityEnum.Tdr.name()
|
|
469
|
497
|
);
|