package br.mp.mprj.portal.portlet.gestaomensagens.search.indexer;

import java.util.Locale;

import javax.portlet.PortletURL;

import br.mp.mprj.portal.portlet.gestaomensagens.search.ConstantesSearch;
import br.mp.mprj.portal.portlet.gestaomensagens.service.model.GrupoDestinatario;
import br.mp.mprj.portal.portlet.gestaomensagens.service.service.GrupoDestinatarioLocalServiceUtil;
import br.mp.mprj.portal.portlet.gestaomensagens.service.service.persistence.GrupoDestinatarioActionableDynamicQuery;

import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.search.BaseIndexer;
import com.liferay.portal.kernel.search.Document;
import com.liferay.portal.kernel.search.Field;
import com.liferay.portal.kernel.search.SearchContext;
import com.liferay.portal.kernel.search.SearchEngineUtil;
import com.liferay.portal.kernel.search.Summary;
import com.liferay.portal.kernel.util.GetterUtil;

public class GrupoDestinatarioIndexer extends BaseIndexer {
	
	private static final String ENTRY_CLASS_NAME = GrupoDestinatario.class.getName();
	public static final String[] CLASS_NAMES = {ENTRY_CLASS_NAME};

	@Override
	public String[] getClassNames() {
		return CLASS_NAMES;
	}

	@Override
	public String getPortletId() {
		return ConstantesSearch.PORTLET_ID_GRUPO_DESTINATARIO;
	}

	@Override
	protected void doDelete(Object obj) throws Exception {
		GrupoDestinatario grupoDestinatario = (GrupoDestinatario) obj;
		deleteDocument(grupoDestinatario.getCompanyId(), grupoDestinatario.getGrupoDestinatarioId());
	}

	@Override
	protected Document doGetDocument(Object obj) throws Exception {
		GrupoDestinatario entry = (GrupoDestinatario) obj;
		
		Document document = getBaseModelDocument(getPortletId(), entry);
		
		document.addKeyword(Field.COMPANY_ID, entry.getCompanyId());
		document.addKeyword(Field.GROUP_ID, entry.getGroupId());
		document.addDate(Field.MODIFIED_DATE, entry.getDataAtualizacao());
		document.addDate(Field.CREATE_DATE, entry.getDataCriacao());
		document.addDate("dataFinalListaTemporaria", entry.getDataFinalListaTemporaria());
		document.addKeyword("listaTemporaria", entry.getListaTemporaria());
		document.addKeyword("ativo", entry.getAtivo());
		document.addKeyword("nome", entry.getNome());
		document.addKeyword("nome_sortable", entry.getNome());
		document.addKeyword("grupoDestinatarioId", entry.getGrupoDestinatarioId());
		document.addText("finalidade", entry.getFinalidade());
		document.addKeyword("tipoLista", entry.getTipoLista());
		
		return document;
	}

	@Override
	protected Summary doGetSummary(Document document, Locale locale,
			String snippet, PortletURL portletURL) throws Exception {
		return null;
	}

	@Override
	protected void doReindex(Object obj) throws Exception {
		GrupoDestinatario grupoDestinatario = (GrupoDestinatario) obj;
		SearchEngineUtil.updateDocument(getSearchEngineId(), 
							grupoDestinatario.getCompanyId(), 
										doGetDocument(grupoDestinatario), 
										false);
	}

	@Override
	protected void doReindex(String className, long classPK) throws Exception {
		if (className.equals(ENTRY_CLASS_NAME)) {
			doReindex(GrupoDestinatarioLocalServiceUtil.getGrupoDestinatario(classPK));
		}
	}

	@Override
	protected void doReindex(String[] ids) throws Exception {
		long companyId = GetterUtil.getLong(ids[0]);

		//define uma actionableQuery customizada para a entidade, gerado pelo ServiceBuilder.
		//possui metodo onde eh possivel definir condicoes p/ query: addCriteria.
		ActionableDynamicQuery actionableQuery = new GrupoDestinatarioActionableDynamicQuery() {
			@Override
			protected void performAction(Object object) throws PortalException,
					SystemException {
				GrupoDestinatario entry = (GrupoDestinatario) object;
				Document document = getDocument(entry);
				addDocument(document);
			}
		};
		
		actionableQuery.setCompanyId(companyId);
		actionableQuery.setSearchEngineId(getSearchEngineId());
		actionableQuery.performActions();
	}

	@Override
	protected String getPortletId(SearchContext searchContext) {
		return getPortletId();
	}


}
