package br.mp.mprj.portal.portlet.gestaomensagens.service.service.impl;

import br.mp.mprj.portal.portlet.gestaomensagens.service.NoSuchTemplateException;
import br.mp.mprj.portal.portlet.gestaomensagens.service.model.Template;
import br.mp.mprj.portal.portlet.gestaomensagens.service.service.base.TemplateLocalServiceBaseImpl;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

/**
 * The implementation of the template local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link br.mp.mprj.portal.portlet.gestaomensagens.service.service.TemplateLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Henrique Simoes
 * @see br.mp.mprj.portal.portlet.gestaomensagens.service.service.base.TemplateLocalServiceBaseImpl
 * @see br.mp.mprj.portal.portlet.gestaomensagens.service.service.TemplateLocalServiceUtil
 */
public class TemplateLocalServiceImpl extends TemplateLocalServiceBaseImpl {
	
	private static final Log _log = LogFactoryUtil.getLog(TemplateLocalServiceImpl.class);
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never reference this interface directly. Always use {@link br.mp.mprj.portal.portlet.gestaomensagens.service.service.TemplateLocalServiceUtil} to access the template local service.
     */
	/**
	 * Metodo com a finalidade de realizar uma exclusao logica
	 * em um template.
	 */
	public boolean removerTemplate(long templateId){
		
		Template template;
		try {
			template = getTemplate(templateId);
			template.setAtivo(false);
			updateTemplate(template);
		} catch (PortalException | SystemException e) {
			_log.error("Ocorreu uma falha durante a atualização do template.");
			return false;
		}
		
		return true;
	}
	
	public Template recuperaTemplatePorNome(final String nome, final long groupId){
		
		try {
			return templatePersistence.findByGroupIdNome(groupId, nome);
		} catch (NoSuchTemplateException | SystemException e) {
			return null;
		}
		
	}
}
