/*
 * oxCore is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */package org.xdi.model.custom.script.model.auth;

import java.util.ArrayList;
import java.util.List;

import org.xdi.model.AuthenticationScriptUsageType;
import org.xdi.model.SimpleCustomProperty;
import org.xdi.model.custom.script.model.CustomScript;
import org.xdi.util.StringHelper;

/**
 * Person authentication script configuration 
 *
 * @author Yuriy Movchan Date: 12/29/2014
 */
public class AuthenticationCustomScript extends CustomScript {

	public AuthenticationCustomScript() {}

	public AuthenticationCustomScript(CustomScript customScript) {
		super(customScript);
	}

	public AuthenticationScriptUsageType getUsageType() {
		AuthenticationScriptUsageType usageType = null;

		List<SimpleCustomProperty> moduleProperties = getModuleProperties();
		if (moduleProperties == null) {
			return usageType;
		}

		for (SimpleCustomProperty moduleProperty : getModuleProperties()) {
			if (StringHelper.equalsIgnoreCase(moduleProperty.getValue1(), "usage_type")) {
				usageType = AuthenticationScriptUsageType.getByValue(moduleProperty.getValue2());
				break;
			}
		}

		return usageType;
	}

	public void setUsageType(AuthenticationScriptUsageType usageType) {
		List<SimpleCustomProperty> moduleProperties = getModuleProperties();
		
		if (moduleProperties == null) {
			moduleProperties = new ArrayList<SimpleCustomProperty>();
			setModuleProperties(moduleProperties);
		}

		SimpleCustomProperty usageTypeModuleProperties = new SimpleCustomProperty("usage_type", usageType.getValue());
		moduleProperties.add(usageTypeModuleProperties);
	}

}

@@ -1524,16 +1524,304 @@
 );%0A%09%09%7D%0A%0A
+%09%09boolean found = false; %0A%09%09for (SimpleCustomProperty moduleProperty : getModuleProperties()) %7B%0A%09%09%09if (StringHelper.equalsIgnoreCase(moduleProperty.getValue1(), %22usage_type%22)) %7B%0A%09%09%09%09moduleProperty.setValue2(usageType.getValue());%0A%09%09%09%09found = true;%0A%09%09%09%09break;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09if (!found) %7B%0A%09
 %09%09Simple
@@ -1927,16 +1927,17 @@
 e());%0A%09%09
+%09
 modulePr
@@ -1977,14 +1977,18 @@
 rties);%0A
+%09%09%7D%0A
 %09%7D%0A%0A%7D%0A

Fix set module properties