package mprj.doerj.caderno.configuration;

import com.liferay.portal.configuration.metatype.bnd.util.ConfigurableUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.portlet.ConfigurationAction;
import com.liferay.portal.kernel.portlet.DefaultConfigurationAction;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;

import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;

import mprj.doerj.caderno.constants.MprjDoerjCadernoPortletKeys;



@Component(
		configurationPid = "mprj.doerj.caderno.configuration.DoerjCadernoPortletConfiguration",
		configurationPolicy = ConfigurationPolicy.OPTIONAL, immediate = true,
		property = "javax.portlet.name=" + MprjDoerjCadernoPortletKeys.MPRJDOERJCADERNO,
		service = ConfigurationAction.class
)

public class DoerjCadernoPortletConfigurationAction extends DefaultConfigurationAction {

	private volatile DoerjCadernoPortletConfiguration _displayConfiguration;
	private static final Log _log = LogFactoryUtil.getLog(DoerjCadernoPortletConfiguration.class);
	
	@Override
	public void include(
			PortletConfig portletConfig, HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse)
		throws Exception {
		
		try {
			//define a variável de configuração já inicializada no @Activate.
			//para ser utilizado na JSP
			httpServletRequest.setAttribute(
					DoerjCadernoPortletConfiguration.class.getName(),
				_displayConfiguration);

			super.include(portletConfig, httpServletRequest, httpServletResponse);
		} catch (Exception e) {
			_log.error("Problemas ao recuperar configuracoes:", e);
		}
		
	}


	@Override
	public void processAction(
			PortletConfig portletConfig, ActionRequest actionRequest,
			ActionResponse actionResponse)
		throws Exception {
		
		setPreference(actionRequest, "urlOrgao", ParamUtil.getString(actionRequest, "urlOrgao"));
		setPreference(actionRequest, "PGJConfig", ParamUtil.getString(actionRequest, "PGJConfig"));
		setPreference(actionRequest, "CGMPConfig", ParamUtil.getString(actionRequest, "CGMPConfig"));
		setPreference(actionRequest, "SGJAConfig", ParamUtil.getString(actionRequest, "SGJAConfig"));
		setPreference(actionRequest, "SGJPIConfig", ParamUtil.getString(actionRequest, "SGJPIConfig"));
		setPreference(actionRequest, "SGJACIConfig", ParamUtil.getString(actionRequest, "SGJACIConfig"));
		setPreference(actionRequest, "SGJACDHConfig", ParamUtil.getString(actionRequest, "SGJACDHConfig"));
		setPreference(actionRequest, "SGJRIDPConfig", ParamUtil.getString(actionRequest, "SGJRIDPConfig"));
		setPreference(actionRequest, "CGConfig", ParamUtil.getString(actionRequest, "CGConfig"));
		setPreference(actionRequest, "CJConfig", ParamUtil.getString(actionRequest, "CJConfig"));
		setPreference(actionRequest, "AEConfig", ParamUtil.getString(actionRequest, "AEConfig"));
		setPreference(actionRequest, "CMPROCJConfig", ParamUtil.getString(actionRequest, "CMPROCJConfig"));
		setPreference(actionRequest, "CMPROMJConfig", ParamUtil.getString(actionRequest, "CMPROMJConfig"));
		setPreference(actionRequest, "CSIConfig", ParamUtil.getString(actionRequest, "CSIConfig"));
		setPreference(actionRequest, "CEAFConfig", ParamUtil.getString(actionRequest, "CEAFConfig"));
		setPreference(actionRequest, "OUVIConfig", ParamUtil.getString(actionRequest, "OUVIConfig"));
		setPreference(actionRequest, "SGMPConfig", ParamUtil.getString(actionRequest, "SGMPConfig"));
		setPreference(actionRequest, "AAOMCConfig", ParamUtil.getString(actionRequest, "AAOMCConfig"));
		setPreference(actionRequest, "ARCCConfig", ParamUtil.getString(actionRequest, "ARCCConfig"));
		setPreference(actionRequest, "GAOMCConfig", ParamUtil.getString(actionRequest, "GAOMCConfig"));
		setPreference(actionRequest, "ARCCRIConfig", ParamUtil.getString(actionRequest, "ARCCRIConfig"));
		setPreference(actionRequest, "ACConfig", ParamUtil.getString(actionRequest, "ACConfig"));
		setPreference(actionRequest, "ADHMConfig", ParamUtil.getString(actionRequest, "ADHMConfig"));
		setPreference(actionRequest, "AAPConfig", ParamUtil.getString(actionRequest, "AAPConfig"));
		setPreference(actionRequest, "AIConfig", ParamUtil.getString(actionRequest, "AIConfig"));
		

		SessionMessages.add(actionRequest, "servico.sucesso.gravar.config");

		super.processAction(portletConfig, actionRequest, actionResponse);
	}


	@Activate
	protected void activate(Map<Object, Object> properties) {
		_displayConfiguration = ConfigurableUtil.createConfigurable(
				DoerjCadernoPortletConfiguration.class, properties);
	}
	
	
	
}
