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