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.SynchroTableMetadata;
28  import fr.ifremer.common.synchro.meta.event.CreateQueryEvent;
29  import fr.ifremer.common.synchro.query.SynchroQueryBuilder;
30  import fr.ifremer.common.synchro.query.SynchroQueryName;
31  import fr.ifremer.quadrige3.synchro.meta.administration.ProgramStrategySynchroTables;
32  
33  /**
34   * Manage table PMFM_APPLIED_STRATEGY:
35   * <ul>
36   * <li>Limit to program list (if specified in configuration)
37   * </ul>
38   * 
39   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
40   * @since 1.0
41   */
42  public class PmfmAppliedStrategyInterceptor extends AbstractProgramStrategyInterceptor {
43  
44  	/**
45  	 * <p>
46  	 * Constructor for PmfmAppliedStrategyInterceptor.
47  	 * </p>
48  	 */
49  	public PmfmAppliedStrategyInterceptor() {
50  		super(ProgramStrategySynchroTables.PMFM_APPLIED_STRATEGY.name());
51  	}
52  
53  	/**
54  	 * <p>
55  	 * handleQuery.
56  	 * </p>
57  	 * 
58  	 * @param e
59  	 *            a {@link fr.ifremer.common.synchro.meta.event.CreateQueryEvent} object.
60  	 */
61  	@Subscribe
62  	public void handleQuery(CreateQueryEvent e) {
63  
64  		switch (e.queryName) {
65  		case count:
66  		case countFromUpdateDate:
67  		case select:
68  		case selectFromUpdateDate:
69  		case selectMaxUpdateDate:
70  			// Add restriction
71  			e.sql = addRestriction(e.source, e.queryName, e.sql);
72  
73  		default:
74  			break;
75  		}
76  	}
77  
78  	/* -- Internal methods -- */
79  
80  	/**
81  	 * <p>
82  	 * addRestriction.
83  	 * </p>
84  	 * 
85  	 * @param table
86  	 *            a {@link fr.ifremer.common.synchro.meta.SynchroTableMetadata} object.
87  	 * @param queryName
88  	 *            a {@link fr.ifremer.common.synchro.query.SynchroQueryName} object.
89  	 * @param sql
90  	 *            a {@link java.lang.String} object.
91  	 * @return a {@link java.lang.String} object.
92  	 */
93  	protected String addRestriction(SynchroTableMetadata table, SynchroQueryName queryName, String sql) {
94  		SynchroQueryBuilder queryBuilder = SynchroQueryBuilder.newBuilder(sql);
95  
96  		// join: filter on program
97  		String programFilterJoin = createProgramWhereClauseOrNull(getConfig(),
98  				"INNER JOIN APPLIED_STRATEGY aps ON aps.applied_strat_id=t.applied_strat_id"
99  						+ " INNER JOIN STRATEGY s ON s.strat_id=aps.strat_id AND s.prog_cd IN (%s)");
100 		if (programFilterJoin != null) {
101 			queryBuilder.addJoin(programFilterJoin);
102 		}
103 
104 		return queryBuilder.build();
105 	}
106 }