/*
 * oxAuth is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.gluu.oxtrust.service.external;

import java.util.Map;

import org.gluu.oxtrust.model.GluuCustomPerson;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Startup;
import org.xdi.model.SimpleCustomProperty;
import org.xdi.model.custom.script.CustomScriptType;
import org.xdi.model.custom.script.conf.CustomScriptConfiguration;
import org.xdi.model.custom.script.type.user.UserRegistrationType;
import org.xdi.service.custom.script.ExternalScriptService;

/**
 * Provides factory methods needed to create external dynamic client registration extension
 *
 * @author Yuriy Movchan Date: 01/08/2015
 */
@Scope(ScopeType.APPLICATION)
@Name("externalUserRegistrationService")
@AutoCreate
@Startup
public class ExternalUserRegistrationService extends ExternalScriptService {

	private static final long serialVersionUID = 1416361273036208685L;

	public ExternalUserRegistrationService() {
		super(CustomScriptType.USER_REGISTRATION);
	}

	public boolean executeExternalUserRegistrationUpdateMethod(CustomScriptConfiguration customScriptConfiguration, GluuCustomPerson person, boolean persisted) {
		try {
			log.debug("Executing python 'updateUser' method");
			UserRegistrationType externalType = (UserRegistrationType) customScriptConfiguration.getExternalType();
			Map<String, SimpleCustomProperty> configurationAttributes = customScriptConfiguration.getConfigurationAttributes();
			return externalType.updateUser(person, persisted, configurationAttributes);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		
		return false;
	}

	public boolean executeExternalDefaultUserRegistrationUpdateMethod(GluuCustomPerson person, boolean persisted) {
		return executeExternalUserRegistrationUpdateMethod(this.defaultExternalCustomScript, person, persisted);
	}

	public boolean executeExternalAddPersonMethods(GluuCustomPerson person) {
		boolean result = true;
		for (CustomScriptConfiguration customScriptConfiguration : this.customScriptConfigurations) {
			result &= executeExternalUserRegistrationUpdateMethod(customScriptConfiguration, person, false);
			if (!result) {
				return result;
			}
		}

		return result;
	}

	public boolean executeExternalUpdatePersonMethods(GluuCustomPerson person) {
		boolean result = true;
		for (CustomScriptConfiguration customScriptConfiguration : this.customScriptConfigurations) {
			result &= executeExternalUserRegistrationUpdateMethod(customScriptConfiguration, person, true);
			if (!result) {
				return result;
			}
		}

		return result;
	}

    public static ExternalUserRegistrationService instance() {
        return (ExternalUserRegistrationService) Component.getInstance(ExternalUserRegistrationService.class);
    }

}

@@ -1380,38 +1380,36 @@
 luuCustomPerson 
-person
+user
 , boolean persis
@@ -1737,22 +1737,20 @@
 ateUser(
-person
+user
 , persis
@@ -1953,22 +1953,20 @@
 mPerson 
-person
+user
 , boolea
@@ -2067,22 +2067,20 @@
 Script, 
-person
+user
 , persis
@@ -2146,38 +2146,36 @@
 luuCustomPerson 
-person
+user
 ) %7B%0A%09%09boolean re
@@ -2367,22 +2367,20 @@
 ration, 
-person
+user
 , false)
@@ -2517,22 +2517,20 @@
 mPerson 
-person
+user
 ) %7B%0A%09%09bo
@@ -2734,14 +2734,12 @@
 on, 
-person
+user
 , tr

Update user interceptor script