<commit_before>package org.xcolab.portlets.proposals.utils;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import javax.portlet.PortletURL;

import com.ext.portlet.model.Proposal;
import com.ext.portlet.service.ProposalLocalServiceUtil;
import org.apache.commons.lang3.StringUtils;

import com.ext.portlet.NoSuchPlanItemException;
import com.ext.portlet.service.PlanItemLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.search.BooleanQuery;
import com.liferay.portal.kernel.search.Document;
import com.liferay.portal.kernel.search.DocumentImpl;
import com.liferay.portal.kernel.search.Field;
import com.liferay.portal.kernel.search.Hits;
import com.liferay.portal.kernel.search.IndexerPostProcessor;
import com.liferay.portal.kernel.search.SearchContext;
import com.liferay.portal.kernel.search.SearchEngineUtil;
import com.liferay.portal.kernel.search.SearchException;
import com.liferay.portal.kernel.search.Summary;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.security.permission.PermissionChecker;

import org.xcolab.portlets.proposals.wrappers.ProposalSectionWrapper;
import org.xcolab.portlets.proposals.wrappers.ProposalWrapper;

/**
 *
 * Indexes a proposal
 * taken from previous proposals portlet and adapted to new environment: https://github.com/jintrone/XCoLab/blob/20b3f27a6f9e9f346ebc6675dcf58fa7052b9ae2/portlets/proposals-portlet/src/main/java/org/climatecollaboratorium/plans/utils/Indexer.java
 *
 *
 * @author pdeboer
 *
 */
public class Indexer implements com.liferay.portal.kernel.search.Indexer {

    public static final String PORTLET_ID = "plans";
    private static final Log _log = LogFactoryUtil.getLog(Indexer.class);
    private long defaultCompanyId = 10112L;

    private static final String[] _CLASS_NAMES = new String[]{Proposal.class.getName()};

    @Override
    public void delete(long companyId, String uid) throws SearchException {

    }

    @Override
    public void delete(Object obj) throws SearchException {
        Proposal plan;
        try {
            plan = ProposalLocalServiceUtil.getProposal((Long)obj);
            SearchEngineUtil.deleteDocument(getSearchEngineId(), defaultCompanyId, getDocument(plan).getUID());
        } catch (NoSuchPlanItemException e) {
            _log.error("Can't remove plan from index: " + obj, e);
        } catch (SystemException | PortalException e) {
            _log.error("Can't remove plan from index: " + obj, e);
        }

    }

