diff options
Diffstat (limited to 'src/main/src/sync/SSSyncTasksFactory.java')
-rw-r--r-- | src/main/src/sync/SSSyncTasksFactory.java | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/src/main/src/sync/SSSyncTasksFactory.java b/src/main/src/sync/SSSyncTasksFactory.java new file mode 100644 index 0000000..a91b3fa --- /dev/null +++ b/src/main/src/sync/SSSyncTasksFactory.java @@ -0,0 +1,170 @@ +/* + * SSSync, a Simple and Stupid Synchronizer for data with multi-valued attributes + * Copyright (C) 2014 Ludovic Pouzenc <ludovic@pouzenc.fr> + * + * This file is part of SSSync. + * + * SSSync is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * SSSync is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with SSSync. If not, see <http://www.gnu.org/licenses/> + */ + +package sync; + +import java.io.File; +import java.io.FileReader; +import java.util.ArrayList; +import java.util.List; + +import conf.ConfigDestBean; +import conf.ConfigRootBean; +import conf.ConfigSourceBean; +import conf.ConfigTaskBean; + +import sync.AbstractSyncTask; +import data.filters.MVDataCombiner; +import data.filters.MVDataCombiner.MVDataCombineMode; +import data.io.ConnectionsHolder; +import data.io.MVDataReadWriterPair; +import data.io.MVDataReader; +import data.io.MVDataWriter; +import data.io.SafeDataReader; +import data.io.csv.CSVDataReader; +import data.io.ldap.LDAPConnectionWrapper; +import data.io.sql.SQLConnectionWrapper; + +/** + * TODO javadoc + * + * @author lpouzenc + */ +public class SSSyncTasksFactory { + + + + /** + * Build tasks objects with all needed resources from a config bean tree + * + * @param conf + * @return + * @throws Exception + */ + public static List<AbstractSyncTask> setupTasks(ConnectionsHolder connections, ConfigRootBean confMain) throws Exception { + List<AbstractSyncTask> tasks = new ArrayList<AbstractSyncTask>(); + + // For each task... + for ( ConfigTaskBean confTask: confMain.getTasks() ) { + MVDataReader srcReader=null; + + // Building all sources + + List<ConfigSourceBean> confSources = confTask.getSources(); + // See if we are in multiple source situation (then MVDataCombiner) or not (then simple MVDataReader) + if ( confSources.size() == 0 ) { + throw new Exception("Bad config : task '" + confTask.getName() + "' has no defined sources"); + } else if ( confSources.size() == 1 ) { + srcReader = new SafeDataReader(_makeSrcReader(connections, confSources.get(0), confTask.getName()), confTask.isSkipReadErrors()); + } else { + List<MVDataReader> readers = new ArrayList<MVDataReader>(); + List<MVDataCombineMode> mergeModes = new ArrayList<MVDataCombineMode>(); + + // For each source of the future MVDataCombiner... + for ( ConfigSourceBean confSource: confSources ) { + // Create and add the reader and his parameters + readers.add(new SafeDataReader(_makeSrcReader(connections, confSource, confTask.getName()), confTask.isSkipReadErrors())); + mergeModes.add(confSource.getMode()); + } + + srcReader = new MVDataCombiner("srcCombiner", readers.toArray(new MVDataReader[0]), mergeModes.toArray(new MVDataCombineMode[0])); + } + + // Building destination + MVDataReadWriterPair dstRWriter = _makeDestRWriter(connections, confTask.getDestination(), confTask.getName()); + MVDataWriter dstWriter = dstRWriter.writer; + MVDataReader dstReader = new SafeDataReader(dstRWriter.reader, false); + + // Extract config opLimits + int maxInserts = confTask.getOpLimits().getInsert(); + int maxUpdates = confTask.getOpLimits().getUpdate(); + int maxDeletes = confTask.getOpLimits().getDelete(); + + // Building the sync task and add it to the task list + BasicSyncTask task = new BasicSyncTask(confTask.getName(), false, srcReader, dstReader, dstWriter); + task.setOperationLimits(maxInserts, maxUpdates, maxDeletes); + task.setSkipEntryDelete(confTask.isSkipEntryDelete()); + tasks.add(task); + } + + return tasks; + } + + /** + * Helper function to make a new reader from an existing connection + * @param connections + * @param confSource + * @param taskName + * @return + * @throws Exception + */ + private static MVDataReader _makeSrcReader(ConnectionsHolder connections, ConfigSourceBean confSource, String taskName) throws Exception { + MVDataReader reader=null; + switch (confSource.getKind()) { + case csv: + reader = new CSVDataReader(confSource.getName(), new FileReader(confSource.getPath()), false); + break; + case ldap: + LDAPConnectionWrapper ldapConnBuilder = connections.getLDAPConnectionBuilder(confSource.getConn()); + //FIXME if conf error, getLDAPConnectionBuilder could return null + reader = ldapConnBuilder.newFlatReader(confSource.getName(), confSource.getBase(), confSource.getAttr(), confSource.getLookahead()); + break; + case sorted_csv: + reader = new CSVDataReader(confSource.getName(), new FileReader(confSource.getPath()), true); + break; + case sql: + SQLConnectionWrapper sqlConnBuilder = connections.getSQLConnectionBuilder(confSource.getConn()); + //FIXME if conf error, getSQLConnectionBuilder could return null + //TODO We assume the query config item is a filepath. It isn't checked anywhere. + reader = sqlConnBuilder.newReader(confSource.getName(), new File(confSource.getQuery())); + break; + default: + throw new Exception("Bad config : task '" + taskName + "' unsupported source kind"); + } + + return reader; + } + + /** + * Helper function to make a new read-writer from an existing connection + * + * @param connections + * @param confDest + * @param taskName + * @return + * @throws Exception + */ + private static MVDataReadWriterPair _makeDestRWriter(ConnectionsHolder connections, ConfigDestBean confDest, String taskName) throws Exception { + MVDataReader reader = null; + MVDataWriter writer = null; + + switch ( confDest.getKind() ) { + case ldap: + LDAPConnectionWrapper builder = connections.getLDAPConnectionBuilder(confDest.getConn()); + reader = builder.newFlatReader(confDest.getName()+"_reader", confDest.getBase(), confDest.getAttr(), confDest.getLookahead()); + writer = builder.newFlatWriter(confDest.getBase(), confDest.getAttr()); + break; + default: + throw new Exception("Bad config : task '" + taskName + "' unsupported destination kind"); + } + + return new MVDataReadWriterPair(reader, writer); + } +} |