View Javadoc
1   package fr.ifremer.quadrige3.synchro.intercept.referential;
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.base.Joiner;
27  import com.google.common.collect.Sets;
28  import com.google.common.eventbus.Subscribe;
29  import fr.ifremer.common.synchro.intercept.SynchroInterceptorBase;
30  import fr.ifremer.common.synchro.meta.event.CreateQueryEvent;
31  import fr.ifremer.common.synchro.query.SynchroQueryBuilder;
32  import fr.ifremer.common.synchro.query.SynchroQueryOperator;
33  import fr.ifremer.quadrige3.core.dao.ObjectTypes;
34  import fr.ifremer.quadrige3.synchro.meta.DatabaseColumns;
35  import fr.ifremer.quadrige3.synchro.meta.referential.ReferentialSynchroTables;
36  import fr.ifremer.quadrige3.synchro.meta.system.RuleSynchroTables;
37  import fr.ifremer.quadrige3.synchro.service.referential.ReferentialSynchroDatabaseConfiguration;
38  import org.apache.commons.collections4.CollectionUtils;
39  
40  import java.util.LinkedHashSet;
41  import java.util.Set;
42  
43  /**
44   * Import DELETED_ITEM_HISTORY rows, limited to referential tables
45   * 
46   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
47   * @since 1.0
48   */
49  public class DeletedItemHistoryInterceptor extends AbstractReferentialInterceptor {
50  
51  	private String whereClauseOnTableIncludes = null;
52  
53  	/**
54  	 * <p>
55  	 * Constructor for DeletedItemHistoryInterceptor.
56  	 * </p>
57  	 */
58  	public DeletedItemHistoryInterceptor() {
59  		super(ReferentialSynchroTables.DELETED_ITEM_HISTORY.name());
60  	}
61  
62  	/** {@inheritDoc} */
63  	@Override
64  	protected void init(ReferentialSynchroDatabaseConfiguration config) {
65  		super.init(config);
66  		whereClauseOnTableIncludes = createWhereClauseOnTableIncludes();
67  
68  	}
69  
70  	/** {@inheritDoc} */
71  	@Override
72  	public SynchroInterceptorBase clone() {
73  		DeletedItemHistoryInterceptor newBean = (DeletedItemHistoryInterceptor) super.clone();
74  		newBean.whereClauseOnTableIncludes = this.whereClauseOnTableIncludes;
75  		return newBean;
76  	}
77  
78  	/**
79  	 * <p>
80  	 * handleQuery.
81  	 * </p>
82  	 * 
83  	 * @param e
84  	 *            a {@link fr.ifremer.common.synchro.meta.event.CreateQueryEvent} object.
85  	 */
86  	@Subscribe
87  	public void handleQuery(CreateQueryEvent e) {
88  		switch (e.queryName) {
89  		case count:
90  		case countFromUpdateDate:
91  		case selectMaxUpdateDate:
92  			e.sql = SynchroQueryBuilder.newBuilder(e.sql)
93  					.addWhere(SynchroQueryOperator.AND, whereClauseOnTableIncludes)
94  					.build();
95  			break;
96  		case select:
97  		case selectFromUpdateDate:
98  			e.sql = SynchroQueryBuilder.newBuilder(e.sql)
99  					.addWhere(SynchroQueryOperator.AND, whereClauseOnTableIncludes)
100 					// Keep original delete order (e.g. to import taxon deletion - mantis 28389)
101 					.addOrderByColumn("t." + DatabaseColumns.UPDATE_DT.name(), true/* ASC */)
102 					.build();
103 			break;
104 
105 		default:
106 			break;
107 		}
108 	}
109 
110 	private String createWhereClauseOnTableIncludes() {
111 		Set<String> tableToIncludes = new LinkedHashSet<>(ReferentialSynchroTables.getImportTablesIncludes());
112 		tableToIncludes.addAll(RuleSynchroTables.tableNames());
113 
114 		// Should return no lines :
115 		// - if no tables to import
116 		// - if status filter enable (see mantis #26399)
117 		if (CollectionUtils.isEmpty(tableToIncludes)
118 				|| hasStatusCodesIncludes()) {
119 			return "1=2";
120 		}
121 
122 		Set<String> allObjectTypes = Sets.newHashSet();
123 		for (String tableName : tableToIncludes) {
124 			Set<String> objectTypes = ObjectTypes.getObjectTypeFromTableName(tableName);
125 			if (CollectionUtils.isNotEmpty(objectTypes)) {
126 				allObjectTypes.addAll(objectTypes);
127 			}
128 			else {
129 				// By default, add the table name itself (i.e. PERSON_SESSION_VESSEL)
130 				allObjectTypes.add(tableName);
131 			}
132 		}
133 		return String.format("%s IN ('%s')",
134 				DatabaseColumns.OBJECT_TYPE_CD.name(),
135 				Joiner.on("','").join(allObjectTypes));
136 	}
137 
138 }