View Javadoc
1   package fr.ifremer.adagio.synchro.service;
2   
3   /*
4    * #%L
5    * SIH-Adagio :: Synchronization
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2014 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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  }