View Javadoc
1   package fr.ifremer.quadrige3.synchro.intercept.administration;
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.meta.SynchroDatabaseMetadata;
28  import fr.ifremer.common.synchro.meta.SynchroTableMetadata;
29  import fr.ifremer.common.synchro.meta.event.CreateQueryEvent;
30  import fr.ifremer.common.synchro.query.SynchroQueryBuilder;
31  import fr.ifremer.common.synchro.query.SynchroQueryName;
32  import fr.ifremer.common.synchro.query.SynchroQueryOperator;
33  import fr.ifremer.quadrige3.synchro.meta.DatabaseColumns;
34  import fr.ifremer.quadrige3.synchro.meta.administration.MoratoriumSynchroTables;
35  import fr.ifremer.quadrige3.synchro.meta.administration.ProgramStrategySynchroTables;
36  import org.apache.commons.collections4.SetUtils;
37  import org.hibernate.tool.hbm2ddl.TableMetadata;
38  
39  /**
40   * Manage table on Program/Strategy, with a column 'PROG_CD' :
41   * <ul>
42   * <li>Limit to program list (if specified in configuration)
43   * </ul>
44   * 
45   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
46   * @since 1.0
47   */
48  public class TableWithProgCdInterceptor extends AbstractProgramStrategyInterceptor {
49  
50  	/**
51  	 * <p>
52  	 * Constructor for TableWithProgCdInterceptor.
53  	 * </p>
54  	 */
55  	public TableWithProgCdInterceptor() {
56  		super(SetUtils.union(ProgramStrategySynchroTables.tableNames(), MoratoriumSynchroTables.tableNames()));
57  	}
58  
59  	/** {@inheritDoc} */
60  	@Override
61  	public boolean doApply(SynchroDatabaseMetadata meta, TableMetadata table) {
62  		return super.doApply(meta, table) && hasColumns(table, DatabaseColumns.PROG_CD.name());
63  	}
64  
65  	/**
66  	 * <p>
67  	 * handleQuery.
68  	 * </p>
69  	 * 
70  	 * @param e
71  	 *            a {@link fr.ifremer.common.synchro.meta.event.CreateQueryEvent} object.
72  	 */
73  	@Subscribe
74  	public void handleQuery(CreateQueryEvent e) {
75  
76  		switch (e.queryName) {
77  		case count:
78  		case countFromUpdateDate:
79  		case select:
80  		case selectFromUpdateDate:
81  		case selectMaxUpdateDate:
82  			// Add restriction
83  			e.sql = addRestriction(e.source, e.queryName, e.sql);
84  
85  		default:
86  			break;
87  		}
88  	}
89  
90  	/* -- Internal methods -- */
91  
92  	/**
93  	 * <p>
94  	 * addRestriction.
95  	 * </p>
96  	 * 
97  	 * @param table
98  	 *            a {@link fr.ifremer.common.synchro.meta.SynchroTableMetadata} object.
99  	 * @param queryName
100 	 *            a {@link fr.ifremer.common.synchro.query.SynchroQueryName} object.
101 	 * @param sql
102 	 *            a {@link java.lang.String} object.
103 	 * @return a {@link java.lang.String} object.
104 	 */
105 	protected String addRestriction(SynchroTableMetadata table, SynchroQueryName queryName, String sql) {
106 		SynchroQueryBuilder queryBuilder = SynchroQueryBuilder.newBuilder(sql);
107 
108 		// where: add program filter
109 		String programWhereClauseOrNull = createProgramWhereClauseOrNull(getConfig(), "t." + DatabaseColumns.PROG_CD.name() + " IN (%s)");
110 		if (programWhereClauseOrNull != null) {
111 			queryBuilder.addWhere(SynchroQueryOperator.AND, programWhereClauseOrNull);
112 		}
113 
114 		return queryBuilder.build();
115 	}
116 }