View Javadoc
1   package fr.ifremer.adagio.synchro.intercept.internal;
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.sql.SQLException;
27  
28  import com.google.common.base.Preconditions;
29  
30  import fr.ifremer.adagio.synchro.SynchroTechnicalException;
31  import fr.ifremer.adagio.synchro.dao.SynchroTableDao;
32  import fr.ifremer.adagio.synchro.intercept.SynchroInterceptorBase;
33  import fr.ifremer.adagio.synchro.intercept.SynchroWriteBuffer;
34  
35  public class RemoteIdWriteInterceptor extends SynchroInterceptorBase {
36  
37  	private final String tableName;
38  
39  	private final int columnIndex;
40  
41  	private final boolean isNullable;
42  
43  	public RemoteIdWriteInterceptor(fr.ifremer.adagio.synchro.intercept.SynchroInterceptor next, String tableName, int columnIndex, boolean isNullable) {
44  		super(next);
45  		Preconditions.checkArgument(columnIndex >= 0);
46  		this.tableName = tableName;
47  		this.columnIndex = columnIndex;
48  		this.isNullable = isNullable;
49  	}
50  
51  	public RemoteIdWriteInterceptor(String tableName, int columnIndex, boolean isNullable) {
52  		super();
53  		Preconditions.checkArgument(columnIndex >= 0);
54  		this.tableName = tableName;
55  		this.columnIndex = columnIndex;
56  		this.isNullable = isNullable;
57  	}
58  
59  	protected void doOnWrite(Object[] data, SynchroTableDao dao, SynchroWriteBuffer buffer) throws SQLException {
60  		Integer remoteId = (Integer) data[columnIndex];
61  		if (remoteId == null) {
62  			return;
63  		}
64  
65  		Integer localId = dao.getIdFromRemoteId(tableName, remoteId);
66  		if (localId == null) {
67  			// Mandatory column, could not continu
68  			if (!isNullable) {
69  				throw new SynchroTechnicalException(String.format("Could not retrieve ID corresponding to REMOTE_ID=%s, for table %s",
70  						remoteId, tableName));
71  			}
72  
73  			// Optional column : add to pending changes (will be processed later)
74  			buffer.addMissingRemoteId(tableName, columnIndex, null, remoteId);
75  			data[columnIndex] = null;
76  		}
77  		else {
78  			data[columnIndex] = localId;
79  		}
80  	}
81  
82  	@Override
83  	public boolean enableOnWrite() {
84  		return true;
85  	}
86  }