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.Template;
import br.mp.mprj.portal.portlet.gestaomensagens.service.service.TemplateLocalServiceUtil;
import br.mp.mprj.portal.portlet.gestaomensagens.service.service.persistence.TemplateActionableDynamicQuery;

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 TemplateIndexer extends BaseIndexer {

	private static final String ENTRY_CLASS_NAME = Template.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_TEMPLATE;
	}

	@Override
	protected void doDelete(Object obj) throws Exception {
		Template template = (Template) obj;
		deleteDocument(template.getCompanyId(), template.getTemplateId());
	}

	@Override
	protected Document doGetDocument(Object obj) throws Exception {
		Template entry = (Template) 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.addKeyword("nome", entry.getNome());
		document.addKeyword("ativo", entry.getAtivo());
		document.addKeyword("nome_sortable", entry.getNome());
		document.addText("cabecalho", entry.getCabecalho());
		document.addText("rodape", entry.getRodape());
		document.addText("corpo", entry.getCorpo());
		document.addKeyword("estilo", entry.getEstilo());
		document.addKeyword("thumbnailFileEntryId", entry.getThumbnailFileEntryId());
		document.addText("thumbnailURL", entry.getThumbnailURL());
		
		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 {
		Template template = (Template) obj;
		SearchEngineUtil.updateDocument(getSearchEngineId(), 
										template.getCompanyId(), 
										doGetDocument(template), 
										false);
	}

	@Override
	protected void doReindex(String className, long classPK) throws Exception {
		if (className.equals(ENTRY_CLASS_NAME)) {
			doReindex(TemplateLocalServiceUtil.getTemplate(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 TemplateActionableDynamicQuery() {
			@Override
			protected void performAction(Object object) throws PortalException,
					SystemException {
				Template entry = (Template) object;
				Document document = getDocument(entry);
				addDocument(document);
			}
		};
		
		actionableQuery.setCompanyId(companyId);
		actionableQuery.setSearchEngineId(getSearchEngineId());
		actionableQuery.performActions();
	}

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

}