    @Override
    public Document getDocument(Object obj) throws SearchException {
        try {
            Proposal proposal = (Proposal) obj;

            Document doc = new DocumentImpl();

            doc.addUID(PORTLET_ID, proposal.getProposalId());

            doc.addModifiedDate(proposal.getUpdatedDate());

            doc.addKeyword(Field.COMPANY_ID, defaultCompanyId);
            doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);

            ProposalWrapper wrapper = new ProposalWrapper(proposal);

            doc.addText(Field.TITLE, wrapper.getName());
            doc.addText("pitch", wrapper.getPitch());
            doc.addText(Field.CONTENT, wrapper.getDescription());

            StringBuilder sectionsCombined = new StringBuilder();
            List<ProposalSectionWrapper> sections = wrapper.getSections();
            if (sections != null) {
                for (ProposalSectionWrapper section : sections) {
                    if (StringUtils.isNotBlank(section.getContent())) {
                        sectionsCombined.append(section.getContent());
                        sectionsCombined.append(" ");
                    }
                }
            }
            doc.addText("sections", sectionsCombined.toString());

            doc.addKeyword(Field.ENTRY_CLASS_NAME, Proposal.class.getName());
            doc.addKeyword(Field.ENTRY_CLASS_PK, proposal.getProposalId());
            // doc.addText(Field.URL, Plan);
            return doc;
        } catch (SystemException e) {
            throw new SearchException("Can't index plan " + obj, e);
        } catch (PortalException e) {
            throw new SearchException("Can't index plan " + obj, e);
        }
    }

    @Override
    public BooleanQuery getFacetQuery(String className, SearchContext searchContext) throws Exception {
        return null;
    }

    @Override
    public BooleanQuery getFullQuery(SearchContext searchContext) throws SearchException {
        return null;
    }

    @Override
    public IndexerPostProcessor[] getIndexerPostProcessors() {
        return null;
    }

    @Override
    public String getPortletId() {
        return null;
    }

    @Override
    public String getSearchEngineId() {
        return SearchEngineUtil.SYSTEM_ENGINE_ID;
    }

    @Override
    public String getSortField(String orderByCol) {
        return null;
    }

    @Override
    public Summary getSummary(Document document, Locale locale, String snippet, PortletURL portletURL)
            throws SearchException {
        return null;
    }

    @Override
    public boolean hasPermission(PermissionChecker permissionChecker, long entryClassPK, String actionId)
            throws Exception {
        return false;
    }

    @Override
    public boolean isFilterSearch() {
        return false;
    }

    @Override
    public boolean isPermissionAware() {
        return false;
    }

    @Override
    public boolean isStagingAware() {
        return false;
    }

    @Override
    public void postProcessContextQuery(BooleanQuery contextQuery, SearchContext searchContext) throws Exception {

    }

    @Override
    public void postProcessSearchQuery(BooleanQuery searchQuery, SearchContext searchContext) throws Exception {

    }

    @Override
    public void registerIndexerPostProcessor(IndexerPostProcessor indexerPostProcessor) {

    }

    @Override
    public void reindex(Object obj) throws SearchException {
        Proposal p = null;
        if (obj instanceof Long) {
            try {
                p = ProposalLocalServiceUtil.getProposal((Long) obj);
            } catch (NoSuchPlanItemException e) {
                _log.error("Can't reindex plan " + p.getProposalId(), e);
            } catch (Exception e) {
                _log.error("Can't reindex plan " + p.getProposalId(), e);
            }

        } else {
            try {
                Method m = obj.getClass().getMethod("getProposalId");
                Long planId = (Long) m.invoke(obj);
                p = ProposalLocalServiceUtil.getProposal(planId);
            } catch (NoSuchPlanItemException e) {
                _log.error("Can't reindex plan " + obj, e);
            } catch (SystemException e) {
                _log.error("Can't reindex plan " + obj, e);
            } catch (IllegalAccessException e) {
                _log.error("Can't reindex plan " + obj, e);
            } catch (IllegalArgumentException e) {
                _log.error("Can't reindex plan " + obj, e);
            } catch (InvocationTargetException e) {
                _log.error("Can't reindex plan " + obj, e);
            } catch (NoSuchMethodException e) {
                _log.error("Can't reindex plan " + obj, e);
            } catch (SecurityException e) {
                _log.error("Can't reindex plan " + obj, e);
            } catch (PortalException e) {
                _log.error("Can't reindex plan " + obj, e);
            }
        }


        if (!p.getVisible()) return;
        Document doc = getDocument(p);
        SearchEngineUtil.deleteDocument(getSearchEngineId(), defaultCompanyId, doc.getUID());
        SearchEngineUtil.addDocument(getSearchEngineId(), defaultCompanyId, doc);

    }

    @Override
    public void reindex(String className, long classPK) throws SearchException {
    }

    @Override
    public void reindex(String[] ids) throws SearchException {
        long companyId = GetterUtil.getLong(ids[0]);
        List<Proposal> proposals = null;
        try {
            proposals = ProposalLocalServiceUtil.getProposals(Integer.MIN_VALUE,Integer.MAX_VALUE);
        } catch (SystemException e) {
            _log.error("Can't reindex plans", e);
            throw new SearchException("Can't reindex plans", e);
        }
        Collection<Document> documents = new ArrayList<Document>();
        for (Proposal p : proposals) {
            if (!p.getVisible()) continue;
            try {
                Document document = getDocument(p);
                documents.add(document);
            } catch (SearchException e) {
                _log.error("An exception has been thrown when reindexing plan with id: " + p.getProposalId(), e);
            }

        }
        SearchEngineUtil.updateDocuments(getSearchEngineId(), companyId, documents);
    }

    @Override
    public Hits search(SearchContext searchContext) throws SearchException {
        return null;
    }

    @Override
    public void unregisterIndexerPostProcessor(IndexerPostProcessor indexerPostProcessor) {

    }

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

}<commit_msg>delete for long id<commit_after>package org.xcolab.portlets.proposals.utils;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import javax.portlet.PortletURL;

import com.ext.portlet.model.Proposal;
import com.ext.portlet.service.ProposalLocalServiceUtil;
import org.apache.commons.lang3.StringUtils;

