<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.internal.mandatory;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.lang3.StringUtils;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.sheet.SheetBinder;

import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.doc.MandatoryDocumentInitializer;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.classes.BaseClass;
import com.xpn.xwiki.objects.classes.BooleanClass;
import com.xpn.xwiki.user.api.XWikiRightService;

/**
 * Base class for standard class providers.
 *
 * @version $Id$
 * @since 4.3M1
 */
public abstract class AbstractMandatoryDocumentInitializer implements MandatoryDocumentInitializer
{
    /**
     * Used to associate a document with a document sheet.
     */
    @Inject
    @Named("document")
    protected SheetBinder documentSheetBinder;

    /**
     * @see #getDocumentReference()
     */
    private EntityReference reference;

    /**
     * @param reference the reference of the document to update. Can be either local or absolute depending if the
     *            document is associated to a specific wiki or not
     */
    public AbstractMandatoryDocumentInitializer(EntityReference reference)
    {
        this.reference = reference;
    }

    /**
     * @param spaceName the space name of the document
     * @param documentName the document name of the document
     */
    public AbstractMandatoryDocumentInitializer(String spaceName, String documentName)
    {
        this(new EntityReference(documentName, EntityType.DOCUMENT, new EntityReference(spaceName, EntityType.SPACE)));
    }

    @Override
    public EntityReference getDocumentReference()
    {
        return this.reference;
    }

    /**
     * Set the fields of the class document passed as parameter. Can generate content for both XWiki Syntax 1.0 and
     * XWiki Syntax 2.0. If new documents are set to be created in XWiki Syntax 1.0 then generate XWiki 1.0 Syntax
     * otherwise generate XWiki Syntax 2.0.
     *
     * @param document the document
     * @param title the page title to set
     * @return true if the document has been modified, false otherwise
     */
    protected boolean setClassDocumentFields(XWikiDocument document, String title)
    {
        boolean needsUpdate = false;

        if (document.getCreatorReference() == null) {
            needsUpdate = true;
            document.setCreator(XWikiRightService.SUPERADMIN_USER);
        }
        if (document.getAuthorReference() == null) {
            needsUpdate = true;
            document.setAuthorReference(document.getCreatorReference());
        }

        if (StringUtils.isBlank(document.getParent())) {
            needsUpdate = true;
            document.setParent("XWiki.XWikiClasses");
        }

        if (StringUtils.isBlank(document.getTitle())) {
            needsUpdate = true;
            document.setTitle(title);
        }

        if (!document.isHidden()) {
            needsUpdate = true;
            document.setHidden(true);
        }

        // Use ClassSheet to display the class document if no other sheet is explicitly specified.
        if (this.documentSheetBinder.getSheets(document).isEmpty()) {
            String wikiName = document.getDocumentReference().getWikiReference().getName();
            DocumentReference sheet = new DocumentReference(wikiName, XWiki.SYSTEM_SPACE, "ClassSheet");
            needsUpdate |= this.documentSheetBinder.bind(document, sheet);
        }

        return needsUpdate;
    }
<commit_msg>XWIKI-11885: Expose a more generic setDocumentFields method in AbstractMandatoryDocumentInitializer<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.internal.mandatory;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.lang3.StringUtils;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.LocalDocumentReference;
import org.xwiki.sheet.SheetBinder;

import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.doc.MandatoryDocumentInitializer;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.classes.BaseClass;
import com.xpn.xwiki.objects.classes.BooleanClass;
import com.xpn.xwiki.user.api.XWikiRightService;

/**
 * Base class for standard class providers.
 *
 * @version $Id$
 * @since 4.3M1
 */
public abstract class AbstractMandatoryDocumentInitializer implements MandatoryDocumentInitializer
{
    /**
     * Used to associate a document with a document sheet.
     */
    @Inject
    @Named("document")
    protected SheetBinder documentSheetBinder;

    /**
     * @see #getDocumentReference()
     */
    private EntityReference reference;

    /**
     * @param reference the reference of the document to update. Can be either local or absolute depending if the
     *            document is associated to a specific wiki or not
     */
    public AbstractMandatoryDocumentInitializer(EntityReference reference)
    {
        this.reference = reference;
    }

    /**
     * @param spaceName the space name of the document
     * @param documentName the document name of the document
     */
    public AbstractMandatoryDocumentInitializer(String spaceName, String documentName)
    {
        this(new EntityReference(documentName, EntityType.DOCUMENT, new EntityReference(spaceName, EntityType.SPACE)));
    }

    @Override
    public EntityReference getDocumentReference()
    {
        return this.reference;
    }

    /**
     * Set the fields of the class document passed as parameter. Can generate content for both XWiki Syntax 1.0 and
     * XWiki Syntax 2.0. If new documents are set to be created in XWiki Syntax 1.0 then generate XWiki 1.0 Syntax
     * otherwise generate XWiki Syntax 2.0.
     *
     * @param document the document
     * @param title the page title to set
     * @return true if the document has been modified, false otherwise
     */
    protected boolean setClassDocumentFields(XWikiDocument document, String title)
    {
        boolean needsUpdate = false;

        // Set the parent since it is different from the current document's space homepage.
        if (document.getParentReference() == null) {
            needsUpdate = true;
            document.setParentReference(new LocalDocumentReference(XWiki.SYSTEM_SPACE, "XWikiClasses"));
        }

        needsUpdate |= setDocumentFields(document, title);

        // Use ClassSheet to display the class document if no other sheet is explicitly specified.
        if (this.documentSheetBinder.getSheets(document).isEmpty()) {
            String wikiName = document.getDocumentReference().getWikiReference().getName();
            DocumentReference sheet = new DocumentReference(wikiName, XWiki.SYSTEM_SPACE, "ClassSheet");
            needsUpdate |= this.documentSheetBinder.bind(document, sheet);
        }

        return needsUpdate;
    }

    /**
     * Set the fields of the document passed as parameter. Can generate content for both XWiki Syntax 1.0 and XWiki
     * Syntax 2.0. If new documents are set to be created in XWiki Syntax 1.0 then generate XWiki 1.0 Syntax otherwise
     * generate XWiki Syntax 2.0.
     *
     * @param document the document
     * @param title the page title to set
     * @return true if the document has been modified, false otherwise
     */
    protected boolean setDocumentFields(XWikiDocument document, String title)
    {
        boolean needsUpdate = false;

        if (document.getCreatorReference() == null) {
            needsUpdate = true;
            document.setCreator(XWikiRightService.SUPERADMIN_USER);
        }
        if (document.getAuthorReference() == null) {
            needsUpdate = true;
            document.setAuthorReference(document.getCreatorReference());
        }

        if (document.getParentReference() == null) {
            needsUpdate = true;
            // Use the current document's space homepage.
            EntityReference spaceReference = getDocumentReference().extractReference(EntityType.SPACE);
            document.setParentReference(spaceReference);
        }

        if (StringUtils.isBlank(document.getTitle())) {
            needsUpdate = true;
            document.setTitle(title);
        }

        if (!document.isHidden()) {
            needsUpdate = true;
            document.setHidden(true);
        }

        return needsUpdate;
    }

{'fraction_non_alphanumeric': 0.07868979552282197, 'fraction_numerical': 0.0066866944471363505, 'mean_word_length': 3.014002333722287, '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': '6446369', 'n_tokens_mistral': 2740, 'n_tokens_neox': 2671, 'n_words': 909}