View Javadoc
1   package fr.ifremer.quadrige2.synchro.intercept.administration;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 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.dao.SynchroTableDao;
28  import fr.ifremer.common.synchro.intercept.SynchroOperationRepository;
29  import fr.ifremer.common.synchro.meta.SynchroTableMetadata;
30  import fr.ifremer.common.synchro.meta.event.CreateQueryEvent;
31  import fr.ifremer.common.synchro.query.SynchroQueryBuilder;
32  import fr.ifremer.common.synchro.query.SynchroQueryName;
33  import fr.ifremer.quadrige2.synchro.intercept.referential.AbstractReferentialInterceptor;
34  import fr.ifremer.quadrige2.synchro.meta.DatabaseColumns;
35  import fr.ifremer.quadrige2.synchro.meta.administration.ProgramStrategySynchroTables;
36  
37  import java.sql.SQLException;
38  import java.util.List;
39  
40  /**
41   * Manage table APPLIED_STRATEGY :
42   * <ul>
43   * <li>Limit to program list (if specified in configuration)
44   * <li>Delete children tables (APPLIED_PERIOD)
45   * </ul>
46   * 
47   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
48   * @since 1.0
49   */
50  public class AppliedStrategyInterceptor extends AbstractProgramStrategyInterceptor {
51  
52  	/**
53  	 * <p>
54  	 * Constructor for AppliedStrategyInterceptor.
55  	 * </p>
56  	 */
57  	public AppliedStrategyInterceptor() {
58  		super(ProgramStrategySynchroTables.APPLIED_STRATEGY.name());
59  		setEnableOnWrite(true);
60  	}
61  
62  	/**
63  	 * <p>
64  	 * handleQuery.
65  	 * </p>
66  	 * 
67  	 * @param e
68  	 *            a {@link fr.ifremer.common.synchro.meta.event.CreateQueryEvent} object.
69  	 */
70  	@Subscribe
71  	public void handleQuery(CreateQueryEvent e) {
72  
73  		switch (e.queryName) {
74  		case count:
75  		case countFromUpdateDate:
76  		case select:
77  		case selectFromUpdateDate:
78  		case selectMaxUpdateDate:
79  			// Add restriction
80  			e.sql = addRestriction(e.source, e.queryName, e.sql);
81  
82  		default:
83  			break;
84  		}
85  	}
86  
87  	/** {@inheritDoc} */
88  	@Override
89  	protected void doOnDelete(List<Object> pk, SynchroTableDao sourceDao, SynchroTableDao targetDao, SynchroOperationRepository buffer)
90  			throws SQLException {
91  		// Getting the stratId
92  		Integer appliedStratId = Integer.parseInt(pk.get(0).toString());
93  
94  		// Delete children tables : APPLIED_PERIOD
95  		buffer.addChildToDeleteFromOneColumn(ProgramStrategySynchroTables.APPLIED_PERIOD.name(), DatabaseColumns.APPLIED_STRAT_ID.name(),
96  				appliedStratId);
97  	}
98  
99  	/* -- Internal methods -- */
100 
101 	/**
102 	 * <p>
103 	 * addRestriction.
104 	 * </p>
105 	 * 
106 	 * @param table
107 	 *            a {@link fr.ifremer.common.synchro.meta.SynchroTableMetadata} object.
108 	 * @param queryName
109 	 *            a {@link fr.ifremer.common.synchro.query.SynchroQueryName} object.
110 	 * @param sql
111 	 *            a {@link java.lang.String} object.
112 	 * @return a {@link java.lang.String} object.
113 	 */
114 	protected String addRestriction(SynchroTableMetadata table, SynchroQueryName queryName, String sql) {
115 		SynchroQueryBuilder queryBuilder = SynchroQueryBuilder.newBuilder(sql);
116 
117 		// join: filter on program
118 		String programFilterJoin = createProgramWhereClauseOrNull(getConfig(), "INNER JOIN STRATEGY s ON s.strat_id=t.strat_id AND s.prog_cd IN (%s)");
119 		if (programFilterJoin != null) {
120 			queryBuilder.addJoin(programFilterJoin);
121 		}
122 
123 		return queryBuilder.build();
124 	}
125 }