import com.ext.portlet.NoSuchPlanItemException;
import com.ext.portlet.service.PlanItemLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.search.BooleanQuery;
import com.liferay.portal.kernel.search.Document;
import com.liferay.portal.kernel.search.DocumentImpl;
import com.liferay.portal.kernel.search.Field;
import com.liferay.portal.kernel.search.Hits;
import com.liferay.portal.kernel.search.IndexerPostProcessor;
import com.liferay.portal.kernel.search.SearchContext;
import com.liferay.portal.kernel.search.SearchEngineUtil;
import com.liferay.portal.kernel.search.SearchException;
import com.liferay.portal.kernel.search.Summary;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.security.permission.PermissionChecker;

import org.xcolab.portlets.proposals.wrappers.ProposalSectionWrapper;
import org.xcolab.portlets.proposals.wrappers.ProposalWrapper;

/**
 *
 * Indexes a proposal
 * taken from previous proposals portlet and adapted to new environment: https://github.com/jintrone/XCoLab/blob/20b3f27a6f9e9f346ebc6675dcf58fa7052b9ae2/portlets/proposals-portlet/src/main/java/org/climatecollaboratorium/plans/utils/Indexer.java
 *
 *
 * @author pdeboer
 *
 */
public class Indexer implements com.liferay.portal.kernel.search.Indexer {

    public static final String PORTLET_ID = "plans";
    private static final Log _log = LogFactoryUtil.getLog(Indexer.class);
    private long defaultCompanyId = 10112L;

    private static final String[] _CLASS_NAMES = new String[]{Proposal.class.getName()};

    @Override
    public void delete(long companyId, String uid) throws SearchException {

    }

    @Override
    public void delete(Object obj) throws SearchException {
        Long id = -1L;
        if(obj instanceof Long) {
            id = (Long) obj;
        }else {
            Proposal p = (Proposal) obj;
            id = p.getProposalId();
        }

        if(id <0) _log.error("id should never be below 0. PAAANIIIIC!!!");
        try {
            Proposal plan = ProposalLocalServiceUtil.getProposal(id);
            SearchEngineUtil.deleteDocument(getSearchEngineId(), defaultCompanyId, getDocument(plan).getUID());
        } catch (NoSuchPlanItemException e) {
            _log.error("Can't remove plan from index: " + obj, e);
        } catch (SystemException | PortalException e) {
            _log.error("Can't remove plan from index: " + obj, e);
        }

    }

