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.TemplateServiceBaseImpl;
import br.mp.mprj.portal.portlet.gestaomensagens.ws.vo.ModeloRetorno;

import com.liferay.portal.kernel.exception.SystemException;

/**
 * The implementation of the template remote 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.TemplateService} interface.
 *
 * <p>
 * This is a remote service. Methods of this service are expected to have security checks based on the propagated JAAS credentials because this service can be accessed remotely.
 * </p>
 *
 * @author Henrique Simoes
 * @see br.mp.mprj.portal.portlet.gestaomensagens.service.service.base.TemplateServiceBaseImpl
 * @see br.mp.mprj.portal.portlet.gestaomensagens.service.service.TemplateServiceUtil
 */
public class TemplateServiceImpl extends TemplateServiceBaseImpl {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never reference this interface directly. Always use {@link br.mp.mprj.portal.portlet.gestaomensagens.service.service.TemplateServiceUtil} to access the template remote service.
     */
	public ModeloRetorno getTemplatePorNome(String nome){
		
		ModeloRetorno wsReturn = new ModeloRetorno();
		
		if (nome == null || "".equals(nome)) {
			wsReturn.setError("[Gestão de Mensagens] - Informe o nome do template.");
			return wsReturn;
		}
		
		Template template = null;
		
		try {

			template = templatePersistence.findByNomeAtivo(nome, true);
			
			if (template != null){
				wsReturn.setCodigo(String.valueOf(template.getTemplateId()));
				
				if (template.getEstilo() == 0){
					
					wsReturn.setCabecalho(template.getCabecalho());
					wsReturn.setRodape(template.getRodape());
					
				}else {
					wsReturn.setConteudoModelo(template.getCorpo());
				}
				
				
				wsReturn.setDescricao(template.getNome());	
			} else {
				wsReturn.setError("[Gestão de Mensagens] - O template "+ nome +", não foi localizado.");
			}
			
			
		} catch (SystemException | NoSuchTemplateException e) {
			wsReturn.setError("[Gestão de Mensagens] - O template "+ nome +", não foi localizado.");
		}
		
		return wsReturn;
		
	}
}
