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