001package fr.ifremer.adagio.synchro.service;
002
003/*
004 * #%L
005 * SIH-Adagio :: Synchronization
006 * $Id:$
007 * $HeadURL:$
008 * %%
009 * Copyright (C) 2012 - 2014 Ifremer
010 * %%
011 * This program is free software: you can redistribute it and/or modify
012 * it under the terms of the GNU Affero General Public License as published by
013 * the Free Software Foundation, either version 3 of the License, or
014 * (at your option) any later version.
015 * 
016 * This program is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
019 * GNU General Public License for more details.
020 * 
021 * You should have received a copy of the GNU Affero General Public License
022 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
023 * #L%
024 */
025
026import java.util.List;
027import java.util.Map;
028
029import org.apache.commons.collections.CollectionUtils;
030import org.apache.commons.collections4.MapUtils;
031
032import com.google.common.collect.Lists;
033import com.google.common.collect.Maps;
034
035import fr.ifremer.adagio.synchro.intercept.SynchroWriteBuffer;
036
037public class SynchroPendingOperationBuffer implements SynchroWriteBuffer {
038
039    private List<List<Object>> pks;
040
041    private Map<Integer, Integer> remoteIdsMap;
042
043    private Map<Integer, Map<String, Integer>> missingRemoteIdByColumnIndex = Maps.newHashMap();
044
045    private final String tableName;
046
047    public SynchroPendingOperationBuffer(String tableName) {
048        this.tableName = tableName;
049    }
050
051    public List<List<Object>> getPks() {
052        return pks;
053    }
054
055    public void setPks(List<List<Object>> pks) {
056        this.pks = pks;
057    }
058
059    public void addPks(List<List<Object>> updatedPks) {
060        if (pks == null) {
061            pks = Lists.newArrayList();
062        }
063        pks.addAll(updatedPks);
064    }
065
066    public String getTableName() {
067        return tableName;
068    }
069
070    public Map<Integer, Integer> getRemoteIdsMap() {
071        return remoteIdsMap;
072    }
073
074    public void addRemoteIdsMap(Map<Integer, Integer> updatedRemoteIds) {
075        if (remoteIdsMap == null) {
076            remoteIdsMap = updatedRemoteIds;
077        }
078        remoteIdsMap.putAll(updatedRemoteIds);
079    }
080
081    public boolean isEmpty() {
082        return MapUtils.isEmpty(remoteIdsMap) && CollectionUtils.isEmpty(pks);
083    }
084
085    @Override
086    public void addMissingRemoteId(String tablename, int columnIndex, String pkStr, Integer remoteId) {
087        Map<String, Integer> missingRemoteIds = missingRemoteIdByColumnIndex.get(columnIndex);
088        if (missingRemoteIds == null) {
089            missingRemoteIds = Maps.newHashMap();
090            missingRemoteIdByColumnIndex.put(columnIndex, missingRemoteIds);
091        }
092        missingRemoteIds.put(pkStr, remoteId);
093    }
094}