1 package fr.ifremer.adagio.synchro.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
26 import java.util.List;
27
28 import javax.sql.DataSource;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.hibernate.dialect.Dialect;
33
34 import com.google.common.base.Preconditions;
35 import com.google.common.collect.Lists;
36
37 import fr.ifremer.adagio.synchro.config.SynchroConfiguration;
38 import fr.ifremer.adagio.synchro.dao.SynchroTableDao;
39 import fr.ifremer.adagio.synchro.intercept.SynchroInterceptor;
40 import fr.ifremer.adagio.synchro.intercept.SynchroInterceptorBase;
41 import fr.ifremer.adagio.synchro.intercept.SynchroInterceptorUtils;
42 import fr.ifremer.adagio.synchro.intercept.internal.RemoteIdWriteInterceptor;
43 import fr.ifremer.adagio.synchro.meta.SynchroDatabaseMetadata;
44 import fr.ifremer.adagio.synchro.meta.SynchroJoinMetadata;
45 import fr.ifremer.adagio.synchro.meta.SynchroTableMetadata;
46
47 public class SynchroBaseService {
48
49
50 private static final Log log =
51 LogFactory.getLog(SynchroBaseService.class);
52
53 protected final SynchroConfiguration config;
54
55 protected final int batchSize;
56
57 protected final DataSource dataSource;
58
59 public SynchroBaseService(DataSource dataSource, SynchroConfiguration config) {
60 Preconditions.checkNotNull(config);
61
62 this.dataSource = dataSource;
63 this.config = config;
64 this.batchSize = config.getImportJdbcBatchSize();
65 }
66
67 public SynchroBaseService() {
68 this.config = SynchroConfiguration.getInstance();
69 Preconditions.checkNotNull(this.config,
70 String.format("%s instance not initialized. Make sure 'setInstance()' is called first.", SynchroConfiguration.class.getName()));
71 this.dataSource = null;
72 this.batchSize = config.getImportJdbcBatchSize();
73 }
74
75 protected List<SynchroInterceptor> daoInterceptors = null;
76
77 public List<SynchroInterceptor> getInterceptors(SynchroContext context) {
78 if (daoInterceptors == null) {
79 daoInterceptors = SynchroInterceptorUtils.load(SynchroInterceptor.class, context);
80 }
81 return daoInterceptors;
82 }
83
84 public SynchroInterceptor getInterceptor(SynchroTableMetadata table, SynchroContext context) {
85 List<SynchroInterceptor> interceptors = getInterceptors(context);
86
87 List<SynchroInterceptor> filteredInterceptors = Lists.newArrayList();
88 filteredInterceptors.addAll(SynchroInterceptorUtils.filter(interceptors,
89 table.getDatabaseMetadata(), table.getDelegate()));
90
91 if (table.hasJoins()) {
92
93 for (SynchroJoinMetadata join : table.getJoins()) {
94 if (join.needRemoteIdInterceptor()) {
95 String pkTableName = join.getTargetTable().getName().toLowerCase();
96 String fkColumnName = join.getSourceColumn().getName().toLowerCase();
97 int fkColumnIndex = join.getSourceTable().getColumnIndex(fkColumnName);
98
99 RemoteIdWriteInterceptor remoteIdInterceptor = new RemoteIdWriteInterceptor(
100 pkTableName,
101 fkColumnIndex,
102 join.getSourceColumn().isNullable());
103
104 filteredInterceptors.add(remoteIdInterceptor);
105 }
106 }
107 }
108
109 SynchroInterceptor result = SynchroInterceptorUtils.chain(filteredInterceptors, SynchroInterceptorBase.class);
110
111 return result;
112 }
113
114 protected void reportProgress(SynchroResult result, SynchroTableDao dao, int countR, String tablePrefix) {
115 if (countR % batchSize == 0) {
116 result.getProgressionModel().increments(batchSize);
117 }
118
119 if (countR % (batchSize * 10) == 0) {
120 if (log.isInfoEnabled()) {
121 log.info(String.format("%s Done: %s (inserts: %s, updates: %s)", tablePrefix, countR, dao.getInsertCount(), dao.getUpdateCount()));
122 }
123 }
124 }
125
126 }