<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.activitystream.impl;

import java.util.List;
import java.util.ArrayList;
import java.io.StringWriter;
import java.net.URL;
import java.net.MalformedURLException;

import org.apache.commons.lang.RandomStringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.notify.DocChangeRule;
import com.xpn.xwiki.notify.XWikiDocChangeNotificationInterface;
import com.xpn.xwiki.notify.XWikiNotificationRule;
import com.xpn.xwiki.plugin.activitystream.api.ActivityEvent;
import com.xpn.xwiki.plugin.activitystream.api.ActivityEventPriority;
import com.xpn.xwiki.plugin.activitystream.api.ActivityEventType;
import com.xpn.xwiki.plugin.activitystream.api.ActivityStream;
import com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException;
import com.xpn.xwiki.plugin.activitystream.plugin.ActivityStreamPlugin;
import com.xpn.xwiki.store.XWikiHibernateStore;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.SyndFeedOutput;

/**
 * @version $Id: $
 */
public class ActivityStreamImpl implements ActivityStream, XWikiDocChangeNotificationInterface
{
    public void init(XWikiContext context) throws XWikiException
    {
        // listen to notifications
        context.getWiki().getNotificationManager().addGeneralRule(new DocChangeRule(this));
        // init activitystream cleaner
        ActivityStreamCleaner.getInstance().init(context);
    }

    protected void prepareEvent(ActivityEvent event, XWikiDocument doc, XWikiContext context)
    {
        if (doc == null)
            doc = context.getDoc();

        if (event.getUser() == null) {
            event.setUser(context.getUser());
        }
        if (event.getStream() == null) {
            String space = (doc == null) ? "" : doc.getSpace();

            event.setStream(getStreamName(space, context));
        }
        if (event.getWiki() == null) {
            event.setWiki(context.getDatabase());
        }
        if (event.getSpace() == null) {
            event.setSpace((doc == null) ? "" : doc.getSpace());
        }
        if (event.getPage() == null) {
            event.setPage((doc == null) ? "" : doc.getFullName());
        }
        if (event.getUrl() == null) {
            event.setUrl((doc == null) ? "" : doc.getURL("view", context));
        }
        if (event.getApplication() == null) {
            event.setApplication("xwiki");
        }
        if (event.getDate() == null) {
            event.setDate(context.getWiki().getCurrentDate());
        }
        if (event.getEventId() == null) {
            event.setEventId(generateEventId(event, context));
        }
        if (event.getRequestId() == null) {
            event.setRequestId((String) context.get("activitystream_requestid"));
        }
    }

    public String getStreamName(String space, XWikiContext context)
    {
        return space;
    }

    protected String generateEventId(ActivityEvent event, XWikiContext context)
    {
        String key =
            event.getStream() + "-" + event.getApplication() + "-" + event.getWiki() + ":" + event.getPage() + "-"
                + event.getType();
        long hash = key.hashCode();
        if (hash < 0)
            hash = -hash;

        String id = "" + hash + "-" + event.getDate().getTime() + "-" + RandomStringUtils.randomAlphanumeric(8);
        if (context.get("activitystream_requestid") == null) {
            context.put("activitystream_requestid", id);
        }
        return id;
    }

    public void addActivityEvent(ActivityEvent event, XWikiContext context) throws ActivityStreamException
    {
        addActivityEvent(event, null, context);
    }

    /**
     * This method determine if events must be store in the local wiki. If the wiki is not running in virtual mode this
     * method will always return true. If it is running in virtual and if the activitystream is set not to store events
     * in the main wiki the method will always return true. It the configuration does not match those 2 conditions, the
     * method retrieves the platform.plugin.activitystream.uselocalstore configuration option. If the option is not
     * found the method returns true (default behavior).
     * 
     * @param context the XWiki context
     * @return true if the activity stream is configured to store events in the main wiki, false otherwise
     */
    private boolean useLocalStore(XWikiContext context)
    {
        if (!context.getWiki().isVirtualMode()) {
            // If we aren't in virtual mode, force local store.
            return true;
        } else if (!useMainStore(context)) {
            // If we are in virtual mode but the main store is disabled, force local store.
            return true;
        }

        ActivityStreamPlugin plugin = (ActivityStreamPlugin) context.getWiki().getPlugin("activitystream", context);
        return Integer.parseInt(plugin.getActivityStreamPreference("uselocalstore", "1", context)) == 1;
    }

