View Javadoc
1   package fr.ifremer.quadrige2.synchro.service.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.collect.Multimap;
27  import fr.ifremer.common.synchro.service.SynchroContext;
28  import fr.ifremer.quadrige2.synchro.service.SynchroDirection;
29  import fr.ifremer.quadrige2.synchro.vo.SynchroDateOperatorVO;
30  
31  import java.io.File;
32  import java.util.Date;
33  import java.util.Map;
34  import java.util.Set;
35  
36  /**
37   * <p>
38   * DataSynchroContext class.
39   * </p>
40   * 
41   */
42  public class DataSynchroContext extends SynchroContext {
43  
44  	private Integer userId;
45  
46  	private SynchroDirection direction;
47  
48  	protected Date dataStartDate;
49  
50  	protected Date dataEndDate;
51  
52  	/**
53  	 * Optional date operator handling dataStartDate and/or dataEndDate
54  	 */
55  	protected SynchroDateOperatorVO dateOperator;
56  
57  	/**
58  	 * Need to filter on 'dirty' synchronization status ?
59  	 */
60  	protected boolean dirtyOnly;
61  
62  	/**
63  	 * Need to process deletion ?
64  	 */
65  	protected boolean enableDelete = true;
66  
67  	/**
68  	 * Need to process insert/update ?
69  	 */
70  	protected boolean enableInsertOrUpdate = true;
71  
72  	/**
73  	 * Map of table/PK to includes. Use to limit import on some data (e.g. after a revert on data)
74  	 */
75  	protected Multimap<String, String> pkIncludes;
76  
77  	/**
78  	 * List of program codes
79  	 */
80  	protected Set<String> programCodes;
81  
82  	/**
83  	 * Could an edited row (synchronizationStatus <> SYNC) be override silently ?
84  	 * <p/>
85  	 * Default is <code>false</code>. Set to <code>true</code> to avoid rejected rows, when importing by Pk (with a
86  	 * pkIncludes) (See class ImportEditedRowInterceptor for usage)
87  	 */
88  	protected boolean forceEditedRowOverride = false;
89  
90  	/**
91  	 * A file to write all changes (see ChangeLogInterceptor)
92  	 */
93  	protected File changeLogFile;
94  
95  	/**
96  	 * If true, force data to be duplicate when imported from a file
97  	 */
98  	protected boolean forceDuplication = false;
99  
100 	/**
101 	 * Use to transcribing column values at write time
102 	 */
103 	protected Map<String, Map<String, Map<String, Object>>> remapValues;
104 
105 	/**
106 	 * <p>
107 	 * Constructor for DataSynchroContext.
108 	 * </p>
109 	 */
110 	public DataSynchroContext() {
111 		super();
112 		this.userId = null;
113 		this.direction = null;
114 	}
115 
116 	/**
117 	 * <p>
118 	 * Constructor for DataSynchroContext.
119 	 * </p>
120 	 * 
121 	 * @param otherBean
122 	 *            a {@link fr.ifremer.quadrige2.synchro.service.data.DataSynchroContext} object.
123 	 */
124 	public DataSynchroContext(DataSynchroContext otherBean) {
125 		copy(otherBean);
126 	}
127 
128 	/**
129 	 * <p>
130 	 * Constructor for DataSynchroContext.
131 	 * </p>
132 	 * 
133 	 * @param direction
134 	 *            a {@link fr.ifremer.quadrige2.synchro.service.SynchroDirection} object.
135 	 * @param userId
136 	 *            a {@link java.lang.Integer} object.
137 	 */
138 	public DataSynchroContext(SynchroDirection direction, Integer userId) {
139 		super();
140 		this.userId = userId;
141 		this.direction = direction;
142 	}
143 
144 	/**
145 	 * <p>
146 	 * Constructor for DataSynchroContext.
147 	 * </p>
148 	 * 
149 	 * @param delegate
150 	 *            a {@link fr.ifremer.common.synchro.service.SynchroContext} object.
151 	 * @param direction
152 	 *            a {@link fr.ifremer.quadrige2.synchro.service.SynchroDirection} object.
153 	 * @param userId
154 	 *            a {@link java.lang.Integer} object.
155 	 */
156 	public DataSynchroContext(SynchroContext delegate, SynchroDirection direction, Integer userId) {
157 		super(delegate);
158 		this.userId = userId;
159 		this.direction = direction;
160 	}
161 
162 	/**
163 	 * <p>
164 	 * Constructor for DataSynchroContext.
165 	 * </p>
166 	 * 
167 	 * @param delegate
168 	 *            a {@link fr.ifremer.common.synchro.service.SynchroContext} object.
169 	 * @param direction
170 	 *            a {@link fr.ifremer.quadrige2.synchro.service.SynchroDirection} object.
171 	 * @param userId
172 	 *            a {@link java.lang.Integer} object.
173 	 * @param sessionId
174 	 *            a {@link java.lang.Integer} object.
175 	 */
176 	public DataSynchroContext(SynchroContext delegate, SynchroDirection direction, Integer userId, Integer sessionId) {
177 		super(delegate);
178 		this.userId = userId;
179 		this.direction = direction;
180 	}
181 
182 	/**
183 	 * <p>
184 	 * Getter for the field <code>userId</code>.
185 	 * </p>
186 	 * 
187 	 * @return a {@link java.lang.Integer} object.
188 	 */
189 	public Integer getUserId() {
190 		return userId;
191 	}
192 
193 	/**
194 	 * <p>
195 	 * Setter for the field <code>userId</code>.
196 	 * </p>
197 	 * 
198 	 * @param userId
199 	 *            a {@link java.lang.Integer} object.
200 	 */
201 	public void setUserId(Integer userId) {
202 		this.userId = userId;
203 	}
204 
205 	/**
206 	 * <p>
207 	 * Getter for the field <code>direction</code>.
208 	 * </p>
209 	 * 
210 	 * @return a {@link fr.ifremer.quadrige2.synchro.service.SynchroDirection} object.
211 	 */
212 	public SynchroDirection getDirection() {
213 		return direction;
214 	}
215 
216 	/**
217 	 * <p>
218 	 * Setter for the field <code>direction</code>.
219 	 * </p>
220 	 * 
221 	 * @param direction
222 	 *            a {@link fr.ifremer.quadrige2.synchro.service.SynchroDirection} object.
223 	 */
224 	public void setDirection(SynchroDirection direction) {
225 		this.direction = direction;
226 	}
227 
228 	/**
229 	 * <p>
230 	 * Getter for the field <code>dataStartDate</code>.
231 	 * </p>
232 	 * 
233 	 * @return a {@link java.util.Date} object.
234 	 */
235 	public Date getDataStartDate() {
236 		return dataStartDate;
237 	}
238 
239 	/**
240 	 * <p>
241 	 * Setter for the field <code>dataStartDate</code>.
242 	 * </p>
243 	 * 
244 	 * @param dataStartDate
245 	 *            a {@link java.util.Date} object.
246 	 */
247 	public void setDataStartDate(Date dataStartDate) {
248 		this.dataStartDate = dataStartDate;
249 	}
250 
251 	/**
252 	 * <p>
253 	 * Getter for the field <code>dataEndDate</code>.
254 	 * </p>
255 	 * 
256 	 * @return a {@link java.util.Date} object.
257 	 */
258 	public Date getDataEndDate() {
259 		return dataEndDate;
260 	}
261 
262 	/**
263 	 * <p>
264 	 * Setter for the field <code>dataEndDate</code>.
265 	 * </p>
266 	 * 
267 	 * @param dataEndDate
268 	 *            a {@link java.util.Date} object.
269 	 */
270 	public void setDataEndDate(Date dataEndDate) {
271 		this.dataEndDate = dataEndDate;
272 	}
273 
274 	/**
275 	 * <p>
276 	 * Setter for the field <code>pkIncludes</code>.
277 	 * </p>
278 	 * 
279 	 * @param pkIncludes
280 	 *            a {@link com.google.common.collect.Multimap} object.
281 	 */
282 	public void setPkIncludes(Multimap<String, String> pkIncludes) {
283 		this.pkIncludes = pkIncludes;
284 	}
285 
286 	/**
287 	 * <p>
288 	 * Getter for the field <code>pkIncludes</code>.
289 	 * </p>
290 	 * 
291 	 * @return a {@link com.google.common.collect.Multimap} object.
292 	 */
293 	public Multimap<String, String> getPkIncludes() {
294 		return this.pkIncludes;
295 	}
296 
297 	/**
298 	 * <p>
299 	 * Getter for the field <code>programCodes</code>.
300 	 * </p>
301 	 * 
302 	 * @return a {@link java.util.Set} object.
303 	 */
304 	public Set<String> getProgramCodes() {
305 		return programCodes;
306 	}
307 
308 	/**
309 	 * <p>
310 	 * Setter for the field <code>programCodes</code>.
311 	 * </p>
312 	 * 
313 	 * @param programCodes
314 	 *            a {@link java.util.Set} object.
315 	 */
316 	public void setProgramCodes(Set<String> programCodes) {
317 		this.programCodes = programCodes;
318 	}
319 
320 	/**
321 	 * <p>
322 	 * isEnableDelete.
323 	 * </p>
324 	 * 
325 	 * @return a boolean.
326 	 */
327 	public boolean isEnableDelete() {
328 		return enableDelete;
329 	}
330 
331 	/**
332 	 * <p>
333 	 * Setter for the field <code>enableDelete</code>.
334 	 * </p>
335 	 * 
336 	 * @param enableDelete
337 	 *            a boolean.
338 	 */
339 	public void setEnableDelete(boolean enableDelete) {
340 		this.enableDelete = enableDelete;
341 	}
342 
343 	/**
344 	 * <p>
345 	 * isEnableInsertOrUpdate.
346 	 * </p>
347 	 * 
348 	 * @return a boolean.
349 	 */
350 	public boolean isEnableInsertOrUpdate() {
351 		return enableInsertOrUpdate;
352 	}
353 
354 	/**
355 	 * <p>
356 	 * Setter for the field <code>enableInsertOrUpdate</code>.
357 	 * </p>
358 	 * 
359 	 * @param enableInsertOrUpdate
360 	 *            a boolean.
361 	 */
362 	public void setEnableInsertOrUpdate(boolean enableInsertOrUpdate) {
363 		this.enableInsertOrUpdate = enableInsertOrUpdate;
364 	}
365 
366 	/**
367 	 * <p>
368 	 * isForceEditedRowOverride.
369 	 * </p>
370 	 * 
371 	 * @return a boolean.
372 	 */
373 	public boolean isForceEditedRowOverride() {
374 		return forceEditedRowOverride;
375 	}
376 
377 	/**
378 	 * <p>
379 	 * Setter for the field <code>forceEditedRowOverride</code>.
380 	 * </p>
381 	 * 
382 	 * @param forceEditedRowOverride
383 	 *            a boolean.
384 	 */
385 	public void setForceEditedRowOverride(boolean forceEditedRowOverride) {
386 		this.forceEditedRowOverride = forceEditedRowOverride;
387 	}
388 
389 	/**
390 	 * <p>
391 	 * Getter for the field <code>changeLogFile</code>.
392 	 * </p>
393 	 * 
394 	 * @return a {@link java.io.File} object.
395 	 */
396 	public File getChangeLogFile() {
397 		return changeLogFile;
398 	}
399 
400 	/**
401 	 * <p>
402 	 * Setter for the field <code>changeLogFile</code>.
403 	 * </p>
404 	 * 
405 	 * @param changeLogFile
406 	 *            a {@link java.io.File} object.
407 	 */
408 	public void setChangeLogFile(File changeLogFile) {
409 		this.changeLogFile = changeLogFile;
410 	}
411 
412 	/**
413 	 * <p>
414 	 * isForceDuplication.
415 	 * </p>
416 	 * 
417 	 * @return a boolean.
418 	 */
419 	public boolean isForceDuplication() {
420 		return forceDuplication;
421 	}
422 
423 	/**
424 	 * <p>
425 	 * Setter for the field <code>forceDuplication</code>.
426 	 * </p>
427 	 * 
428 	 * @param forceDuplication
429 	 *            a boolean.
430 	 */
431 	public void setForceDuplication(boolean forceDuplication) {
432 		this.forceDuplication = forceDuplication;
433 	}
434 
435 	/**
436 	 * <p>
437 	 * Getter for the field <code>remapValues</code>.
438 	 * </p>
439 	 * 
440 	 * @return a {@link java.util.Map} object.
441 	 */
442 	public Map<String, Map<String, Map<String, Object>>> getRemapValues() {
443 		return remapValues;
444 	}
445 
446 	/**
447 	 * <p>
448 	 * Setter for the field <code>remapValues</code>.
449 	 * </p>
450 	 * 
451 	 * @param remapValues
452 	 *            a {@link java.util.Map} object.
453 	 */
454 	public void setRemapValues(Map<String, Map<String, Map<String, Object>>> remapValues) {
455 		this.remapValues = remapValues;
456 	}
457 
458 	/**
459 	 * <p>
460 	 * Getter for the field <code>dateOperator</code>.
461 	 * </p>
462 	 * 
463 	 * @return a {@link fr.ifremer.quadrige2.synchro.vo.SynchroDateOperatorVO} object.
464 	 */
465 	public SynchroDateOperatorVO getDateOperator() {
466 		return dateOperator;
467 	}
468 
469 	/**
470 	 * <p>
471 	 * Setter for the field <code>dateOperator</code>.
472 	 * </p>
473 	 * 
474 	 * @param dateOperator
475 	 *            a {@link fr.ifremer.quadrige2.synchro.vo.SynchroDateOperatorVO} object.
476 	 */
477 	public void setDateOperator(SynchroDateOperatorVO dateOperator) {
478 		this.dateOperator = dateOperator;
479 	}
480 
481 	/**
482 	 * <p>
483 	 * isDirtyOnly.
484 	 * </p>
485 	 * 
486 	 * @return a boolean.
487 	 */
488 	public boolean isDirtyOnly() {
489 		return dirtyOnly;
490 	}
491 
492 	/**
493 	 * <p>
494 	 * Setter for the field <code>dirtyOnly</code>.
495 	 * </p>
496 	 * 
497 	 * @param dirtyOnly
498 	 *            a boolean.
499 	 */
500 	public void setDirtyOnly(boolean dirtyOnly) {
501 		this.dirtyOnly = dirtyOnly;
502 	}
503 
504 	/** {@inheritDoc} */
505 	@Override
506 	public void copy(SynchroContext otherBean) {
507 		super.copy(otherBean);
508 
509 		// Change database configuration class
510 		if (otherBean.getTarget() != null) {
511 			setTarget(new DataSynchroDatabaseConfiguration(this, otherBean.getTarget()));
512 		}
513 		if (otherBean.getSource() != null) {
514 			setSource(new DataSynchroDatabaseConfiguration(this, otherBean.getSource()));
515 		}
516 
517 		if (otherBean instanceof DataSynchroContext) {
518 			this.userId = ((DataSynchroContext) otherBean).userId;
519 			this.direction = ((DataSynchroContext) otherBean).direction;
520 			this.dataStartDate = ((DataSynchroContext) otherBean).dataStartDate;
521 			this.dataEndDate = ((DataSynchroContext) otherBean).dataEndDate;
522 			this.dateOperator = ((DataSynchroContext) otherBean).dateOperator;
523 			this.dirtyOnly = ((DataSynchroContext) otherBean).dirtyOnly;
524 			this.pkIncludes = ((DataSynchroContext) otherBean).pkIncludes;
525 			this.programCodes = ((DataSynchroContext) otherBean).programCodes;
526 			this.enableDelete = ((DataSynchroContext) otherBean).enableDelete;
527 			this.enableInsertOrUpdate = ((DataSynchroContext) otherBean).enableInsertOrUpdate;
528 			this.forceEditedRowOverride = ((DataSynchroContext) otherBean).forceEditedRowOverride;
529 			this.forceDuplication = ((DataSynchroContext) otherBean).forceDuplication;
530 			this.remapValues = ((DataSynchroContext) otherBean).remapValues;
531 			this.changeLogFile = ((DataSynchroContext) otherBean).changeLogFile;
532 		}
533 	}
534 
535 	/** {@inheritDoc} */
536 	@Override
537 	public String toString() {
538 		StringBuilder sb = new StringBuilder(super.toString());
539 		sb.append("\n        direction: ").append(getDirection());
540 		sb.append("\n          user id: ").append(getUserId());
541 		sb.append("\n    enable insert: ").append(isEnableInsertOrUpdate());
542 		sb.append("\n    enable delete: ").append(isEnableDelete());
543 		sb.append("\n  override dirty")
544 				.append("\n    rows silently: ").append(forceEditedRowOverride);
545 		sb.append("\n force duplicates: ").append(forceDuplication);
546 		sb.append("\n  only dirty data: ").append(dirtyOnly);
547 
548 		if (getDataStartDate() != null) {
549 			sb.append("\n   start date: ").append(getDataStartDate());
550 			sb.append("\n     end date: ").append(getDataEndDate());
551 			if (getDateOperator() != null) {
552 				sb.append("\n     operator: ").append(getDateOperator());
553 			}
554 		}
555 		if (getProgramCodes() != null) {
556 			sb.append("\n     programs: ").append(programCodes == null ? "all" : programCodes.toString());
557 		}
558 		if (getPkIncludes() != null) {
559 			sb.append("\n  PK includes: ").append(getPkIncludes().toString());
560 		}
561 
562 		return sb.toString();
563 	}
564 
565 }