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