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

package org.gluu.service.external;

import java.util.Iterator;
import java.util.Map;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;

import org.gluu.model.SimpleCustomProperty;
import org.gluu.model.custom.script.CustomScriptType;
import org.gluu.model.custom.script.conf.CustomScriptConfiguration;
import org.gluu.model.custom.script.type.persistence.PersistenceType;
import org.gluu.persist.PersistenceEntryManager;
import org.gluu.persist.exception.extension.PersistenceExtension;
import org.gluu.service.custom.script.ExternalScriptService;
import org.gluu.service.external.context.PersistenceExternalContext;

/**
 * Provides factory methods needed to create persistence extension
 *
 * @author Yuriy Movchan Date: 06/04/2020
 */
@ApplicationScoped
public class ExternalPersistenceExtensionService extends ExternalScriptService {

	private static final long serialVersionUID = 5466361778036208685L;

	@Inject
	private Instance<PersistenceEntryManager> persistenceMetricEntryManagerInstance;

	public ExternalPersistenceExtensionService() {
		super(CustomScriptType.PERSISTENCE_EXTENSION);
	}

	@Override
	protected void reloadExternal() {
		PersistenceExtension persistenceExtension = null;
		if (isEnabled()) {
			persistenceExtension = (PersistenceExtension) this.defaultExternalCustomScript.getExternalType();
		}
		
		for (Iterator<PersistenceEntryManager> it = persistenceMetricEntryManagerInstance.iterator(); it.hasNext();) {
			PersistenceEntryManager persistenceEntryManager = it.next();
			persistenceEntryManager.setPersistenceExtension(persistenceExtension);
		}
    }

	public void setPersistenceExtension(PersistenceEntryManager persistenceEntryManager) {
		PersistenceExtension persistenceExtension = null;
		if (isEnabled()) {
			persistenceExtension = (PersistenceExtension) this.defaultExternalCustomScript.getExternalType();
		}

		persistenceEntryManager.setPersistenceExtension(persistenceExtension);
	}

	public void executeExternalOnAfterCreateMethod(PersistenceExternalContext context) {
		executeExternalOnAfterCreateMethod(this.defaultExternalCustomScript, context);
	}

	public void executeExternalOnAfterCreateMethod(CustomScriptConfiguration customScriptConfiguration, PersistenceExternalContext context) {
		try {
			log.debug("Executing python 'onAfterCreate' method");
			PersistenceType persistenceType = (PersistenceType) customScriptConfiguration.getExternalType();
			Map<String, SimpleCustomProperty> configurationAttributes = customScriptConfiguration.getConfigurationAttributes();
			persistenceType.onAfterCreate(context, configurationAttributes);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
            saveScriptError(customScriptConfiguration.getCustomScript(), ex);
		}
	}

	public void executeExternalOnAfterDestroyMethod(PersistenceExternalContext context) {
		executeExternalOnAfterDestroyMethod(this.defaultExternalCustomScript, context);
	}

	public void executeExternalOnAfterDestroyMethod(CustomScriptConfiguration customScriptConfiguration, PersistenceExternalContext context) {
		try {
			log.debug("Executing python 'onAfterDestroy' method");
			PersistenceType persistenceType = (PersistenceType) customScriptConfiguration.getExternalType();
			Map<String, SimpleCustomProperty> configurationAttributes = customScriptConfiguration.getConfigurationAttributes();
			persistenceType.onAfterDestroy(context, configurationAttributes);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
            saveScriptError(customScriptConfiguration.getCustomScript(), ex);
		}
	}

}

@@ -202,16 +202,39 @@
 erator;%0A
+import java.util.List;%0A
 import j
@@ -1196,38 +1196,32 @@
 ger%3E persistence
-Metric
 EntryManagerInst
@@ -1227,16 +1227,112 @@
 tance;%0A%0A
+%09@Inject%0A%09private Instance%3CList%3CPersistenceEntryManager%3E%3E persistenceEntryManagerListInstance;%0A%0A
 %09public 
@@ -1712,14 +1712,8 @@
 ence
-Metric
 Entr
@@ -1899,16 +1899,380 @@
 on);%0A%09%09%7D
+%0A%0A%09%09for (Iterator%3CList%3CPersistenceEntryManager%3E%3E it = persistenceEntryManagerListInstance.iterator(); it.hasNext();) %7B%0A%09%09%09List%3CPersistenceEntryManager%3E persistenceEntryManagerList = it.next();%0A%09%09%09for (PersistenceEntryManager persistenceEntryManager: persistenceEntryManagerList) %7B%0A%09%09%09%09persistenceEntryManager.setPersistenceExtension(persistenceExtension);%0A%09%09%09%7D%0A%09%09%7D
 %0A    %7D%0A%0A

Add new persistence custom script oxCore #194