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