001package fr.ifremer.adagio.synchro.intercept.internal;
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.sql.SQLException;
027
028import com.google.common.base.Preconditions;
029
030import fr.ifremer.adagio.synchro.SynchroTechnicalException;
031import fr.ifremer.adagio.synchro.dao.SynchroTableDao;
032import fr.ifremer.adagio.synchro.intercept.SynchroInterceptorBase;
033import fr.ifremer.adagio.synchro.intercept.SynchroWriteBuffer;
034
035public class RemoteIdWriteInterceptor extends SynchroInterceptorBase {
036
037    private final String tableName;
038
039    private final int columnIndex;
040
041    private final boolean isNullable;
042
043    public RemoteIdWriteInterceptor(fr.ifremer.adagio.synchro.intercept.SynchroInterceptor next, String tableName, int columnIndex, boolean isNullable) {
044        super(next);
045        Preconditions.checkArgument(columnIndex >= 0);
046        this.tableName = tableName;
047        this.columnIndex = columnIndex;
048        this.isNullable = isNullable;
049    }
050
051    public RemoteIdWriteInterceptor(String tableName, int columnIndex, boolean isNullable) {
052        super();
053        Preconditions.checkArgument(columnIndex >= 0);
054        this.tableName = tableName;
055        this.columnIndex = columnIndex;
056        this.isNullable = isNullable;
057    }
058
059    protected void doOnWrite(Object[] data, SynchroTableDao dao, SynchroWriteBuffer buffer) throws SQLException {
060        Integer remoteId = (Integer) data[columnIndex];
061        if (remoteId == null) {
062            return;
063        }
064
065        Integer localId = dao.getIdFromRemoteId(tableName, remoteId);
066        if (localId == null) {
067            // Mandatory column, could not continu
068            if (!isNullable) {
069                throw new SynchroTechnicalException(String.format("Could not retrieve ID corresponding to REMOTE_ID=%s, for table %s",
070                        remoteId, tableName));
071            }
072
073            // Optional column : add to pending changes (will be processed later)
074            buffer.addMissingRemoteId(tableName, columnIndex, null, remoteId);
075            data[columnIndex] = null;
076        }
077        else {
078            data[columnIndex] = localId;
079        }
080    }
081
082    @Override
083    public boolean enableOnWrite() {
084        return true;
085    }
086}