View Javadoc
1   package fr.ifremer.quadrige2.synchro.intercept.referential;
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.intercept.SynchroInterceptorBase;
28  import fr.ifremer.common.synchro.meta.SynchroTableMetadata;
29  import fr.ifremer.common.synchro.meta.event.CreateQueryEvent;
30  import fr.ifremer.common.synchro.meta.event.LoadTableEvent;
31  import fr.ifremer.common.synchro.query.SynchroQueryBuilder;
32  import fr.ifremer.common.synchro.query.SynchroQueryName;
33  import fr.ifremer.common.synchro.query.SynchroQueryOperator;
34  import fr.ifremer.quadrige2.core.dao.referential.StatusCode;
35  import fr.ifremer.quadrige2.synchro.meta.DatabaseColumns;
36  import fr.ifremer.quadrige2.synchro.meta.referential.ReferentialSynchroTables;
37  import fr.ifremer.quadrige2.synchro.service.SynchroDirection;
38  import fr.ifremer.quadrige2.synchro.service.referential.ReferentialSynchroDatabaseConfiguration;
39  import org.apache.commons.collections4.CollectionUtils;
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  import java.util.Set;
44  
45  /**
46   * Manage table TAXON_REFERENCE :
47   * <ul>
48   * <li>Define this table as a root table, even if filtering on status</li>
49   * <li>If filtering on a local status, then add a where clause 'REF_TAXON_ID &lt; 0'</li>
50   * </ul>
51   * 
52   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
53   * @since 1.0
54   */
55  public class ReferenceTaxonInterceptor extends AbstractReferentialInterceptor {
56  
57  	private static final Log log = LogFactory.getLog(ReferenceTaxonInterceptor.class);
58  
59  	private String localOnlyWhereClause;
60  
61  	/**
62  	 * <p>
63  	 * Constructor for ReferenceTaxonInterceptor.
64  	 * </p>
65  	 */
66  	public ReferenceTaxonInterceptor() {
67  		super(ReferentialSynchroTables.REFERENCE_TAXON.name());
68  	}
69  
70  	/** {@inheritDoc} */
71  	@Override
72  	protected void init(ReferentialSynchroDatabaseConfiguration config) {
73  		localOnlyWhereClause = createLocalOnlyWhereClause(config);
74  	}
75  
76  	/** {@inheritDoc} */
77  	@Override
78  	public SynchroInterceptorBase clone() {
79  		ReferenceTaxonInterceptor result = (ReferenceTaxonInterceptor) super.clone();
80  		result.localOnlyWhereClause = this.localOnlyWhereClause;
81  		return result;
82  	}
83  
84  	/**
85  	 * <p>
86  	 * handleQuery.
87  	 * </p>
88  	 * 
89  	 * @param e
90  	 *            a {@link fr.ifremer.common.synchro.meta.event.CreateQueryEvent} object.
91  	 */
92  	@Subscribe
93  	public void handleQuery(CreateQueryEvent e) {
94  
95  		switch (e.queryName) {
96  		case count:
97  		case countFromUpdateDate:
98  		case select:
99  		case selectFromUpdateDate:
100 		case selectMaxUpdateDate:
101 			// Add restriction
102 			e.sql = addRestriction(e.source, e.queryName, e.sql);
103 
104 		default:
105 			break;
106 		}
107 	}
108 
109 	/**
110 	 * <p>
111 	 * handleTableLoad.
112 	 * </p>
113 	 * 
114 	 * @param e
115 	 *            a {@link fr.ifremer.common.synchro.meta.event.LoadTableEvent} object.
116 	 */
117 	@Subscribe
118 	public void handleTableLoad(LoadTableEvent e) {
119 		SynchroTableMetadata table = e.table;
120 		SynchroDirection direction = getConfig().getDirection();
121 
122 		// Set has root (if not already done by ReferentialTableInterceptor)
123 		if (!table.isRoot()
124 				&& localOnlyWhereClause != null) {
125 			table.setRoot(true);
126 		}
127 	}
128 
129 	/* -- Internal methods -- */
130 
131 	/**
132 	 * <p>
133 	 * createLocalOnlyWhereClause.
134 	 * </p>
135 	 * 
136 	 * @param config
137 	 *            a {@link fr.ifremer.quadrige2.synchro.service.referential.ReferentialSynchroDatabaseConfiguration}
138 	 *            object.
139 	 * @return a {@link java.lang.String} object.
140 	 */
141 	protected String createLocalOnlyWhereClause(ReferentialSynchroDatabaseConfiguration config) {
142 
143 		Set<String> statusCodeIncludes = config.getStatusCodeIncludes();
144 		if (CollectionUtils.isEmpty(statusCodeIncludes)
145 				|| (!statusCodeIncludes.contains(StatusCode.LOCAL_DISABLE.getValue())
146 				&& !statusCodeIncludes.contains(StatusCode.LOCAL_ENABLE.getValue()))) {
147 			return null;
148 		}
149 
150 		// Filter on ID, because this table does NOT have a STATUS_CD
151 		return String.format("t.%s < 0",
152 				DatabaseColumns.REF_TAXON_ID);
153 	}
154 
155 	/**
156 	 * <p>
157 	 * addRestriction.
158 	 * </p>
159 	 * 
160 	 * @param table
161 	 *            a {@link fr.ifremer.common.synchro.meta.SynchroTableMetadata} object.
162 	 * @param queryName
163 	 *            a {@link fr.ifremer.common.synchro.query.SynchroQueryName} object.
164 	 * @param sql
165 	 *            a {@link java.lang.String} object.
166 	 * @return a {@link java.lang.String} object.
167 	 */
168 	protected String addRestriction(SynchroTableMetadata table, SynchroQueryName queryName, String sql) {
169 
170 		// where: add status filter
171 		if (localOnlyWhereClause == null) {
172 			return sql;
173 		}
174 
175 		SynchroQueryBuilder queryBuilder = SynchroQueryBuilder.newBuilder(sql);
176 		queryBuilder.addWhere(SynchroQueryOperator.AND, localOnlyWhereClause);
177 
178 		return queryBuilder.build();
179 	}
180 
181 }