View Javadoc
1   package fr.ifremer.quadrige2.synchro.intercept.data.internal;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 Synchro Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 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 fr.ifremer.common.synchro.dao.SynchroTableDao;
27  import fr.ifremer.common.synchro.intercept.SynchroInterceptorBase;
28  import fr.ifremer.common.synchro.intercept.SynchroOperationRepository;
29  
30  import java.sql.SQLException;
31  import java.util.List;
32  import java.util.Map;
33  
34  /**
35   * Interceptor on data table (for import from file only) that remap source FK to local FK.
36   * Used with ImportFromFileNumericalPkInterceptor to collect local PK
37   * <p/>
38   * Created by Ludovic on 30/11/2015.
39   */
40  public class ReplaceColumnValuesInterceptor extends SynchroInterceptorBase {
41  
42  	private final int columnIndex;
43  
44  	private final Map<String, Object> valuesMap;
45  
46  	/**
47  	 * <p>
48  	 * Constructor for ReplaceColumnValuesInterceptor.
49  	 * </p>
50  	 * 
51  	 * @param columnIndex
52  	 *            a int.
53  	 * @param columnValuesMap
54  	 *            a {@link java.util.Map} object.
55  	 */
56  	public ReplaceColumnValuesInterceptor(int columnIndex,
57  			Map<String, Object> columnValuesMap) {
58  		this.columnIndex = columnIndex;
59  		this.valuesMap = columnValuesMap;
60  		setEnableOnRead(true);
61  		setEnableOnWrite(true);
62  	}
63  
64  	/** {@inheritDoc} */
65  	@Override
66  	public SynchroInterceptorBase clone() {
67  		return new ReplaceColumnValuesInterceptor(columnIndex, valuesMap);
68  	}
69  
70  	/** {@inheritDoc} */
71  	@Override
72  	protected void doOnRead(Object[] data,
73  			SynchroTableDao sourceDao,
74  			SynchroTableDao targetDao) throws SQLException {
75  
76  		Object sourceValue = data[columnIndex];
77  		if (sourceValue != null) {
78  			Object targetValue = valuesMap.get(sourceValue.toString());
79  			if (targetValue != null) {
80  				data[columnIndex] = targetValue;
81  			}
82  		}
83  	}
84  
85  	/** {@inheritDoc} */
86  	@Override
87  	protected void doOnWrite(Object[] data, List<Object> pk, SynchroTableDao sourceDao, SynchroTableDao targetDao, SynchroOperationRepository buffer,
88  			boolean insert) throws SQLException {
89  
90  		Object sourceValue = data[columnIndex];
91  		if (sourceValue != null) {
92  			Object targetValue = valuesMap.get(sourceValue.toString());
93  			if (targetValue != null) {
94  				data[columnIndex] = targetValue;
95  			}
96  		}
97  	}
98  
99  }