summaryrefslogtreecommitdiff
path: root/src/main/src/conf/SSSyncTasksFactory.java
blob: de3e8f68c755da431f9b7a318066d59c0035e600 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * 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 conf;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import sync.BasicSyncTask;
import data.filters.MVDataCombiner;
import data.filters.MVDataCombiner.MVDataCombineMode;
import data.io.ConnectionsHolder;
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 beans tree
	 * @param conf
	 * @return
	 * @throws Exception
	 */
	public static List<BasicSyncTask> setupTasks(ConnectionsHolder connections, ConfigRootBean confMain) throws Exception {
		List<BasicSyncTask> tasks = new ArrayList<BasicSyncTask>();
		
		// For each task...
		for ( ConfigTaskBean confTask: confMain.getTasks() ) {
			MVDataReader srcReader=null;
			
			// Building all sources
			
			List<ConfigSrcOrDestBean> 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(_makeReader(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 ( ConfigSrcOrDestBean confSource: confSources ) {
					// Create and add the reader and his parameters
					readers.add(new SafeDataReader(_makeReader(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 
			
			MVDataReader dstReader=null;
			MVDataWriter dstWriter=null;
			
			ConfigSrcOrDestBean confDestination = confTask.getDestination();
			switch ( confDestination.getKind() ) {
			case ldap:
				LDAPConnectionWrapper builder = connections.getLDAPConnectionBuilder(confDestination.getConn());
				// TODO : configurable lookAhead
				MVDataReader tmpReader = builder.newFlatReader(confDestination.getName()+"_reader", confDestination.getBase(), confDestination.getAttr(), 128);
				dstReader = new SafeDataReader(tmpReader, false);
				dstWriter = builder.newFlatWriter(confDestination.getBase(), confDestination.getAttr());
				break;
			default:
				throw new Exception("Bad config : task '" + confTask.getName() + "' unsupported destination kind");
			}
			
			// Then building the sync task and add it to the task list
			int maxInserts = confTask.getOpLimits().getInsert();
			int maxUpdates = confTask.getOpLimits().getUpdate();
			int maxDeletes = confTask.getOpLimits().getDelete();
			
			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 confSource
	 * @param taskName
	 * @return
	 * @throws Exception
	 */
	private static MVDataReader _makeReader(ConnectionsHolder connections, ConfigSrcOrDestBean 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, get...ConnectionBuilder could return null
			//TODO : configurable lookAhead
			reader = ldapConnBuilder.newFlatReader(confSource.getName(), confSource.getBase(), confSource.getAttr(), 128); 
			break;
		case sorted_csv:
			reader = new CSVDataReader(confSource.getName(), new FileReader(confSource.getPath()), true);
			break;
		case sql:
			SQLConnectionWrapper sqlConnBuilder = connections.getSQLConnectionBuilder(confSource.getConn());
			//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;
	}
	
}