CreateScript.groovy revision 185941104719f48494a107996e17a23aad237c7d
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste/*
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste *
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * Copyright (c) 2014 ForgeRock AS. All Rights Reserved
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste *
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * The contents of this file are subject to the terms
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * of the Common Development and Distribution License
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * (the License). You may not use this file except in
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * compliance with the License.
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste *
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * You can obtain a copy of the License at
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * http://forgerock.org/license/CDDLv1.0.html
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * See the License for the specific language governing
ccf9d4a5c6453fa9f8b839baeee25147865fbb7dJames Phillpotts * permission and limitations under the License.
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste *
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * When distributing Covered Code, include this CDDL
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste * Header Notice in each file and include the License file
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts * at http://forgerock.org/license/CDDLv1.0.html
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts * If applicable, add the following below the CDDL Header,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts * with the fields enclosed by brackets [] replaced by
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts * your own identifying information:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts * "Portions Copyrighted [year] [name of copyright owner]"
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts *
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts * @author Gael Allioux <gael.allioux@forgerock.com>
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts */
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport groovy.sql.Sql
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport org.forgerock.openicf.connectors.scriptedsql.ScriptedSQLConfiguration
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport org.forgerock.openicf.misc.scriptedcommon.OperationType
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport org.identityconnectors.common.logging.Log
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport org.identityconnectors.framework.common.objects.Attribute
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport org.identityconnectors.framework.common.objects.AttributesAccessor
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport org.identityconnectors.framework.common.objects.ObjectClass
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport org.identityconnectors.framework.common.objects.OperationOptions
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport org.identityconnectors.framework.common.objects.Uid
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsimport java.sql.Connection
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsdef operation = operation as OperationType
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsdef createAttributes = new AttributesAccessor(attributes as Set<Attribute>)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsdef configuration = configuration as ScriptedSQLConfiguration
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsdef connection = connection as Connection
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsdef uid = id as String
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Joostedef log = log as Log
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Joostedef objectClass = objectClass as ObjectClass
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsdef options = options as OperationOptions
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Joostedef ORG = new ObjectClass("organization")
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottslog.info("Entering " + operation + " Script");
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsdef sql = new Sql(connection);
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpottsswitch (objectClass) {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case ObjectClass.ACCOUNT:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts def retUid
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts def generatedKeys = sql.executeInsert(
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts "INSERT INTO users (uid,password,firstname,lastname,fullname,email,organization) values (?,sha1(?),?,?,?,?,?)",
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts [
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts uid,
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste createAttributes.hasAttribute("password") ? createAttributes.findString("firstname") : "",
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste createAttributes.hasAttribute("firstname") ? createAttributes.findString("firstname") : "",
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts createAttributes.hasAttribute("lastname") ? createAttributes.findString("lastname") : "",
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste createAttributes.hasAttribute("fullname") ? createAttributes.findString("fullname") : "",
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste createAttributes.hasAttribute("email") ? createAttributes.findString("email") : "",
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts createAttributes.hasAttribute("organization") ? createAttributes.findString("organization") : ""
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts ]
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts )
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts retUid = new Uid(generatedKeys[0][0] as String)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts createAttributes.findList("cars").each {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts sql.executeInsert(
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts "INSERT INTO car (users_id,year,make,model) VALUES (?,?,?,?)",
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts [
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts generatedKeys[0][0] as Integer,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts it.year,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts it.make,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts it.model
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts ]
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts )
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste return retUid
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste break
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste case ObjectClass.GROUP:
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste def retUid
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste def generatedKeys = sql.executeInsert(
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts "INSERT INTO groups (name,gid,description) values (?,?,?)",
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste [
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts uid,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts createAttributes.hasAttribute("gid") ? createAttributes.findString("gid") : "",
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste createAttributes.hasAttribute("description") ? createAttributes.findString("description") : "",
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste ]
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts )
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts retUid = new Uid(generatedKeys[0][0] as String)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts createAttributes.findList("users").each {
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts sql.executeInsert(
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts "INSERT INTO groups_users (users_id,groups_id) SELECT id,? FROM users WHERE uid=?",
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts [
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste generatedKeys[0][0] as Integer,
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste it.uid
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste ]
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts )
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts }
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts return retUid
b9e697e180c323785bf1cb8a29f5ec6288da8ec4Jaco Jooste break
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts case ORG:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts def generatedKeys = sql.executeInsert(
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste "INSERT INTO organizations (name ,description) values (?,?)",
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste [
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts uid,
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts createAttributes.hasAttribute("description") ? createAttributes.findString("description") : ""
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts ]
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts )
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts return new Uid(generatedKeys[0][0] as String)
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts break
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts default:
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts throw new UnsupportedOperationException(operation.name() + " operation of type:" +
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts objectClass.objectClassValue + " is not supported.")
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts}
ca7a323358dc50374d5f281355842fb7edb3983eJames Phillpotts