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.Agendamento;
import br.mp.mprj.portal.portlet.gestaomensagens.service.service.AgendamentoLocalServiceUtil;
import br.mp.mprj.portal.portlet.gestaomensagens.service.service.persistence.AgendamentoActionableDynamicQuery;

import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.Property;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
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;

/**
 * 
 * @author Henrique Simoes
 *
 */
public class AgendamentoIndexer extends BaseIndexer {
	
	private static final String ENTRY_CLASS_NAME = Agendamento.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_AGENDAMENTO;
	}

	@Override
	protected void doDelete(Object obj) throws Exception {
		Agendamento agendamento = (Agendamento) obj;
		deleteDocument(agendamento.getCompanyId(), agendamento.getAgendamentoId());
	}

	@Override
	protected Document doGetDocument(Object obj) throws Exception {
		Agendamento entry = (Agendamento) obj;
		
		Document document = getBaseModelDocument(getPortletId(), entry);
		
		document.addKeyword(Field.COMPANY_ID, entry.getCompanyId());		
		document.addDate(Field.MODIFIED_DATE, entry.getDataAtualizacao());
		
		document.addKeyword("mensagemId", entry.getMensagemId());
		document.addKeyword("tipoAgendamento", entry.getTipoAgendamento());
		document.addKeyword("padraoRecorrencia", entry.getPadraoRecorrencia());
		document.addKeyword("frequencia", entry.getFrequencia());
		document.addKeyword("situacaoEnvio", entry.getSituacaoEnvio());
		
		document.addDate("dataInicio", entry.getDataInicio());
		document.addDate("dataFim", entry.getDataInicio());
		document.addDate("dataCalendario", entry.getDataInicio());

		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 {
		Agendamento agendamento = (Agendamento) obj;
		SearchEngineUtil.updateDocument(getSearchEngineId(), 
				agendamento.getCompanyId(), 
				doGetDocument(agendamento), 
				false);
	}

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

	@Override
	protected void doReindex(String[] ids) throws Exception {
		final 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 AgendamentoActionableDynamicQuery() {
			@Override
			protected void performAction(Object object) throws PortalException,
					SystemException {
				Agendamento entry = (Agendamento) object;
				Document document = getDocument(entry);
				addDocument(document);
			}
			
			@Override
			protected void addCriteria(DynamicQuery dynamicQuery) {
				Property property = PropertyFactoryUtil.forName("companyId");
				dynamicQuery.add(property.eq(companyId));
			}
		};
		
		actionableQuery.setSearchEngineId(getSearchEngineId());
		actionableQuery.performActions();
	}

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

}
