summaryrefslogtreecommitdiffstats
path: root/src/main/resources/tools/fiddles/CreateScript.groovy
blob: c419aaf272c4da1c7b34658644d5ec6f436a4870 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 *
 * Copyright (c) 2010 ForgeRock Inc. All Rights Reserved
 *
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the License). You may not use this file except in
 * compliance with the License.
 *
 * You can obtain a copy of the License at
 * http://www.opensource.org/licenses/cddl1.php or
 * OpenIDM/legal/CDDLv1.0.txt
 * See the License for the specific language governing
 * permission and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * Header Notice in each file and include the License file
 * at OpenIDM/legal/CDDLv1.0.txt.
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted 2010 [name of copyright owner]"
 *
 * $Id$
 */
import groovy.json.JsonBuilder
import groovy.sql.Sql
import groovy.sql.DataSet
import java.security.MessageDigest
import org.identityconnectors.framework.common.objects.AttributesAccessor
import org.identityconnectors.framework.common.objects.Uid

def createAttributes = new AttributesAccessor(attributes as Set<Attribute>)

def digest = MessageDigest.getInstance("MD5")
def sql = new Sql(connection)
def returnVal = null

//Create must return UID. 

switch ( objectClass.objectClassValue ) {

    case "users":

        sql.executeInsert("""
            INSERT INTO 
                users
            (
                issuer,
                subject,
                email
            ) 
            VALUES (?,?,?)
            """,
            [
                createAttributes.findString("issuer"),
                id,
                createAttributes.findString("email")
            ])

        returnVal = new Uid(createAttributes.findString("issuer") + ":" + id)

    break

    case "schema_defs":
        def structure = createAttributes.findList("structure")

        sql.executeInsert("""
            INSERT INTO 
                schema_defs 
            (
                db_type_id,
                short_code,
                ddl,
                md5,
                statement_separator,
                structure_json,
                last_used
            ) 
            VALUES (?,?,?,?,?,?,current_timestamp)
            """,
            [
                createAttributes.findInteger("db_type_id"),
                createAttributes.findString("short_code"),
                createAttributes.findString("ddl"),
                id,
                createAttributes.findString("statement_separator"),
                structure != null ? (new JsonBuilder(structure).toString()) : null
            ])

        returnVal = new Uid(createAttributes.findInteger("db_type_id").toString() + "_" + createAttributes.findString("short_code") as String)


    break

    // queries will return an existing ID if provided with duplicate sql
    case "queries":

        def statement_separator = createAttributes.findString("statement_separator")
        def sql_query = createAttributes.findString("sql")
        def schema_def_id = createAttributes.findInteger("schema_def_id")

        def md5hash

        md5hash = new BigInteger(
                            1, digest.digest( (statement_separator + sql_query).getBytes() )
                        ).toString(16).padLeft(32,"0")

        def existing_query = sql.firstRow("""
            SELECT 
                q.id as queryId,
                s.db_type_id,
                s.short_code
            FROM
                schema_defs s
                    LEFT OUTER JOIN queries q ON
                       s.id = q.schema_def_id AND
                       q.md5 = ?
            WHERE
                s.id = ?
            ORDER BY
                q.id
            """, [md5hash, schema_def_id])

        if (!existing_query.queryId) {
            def new_query
            sql.withTransaction {

                sql.execute(
                    """
                    INSERT INTO
                        queries
                    (
                        id,
                        md5,
                        sql,
                        statement_separator,
                        schema_def_id
                    )
                    SELECT  
                        count(*) + 1, ?, ?, ?, ?
                    FROM
                        queries
                    WHERE
                        schema_def_id = ?
                    """, 
                    [
                        md5hash,
                        sql_query,
                        statement_separator,
                        schema_def_id,
                        schema_def_id
                    ]
                )

                new_query = sql.firstRow("""
                    SELECT
                        max(id) as queryId
                    FROM
                        queries
                    WHERE
                        schema_def_id = ?
                    """,
                    [
                        schema_def_id
                    ]
                )
            }
            returnVal = new Uid((existing_query.db_type_id + "_" + existing_query.short_code + "_" + new_query.queryId) as String)
        } else {
            returnVal = new Uid((existing_query.db_type_id + "_" + existing_query.short_code + "_" + existing_query.queryId) as String)
        }

    break

    default:
        sql.close()
        throw new UnsupportedOperationException(operation.name() + " operation of type:" +
                        objectClass.objectClassValue + " is not supported.")
}

sql.close()

return returnVal