<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.calendar;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.commons.lang.StringUtils;
import org.apache.velocity.VelocityContext;

import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.objects.DateProperty;
import com.xpn.xwiki.objects.LargeStringProperty;
import com.xpn.xwiki.objects.StringListProperty;
import com.xpn.xwiki.objects.StringProperty;
import com.xpn.xwiki.render.XWikiVelocityRenderer;

/**
 * CalendarData stores a list of events that can be displayed in an Event Calendar. The internal data is of type
 * {@link com.xpn.xwiki.plugin.calendar.CalendarEvent}. The list can be populated with CalendarEvent XWiki objects from
 * a document, with document names retrieved by a custom hibernate query, or with recently changed document names.
 */
public class CalendarData
{
    private Map cdata = new HashMap();

    /**
     * Default constructor. Leaves the list of events empty.
     */
    public CalendarData()
    {
    }

    /**
     * The most used constructor, which populates the list with CalendarEvent XWiki objects from the current document.
     * 
     * @param user The default user to be used.
     * @param context The request context
     * @throws XWikiException
     */
    public CalendarData(String user, XWikiContext context) throws XWikiException
    {
        addCalendarData(context.getDoc(), user, context);
    }

    public CalendarData(XWikiDocument doc, String user, XWikiContext context) throws XWikiException
    {
        addCalendarData(doc, user, context);
    }

    public CalendarData(String hql, String user, XWikiContext context) throws XWikiException
    {
        XWiki xwiki = context.getWiki();
        List list = xwiki.getStore().searchDocumentsNames(hql, context);
        for (int i = 0; i < list.size(); i++) {
            String docname = (String) list.get(i);
            XWikiDocument doc = xwiki.getDocument(docname, context);
            addCalendarData(doc, user, context);
        }
    }

    public CalendarData(String hql, int nb, XWikiContext context) throws XWikiException
    {
        XWiki xwiki = context.getWiki();
        List list = xwiki.getStore().searchDocumentsNames(hql, nb, 0, context);
        SimpleDateFormat df = new SimpleDateFormat("yyMMdd");
        for (int i = 0; i < list.size(); i++) {
            String docname = (String) list.get(i);
            XWikiDocument doc = xwiki.getDocument(docname, context);
            Date date = doc.getDate();
            Calendar cdate = Calendar.getInstance();
            cdate.setTime(date);
            this.cdata.put(df.format(date), new CalendarEvent(cdate, cdate, "", "[" + doc.getDisplayTitle(context) + ">"
                + doc.getFullName() + "] by " + context.getWiki().getLocalUserName(doc.getAuthor(), context)));
        }
    }

    public List getCalendarData()
    {
        List ldata = new ArrayList();
        List sortedKeys = new ArrayList(this.cdata.keySet());
        java.util.Collections.sort(sortedKeys);
        for (Iterator it = sortedKeys.iterator(); it.hasNext();) {
            ldata.addAll((List) this.cdata.get(it.next()));
        }
        return ldata;
    }

    public Map getMappedCalendarData()
    {
        return this.cdata;
    }

    public List getCalendarData(Calendar date)
    {
        Object result = this.cdata.get(new SimpleDateFormat("yyMMdd").format(date.getTime()));
        if (result == null) {
            result = new ArrayList();
        }
        return (List) result;
    }

