3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay/*
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay *
c8408fe9c4eb7f97ffa5b6aafcd4157afd478bc8Jon Branch * Copyright 2014-2015 ForgeRock AS
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay *
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay * The contents of this file are subject to the terms
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay * of the Common Development and Distribution License
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay * (the License). You may not use this file except in
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay * compliance with the License.
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay *
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay * You can obtain a copy of the License at
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay * http://forgerock.org/license/CDDLv1.0.html
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay * See the License for the specific language governing
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay * permission and limitations under the License.
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay *
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay * When distributing Covered Code, include this CDDL
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay * Header Notice in each file and include the License file
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay * at http://forgerock.org/license/CDDLv1.0.html
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay * If applicable, add the following below the CDDL Header,
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay * with the fields enclosed by brackets [] replaced by
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay * your own identifying information:
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay * "Portions Copyrighted [year] [name of copyright owner]"
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay */
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemayimport org.forgerock.json.resource.Connection
c8408fe9c4eb7f97ffa5b6aafcd4157afd478bc8Jon Branchimport org.forgerock.util.query.QueryFilter
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemayimport org.forgerock.json.resource.QueryRequest
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemayimport org.forgerock.json.resource.Requests
c8408fe9c4eb7f97ffa5b6aafcd4157afd478bc8Jon Branchimport org.forgerock.json.resource.ResourceResponse
c8408fe9c4eb7f97ffa5b6aafcd4157afd478bc8Jon Branchimport org.forgerock.services.context.RootContext
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemayimport org.forgerock.openicf.connectors.scriptedcrest.ScriptedCRESTConfiguration
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemayimport org.forgerock.openicf.misc.scriptedcommon.OperationType
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemayimport org.identityconnectors.common.logging.Log
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemayimport org.identityconnectors.framework.common.exceptions.UnknownUidException
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemayimport org.identityconnectors.framework.common.objects.Name
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemayimport org.identityconnectors.framework.common.objects.ObjectClass
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemayimport org.identityconnectors.framework.common.objects.OperationOptions
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemayimport org.identityconnectors.framework.common.objects.Uid
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemaydef operation = operation as OperationType
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemaydef configuration = configuration as ScriptedCRESTConfiguration
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemaydef connection = connection as Connection
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemaydef username = username as String
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemaydef log = log as Log
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemaydef objectClass = objectClass as ObjectClass
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemaydef options = options as OperationOptions
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemaydef objectClassInfo = configuration.propertyBag[objectClass.objectClassValue];
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemayif (objectClassInfo != null) {
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay QueryRequest request = Requests.newQueryRequest(objectClassInfo.resourceContainer)
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay def attributeDefinition = objectClassInfo.attributes[name]
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay if (null != attributeDefinition) {
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay request.queryFilter = QueryFilter.equalTo(String.valueOf(attributeDefinition.jsonName), username)
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay } else {
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay request.queryFilter = QueryFilter.equalTo(Name.NAME, username)
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay }
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay request.addField("_id", "_rev")
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay def results = []
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay def queryResult = connection.query(new RootContext(), request, results)
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay if (results.empty) {
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay throw new UnknownUidException()
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay } else if (results.size() > 1) {
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay throw new ConnectException("Multiple results 'userName' is not unique!")
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay } else {
c8408fe9c4eb7f97ffa5b6aafcd4157afd478bc8Jon Branch ResourceResponse r = results.get(0) as ResourceResponse;
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay return new Uid(r.id, r.revision)
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay }
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay} else {
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay throw new UnsupportedOperationException(operation.name() + " operation of type:" +
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay objectClass.objectClassValue + " is not supported.")
3b9de50fdf0980762d6930a49e1a0f20b61a6f6bJason Lemay}