SyncScript.groovy revision 27729d5a6fae13e145aa4829793bd240ff10373a
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/*
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Copyright (c) 2010 ForgeRock Inc. All Rights Reserved
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * The contents of this file are subject to the terms
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * of the Common Development and Distribution License
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * (the License). You may not use this file except in
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * compliance with the License.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * You can obtain a copy of the License at
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * http://www.opensource.org/licenses/cddl1.php or
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * OpenIDM/legal/CDDLv1.0.txt
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * See the License for the specific language governing
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * permission and limitations under the License.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * When distributing Covered Code, include this CDDL
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Header Notice in each file and include the License file
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * at OpenIDM/legal/CDDLv1.0.txt.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * If applicable, add the following below the CDDL Header,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * with the fields enclosed by brackets [] replaced by
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * your own identifying information:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * "Portions Copyrighted 2010 [name of copyright owner]"
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * $Id$
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncimport groovy.sql.Sql;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncimport groovy.sql.DataSet;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// Parameters:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// The connector sends the following:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// connection: handler to the SQL connection
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// objectClass: a String describing the Object class (__ACCOUNT__ / __GROUP__ / other)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// action: a string describing the action ("SYNC" or "GET_LATEST_SYNC_TOKEN" here)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// log: a handler to the Log facility
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// options: a handler to the OperationOptions Map (null if action = "GET_LATEST_SYNC_TOKEN")
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// token: a handler to an Object representing the sync token (null if action = "GET_LATEST_SYNC_TOKEN")
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync//
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync//
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// Returns:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// if action = "GET_LATEST_SYNC_TOKEN", it must return an object representing the last known
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// sync token for the corresponding ObjectClass
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync//
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// if action = "SYNC":
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// A list of Maps . Each map describing one update:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// Map should look like the following:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync//
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// [
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// "token": <Object> token object (could be Integer, Date, String) , [!! could be null]
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// "operation":<String> ("CREATE_OR_UPDATE"|"DELETE"),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// "uid":<String> uid (uid of the entry) ,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// "previousUid":<String> prevuid (This is for rename ops) ,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// "password":<String> password (optional... allows to pass clear text password if needed),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// "attributes":Map<String,List> of attributes name/values
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync// ]
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsynclog.info("Entering "+action+" Script");
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncdef sql = new Sql(connection);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncif (action.equalsIgnoreCase("GET_LATEST_SYNC_TOKEN")) {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync row = sql.firstRow("select timestamp from Users order by timestamp desc")
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync log.ok("Get Latest Sync Token script: last token is: "+row["timestamp"])
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync // We don't wanna return the java.sql.Timestamp, it is not a supported data type
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync // Get the 'long' version
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return row["timestamp"].getTime();
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync}
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncelse if (action.equalsIgnoreCase("SYNC")) {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync def result = []
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync def tstamp = null
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if (token != null){
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync tstamp = new java.sql.Timestamp(token)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync else{
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync def today= new Date()
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync tstamp = new java.sql.Timestamp(today.time)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
sql.eachRow("select * from Users where timestamp > ${tstamp}",
{result.add([operation:"CREATE_OR_UPDATE", uid:it.uid, token:it.timestamp.getTime(), attributes:[firstname:it.firstname, lastname:it.lastname, email:it.email]])}
)
log.ok("Sync script: found "+result.size()+" events to sync")
return result;
}
else { // action not implemented
log.error("Sync script: action '"+action+"' is not implemented in this script")
return null;
}