View Javadoc
1   package fr.ifremer.quadrige2.synchro.intercept.data;
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 com.google.common.collect.Maps;
27  import com.google.common.eventbus.Subscribe;
28  import fr.ifremer.common.synchro.intercept.SynchroInterceptorBase;
29  import fr.ifremer.common.synchro.meta.SynchroDatabaseMetadata;
30  import fr.ifremer.common.synchro.meta.SynchroTableMetadata;
31  import fr.ifremer.common.synchro.meta.event.LoadTableEvent;
32  import fr.ifremer.quadrige2.synchro.intercept.data.internal.ReplaceColumnValuesInterceptor;
33  import fr.ifremer.quadrige2.synchro.service.SynchroDirection;
34  import fr.ifremer.quadrige2.synchro.service.data.DataSynchroDatabaseConfiguration;
35  import org.apache.commons.collections4.MapUtils;
36  import org.hibernate.tool.hbm2ddl.TableMetadata;
37  
38  import java.util.Map;
39  
40  /**
41   * Rewrite column values, using a input map (store in DataSynchroContext).
42   * Used in file import, when some local referential have not same ID (see mantis #26721)
43   * 
44   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
45   * @since 1.0
46   */
47  public class ReplaceTableValuesInterceptor extends AbstractDataInterceptor {
48  
49  	private boolean isEnable = false;
50  
51  	/**
52  	 * <p>
53  	 * Constructor for ReplaceTableValuesInterceptor.
54  	 * </p>
55  	 */
56  	public ReplaceTableValuesInterceptor() {
57  		super(SynchroDirection.IMPORT_FILE2LOCAL);
58  	}
59  
60  	/** {@inheritDoc} */
61  	@Override
62  	protected void init(DataSynchroDatabaseConfiguration config) {
63  		super.init(config);
64  		isEnable = MapUtils.isNotEmpty(config.getRemapValues());
65  	}
66  
67  	/** {@inheritDoc} */
68  	@Override
69  	public SynchroInterceptorBase clone() {
70  		ReplaceTableValuesInterceptor result = (ReplaceTableValuesInterceptor) super.clone();
71  		result.isEnable = isEnable;
72  		return result;
73  	}
74  
75  	/** {@inheritDoc} */
76  	@Override
77  	public boolean doApply(SynchroDatabaseMetadata meta, TableMetadata table) {
78  
79  		return isEnable && getConfig().getRemapValues().containsKey(table.getName());
80  	}
81  
82  	/**
83  	 * <p>
84  	 * handleLoadTable.
85  	 * </p>
86  	 * 
87  	 * @param e
88  	 *            a {@link fr.ifremer.common.synchro.meta.event.LoadTableEvent} object.
89  	 */
90  	@Subscribe
91  	public void handleLoadTable(LoadTableEvent e) {
92  		SynchroTableMetadata table = e.table;
93  
94  		Map<String, Map<String, Object>> valuesMapByColumnName = getConfig().getRemapValues().get(table.getName());
95  		for (String columnName : valuesMapByColumnName.keySet()) {
96  
97  			int columnIndex = table.getSelectColumnIndex(columnName);
98  			if (columnIndex != -1) {
99  				Map<String, Object> valuesMap = valuesMapByColumnName.get(columnName);
100 
101 				ReplaceColumnValuesInterceptor replaceInterceptor = new ReplaceColumnValuesInterceptor(
102 						columnIndex,
103 						valuesMap);
104 				table.addInterceptor(replaceInterceptor);
105 			}
106 		}
107 	}
108 
109 	/* -- Internal methods -- */
110 
111 }