View Javadoc
1   package fr.ifremer.quadrige2.synchro.intercept.data;
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.meta.SynchroTableMetadata;
28  import fr.ifremer.common.synchro.meta.event.CreateQueryEvent;
29  import fr.ifremer.common.synchro.meta.event.LoadTableEvent;
30  import fr.ifremer.common.synchro.query.SynchroQueryBuilder;
31  import fr.ifremer.common.synchro.query.SynchroQueryOperator;
32  import fr.ifremer.quadrige2.core.dao.referential.ObjectTypeCode;
33  import fr.ifremer.quadrige2.synchro.intercept.data.internal.ExportFkRemoteIdInterceptor;
34  import fr.ifremer.quadrige2.synchro.intercept.data.internal.ImportRemoteIdInterceptor;
35  import fr.ifremer.quadrige2.synchro.meta.DatabaseColumns;
36  import fr.ifremer.quadrige2.synchro.meta.data.DataSynchroTables;
37  import fr.ifremer.quadrige2.synchro.service.SynchroDirection;
38  
39  /**
40   * Manage columns rewriting ('id' AND 'remote_id') on table QUALIFICATION_HISTORY
41   * 
42   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
43   * @since 1.0
44   */
45  public class QualificationHistoryInterceptor extends AbstractDataInterceptor {
46  
47  	/**
48  	 * <p>
49  	 * Constructor for QualificationHistoryInterceptor.
50  	 * </p>
51  	 */
52  	public QualificationHistoryInterceptor() {
53  		super(
54  				DataSynchroTables.QUALIFICATION_HISTORY.name(),
55  				// IMPORT: Server DB -> Temp DB
56  				SynchroDirection.IMPORT_TEMP2LOCAL,
57  				// EXPORT: Local DB -> Temp DB && Temp DB -> Server DB
58  				SynchroDirection.EXPORT_TEMP2SERVER,
59  				SynchroDirection.EXPORT_LOCAL2TEMP);
60  	}
61  
62  	/**
63  	 * <p>
64  	 * handleQuery.
65  	 * </p>
66  	 * 
67  	 * @param e
68  	 *            a {@link fr.ifremer.common.synchro.meta.event.CreateQueryEvent} object.
69  	 */
70  	@Subscribe
71  	public void handleQuery(CreateQueryEvent e) {
72  
73  		SynchroDirection direction = getConfig().getDirection();
74  
75  		switch (e.queryName) {
76  		// Select queries :
77  		case count:
78  		case countFromUpdateDate:
79  		case select:
80  		case selectFromUpdateDate:
81  		case selectMaxUpdateDate:
82  
83  			if (direction == SynchroDirection.IMPORT_TEMP2LOCAL) {
84  				e.sql = addRestrictionsOnImport(e.sql);
85  
86  			} else if (direction == SynchroDirection.EXPORT_LOCAL2TEMP
87  					|| direction == SynchroDirection.EXPORT_TEMP2SERVER) {
88  				e.sql = addRestrictionsOnExport(e.sql);
89  			}
90  			break;
91  		default:
92  			break;
93  		}
94  	}
95  
96  	/**
97  	 * <p>
98  	 * handleTableLoad.
99  	 * </p>
100 	 * 
101 	 * @param e
102 	 *            a {@link fr.ifremer.common.synchro.meta.event.LoadTableEvent} object.
103 	 */
104 	@Subscribe
105 	public void handleTableLoad(LoadTableEvent e) {
106 
107 		SynchroTableMetadata table = e.table;
108 		SynchroDirection direction = getConfig().getDirection();
109 
110 		// IMPORT: Temp DB -> Local DB
111 		if (direction == SynchroDirection.IMPORT_TEMP2LOCAL) {
112 
113 			// Create and configure a interceptor, to rewrite survey remote ID into local ID
114 			ImportRemoteIdInterceptor remoteIdInterceptor = new ImportRemoteIdInterceptor(
115 					getConfig(),
116 					DataSynchroTables.SURVEY.name(),
117 					DatabaseColumns.QUAL_HIST_ELEMENT_ID.name(),
118 					table.getSelectColumnIndex(DatabaseColumns.QUAL_HIST_ELEMENT_ID.name()),
119 					DatabaseColumns.SURVEY_ID.name(),
120 					false);
121 			table.addInterceptor(remoteIdInterceptor);
122 		}
123 
124 		// EXPORT: Temp DB -> Server DB
125 		else if (direction == SynchroDirection.EXPORT_TEMP2SERVER) {
126 
127 			// Create and configure a interceptor, to rewrite survey local ID into remote ID
128 			ExportFkRemoteIdInterceptor remoteIdInterceptor = new ExportFkRemoteIdInterceptor(
129 					getConfig(),
130 					DataSynchroTables.SURVEY.name(),
131 					DatabaseColumns.QUAL_HIST_ELEMENT_ID.name(),
132 					table.getSelectColumnIndex(DatabaseColumns.QUAL_HIST_ELEMENT_ID.name()),
133 					DatabaseColumns.SURVEY_ID.name(),
134 					false);
135 			table.addInterceptor(remoteIdInterceptor);
136 		}
137 
138 	}
139 
140 	/* -- Internal methods -- */
141 
142 	/**
143 	 * <p>
144 	 * add restrictions on import
145 	 * </p>
146 	 * 
147 	 * @param sql
148 	 *            a {@link java.lang.String} object.
149 	 * @return a {@link java.lang.String} object.
150 	 */
151 	protected String addRestrictionsOnImport(String sql) {
152 
153 		SynchroQueryBuilder query = SynchroQueryBuilder.newBuilder(sql);
154 
155 		// Limit to line on survey (object type code = PASS)
156 		query.addWhere(SynchroQueryOperator.AND, String.format("t.%s = '%s'",
157 				DatabaseColumns.OBJECT_TYPE_CD,
158 				ObjectTypeCode.SURVEY.value()));
159 
160 		return query.build();
161 	}
162 
163 	/**
164 	 * <p>
165 	 * add restrictions on export
166 	 * </p>
167 	 *
168 	 * @param sql
169 	 *            a {@link java.lang.String} object.
170 	 * @return a {@link java.lang.String} object.
171 	 */
172 	protected String addRestrictionsOnExport(String sql) {
173 
174 		SynchroQueryBuilder query = SynchroQueryBuilder.newBuilder(sql);
175 
176 		// Limit to line on survey (object type code = PASS)
177 		query.addWhere(SynchroQueryOperator.AND, String.format("t.%s = '%s'",
178 				DatabaseColumns.OBJECT_TYPE_CD,
179 				ObjectTypeCode.SURVEY.value()));
180 
181 		return query.build();
182 	}
183 
184 
185 }