    @Override
    public Document getDocument(Object obj) throws SearchException {
        try {
            Proposal proposal = (Proposal) obj;

            Document doc = new DocumentImpl();

            doc.addUID(PORTLET_ID, proposal.getProposalId());

            doc.addModifiedDate(proposal.getUpdatedDate());

            doc.addKeyword(Field.COMPANY_ID, defaultCompanyId);
            doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);

            ProposalWrapper wrapper = new ProposalWrapper(proposal);

            doc.addText(Field.TITLE, wrapper.getName());
            doc.addText("pitch", wrapper.getPitch());
            doc.addText(Field.CONTENT, wrapper.getDescription());

            StringBuilder sectionsCombined = new StringBuilder();
            List<ProposalSectionWrapper> sections = wrapper.getSections();
            if (sections != null) {
                for (ProposalSectionWrapper section : sections) {
                    if (StringUtils.isNotBlank(section.getContent())) {
                        sectionsCombined.append(section.getContent());
                        sectionsCombined.append(" ");
                    }
                }
            }
            doc.addText("sections", sectionsCombined.toString());

            doc.addKeyword(Field.ENTRY_CLASS_NAME, Proposal.class.getName());
            doc.addKeyword(Field.ENTRY_CLASS_PK, proposal.getProposalId());
            // doc.addText(Field.URL, Plan);
            return doc;
        } catch (SystemException e) {
            throw new SearchException("Can't index plan " + obj, e);
        } catch (PortalException e) {
            throw new SearchException("Can't index plan " + obj, e);
        }
    }

    @Override
    public BooleanQuery getFacetQuery(String className, SearchContext searchContext) throws Exception {
        return null;
    }

    @Override
    public BooleanQuery getFullQuery(SearchContext searchContext) throws SearchException {
        return null;
    }

    @Override
    public IndexerPostProcessor[] getIndexerPostProcessors() {
        return null;
    }

    @Override
    public String getPortletId() {
        return null;
    }

    @Override
    public String getSearchEngineId() {
        return SearchEngineUtil.SYSTEM_ENGINE_ID;
    }

    @Override
    public String getSortField(String orderByCol) {
        return null;
    }

    @Override
    public Summary getSummary(Document document, Locale locale, String snippet, PortletURL portletURL)
            throws SearchException {
        return null;
    }

    @Override
    public boolean hasPermission(PermissionChecker permissionChecker, long entryClassPK, String actionId)
            throws Exception {
        return false;
    }

    @Override
    public boolean isFilterSearch() {
        return false;
    }

    @Override
    public boolean isPermissionAware() {
        return false;
    }

    @Override
    public boolean isStagingAware() {
        return false;
    }

    @Override
    public void postProcessContextQuery(BooleanQuery contextQuery, SearchContext searchContext) throws Exception {

    }

    @Override
    public void postProcessSearchQuery(BooleanQuery searchQuery, SearchContext searchContext) throws Exception {

    }

    @Override
    public void registerIndexerPostProcessor(IndexerPostProcessor indexerPostProcessor) {

    }

    @Override
    public void reindex(Object obj) throws SearchException {
        Proposal p = null;
        if (obj instanceof Long) {
            try {
                p = ProposalLocalServiceUtil.getProposal((Long) obj);
            } catch (NoSuchPlanItemException e) {
                _log.error("Can't reindex plan " + p.getProposalId(), e);
            } catch (Exception e) {
                _log.error("Can't reindex plan " + p.getProposalId(), e);
            }

        } else {
            try {
                Method m = obj.getClass().getMethod("getProposalId");
                Long planId = (Long) m.invoke(obj);
                p = ProposalLocalServiceUtil.getProposal(planId);
            } catch (NoSuchPlanItemException e) {
                _log.error("Can't reindex plan " + obj, e);
            } catch (SystemException e) {
                _log.error("Can't reindex plan " + obj, e);
            } catch (IllegalAccessException e) {
                _log.error("Can't reindex plan " + obj, e);
            } catch (IllegalArgumentException e) {
                _log.error("Can't reindex plan " + obj, e);
            } catch (InvocationTargetException e) {
                _log.error("Can't reindex plan " + obj, e);
            } catch (NoSuchMethodException e) {
                _log.error("Can't reindex plan " + obj, e);
            } catch (SecurityException e) {
                _log.error("Can't reindex plan " + obj, e);
            } catch (PortalException e) {
                _log.error("Can't reindex plan " + obj, e);
            }
        }


        if (!p.getVisible()) return;
        Document doc = getDocument(p);
        SearchEngineUtil.deleteDocument(getSearchEngineId(), defaultCompanyId, doc.getUID());
        SearchEngineUtil.addDocument(getSearchEngineId(), defaultCompanyId, doc);

    }

    @Override
    public void reindex(String className, long classPK) throws SearchException {
    }

    @Override
    public void reindex(String[] ids) throws SearchException {
        long companyId = GetterUtil.getLong(ids[0]);
        List<Proposal> proposals = null;
        try {
            proposals = ProposalLocalServiceUtil.getProposals(Integer.MIN_VALUE,Integer.MAX_VALUE);
        } catch (SystemException e) {
            _log.error("Can't reindex plans", e);
            throw new SearchException("Can't reindex plans", e);
        }
        Collection<Document> documents = new ArrayList<Document>();
        for (Proposal p : proposals) {
            if (!p.getVisible()) continue;
            try {
                Document document = getDocument(p);
                documents.add(document);
            } catch (SearchException e) {
                _log.error("An exception has been thrown when reindexing plan with id: " + p.getProposalId(), e);
            }

        }
        SearchEngineUtil.updateDocuments(getSearchEngineId(), companyId, documents);
    }

    @Override
    public Hits search(SearchContext searchContext) throws SearchException {
        return null;
    }

    @Override
    public void unregisterIndexerPostProcessor(IndexerPostProcessor indexerPostProcessor) {

    }

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

}
{'fraction_non_alphanumeric': 0.0959698917986514, 'fraction_numerical': 0.003293084522502744, 'mean_word_length': 2.6166351606805294, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2148574', 'n_tokens_mistral': 5295, 'n_tokens_neox': 5211, 'n_words': 1145}