    /**
     * List populating method. It iterates the CalendarEvent objects stored in the given document and creates Java
     * wrappers that can be used by the Calendar plugin.
     * 
     * @param doc The source document, populated with CalendarEvent objects.
     * @param defaultUser The username to be used ig objects do not have a User field.
     * @throws XWikiException
     */
    public void addCalendarData(XWikiDocument doc, String defaultUser, XWikiContext context) throws XWikiException
    {
        if (doc == null) {
            return;
        }
        if (defaultUser == null) {
            BaseObject bobj = doc.getObject("XWiki.XWikiUsers");<commit_msg>XACALENDAR-12 : Users see all events in the calendar, even those from documents they're not able to view<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.calendar;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.commons.lang.StringUtils;
import org.apache.velocity.VelocityContext;

import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.objects.DateProperty;
import com.xpn.xwiki.objects.LargeStringProperty;
import com.xpn.xwiki.objects.StringListProperty;
import com.xpn.xwiki.objects.StringProperty;
import com.xpn.xwiki.render.XWikiVelocityRenderer;

/**
 * CalendarData stores a list of events that can be displayed in an Event Calendar. The internal data is of type
 * {@link com.xpn.xwiki.plugin.calendar.CalendarEvent}. The list can be populated with CalendarEvent XWiki objects from
 * a document, with document names retrieved by a custom hibernate query, or with recently changed document names.
 */
public class CalendarData
{
    private Map cdata = new HashMap();

    /**
     * Default constructor. Leaves the list of events empty.
     */
    public CalendarData()
    {
    }

    /**
     * The most used constructor, which populates the list with CalendarEvent XWiki objects from the current document.
     * 
     * @param user The default user to be used.
     * @param context The request context
     * @throws XWikiException
     */
    public CalendarData(String user, XWikiContext context) throws XWikiException
    {
        addCalendarData(context.getDoc(), user, context);
    }

    public CalendarData(XWikiDocument doc, String user, XWikiContext context) throws XWikiException
    {
        addCalendarData(doc, user, context);
    }

    public CalendarData(String hql, String user, XWikiContext context) throws XWikiException
    {
        XWiki xwiki = context.getWiki();
        List list = xwiki.getStore().searchDocumentsNames(hql, context);
        for (int i = 0; i < list.size(); i++) {
            String docname = (String) list.get(i);
            XWikiDocument doc = xwiki.getDocument(docname, context);
            addCalendarData(doc, user, context);
        }
    }

    public CalendarData(String hql, int nb, XWikiContext context) throws XWikiException
    {
        XWiki xwiki = context.getWiki();
        List list = xwiki.getStore().searchDocumentsNames(hql, nb, 0, context);
        SimpleDateFormat df = new SimpleDateFormat("yyMMdd");
        for (int i = 0; i < list.size(); i++) {
            String docname = (String) list.get(i);
            XWikiDocument doc = xwiki.getDocument(docname, context);
            Date date = doc.getDate();
            Calendar cdate = Calendar.getInstance();
            cdate.setTime(date);
            this.cdata.put(df.format(date), new CalendarEvent(cdate, cdate, "", "[" + doc.getDisplayTitle(context) + ">"
                + doc.getFullName() + "] by " + context.getWiki().getLocalUserName(doc.getAuthor(), context)));
        }
    }

    public List getCalendarData()
    {
        List ldata = new ArrayList();
        List sortedKeys = new ArrayList(this.cdata.keySet());
        java.util.Collections.sort(sortedKeys);
        for (Iterator it = sortedKeys.iterator(); it.hasNext();) {
            ldata.addAll((List) this.cdata.get(it.next()));
        }
        return ldata;
    }

    public Map getMappedCalendarData()
    {
        return this.cdata;
    }

    public List getCalendarData(Calendar date)
    {
        Object result = this.cdata.get(new SimpleDateFormat("yyMMdd").format(date.getTime()));
        if (result == null) {
            result = new ArrayList();
        }
        return (List) result;
    }

    /**
     * List populating method. It iterates the CalendarEvent objects stored in the given document and creates Java
     * wrappers that can be used by the Calendar plugin.
     * 
     * @param doc The source document, populated with CalendarEvent objects.
     * @param defaultUser The username to be used ig objects do not have a User field.
     * @throws XWikiException
     */
    public void addCalendarData(XWikiDocument doc, String defaultUser, XWikiContext context) throws XWikiException
    {
        if (doc == null) {
            return;
        }
        
        if (!context.getWiki().getRightService().hasAccessLevel("view", context.getUser(), doc.getFullName(), context)) {
        	return;
        }
        
        if (defaultUser == null) {
            BaseObject bobj = doc.getObject("XWiki.XWikiUsers");
{'fraction_non_alphanumeric': 0.0954683861723088, 'fraction_numerical': 0.003044957907934802, 'mean_word_length': 2.9982098102398855, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '538866', 'n_tokens_mistral': 3210, 'n_tokens_neox': 3113, 'n_words': 1023}