001package fr.ifremer.adagio.synchro.service;
002
003/*
004 * #%L
005 * SIH-Adagio :: Synchronization
006 * $Id:$
007 * $HeadURL:$
008 * %%
009 * Copyright (C) 2012 - 2014 Ifremer
010 * %%
011 * This program is free software: you can redistribute it and/or modify
012 * it under the terms of the GNU Affero General Public License as published by
013 * the Free Software Foundation, either version 3 of the License, or
014 * (at your option) any later version.
015 * 
016 * This program is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
019 * GNU General Public License for more details.
020 * 
021 * You should have received a copy of the GNU Affero General Public License
022 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
023 * #L%
024 */
025
026import java.util.Map;
027import java.util.Properties;
028import java.util.Set;
029
030import com.google.common.base.Predicate;
031import com.google.common.collect.Maps;
032
033import fr.ifremer.adagio.synchro.meta.SynchroMetadataUtils;
034
035/**
036 * A context class, need for referential and data synchronization
037 * Created on 5/05/14.
038 * 
039 * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
040 * @since 3.5.3
041 */
042public class SynchroContext {
043
044    protected Properties sourceConnectionProperties;
045
046    protected Properties targetConnectionProperties;
047
048    protected SynchroResult result;
049
050    protected Predicate<String> tableFilter;
051
052    protected Set<String> tableNames;
053
054    protected Map<String, SynchroPendingOperationBuffer> tableContextMap;
055
056    /**
057     * Create a new synchro context
058     * 
059     * @param tableNames
060     *            Table set to includes. If null, all tables will be processed by table filter
061     * @param tableFilter
062     *            Use to filter tables to process. If null, given tables names will be processed. See
063     *            {@link SynchroMetadataUtils} for predicate examples with includes/excludes.
064     * @param sourceConnectionProperties
065     *            Connection properties for source database
066     * @param targetConnectionProperties
067     *            Connection properties for target database
068     * @param securityContext
069     *            A security context (use for data synchronization: see {@link SynchroInterceptor})
070     * @param result
071     *            Store the synchronization result
072     * @return a new synchro context
073     * @see SynchroInterceptor
074     */
075    public static SynchroContext newContext(
076            Set<String> tableNames,
077            Predicate<String> tableFilter,
078            Properties sourceConnectionProperties,
079            Properties targetConnectionProperties,
080            SynchroResult result) {
081        SynchroContext context = new SynchroContext();
082        context.setTableNames(tableNames);
083        context.setTableFilter(tableFilter);
084        context.setSourceConnectionProperties(sourceConnectionProperties);
085        context.setTargetConnectionProperties(targetConnectionProperties);
086        context.setResult(result);
087        return context;
088    }
089
090    /**
091     * Create a new synchro context
092     * 
093     * @param tableNames
094     *            Table set to includes. If null, all tables will be retrieve.
095     * @param sourceConnectionProperties
096     *            Connection properties for source database
097     * @param targetConnectionProperties
098     *            Connection properties for target database
099     * @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}