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