<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 org.xwiki.component.wiki.internal;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;

import org.slf4j.Logger;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.component.util.ReflectionUtils;
import org.xwiki.component.wiki.WikiComponent;
import org.xwiki.component.wiki.WikiComponentBuilder;
import org.xwiki.component.wiki.WikiComponentException;
import org.xwiki.component.wiki.internal.bridge.WikiComponentBridge;
import org.xwiki.model.reference.DocumentReference;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;

/**
 * Default implementation of a wiki component builder, that is using the legacy XWiki core module.
 * 
 * @version $Id$
 * @since 4.2M3
 */
@Component
@Singleton
public class DefaultWikiComponentBuilder implements WikiComponentBuilder, WikiComponentConstants
{
    /**
     * The logger to log.
     */
    @Inject
    private Logger logger;

    @Inject
    @Named("context")
    private ComponentManager contextComponentManager;

    /**
     * Used to access the current {@link XWikiContext}.
     */
    @Inject
    private Provider<XWikiContext> xcontextProvider;

    /**
     * Bridge to isolate the old model.
     */
    @Inject
    private WikiComponentBridge componentBridge;

    @Override
    public List<DocumentReference> getDocumentReferences()
    {
        List<DocumentReference> results = new ArrayList<>();
        // Note that the query is made to work with Oracle which treats empty strings as null.
        String query = ", BaseObject as obj, StringProperty as role where obj.className=?1 and obj.name=doc.fullName "
            + "and role.id.id=obj.id and role.id.name=?2 "
            + "and  (role.value <> '' or (role.value is not null and '' is null))";
        List<String> parameters = new ArrayList<>();
        parameters.add(COMPONENT_CLASS);
        parameters.add(COMPONENT_ROLE_TYPE_FIELD);

        try {
            XWikiContext xcontext = xcontextProvider.get();
            results.addAll(xcontext.getWiki().getStore().searchDocumentReferences(query, parameters, xcontext));
        } catch (XWikiException e) {
            this.logger.error("Failed to search for existing wiki components [{}]", e.getMessage());
        }

        return results;
    }

    @Override
    public List<WikiComponent> buildComponents(DocumentReference reference) throws WikiComponentException
    {
        List<WikiComponent> components = new ArrayList<WikiComponent>();

        if (!this.componentBridge.hasProgrammingRights(reference)) {
            throw new WikiComponentException("Registering wiki components requires programming rights");
        }

        DefaultWikiComponent rawComponent =
            new DefaultWikiComponent(reference, componentBridge.getAuthorReference(reference),
                componentBridge.getRoleType(reference), componentBridge.getRoleHint(reference),
                componentBridge.getScope(reference));
        rawComponent.setHandledMethods(componentBridge.getHandledMethods(reference));
        rawComponent.setImplementedInterfaces(componentBridge.getDeclaredInterfaces(reference));
        rawComponent.setDependencies(componentBridge.getDependencies(reference));
        rawComponent.setSyntax(componentBridge.getSyntax(reference));<commit_msg>[Misc] Replace error log by a warning one since it's not a critical error (we could also decide it's a critical one but in that case we should probably throw an exception)<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 org.xwiki.component.wiki.internal;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.component.util.ReflectionUtils;
import org.xwiki.component.wiki.WikiComponent;
import org.xwiki.component.wiki.WikiComponentBuilder;
import org.xwiki.component.wiki.WikiComponentException;
import org.xwiki.component.wiki.internal.bridge.WikiComponentBridge;
import org.xwiki.model.reference.DocumentReference;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;

/**
 * Default implementation of a wiki component builder, that is using the legacy XWiki core module.
 * 
 * @version $Id$
 * @since 4.2M3
 */
@Component
@Singleton
public class DefaultWikiComponentBuilder implements WikiComponentBuilder, WikiComponentConstants
{
    /**
     * The logger to log.
     */
    @Inject
    private Logger logger;

    @Inject
    @Named("context")
    private ComponentManager contextComponentManager;

    /**
     * Used to access the current {@link XWikiContext}.
     */
    @Inject
    private Provider<XWikiContext> xcontextProvider;

    /**
     * Bridge to isolate the old model.
     */
    @Inject
    private WikiComponentBridge componentBridge;

    @Override
    public List<DocumentReference> getDocumentReferences()
    {
        List<DocumentReference> results = new ArrayList<>();
        // Note that the query is made to work with Oracle which treats empty strings as null.
        String query = ", BaseObject as obj, StringProperty as role where obj.className=?1 and obj.name=doc.fullName "
            + "and role.id.id=obj.id and role.id.name=?2 "
            + "and  (role.value <> '' or (role.value is not null and '' is null))";
        List<String> parameters = new ArrayList<>();
        parameters.add(COMPONENT_CLASS);
        parameters.add(COMPONENT_ROLE_TYPE_FIELD);

        try {
            XWikiContext xcontext = xcontextProvider.get();
            results.addAll(xcontext.getWiki().getStore().searchDocumentReferences(query, parameters, xcontext));
        } catch (XWikiException e) {
            this.logger.warn("Failed to get document references for existing wiki components. Considering there's no "
                + "wiki component. Root cause: [{}]", ExceptionUtils.getRootCauseMessage(e));
        }

        return results;
    }

    @Override
    public List<WikiComponent> buildComponents(DocumentReference reference) throws WikiComponentException
    {
        List<WikiComponent> components = new ArrayList<WikiComponent>();

        if (!this.componentBridge.hasProgrammingRights(reference)) {
            throw new WikiComponentException("Registering wiki components requires programming rights");
        }

        DefaultWikiComponent rawComponent =
            new DefaultWikiComponent(reference, componentBridge.getAuthorReference(reference),
                componentBridge.getRoleType(reference), componentBridge.getRoleHint(reference),
                componentBridge.getScope(reference));
        rawComponent.setHandledMethods(componentBridge.getHandledMethods(reference));
        rawComponent.setImplementedInterfaces(componentBridge.getDeclaredInterfaces(reference));
        rawComponent.setDependencies(componentBridge.getDependencies(reference));
        rawComponent.setSyntax(componentBridge.getSyntax(reference));
{'fraction_non_alphanumeric': 0.08615484149234419, 'fraction_numerical': 0.004205305154194522, 'mean_word_length': 3.7515368852459017, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '1470984', 'n_tokens_mistral': 2454, 'n_tokens_neox': 2357, 'n_words': 737}