    /**
     * This method determine if events must be store in the main wiki. If the wiki is not running in virtual mode this
     * method will always return false. If it is running in virtual mode this method retrieves the
     * platform.plugin.activitystream.usemainstore configuration option. If the option is not found the method returns
     * true (default behavior).
     * 
     * @param context the XWiki context
     * @return true if the activity stream is configured to store events in the main wiki, false otherwise
     */
    private boolean useMainStore(XWikiContext context)
    {
        if (context.getWiki().isVirtualMode() && context.getDatabase().equals(context.getMainXWiki())) {
            // We're in the main database, we don't have to store the data twice.
            return false;
        }
        
        ActivityStreamPlugin plugin = (ActivityStreamPlugin) context.getWiki().getPlugin("activitystream", context);
        return Integer.parseInt(plugin.getActivityStreamPreference("usemainstore", "1", context)) == 1;
    }

    public void addActivityEvent(ActivityEvent event, XWikiDocument doc, XWikiContext context)
        throws ActivityStreamException
    {
        prepareEvent(event, doc, context);

        if (useLocalStore(context)) {
            // store event in the local database
            XWikiHibernateStore localHibernateStore = context.getWiki().getHibernateStore();<commit_msg>Fixed bug introduced in a previous commit.<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.activitystream.impl;

import java.util.List;
import java.util.ArrayList;
import java.io.StringWriter;
import java.net.URL;
import java.net.MalformedURLException;

import org.apache.commons.lang.RandomStringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.notify.DocChangeRule;
import com.xpn.xwiki.notify.XWikiDocChangeNotificationInterface;
import com.xpn.xwiki.notify.XWikiNotificationRule;
import com.xpn.xwiki.plugin.activitystream.api.ActivityEvent;
import com.xpn.xwiki.plugin.activitystream.api.ActivityEventPriority;
import com.xpn.xwiki.plugin.activitystream.api.ActivityEventType;
import com.xpn.xwiki.plugin.activitystream.api.ActivityStream;
import com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException;
import com.xpn.xwiki.plugin.activitystream.plugin.ActivityStreamPlugin;
import com.xpn.xwiki.store.XWikiHibernateStore;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.SyndFeedOutput;

/**
 * @version $Id: $
 */
public class ActivityStreamImpl implements ActivityStream, XWikiDocChangeNotificationInterface
{
    public void init(XWikiContext context) throws XWikiException
    {
        // listen to notifications
        context.getWiki().getNotificationManager().addGeneralRule(new DocChangeRule(this));
        // init activitystream cleaner
        ActivityStreamCleaner.getInstance().init(context);
    }

    protected void prepareEvent(ActivityEvent event, XWikiDocument doc, XWikiContext context)
    {
        if (doc == null)
            doc = context.getDoc();

        if (event.getUser() == null) {
            event.setUser(context.getUser());
        }
        if (event.getStream() == null) {
            String space = (doc == null) ? "" : doc.getSpace();

            event.setStream(getStreamName(space, context));
        }
        if (event.getWiki() == null) {
            event.setWiki(context.getDatabase());
        }
        if (event.getSpace() == null) {
            event.setSpace((doc == null) ? "" : doc.getSpace());
        }
        if (event.getPage() == null) {
            event.setPage((doc == null) ? "" : doc.getFullName());
        }
        if (event.getUrl() == null) {
            event.setUrl((doc == null) ? "" : doc.getURL("view", context));
        }
        if (event.getApplication() == null) {
            event.setApplication("xwiki");
        }
        if (event.getDate() == null) {
            event.setDate(context.getWiki().getCurrentDate());
        }
        if (event.getEventId() == null) {
            event.setEventId(generateEventId(event, context));
        }
        if (event.getRequestId() == null) {
            event.setRequestId((String) context.get("activitystream_requestid"));
        }
    }

    public String getStreamName(String space, XWikiContext context)
    {
        return space;
    }

    protected String generateEventId(ActivityEvent event, XWikiContext context)
    {
        String key =
            event.getStream() + "-" + event.getApplication() + "-" + event.getWiki() + ":" + event.getPage() + "-"
                + event.getType();
        long hash = key.hashCode();
        if (hash < 0)
            hash = -hash;

        String id = "" + hash + "-" + event.getDate().getTime() + "-" + RandomStringUtils.randomAlphanumeric(8);
        if (context.get("activitystream_requestid") == null) {
            context.put("activitystream_requestid", id);
        }
        return id;
    }

    public void addActivityEvent(ActivityEvent event, XWikiContext context) throws ActivityStreamException
    {
        addActivityEvent(event, null, context);
    }

    /**
     * This method determine if events must be store in the local wiki. If the wiki is not running in virtual mode this
     * method will always return true. If it is running in virtual and if the activitystream is set not to store events
     * in the main wiki the method will always return true. It the configuration does not match those 2 conditions, the
     * method retrieves the platform.plugin.activitystream.uselocalstore configuration option. If the option is not
     * found the method returns true (default behavior).
     * 
     * @param context the XWiki context
     * @return true if the activity stream is configured to store events in the main wiki, false otherwise
     */
    private boolean useLocalStore(XWikiContext context)
    {
        if (!context.getWiki().isVirtualMode()) {
            // If we aren't in virtual mode, force local store.
            return true;
        } else if (!useMainStore(context)) {
            // If we are in virtual mode but the main store is disabled, force local store.
            return true;
        }

        ActivityStreamPlugin plugin = (ActivityStreamPlugin) context.getWiki().getPlugin("activitystream", context);
        return Integer.parseInt(plugin.getActivityStreamPreference("uselocalstore", "1", context)) == 1;
    }

    /**
     * This method determine if events must be store in the main wiki. If the wiki is not running in virtual mode this
     * method will always return false. If it is running in virtual mode this method retrieves the
     * platform.plugin.activitystream.usemainstore configuration option. If the option is not found the method returns
     * true (default behavior).
     * 
     * @param context the XWiki context
     * @return true if the activity stream is configured to store events in the main wiki, false otherwise
     */
    private boolean useMainStore(XWikiContext context)
    {
        if (!context.getWiki().isVirtualMode()) {
            // If we aren't in virtual mode, local store is forced.
            return false;
        }

        if (context.getWiki().isVirtualMode() && context.getDatabase().equals(context.getMainXWiki())) {
            // We're in the main database, we don't have to store the data twice.
            return false;
        }
        
        ActivityStreamPlugin plugin = (ActivityStreamPlugin) context.getWiki().getPlugin("activitystream", context);
        return Integer.parseInt(plugin.getActivityStreamPreference("usemainstore", "1", context)) == 1;
    }

    public void addActivityEvent(ActivityEvent event, XWikiDocument doc, XWikiContext context)
        throws ActivityStreamException
    {
        prepareEvent(event, doc, context);

        if (useLocalStore(context)) {
            // store event in the local database
            XWikiHibernateStore localHibernateStore = context.getWiki().getHibernateStore();
{'fraction_non_alphanumeric': 0.10009052114315273, 'fraction_numerical': 0.00258631837579206, 'mean_word_length': 3.0080331692148223, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, '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': '425555', 'n_tokens_mistral': 4260, 'n_tokens_neox': 4173, 'n_words': 1251}