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