<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.
 *
 * @author jeremi
 */
package org.curriki.gwt.server;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiAttachment;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.gwt.api.client.Document;
import com.xpn.xwiki.gwt.api.client.XWikiGWTException;
import com.xpn.xwiki.gwt.api.server.XWikiServiceImpl;
import com.xpn.xwiki.objects.BaseElement;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.objects.ListProperty;
import com.xpn.xwiki.objects.LongProperty;
import com.xpn.xwiki.objects.PropertyInterface;
import com.xpn.xwiki.objects.classes.ListItem;
import com.xpn.xwiki.plugin.image.ImagePlugin;
import com.xpn.xwiki.plugin.lucene.LucenePlugin;
import com.xpn.xwiki.plugin.lucene.LucenePluginApi;
import com.xpn.xwiki.plugin.lucene.SearchResult;
import com.xpn.xwiki.plugin.lucene.SearchResults;
import com.xpn.xwiki.plugin.zipexplorer.ZipExplorerPlugin;
import com.xpn.xwiki.web.XWikiEngineContext;
import com.xpn.xwiki.web.XWikiMessageTool;
import com.xpn.xwiki.web.XWikiRequest;
import com.xpn.xwiki.web.XWikiResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.curriki.gwt.client.AssetDocument;
import org.curriki.gwt.client.Constants;
import org.curriki.gwt.client.CurrikiService;
import org.curriki.gwt.client.TreeListItem;
import org.curriki.gwt.client.search.queries.AssetDocumentWithOwnerName;
import org.curriki.gwt.client.widgets.browseasset.AssetItem;
import org.curriki.gwt.client.widgets.template.TemplateInfo;
import org.curriki.xwiki.plugin.asset.Asset;
import org.curriki.xwiki.plugin.mimetype.MimeType;
import org.curriki.xwiki.plugin.mimetype.MimeTypePlugin;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

public class CurrikiServiceImpl extends XWikiServiceImpl implements CurrikiService {
    private static final Log log = LogFactory.getLog(CurrikiServiceImpl.class);

    public CurrikiServiceImpl() {
        super();
    }

    public CurrikiServiceImpl(XWikiRequest request, XWikiResponse response, XWikiEngineContext engine) {
        super(request, response, engine);
    }

    public boolean isDefaultCollectionExists(String space) throws XWikiGWTException {
        return isCollectionExists(space, Constants.DEFAULT_COLLECTION_PAGE);
    }

    private boolean isCollectionExists(String space, String pageName) throws XWikiGWTException {
        try {
            XWikiContext context = getXWikiContext();

            XWikiDocument doc = context.getWiki().getDocument(space, pageName, context);
            if (doc.isNew())
                return false;
            if (doc.getObjectNumbers(Constants.COMPOSITEASSET_CLASS) == 0){
                return false;
            } else {
                // Work around a bug XWIKI-1624
                // TODO: Remove the work-around once XWIKI-1624 is fixed
                List subAssets = doc.getObjects(Constants.COMPOSITEASSET_CLASS);
                Iterator i = subAssets.iterator();
                int count = 0;
                while (i.hasNext() && count == 0){
                    if (i.next() != null){
                        count++;
                    }
                }
                if (count == 0){
                    return false;
                }
            }
            return true;
        } catch (Exception e) {
            throw getXWikiGWTException(e);
        }
    }

    private boolean createDefaultCollection(String space) throws XWikiGWTException {
        return createCollection(space, Constants.DEFAULT_COLLECTION_PAGE);
    }

    public boolean createCollection(String space, String pageName) throws XWikiGWTException {
        try {
            XWikiContext context = getXWikiContext();

            XWikiMessageTool msg = (XWikiMessageTool) context.get("msg");
            String pageTitle = msg.get("Untitled");
            return (createCollection(space, pageName, pageTitle) != null);
        } catch (Exception e) {
            throw getXWikiGWTException(e);
        }
    }

    public String createCollection(String space, String pageName, String pageTitle) throws XWikiGWTException {
        Document doc = createCollectionDocument(space, pageName, pageTitle);
        if (doc != null){
            return doc.getFullName();
        } else {
            return null;
        }
    }

