View Javadoc
1   package fr.ifremer.quadrige3.synchro.intercept.data;
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.collect.ImmutableSet;
27  import com.google.common.collect.Lists;
28  import com.google.common.eventbus.Subscribe;
29  import fr.ifremer.common.synchro.dao.SynchroTableDao;
30  import fr.ifremer.common.synchro.intercept.SynchroInterceptorBase;
31  import fr.ifremer.common.synchro.intercept.SynchroOperationRepository;
32  import fr.ifremer.common.synchro.meta.event.LoadJoinEvent;
33  import fr.ifremer.common.synchro.meta.event.LoadTableEvent;
34  import fr.ifremer.quadrige3.core.dao.referential.ObjectTypeCode;
35  import fr.ifremer.quadrige3.synchro.meta.DatabaseColumns;
36  import fr.ifremer.quadrige3.synchro.meta.data.DataSynchroTables;
37  import fr.ifremer.quadrige3.synchro.service.SynchroDirection;
38  
39  import java.sql.SQLException;
40  import java.util.List;
41  
42  /**
43   * <p>
44   * SamplingOperationInterceptor class.
45   * </p>
46   * 
47   */
48  public class SamplingOperationInterceptor extends AbstractDataInterceptor {
49  
50  	private int columnSamplingOperationIdIndex = -1;
51  
52  	/**
53  	 * <p>
54  	 * Constructor for SamplingOperationInterceptor.
55  	 * </p>
56  	 */
57  	public SamplingOperationInterceptor() {
58  
59  		super(DataSynchroTables.SAMPLING_OPERATION.name());
60  		setEnableOnWrite(true);
61  
62  	}
63  
64  	@Override
65  	public SynchroInterceptorBase clone() {
66  		SamplingOperationInterceptor clone = (SamplingOperationInterceptor) super.clone();
67  		clone.columnSamplingOperationIdIndex = this.columnSamplingOperationIdIndex;
68  		return clone;
69  	}
70  
71  	@Subscribe
72  	public void handleLoadTable(LoadTableEvent e) {
73  		columnSamplingOperationIdIndex = e.table.getSelectColumnIndex(DatabaseColumns.SAMPLING_OPER_ID.name());
74  	}
75  
76  	/**
77  	 * <p>
78  	 * handleJoinLoad.
79  	 * </p>
80  	 * 
81  	 * @param e
82  	 *            a {@link fr.ifremer.common.synchro.meta.event.LoadJoinEvent} object.
83  	 */
84  	@Subscribe
85  	public void handleJoinLoad(LoadJoinEvent e) {
86  
87  		if (!e.join.isValid()) {
88  			return;
89  		}
90  
91  		String targetTableName = e.join.getTargetTable().getName();
92  		SynchroDirection direction = getConfig().getDirection();
93  
94  		// Server DB -> Temp DB
95  		if (direction == SynchroDirection.IMPORT_SERVER2TEMP) {
96  			// Disable join to Measurement (already processed in SurveyInterceptor)
97  			if (DataSynchroTables.MEASUREMENT.name().equalsIgnoreCase(targetTableName)) {
98  				e.join.setIsValid(false);
99  			}
100 
101 			// Disable join to TaxonMeasurement (already processed in SurveyInterceptor)
102 			if (DataSynchroTables.TAXON_MEASUREMENT.name().equalsIgnoreCase(targetTableName)) {
103 				e.join.setIsValid(false);
104 			}
105 
106 			// Disable join to TaxonMeasurement (already processed in SurveyInterceptor)
107 			if (DataSynchroTables.MEASUREMENT_FILE.name().equalsIgnoreCase(targetTableName)) {
108 				e.join.setIsValid(false);
109 			}
110 
111 			// Disable join to Photo (already processed in SurveyInterceptor)
112 			if (DataSynchroTables.PHOTO.name().equalsIgnoreCase(targetTableName)) {
113 				e.join.setIsValid(false);
114 			}
115 		}
116 	}
117 
118 	@Override
119 	protected void doOnWrite(Object[] data, List<Object> pk, SynchroTableDao sourceDao, SynchroTableDao targetDao, SynchroOperationRepository buffer, boolean insert) throws SQLException {
120 
121 		// If root entity: no transformation
122 		if (buffer == null) {
123 			return;
124 		}
125 
126 		Object samplingOperationId = data[columnSamplingOperationIdIndex];
127 
128 		if (samplingOperationId != null) {
129 			buffer.addChildToUpdateFromManyColumns(DataSynchroTables.QUALIFICATION_HISTORY.name(),
130 				ImmutableSet.of(DatabaseColumns.OBJECT_TYPE_CD.name(), DatabaseColumns.QUAL_HIST_ELEMENT_ID.name()),
131 				Lists.newArrayList(ObjectTypeCode.SAMPLING_OPERATION.value(), Long.parseLong(samplingOperationId.toString())));
132 		}
133 	}
134 
135 	/* -- Internal methods -- */
136 
137 }