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 import java.util.Map;
28
29 import org.apache.commons.collections.CollectionUtils;
30 import org.apache.commons.collections4.MapUtils;
31
32 import com.google.common.collect.Lists;
33 import com.google.common.collect.Maps;
34
35 import fr.ifremer.adagio.synchro.intercept.SynchroWriteBuffer;
36
37 public class SynchroPendingOperationBuffer implements SynchroWriteBuffer {
38
39 private List<List<Object>> pks;
40
41 private Map<Integer, Integer> remoteIdsMap;
42
43 private Map<Integer, Map<String, Integer>> missingRemoteIdByColumnIndex = Maps.newHashMap();
44
45 private final String tableName;
46
47 public SynchroPendingOperationBuffer(String tableName) {
48 this.tableName = tableName;
49 }
50
51 public List<List<Object>> getPks() {
52 return pks;
53 }
54
55 public void setPks(List<List<Object>> pks) {
56 this.pks = pks;
57 }
58
59 public void addPks(List<List<Object>> updatedPks) {
60 if (pks == null) {
61 pks = Lists.newArrayList();
62 }
63 pks.addAll(updatedPks);
64 }
65
66 public String getTableName() {
67 return tableName;
68 }
69
70 public Map<Integer, Integer> getRemoteIdsMap() {
71 return remoteIdsMap;
72 }
73
74 public void addRemoteIdsMap(Map<Integer, Integer> updatedRemoteIds) {
75 if (remoteIdsMap == null) {
76 remoteIdsMap = updatedRemoteIds;
77 }
78 remoteIdsMap.putAll(updatedRemoteIds);
79 }
80
81 public boolean isEmpty() {
82 return MapUtils.isEmpty(remoteIdsMap) && CollectionUtils.isEmpty(pks);
83 }
84
85 @Override
86 public void addMissingRemoteId(String tablename, int columnIndex, String pkStr, Integer remoteId) {
87 Map<String, Integer> missingRemoteIds = missingRemoteIdByColumnIndex.get(columnIndex);
88 if (missingRemoteIds == null) {
89 missingRemoteIds = Maps.newHashMap();
90 missingRemoteIdByColumnIndex.put(columnIndex, missingRemoteIds);
91 }
92 missingRemoteIds.put(pkStr, remoteId);
93 }
94 }