View Javadoc
1   package fr.ifremer.adagio.synchro.service;
2   
3   /*
4    * #%L
5    * SIH-Adagio :: Synchronization
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2014 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 java.util.Map;
27  import java.util.Properties;
28  import java.util.Set;
29  
30  import com.google.common.base.Predicate;
31  import com.google.common.collect.Maps;
32  
33  import fr.ifremer.adagio.synchro.meta.SynchroMetadataUtils;
34  
35  /**
36   * A context class, need for referential and data synchronization
37   * Created on 5/05/14.
38   * 
39   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
40   * @since 3.5.3
41   */
42  public class SynchroContext {
43  
44  	protected Properties sourceConnectionProperties;
45  
46  	protected Properties targetConnectionProperties;
47  
48  	protected SynchroResult result;
49  
50  	protected Predicate<String> tableFilter;
51  
52  	protected Set<String> tableNames;
53  
54  	protected Map<String, SynchroPendingOperationBuffer> tableContextMap;
55  
56  	/**
57  	 * Create a new synchro context
58  	 * 
59  	 * @param tableNames
60  	 *            Table set to includes. If null, all tables will be processed by table filter
61  	 * @param tableFilter
62  	 *            Use to filter tables to process. If null, given tables names will be processed. See
63  	 *            {@link SynchroMetadataUtils} for predicate examples with includes/excludes.
64  	 * @param sourceConnectionProperties
65  	 *            Connection properties for source database
66  	 * @param targetConnectionProperties
67  	 *            Connection properties for target database
68  	 * @param securityContext
69  	 *            A security context (use for data synchronization: see {@link SynchroInterceptor})
70  	 * @param result
71  	 *            Store the synchronization result
72  	 * @return a new synchro context
73  	 * @see SynchroInterceptor
74  	 */
75  	public static SynchroContext newContext(
76  			Set<String> tableNames,
77  			Predicate<String> tableFilter,
78  			Properties sourceConnectionProperties,
79  			Properties targetConnectionProperties,
80  			SynchroResult result) {
81  		SynchroContext context = new SynchroContext();
82  		context.setTableNames(tableNames);
83  		context.setTableFilter(tableFilter);
84  		context.setSourceConnectionProperties(sourceConnectionProperties);
85  		context.setTargetConnectionProperties(targetConnectionProperties);
86  		context.setResult(result);
87  		return context;
88  	}
89  
90  	/**
91  	 * Create a new synchro context
92  	 * 
93  	 * @param tableNames
94  	 *            Table set to includes. If null, all tables will be retrieve.
95  	 * @param sourceConnectionProperties
96  	 *            Connection properties for source database
97  	 * @param targetConnectionProperties
98  	 *            Connection properties for target database
99  	 * @param securityContext
100 	 *            A security context (use for data synchronisation)
101 	 * @param result
102 	 *            Store the synchronization result
103 	 * @return a new synchro context
104 	 */
105 	public static SynchroContext newContext(
106 			Set<String> tableNames,
107 			Properties sourceConnectionProperties,
108 			Properties targetConnectionProperties,
109 			SynchroResult result) {
110 
111 		return newContext(
112 				tableNames,
113 				null,
114 				sourceConnectionProperties,
115 				targetConnectionProperties,
116 				result);
117 	}
118 
119 	public SynchroContext() {
120 		this.tableContextMap = Maps.newHashMap();
121 	}
122 
123 	public SynchroResult getResult() {
124 		return result;
125 	}
126 
127 	public void setResult(SynchroResult result) {
128 		this.result = result;
129 	}
130 
131 	public void setSourceConnectionProperties(Properties sourceConnectionProperties) {
132 		this.sourceConnectionProperties = sourceConnectionProperties;
133 	}
134 
135 	public void setTargetConnectionProperties(Properties targetConnectionProperties) {
136 		this.targetConnectionProperties = targetConnectionProperties;
137 	}
138 
139 	public void setTableFilter(Predicate<String> tableFilter) {
140 		this.tableFilter = tableFilter;
141 	}
142 
143 	public void setTableNames(Set<String> tableNames) {
144 		this.tableNames = tableNames;
145 	}
146 
147 	public Properties getSourceConnectionProperties() {
148 		return sourceConnectionProperties;
149 	}
150 
151 	public Properties getTargetConnectionProperties() {
152 		return targetConnectionProperties;
153 	}
154 
155 	public Predicate<String> getTableFilter() {
156 		return tableFilter;
157 	}
158 
159 	public Set<String> getTableNames() {
160 		return this.tableNames;
161 	}
162 
163 	public SynchroPendingOperationBuffer getTableContext(String tableName) {
164 		return tableContextMap.get(tableName);
165 	}
166 
167 	public void addTableContext(String tableName, SynchroPendingOperationBuffer tableContext) {
168 		tableContextMap.put(tableName, tableContext);
169 	}
170 
171 	public void removeTableContext(String tableName) {
172 		tableContextMap.remove(tableName);
173 	}
174 
175 }