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.event.LoadTableEvent;
30  import fr.ifremer.common.synchro.service.SynchroDatabaseConfiguration;
31  import fr.ifremer.quadrige3.core.dao.technical.Assert;
32  import fr.ifremer.quadrige3.synchro.intercept.data.internal.ExportUpdateDateInterceptor;
33  import fr.ifremer.quadrige3.synchro.meta.data.DataSynchroTables;
34  import fr.ifremer.quadrige3.synchro.service.SynchroDirection;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.hibernate.tool.hbm2ddl.TableMetadata;
38  
39  import java.sql.Timestamp;
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  /**
44   * replace update_date column by a system timestamp, when exported to server.
45   * This interceptor is only used for table without column 'id' AND 'remote_id', because this tables are managed by
46   * DataTableInterceptor.
47   * 
48   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
49   * @since 1.0
50   */
51  public class DataTableWithUpdateDateInterceptor extends AbstractDataInterceptor {
52  
53  	private static final Log log = LogFactory.getLog(DataTableWithUpdateDateInterceptor.class);
54  
55  	private Timestamp systimestamp = null;
56  	private static final List<String> excludedTableNames;
57  
58  	static {
59  		excludedTableNames = initExcludedTableNames();
60  	}
61  
62  	/**
63  	 * <p>
64  	 * Constructor for DataTableWithUpdateDateInterceptor.
65  	 * </p>
66  	 */
67  	public DataTableWithUpdateDateInterceptor() {
68  		super(SynchroDirection.EXPORT_TEMP2SERVER);
69  	}
70  
71  	/** {@inheritDoc} */
72  	@Override
73  	public SynchroInterceptorBase clone() {
74  		DataTableWithUpdateDateInterceptor result = (DataTableWithUpdateDateInterceptor) super.clone();
75  		result.systimestamp = this.systimestamp;
76  		return result;
77  	}
78  
79  	/** {@inheritDoc} */
80  	public boolean doApply(SynchroDatabaseMetadata dbMeta, TableMetadata table) {
81  
82  		return hasColumns(table, getConfig().getColumnUpdateDate())
83  				// Exclude tables already managed by DataTableInterceptor
84  				&& !hasColumns(table, getConfig().getColumnRemoteId())
85  				// Exclude some specific tables
86  				&& isIncludedTable(table);
87  	}
88  
89  	private boolean isIncludedTable(TableMetadata table) {
90  		return !excludedTableNames.contains(table.getName().toUpperCase());
91  	}
92  
93  	/**
94  	 * <p>
95  	 * handleTableLoad.
96  	 * </p>
97  	 * 
98  	 * @param e
99  	 *            a {@link fr.ifremer.common.synchro.meta.event.LoadTableEvent} object.
100 	 */
101 	@Subscribe
102 	public void handleTableLoad(LoadTableEvent e) {
103 		if (systimestamp == null) {
104 			systimestamp = checkAndGetSystemTimestamp(getConfig());
105 		}
106 
107 		int updateDateColumnIndex = e.table.getSelectColumnIndex(getConfig().getColumnUpdateDate());
108 		if (updateDateColumnIndex == -1) {
109 			log.warn(String.format("[%s] Unable to find %s column in the select query. %s will be ignore.",
110 					e.table.getName(),
111 					getConfig().getColumnUpdateDate(),
112 					getClass().getSimpleName()));
113 		}
114 		else {
115 			ExportUpdateDateInterceptor setUpdateDateInterceptor =
116 					new ExportUpdateDateInterceptor(updateDateColumnIndex, systimestamp);
117 
118 			e.table.addInterceptor(setUpdateDateInterceptor);
119 		}
120 	}
121 
122 	/* -- Protected methods -- */
123 
124 	/**
125 	 * <p>
126 	 * checkAndGetSystemTimestamp.
127 	 * </p>
128 	 * 
129 	 * @param configuration
130 	 *            a {@link fr.ifremer.common.synchro.service.SynchroDatabaseConfiguration} object.
131 	 * @return a {@link java.sql.Timestamp} object.
132 	 */
133 	protected Timestamp checkAndGetSystemTimestamp(SynchroDatabaseConfiguration configuration) {
134 		Timestamp systimestamp = configuration.getSystemTimestamp();
135 		Assert.notNull(systimestamp,
136 				String.format("Could not found system timestamp in database configuration. This is need for %s", getClass().getSimpleName()));
137 		return systimestamp;
138 	}
139 
140 	private static List<String> initExcludedTableNames() {
141 		List<String> result = new ArrayList<>();
142 
143 		result.add(DataSynchroTables.VALIDATION_HISTORY.name());
144 		result.add(DataSynchroTables.QUALIFICATION_HISTORY.name());
145 
146 		return result;
147 	}
148 }