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.base.Joiner;
27  import com.google.common.eventbus.Subscribe;
28  import fr.ifremer.common.synchro.meta.SynchroDatabaseMetadata;
29  import fr.ifremer.common.synchro.meta.event.CreateQueryEvent;
30  import fr.ifremer.common.synchro.query.SynchroQueryBuilder;
31  import fr.ifremer.common.synchro.query.SynchroQueryOperator;
32  import fr.ifremer.quadrige3.synchro.meta.DatabaseColumns;
33  import fr.ifremer.quadrige3.synchro.service.SynchroDirection;
34  import org.apache.commons.collections4.CollectionUtils;
35  import org.hibernate.tool.hbm2ddl.TableMetadata;
36  
37  import java.util.Set;
38  
39  /**
40   * Interceptor and filter tables with PROG_CD column, when importing from Server DB -> Temp DB
41   */
42  public class DataTableWithProgCdInterceptor extends AbstractDataInterceptor {
43  
44  	/**
45  	 * <p>
46  	 * Constructor for DataTableWithProgCdInterceptor.
47  	 * </p>
48  	 */
49  	public DataTableWithProgCdInterceptor() {
50  		super(SynchroDirection.IMPORT_SERVER2TEMP);
51  	}
52  
53  	/** {@inheritDoc} */
54  	@Override
55  	public boolean doApply(SynchroDatabaseMetadata meta, TableMetadata table) {
56  		return hasColumns(table, DatabaseColumns.PROG_CD.name());
57  	}
58  
59  	/**
60  	 * <p>
61  	 * handleCreateQuery.
62  	 * </p>
63  	 * 
64  	 * @param e
65  	 *            a {@link fr.ifremer.common.synchro.meta.event.CreateQueryEvent} object.
66  	 */
67  	@Subscribe
68  	public void handleCreateQuery(CreateQueryEvent e) {
69  		switch (e.queryName) {
70  		case count:
71  		case countFromUpdateDate:
72  		case select:
73  		case selectFromUpdateDate:
74  		case selectMaxUpdateDate:
75  
76  			e.sql = addRestrictionOnImport(e.sql);
77  			break;
78  
79  		default:
80  			break;
81  		}
82  	}
83  
84  	/* -- internal methods -- */
85  
86  	private String addRestrictionOnImport(String sql) {
87  		Set<String> programCodes = getConfig().getProgramCodes();
88  
89  		// If no filter programs : nothing to do
90  		if (CollectionUtils.isEmpty(programCodes)) {
91  			return sql;
92  		}
93  
94  		StringBuilder sb = new StringBuilder();
95  		sb.append('\'');
96  		Joiner.on("','").appendTo(sb, programCodes);
97  		sb.append('\'');
98  
99  		// Add restriction on PROG_CD
100 		return SynchroQueryBuilder.newBuilder(sql)
101 				.addWhere(SynchroQueryOperator.AND, String.format("t.%s in (%s)",
102 						DatabaseColumns.PROG_CD.name(),
103 						sb.toString()))
104 				.build();
105 	}
106 }