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 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;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.service.persistence.UserActionableDynamicQuery;

public class UserIndexer extends BaseIndexer {

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

	@Override
	protected void doDelete(Object obj) throws Exception {
		User user = (User) obj;
		deleteDocument(user.getCompanyId(), user.getUserId());
	}

	@Override
	protected Document doGetDocument(Object obj) throws Exception {
		User entry = (User) obj;
		
		Document document = getBaseModelDocument(getPortletId(), entry);
		
		document.addKeyword(Field.USER_ID, entry.getUserId());
		document.addKeyword(Field.COMPANY_ID, entry.getCompanyId());
		document.addKeyword("fullName", entry.getFullName());
		document.addKeyword("fullName_sortable", entry.getFullName());
		document.addText("emailAddress", entry.getEmailAddress());

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

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

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