<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.xpn.xwiki.plugin.watchlist;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.plugin.activitystream.api.ActivityEventType;
import com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException;
import com.xpn.xwiki.plugin.activitystream.plugin.ActivityEvent;
import com.xpn.xwiki.plugin.activitystream.plugin.ActivityStreamPluginApi;

/**
 * Matcher for WatchList events. This class store all the events fired during a given interval. It also allows to 
 * perform a match between events and elements watched by a user.
 * 
 * @version $Id$
 */
@SuppressWarnings("serial")
public class WatchListEventMatcher
{
    /**
     * Logger.
     */
    private static final Log LOG = LogFactory.getLog(WatchListEventMatcher.class);

    /**
     * Events to match.
     */
    private static final List<String> MATCHING_EVENT_TYPES = new ArrayList<String>()
    {
        {
            add(ActivityEventType.CREATE);
            add(ActivityEventType.UPDATE);
            add(ActivityEventType.DELETE);
        }
    };

    /**
     * ActivityStream plugin API.
     */
    private final ActivityStreamPluginApi asApi;

    /**
     * List of events which have occurred between the start date and the current time.
     */
    private final List<WatchListEvent> events = new ArrayList<WatchListEvent>();

    /**
     * Constructor. Gets all the events fired during the interval between the given date and the current date.
     * 
     * @param context the XWiki context
     * @param start start date to use for document matching
     */
    public WatchListEventMatcher(Date start, XWikiContext context)
    {
        asApi = (ActivityStreamPluginApi) context.getWiki().getPluginApi("activitystream", context);
        List<Object> parameters = new ArrayList<Object>();
        List<ActivityEvent> rawEvents;

        parameters.add(start);
        
        try {
            rawEvents =
                asApi.searchEvents("act.date > ? and act.type in ('" + StringUtils.join(MATCHING_EVENT_TYPES, "','")
                    + "')", false, true, 0, 0, parameters);

            // If the page has been modified several times we want to display only one diff, if the page has been
            // delete after update events we want to discard the update events since we won't be able to display diff
            // from a deleted document. See WatchListEvent#addEvent(WatchListEvent) and
            // WatchListEvent#equals(WatchListEvent).
            for (ActivityEvent rawEvent : rawEvents) {
                WatchListEvent event = new WatchListEvent(rawEvent);
                if (!events.contains(event)) {
                    events.add(new WatchListEvent(rawEvent));
                } else {
                    WatchListEvent existingCompositeEvent = events.get(events.indexOf(event));
                    existingCompositeEvent.addEvent(event);
                }
            }
        } catch (ActivityStreamException e) {
            LOG.error("Failed to retrieve updated documents from activity stream");<commit_msg>XPWATCHLIST-67 : Possible NPE during wiki initialization<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.xpn.xwiki.plugin.watchlist;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.plugin.activitystream.api.ActivityEventType;
import com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException;
import com.xpn.xwiki.plugin.activitystream.plugin.ActivityEvent;
import com.xpn.xwiki.plugin.activitystream.plugin.ActivityStreamPluginApi;

/**
 * Matcher for WatchList events. This class store all the events fired during a given interval. It also allows to 
 * perform a match between events and elements watched by a user.
 * 
 * @version $Id$
 */
@SuppressWarnings("serial")
public class WatchListEventMatcher
{
    /**
     * Logger.
     */
    private static final Log LOG = LogFactory.getLog(WatchListEventMatcher.class);

    /**
     * Events to match.
     */
    private static final List<String> MATCHING_EVENT_TYPES = new ArrayList<String>()
    {
        {
            add(ActivityEventType.CREATE);
            add(ActivityEventType.UPDATE);
            add(ActivityEventType.DELETE);
        }
    };

    /**
     * ActivityStream plugin API.
     */
    private final ActivityStreamPluginApi asApi;

    /**
     * List of events which have occurred between the start date and the current time.
     */
    private final List<WatchListEvent> events = new ArrayList<WatchListEvent>();

    /**
     * Constructor. Gets all the events fired during the interval between the given date and the current date.
     * 
     * @param context the XWiki context
     * @param start start date to use for document matching
     */
    public WatchListEventMatcher(Date start, XWikiContext context)
    {
        asApi = (ActivityStreamPluginApi) context.getWiki().getPluginApi("activitystream", context);
        List<Object> parameters = new ArrayList<Object>();
        List<ActivityEvent> rawEvents;

        parameters.add(start);
        
        try {
            rawEvents =
                asApi.searchEvents("act.date > ? and act.type in ('" + StringUtils.join(MATCHING_EVENT_TYPES, "','")
                    + "')", false, true, 0, 0, parameters);
            
            // During wiki initialization searchEvents may return null.
            if (rawEvents != null) {
                // If the page has been modified several times we want to display only one diff, if the page has been
                // delete after update events we want to discard the update events since we won't be able to display 
                // diff from a deleted document. See WatchListEvent#addEvent(WatchListEvent) and 
                // WatchListEvent#equals(WatchListEvent).
                for (ActivityEvent rawEvent : rawEvents) {
                    WatchListEvent event = new WatchListEvent(rawEvent);
                    if (!events.contains(event)) {
                        events.add(new WatchListEvent(rawEvent));
                    } else {
                        WatchListEvent existingCompositeEvent = events.get(events.indexOf(event));
                        existingCompositeEvent.addEvent(event);
                    }
                }
            }
        } catch (ActivityStreamException e) {
            LOG.error("Failed to retrieve updated documents from activity stream");
{'fraction_non_alphanumeric': 0.08306745670009116, 'fraction_numerical': 0.0036463081130355514, 'mean_word_length': 2.77505376344086, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3442379', 'n_tokens_mistral': 2276, 'n_tokens_neox': 2205, 'n_words': 780}