View Javadoc
1   package fr.ifremer.quadrige2.synchro.service.referential;
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.base.Joiner;
27  import com.google.common.collect.Multimap;
28  import fr.ifremer.common.synchro.config.SynchroConfiguration;
29  import fr.ifremer.common.synchro.service.SynchroContext;
30  import fr.ifremer.quadrige2.synchro.service.SynchroDirection;
31  
32  import java.io.File;
33  import java.util.Set;
34  
35  /**
36   * <p>
37   * ReferentialSynchroContext class.
38   * </p>
39   * 
40   */
41  public class ReferentialSynchroContext extends SynchroContext {
42  
43  	private Integer userId;
44  
45  	private SynchroDirection direction;
46  
47  	/**
48  	 * Need to process deletion ?
49  	 */
50  	protected boolean enableDelete = true;
51  
52  	/**
53  	 * Need to process insert/update ?
54  	 */
55  	protected boolean enableInsertOrUpdate = true;
56  
57  	/**
58  	 * List of status to inludes. If null, include all
59  	 */
60  	protected Set<String> statusCodeIncludes;
61  
62  	/**
63  	 * Change log file, used to record all row operations (PKs for all insert/update/delete operations)
64  	 */
65  	protected File changeLogFile;
66  
67  	/**
68  	 * List of program codes
69  	 */
70  	protected Set<String> programCodes;
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  	 * Table to import, without check table max(update_dt) in method 'prepare()'.
79  	 * <p/>
80  	 * Useful to import new program/strategy, when user rights changes, even if [update_dt < lastSynchroDate]
81  	 */
82  	protected Set<String> tableNamesForced;
83  
84  	/**
85  	 * <p>
86  	 * Constructor for ReferentialSynchroContext.
87  	 * </p>
88  	 */
89  	public ReferentialSynchroContext() {
90  		super();
91  	}
92  
93  	/**
94  	 * <p>
95  	 * Constructor for ReferentialSynchroContext.
96  	 * </p>
97  	 * 
98  	 * @param direction
99  	 *            a {@link fr.ifremer.quadrige2.synchro.service.SynchroDirection} object.
100 	 * @param userId
101 	 *            a {@link java.lang.Integer} object.
102 	 */
103 	public ReferentialSynchroContext(SynchroDirection direction, Integer userId) {
104 		super();
105 		this.userId = userId;
106 		this.direction = direction;
107 	}
108 
109 	/**
110 	 * <p>
111 	 * Constructor for ReferentialSynchroContext.
112 	 * </p>
113 	 * 
114 	 * @param delegate
115 	 *            a {@link fr.ifremer.common.synchro.service.SynchroContext} object.
116 	 * @param direction
117 	 *            a {@link fr.ifremer.quadrige2.synchro.service.SynchroDirection} object.
118 	 * @param userId
119 	 *            a {@link java.lang.Integer} object.
120 	 */
121 	public ReferentialSynchroContext(SynchroContext delegate, SynchroDirection direction, Integer userId) {
122 		super(delegate);
123 		this.direction = direction;
124 		this.userId = userId;
125 	}
126 
127 	/**
128 	 * <p>
129 	 * isEnableDelete.
130 	 * </p>
131 	 * 
132 	 * @return a boolean.
133 	 */
134 	public boolean isEnableDelete() {
135 		return enableDelete;
136 	}
137 
138 	/**
139 	 * <p>
140 	 * Setter for the field <code>enableDelete</code>.
141 	 * </p>
142 	 * 
143 	 * @param enableDelete
144 	 *            a boolean.
145 	 */
146 	public void setEnableDelete(boolean enableDelete) {
147 		this.enableDelete = enableDelete;
148 	}
149 
150 	/**
151 	 * <p>
152 	 * isEnableInsertOrUpdate.
153 	 * </p>
154 	 * 
155 	 * @return a boolean.
156 	 */
157 	public boolean isEnableInsertOrUpdate() {
158 		return enableInsertOrUpdate;
159 	}
160 
161 	/**
162 	 * <p>
163 	 * Setter for the field <code>enableInsertOrUpdate</code>.
164 	 * </p>
165 	 * 
166 	 * @param enableInsertOrUpdate
167 	 *            a boolean.
168 	 */
169 	public void setEnableInsertOrUpdate(boolean enableInsertOrUpdate) {
170 		this.enableInsertOrUpdate = enableInsertOrUpdate;
171 	}
172 
173 	/**
174 	 * <p>
175 	 * Getter for the field <code>statusCodeIncludes</code>.
176 	 * </p>
177 	 * 
178 	 * @return a {@link java.util.Set} object.
179 	 */
180 	public Set<String> getStatusCodeIncludes() {
181 		return statusCodeIncludes;
182 	}
183 
184 	/**
185 	 * <p>
186 	 * Setter for the field <code>statusCodeIncludes</code>.
187 	 * </p>
188 	 * 
189 	 * @param statusCodeIncludes
190 	 *            a {@link java.util.Set} object.
191 	 */
192 	public void setStatusCodeIncludes(Set<String> statusCodeIncludes) {
193 		this.statusCodeIncludes = statusCodeIncludes;
194 	}
195 
196 	/**
197 	 * <p>
198 	 * Getter for the field <code>userId</code>.
199 	 * </p>
200 	 * 
201 	 * @return a {@link java.lang.Integer} object.
202 	 */
203 	public Integer getUserId() {
204 		return userId;
205 	}
206 
207 	/**
208 	 * <p>
209 	 * Setter for the field <code>userId</code>.
210 	 * </p>
211 	 * 
212 	 * @param userId
213 	 *            a {@link java.lang.Integer} object.
214 	 */
215 	public void setUserId(Integer userId) {
216 		this.userId = userId;
217 	}
218 
219 	/**
220 	 * <p>
221 	 * Getter for the field <code>direction</code>.
222 	 * </p>
223 	 * 
224 	 * @return a {@link fr.ifremer.quadrige2.synchro.service.SynchroDirection} object.
225 	 */
226 	public SynchroDirection getDirection() {
227 		return direction;
228 	}
229 
230 	/**
231 	 * <p>
232 	 * Setter for the field <code>direction</code>.
233 	 * </p>
234 	 * 
235 	 * @param direction
236 	 *            a {@link fr.ifremer.quadrige2.synchro.service.SynchroDirection} object.
237 	 */
238 	public void setDirection(SynchroDirection direction) {
239 		this.direction = direction;
240 	}
241 
242 	/**
243 	 * <p>
244 	 * Getter for the field <code>pkIncludes</code>.
245 	 * </p>
246 	 * 
247 	 * @return a {@link com.google.common.collect.Multimap} object.
248 	 */
249 	public Multimap<String, String> getPkIncludes() {
250 		return pkIncludes;
251 	}
252 
253 	/**
254 	 * <p>
255 	 * Setter for the field <code>pkIncludes</code>.
256 	 * </p>
257 	 * 
258 	 * @param pkIncludes
259 	 *            a {@link com.google.common.collect.Multimap} object.
260 	 */
261 	public void setPkIncludes(Multimap<String, String> pkIncludes) {
262 		this.pkIncludes = pkIncludes;
263 	}
264 
265 	/**
266 	 * <p>
267 	 * Getter for the field <code>changeLogFile</code>.
268 	 * </p>
269 	 * 
270 	 * @return a {@link java.io.File} object.
271 	 */
272 	public File getChangeLogFile() {
273 		return changeLogFile;
274 	}
275 
276 	/**
277 	 * <p>
278 	 * Setter for the field <code>changeLogFile</code>.
279 	 * </p>
280 	 * 
281 	 * @param changeLogFile
282 	 *            a {@link java.io.File} object.
283 	 */
284 	public void setChangeLogFile(File changeLogFile) {
285 		this.changeLogFile = changeLogFile;
286 	}
287 
288 	/**
289 	 * <p>
290 	 * Getter for the field <code>programCodes</code>.
291 	 * </p>
292 	 * 
293 	 * @return a {@link java.util.Set} object.
294 	 */
295 	public Set<String> getProgramCodes() {
296 		return programCodes;
297 	}
298 
299 	/**
300 	 * <p>
301 	 * Setter for the field <code>programCodes</code>.
302 	 * </p>
303 	 * 
304 	 * @param programCodes
305 	 *            a {@link java.util.Set} object.
306 	 */
307 	public void setProgramCodes(Set<String> programCodes) {
308 		this.programCodes = programCodes;
309 	}
310 
311 	/**
312 	 * <p>
313 	 * Setter for the field <code>tableNamesForced</code>.
314 	 * </p>
315 	 * 
316 	 * @param tableNamesForced
317 	 *            a {@link java.util.Set} object.
318 	 */
319 	public void setTableNamesForced(Set<String> tableNamesForced) {
320 		this.tableNamesForced = tableNamesForced;
321 	}
322 
323 	/**
324 	 * <p>
325 	 * Getter for the field <code>tableNamesForced</code>.
326 	 * </p>
327 	 * 
328 	 * @return a {@link java.util.Set} object.
329 	 */
330 	public Set<String> getTableNamesForced() {
331 		return tableNamesForced;
332 	}
333 
334 	/** {@inheritDoc} */
335 	@Override
336 	public void copy(SynchroContext otherBean) {
337 		super.copy(otherBean);
338 
339 		SynchroConfiguration config = SynchroConfiguration.getInstance();
340 		// Change database configuration class
341 		if (otherBean.getTarget() != null) {
342 			setTarget(new ReferentialSynchroDatabaseConfiguration(this, otherBean.getTarget()));
343 		}
344 		if (otherBean.getSource() != null) {
345 			setSource(new ReferentialSynchroDatabaseConfiguration(this, otherBean.getSource()));
346 		}
347 
348 		if (otherBean instanceof ReferentialSynchroContext) {
349 			this.direction = ((ReferentialSynchroContext) otherBean).direction;
350 			this.enableDelete = ((ReferentialSynchroContext) otherBean).enableDelete;
351 			this.enableInsertOrUpdate = ((ReferentialSynchroContext) otherBean).enableInsertOrUpdate;
352 			this.statusCodeIncludes = ((ReferentialSynchroContext) otherBean).statusCodeIncludes;
353 			this.userId = ((ReferentialSynchroContext) otherBean).userId;
354 			this.pkIncludes = ((ReferentialSynchroContext) otherBean).pkIncludes;
355 			this.changeLogFile = ((ReferentialSynchroContext) otherBean).changeLogFile;
356 			this.programCodes = ((ReferentialSynchroContext) otherBean).programCodes;
357 			this.tableNamesForced = ((ReferentialSynchroContext) otherBean).tableNamesForced;
358 		}
359 	}
360 
361 	/** {@inheritDoc} */
362 	@Override
363 	public String toString() {
364 		return new StringBuilder()
365 				.append(super.toString())
366 				.append("\n  direction: ").append(direction)
367 				.append("\n  enable update/insert: ").append(isEnableInsertOrUpdate())
368 				.append("\n  enable delete: ").append(isEnableDelete())
369 				.append("\n  programs: ").append(programCodes == null ? "all" : programCodes.toString())
370 				.append("\n  change log file: ").append(changeLogFile == null ? "disable" : changeLogFile.getPath())
371 				.append("\n  status codes: ").append(statusCodeIncludes == null ? "all" : Joiner.on('\'').join(statusCodeIncludes))
372 				.append("\n  user: ").append(getUserId())
373 				.append("\n  pkIncludes: ").append(pkIncludes == null ? "all" : pkIncludes.toString())
374 				.toString();
375 	}
376 
377 }