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.Daos;
28  import fr.ifremer.common.synchro.dao.SynchroBaseDao;
29  import fr.ifremer.common.synchro.dao.SynchroTableDao;
30  import fr.ifremer.common.synchro.intercept.SynchroOperationRepository;
31  import fr.ifremer.common.synchro.meta.SynchroTableMetadata;
32  import fr.ifremer.common.synchro.meta.event.CreateQueryEvent;
33  import fr.ifremer.common.synchro.query.SynchroQueryBuilder;
34  import fr.ifremer.common.synchro.query.SynchroQueryName;
35  import fr.ifremer.quadrige3.synchro.meta.DatabaseColumns;
36  import fr.ifremer.quadrige3.synchro.meta.administration.ProgramStrategySynchroTables;
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  import java.io.IOException;
41  import java.sql.PreparedStatement;
42  import java.sql.SQLException;
43  import java.util.List;
44  
45  /**
46   * Manage table PMFM_STRATEGY :
47   * <ul>
48   * <li>Limit to program list (if specified in configuration)
49   * <li>Delete PMFM_APPLIED_STRATEGY before deleting PMFM_STRATEGY
50   * </ul>
51   * 
52   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
53   * @since 1.0
54   */
55  public class PmfmStrategyInterceptor extends AbstractProgramStrategyInterceptor {
56  
57  	private static final Log log = LogFactory.getLog(PmfmStrategyInterceptor.class);
58  
59  	private PreparedStatement deletePmfmAppliedStrategyStatement = null;
60  
61  	/**
62  	 * <p>
63  	 * Constructor for PmfmStrategyInterceptor.
64  	 * </p>
65  	 */
66  	public PmfmStrategyInterceptor() {
67  		super(ProgramStrategySynchroTables.PMFM_STRATEGY.name());
68  		setEnableOnWrite(true);
69  	}
70  
71  	/** {@inheritDoc} */
72  	@Override
73  	protected void doClose() throws IOException {
74  		super.doClose();
75  
76  		Daos.closeSilently(deletePmfmAppliedStrategyStatement);
77  		deletePmfmAppliedStrategyStatement = null;
78  	}
79  
80  	/**
81  	 * <p>
82  	 * handleQuery.
83  	 * </p>
84  	 * 
85  	 * @param e
86  	 *            a {@link fr.ifremer.common.synchro.meta.event.CreateQueryEvent} object.
87  	 */
88  	@Subscribe
89  	public void handleQuery(CreateQueryEvent e) {
90  
91  		switch (e.queryName) {
92  		case count:
93  		case countFromUpdateDate:
94  		case select:
95  		case selectFromUpdateDate:
96  		case selectMaxUpdateDate:
97  			// Add restriction
98  			e.sql = addRestriction(e.source, e.queryName, e.sql);
99  
100 		default:
101 			break;
102 		}
103 	}
104 
105 	/** {@inheritDoc} */
106 	@Override
107 	protected void doOnDelete(List<Object> pk, SynchroTableDao sourceDao, SynchroTableDao targetDao, SynchroOperationRepository buffer)
108 			throws SQLException {
109 		// Getting the pmfmStratId
110 		Integer pmfmStratId = Integer.parseInt(pk.get(0).toString());
111 
112 		// If need, delete referenced PMFM_APPLIED_STRATEGY
113 		if (getConfig().isIntegrityConstraintEnable()) {
114 			deletePmfmAppliedStrategyByPmfmStratId(targetDao, pmfmStratId);
115 		}
116 	}
117 
118 	/* -- Internal methods -- */
119 
120 	/**
121 	 * <p>
122 	 * addRestriction.
123 	 * </p>
124 	 * 
125 	 * @param table
126 	 *            a {@link fr.ifremer.common.synchro.meta.SynchroTableMetadata} object.
127 	 * @param queryName
128 	 *            a {@link fr.ifremer.common.synchro.query.SynchroQueryName} object.
129 	 * @param sql
130 	 *            a {@link java.lang.String} object.
131 	 * @return a {@link java.lang.String} object.
132 	 */
133 	protected String addRestriction(SynchroTableMetadata table, SynchroQueryName queryName, String sql) {
134 		SynchroQueryBuilder queryBuilder = SynchroQueryBuilder.newBuilder(sql);
135 
136 		// join: filter on program
137 		String programFilterJoin = createProgramWhereClauseOrNull(getConfig(), "INNER JOIN STRATEGY s ON s.strat_id=t.strat_id AND s.prog_cd IN (%s)");
138 		if (programFilterJoin != null) {
139 			queryBuilder.addJoin(programFilterJoin);
140 		}
141 
142 		return queryBuilder.build();
143 	}
144 
145 	/**
146 	 * <p>
147 	 * createDeletePmfmAppliedStrategyByPmfmStratIdSql.
148 	 * </p>
149 	 * 
150 	 * @return a {@link java.lang.String} object.
151 	 */
152 	protected String createDeletePmfmAppliedStrategyByPmfmStratIdSql() {
153 		return String.format("DELETE FROM %s where %s=?",
154 				ProgramStrategySynchroTables.PMFM_APPLIED_STRATEGY.name(),
155 				DatabaseColumns.PMFM_STRAT_ID);
156 	}
157 
158 	/**
159 	 * <p>
160 	 * deletePmfmAppliedStrategyByPmfmStratId.
161 	 * </p>
162 	 * 
163 	 * @param targetDao
164 	 *            a {@link fr.ifremer.common.synchro.dao.SynchroBaseDao} object.
165 	 * @param pmfmStratId
166 	 *            a int.
167 	 * @throws java.sql.SQLException
168 	 *             if any.
169 	 */
170 	protected void deletePmfmAppliedStrategyByPmfmStratId(SynchroBaseDao targetDao, int pmfmStratId) throws SQLException {
171 
172 		// Init the statement (if need)
173 		if (deletePmfmAppliedStrategyStatement == null || deletePmfmAppliedStrategyStatement.isClosed()) {
174 			deletePmfmAppliedStrategyStatement = targetDao.getPreparedStatement(createDeletePmfmAppliedStrategyByPmfmStratIdSql());
175 		}
176 
177 		// Execute the DELETE sql command
178 		deletePmfmAppliedStrategyStatement.setInt(1, pmfmStratId);
179 		deletePmfmAppliedStrategyStatement.executeUpdate();
180 	}
181 
182 	/* -- Internal methods -- */
183 
184 }