    public Document createCollectionDocument(String space, String pageName, String pageTitle) throws XWikiGWTException {

        try {
            XWikiContext context = getXWikiContext();
            if (space == null){
                // Use default space for user
                space = "Coll_" + context.getUser().replaceFirst("XWiki.", "");
            }

            if (pageName == null){
                // Generate a random page name
                pageName = context.getWiki().getUniquePageName(space, context);
            }

            if (pageTitle == null){
                XWikiMessageTool msg = (XWikiMessageTool) context.get("msg");
                pageTitle = msg.get("Untitled");
            }

            // TODO: Why in createCollection we don't raise an error if the collection exist instead of returning it ?
            if (isCollectionExists(space, pageName)){
                try {
                    XWikiDocument doc = context.getWiki().getDocument(space, pageName, context);
                    return newDocument(new Document(), doc, true, false, true, false, context);
                } catch (Exception e) {
                    throw getXWikiGWTException(e);
                }
            }


            if (!isRootCollectionExists(space, context)){
                createRootCollection(space, context);
            }

            if (!pageName.equals(Constants.DEFAULT_COLLECTION_PAGE)){
                if (!isDefaultCollectionExists(space)){
                    createDefaultCollection(space);
                }
            }

            Document doc;
            if (pageName.equals(Constants.DEFAULT_COLLECTION_PAGE)){
                // Just create the Default collection
                doc = createCompositeAsset(space, space+"."+Constants.ROOT_COLLECTION_PAGE, pageName, Constants.COMPOSITE_COLLECTION, -1);
            } else {
                doc = createTempCompositeAsset(space+"."+Constants.ROOT_COLLECTION_PAGE, Constants.COMPOSITE_COLLECTION);
            }

            XWikiDocument xDoc = initCollectionSettings(doc.getFullName(), pageTitle, context);

            return newDocument(new Document(), xDoc, true, false, true, false, context);
        } catch (Exception e) {
            throw getXWikiGWTException(e);
        }<commit_msg>- CURRIKI-1379  - Don't create a DEFAULT_COLLECTION_PAGE when dealing with group spaces<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.
 *
 * @author jeremi
 */
package org.curriki.gwt.server;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiAttachment;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.gwt.api.client.Document;
import com.xpn.xwiki.gwt.api.client.XWikiGWTException;
import com.xpn.xwiki.gwt.api.server.XWikiServiceImpl;
import com.xpn.xwiki.objects.BaseElement;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.objects.ListProperty;
import com.xpn.xwiki.objects.LongProperty;
import com.xpn.xwiki.objects.PropertyInterface;
import com.xpn.xwiki.objects.classes.ListItem;
import com.xpn.xwiki.plugin.image.ImagePlugin;
import com.xpn.xwiki.plugin.lucene.LucenePlugin;
import com.xpn.xwiki.plugin.lucene.LucenePluginApi;
import com.xpn.xwiki.plugin.lucene.SearchResult;
import com.xpn.xwiki.plugin.lucene.SearchResults;
import com.xpn.xwiki.plugin.zipexplorer.ZipExplorerPlugin;
import com.xpn.xwiki.web.XWikiEngineContext;
import com.xpn.xwiki.web.XWikiMessageTool;
import com.xpn.xwiki.web.XWikiRequest;
import com.xpn.xwiki.web.XWikiResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.curriki.gwt.client.AssetDocument;
import org.curriki.gwt.client.Constants;
import org.curriki.gwt.client.CurrikiService;
import org.curriki.gwt.client.TreeListItem;
import org.curriki.gwt.client.search.queries.AssetDocumentWithOwnerName;
import org.curriki.gwt.client.widgets.browseasset.AssetItem;
import org.curriki.gwt.client.widgets.template.TemplateInfo;
import org.curriki.xwiki.plugin.asset.Asset;
import org.curriki.xwiki.plugin.mimetype.MimeType;
import org.curriki.xwiki.plugin.mimetype.MimeTypePlugin;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

public class CurrikiServiceImpl extends XWikiServiceImpl implements CurrikiService {
    private static final Log log = LogFactory.getLog(CurrikiServiceImpl.class);

    public CurrikiServiceImpl() {
        super();
    }

    public CurrikiServiceImpl(XWikiRequest request, XWikiResponse response, XWikiEngineContext engine) {
        super(request, response, engine);
    }

    public boolean isDefaultCollectionExists(String space) throws XWikiGWTException {
        return isCollectionExists(space, Constants.DEFAULT_COLLECTION_PAGE);
    }

    private boolean isCollectionExists(String space, String pageName) throws XWikiGWTException {
        try {
            XWikiContext context = getXWikiContext();

            XWikiDocument doc = context.getWiki().getDocument(space, pageName, context);
            if (doc.isNew())
                return false;
            if (doc.getObjectNumbers(Constants.COMPOSITEASSET_CLASS) == 0){
                return false;
            } else {
                // Work around a bug XWIKI-1624
                // TODO: Remove the work-around once XWIKI-1624 is fixed
                List subAssets = doc.getObjects(Constants.COMPOSITEASSET_CLASS);
                Iterator i = subAssets.iterator();
                int count = 0;
                while (i.hasNext() && count == 0){
                    if (i.next() != null){
                        count++;
                    }
                }
                if (count == 0){
                    return false;
                }
            }
            return true;
        } catch (Exception e) {
            throw getXWikiGWTException(e);
        }
    }

    private boolean createDefaultCollection(String space) throws XWikiGWTException {
        return createCollection(space, Constants.DEFAULT_COLLECTION_PAGE);
    }

    public boolean createCollection(String space, String pageName) throws XWikiGWTException {
        try {
            XWikiContext context = getXWikiContext();

            XWikiMessageTool msg = (XWikiMessageTool) context.get("msg");
            String pageTitle = msg.get("Untitled");
            return (createCollection(space, pageName, pageTitle) != null);
        } catch (Exception e) {
            throw getXWikiGWTException(e);
        }
    }

    public String createCollection(String space, String pageName, String pageTitle) throws XWikiGWTException {
        Document doc = createCollectionDocument(space, pageName, pageTitle);
        if (doc != null){
            return doc.getFullName();
        } else {
            return null;
        }
    }

    public Document createCollectionDocument(String space, String pageName, String pageTitle) throws XWikiGWTException {

        try {
            XWikiContext context = getXWikiContext();
            if (space == null){
                // Use default space for user
                space = "Coll_" + context.getUser().replaceFirst("XWiki.", "");
            }

            if (pageName == null){
                // Generate a random page name
                pageName = context.getWiki().getUniquePageName(space, context);
            }

            if (pageTitle == null){
                XWikiMessageTool msg = (XWikiMessageTool) context.get("msg");
                pageTitle = msg.get("Untitled");
            }

            // TODO: Why in createCollection we don't raise an error if the collection exist instead of returning it ?
            if (isCollectionExists(space, pageName)){
                try {
                    XWikiDocument doc = context.getWiki().getDocument(space, pageName, context);
                    return newDocument(new Document(), doc, true, false, true, false, context);
                } catch (Exception e) {
                    throw getXWikiGWTException(e);
                }
            }


            if (!isRootCollectionExists(space, context)){
                createRootCollection(space, context);
            }

            // CURRIKI-1379 - Group spaces don't get Favorites collection
            if (!pageName.equals(Constants.DEFAULT_COLLECTION_PAGE) && !space.startsWith("Coll_Group_")){
                if (!isDefaultCollectionExists(space)){
                    createDefaultCollection(space);
                }
            }

            Document doc;
            if (pageName.equals(Constants.DEFAULT_COLLECTION_PAGE)){
                // Just create the Default collection
                doc = createCompositeAsset(space, space+"."+Constants.ROOT_COLLECTION_PAGE, pageName, Constants.COMPOSITE_COLLECTION, -1);
            } else {
                doc = createTempCompositeAsset(space+"."+Constants.ROOT_COLLECTION_PAGE, Constants.COMPOSITE_COLLECTION);
            }

            XWikiDocument xDoc = initCollectionSettings(doc.getFullName(), pageTitle, context);

            return newDocument(new Document(), xDoc, true, false, true, false, context);
        } catch (Exception e) {
            throw getXWikiGWTException(e);
        }
{'fraction_non_alphanumeric': 0.0914852752880922, 'fraction_numerical': 0.0038412291933418692, 'mean_word_length': 2.6226808905380334, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6888676', 'n_tokens_mistral': 4499, 'n_tokens_neox': 4325, 'n_words': 1066}