<commit_before>/*
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
 *
 * This library 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 library 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.
 *
 * For further information about Alkacon Software, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.ade.publish;

import org.opencms.ade.containerpage.CmsDetailOnlyContainerUtil;
import org.opencms.ade.publish.shared.CmsProjectBean;
import org.opencms.ade.publish.shared.CmsPublishOptions;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.collectors.I_CmsCollectorPublishListProvider;
import org.opencms.gwt.shared.I_CmsCollectorInfoFactory;
import org.opencms.gwt.shared.I_CmsContentLoadCollectorInfo;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.util.CmsUUID;

import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanCodex;
import com.google.web.bindery.autobean.vm.AutoBeanFactorySource;

/**
 * Virtual project which includes the currently edited resource and all its related resources.
 */
public class CmsCurrentPageProject implements I_CmsVirtualProject {

    /** The uuid of this virtual project. */
    public static final CmsUUID ID = CmsUUID.getConstantUUID("currentpage");

    /** The logger for this class. */
    static final Log LOG = CmsLog.getLog(CmsCurrentPageProject.class);

    /**
     * @see org.opencms.ade.publish.I_CmsVirtualProject#getProjectBean(org.opencms.file.CmsObject, java.util.Map)
     */
    public CmsProjectBean getProjectBean(CmsObject cms, Map<String, String> params) {

        String pageId = params.get(CmsPublishOptions.PARAM_CONTAINERPAGE);
        String elementId = params.get(CmsPublishOptions.PARAM_CONTENT);
        Locale locale = OpenCms.getWorkplaceManager().getWorkplaceLocale(cms);
        String title = Messages.get().getBundle(locale).key(Messages.GUI_CURRENTPAGE_PROJECT_0);
        CmsUUID structureIdForTitle;
        if ((pageId == null) && (elementId == null)) {
            return null;
        } else {
            structureIdForTitle = pageId != null ? new CmsUUID(pageId) : new CmsUUID(elementId);
        }

        CmsProjectBean bean = new CmsProjectBean(ID, 0, title, title);
        bean.setRank(100);
        bean.setDefaultGroupName("");
        try {
            CmsResource titleResource = cms.readResource(structureIdForTitle, CmsResourceFilter.IGNORE_EXPIRATION);
            CmsProperty titleProp = cms.readPropertyObject(titleResource, CmsPropertyDefinition.PROPERTY_TITLE, true);
            String rawName;
            if (titleProp.isNullProperty()) {
                rawName = cms.getSitePath(titleResource);
            } else {
                rawName = titleProp.getValue();
            }
            bean.setDefaultGroupName(Messages.get().getBundle(locale).key(Messages.GUI_PAGE_1, rawName));
        } catch (Exception e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
        return bean;
    }

    /**
     * @see org.opencms.ade.publish.I_CmsVirtualProject#getProjectId()
     */
    public CmsUUID getProjectId() {

        return ID;
    }

    /**
     * @see org.opencms.ade.publish.I_CmsVirtualProject#getRelatedResourceProvider(org.opencms.file.CmsObject, org.opencms.ade.publish.shared.CmsPublishOptions)
     */
    public I_CmsPublishRelatedResourceProvider getRelatedResourceProvider(
        final CmsObject cmsObject,
        final CmsPublishOptions options) {

        return new I_CmsPublishRelatedResourceProvider() {

            public Set<CmsResource> getAdditionalRelatedResources(CmsObject cms, CmsResource res) {

                Map<String, String> params = options.getParameters();

                String pageId = options.getParameters().get(CmsPublishOptions.PARAM_CONTAINERPAGE);
                String detailId = options.getParameters().get(CmsPublishOptions.PARAM_DETAIL);

                Set<CmsResource> result = Sets.newHashSet();

                if (res.getStructureId().toString().equals(detailId)) {
                    result.addAll(CmsDetailOnlyContainerUtil.getDetailOnlyResources(cms, res));
                }
                if (res.getStructureId().toString().equals(pageId)) {

                    I_CmsCollectorInfoFactory collectorInfoFactory = AutoBeanFactorySource.create(
                        I_CmsCollectorInfoFactory.class);
                    for (Map.Entry<String, String> entry : params.entrySet()) {
                        if (entry.getKey().startsWith(CmsPublishOptions.PARAM_COLLECTOR_INFO)) {
                            try {
                                AutoBean<I_CmsContentLoadCollectorInfo> autoBean = AutoBeanCodex.decode(
                                    collectorInfoFactory,
                                    I_CmsContentLoadCollectorInfo.class,
                                    entry.getValue());
                                String collectorName = autoBean.as().getCollectorName();
                                I_CmsCollectorPublishListProvider publishListProvider = null;
                                if (null != collectorName) { // registered collector
                                    publishListProvider = OpenCms.getResourceManager().getContentCollector(
                                        collectorName);
                                }
                                if (null == publishListProvider) { // unregistered collector
                                    String collectorClassName = autoBean.as().getCollectorClass();
                                    Class<?> collectorClass = Class.forName(collectorClassName);
                                    publishListProvider = (I_CmsCollectorPublishListProvider)collectorClass.newInstance();
                                }
                                if (publishListProvider == null) {
                                    continue;
                                }
                                result.addAll(publishListProvider.getPublishResources(cmsObject, autoBean.as()));
                            } catch (Exception e) {
                                LOG.error(e.getLocalizedMessage(), e);<commit_msg>Improved handling of deleted container element in publish dialog.<commit_after>/*
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
 *
 * This library 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 library 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.
 *
 * For further information about Alkacon Software, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.ade.publish;

import org.opencms.ade.containerpage.CmsDetailOnlyContainerUtil;
import org.opencms.ade.publish.shared.CmsProjectBean;
import org.opencms.ade.publish.shared.CmsPublishOptions;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.collectors.I_CmsCollectorPublishListProvider;
import org.opencms.gwt.shared.I_CmsCollectorInfoFactory;
import org.opencms.gwt.shared.I_CmsContentLoadCollectorInfo;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.relations.CmsRelation;
import org.opencms.relations.CmsRelationFilter;
import org.opencms.util.CmsUUID;

import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanCodex;
import com.google.web.bindery.autobean.vm.AutoBeanFactorySource;

/**
 * Virtual project which includes the currently edited resource and all its related resources.
 */
public class CmsCurrentPageProject implements I_CmsVirtualProject {

    /** The uuid of this virtual project. */
    public static final CmsUUID ID = CmsUUID.getConstantUUID("currentpage");

    /** The logger for this class. */
    static final Log LOG = CmsLog.getLog(CmsCurrentPageProject.class);

    /**
     * @see org.opencms.ade.publish.I_CmsVirtualProject#getProjectBean(org.opencms.file.CmsObject, java.util.Map)
     */
    public CmsProjectBean getProjectBean(CmsObject cms, Map<String, String> params) {

        String pageId = params.get(CmsPublishOptions.PARAM_CONTAINERPAGE);
        String elementId = params.get(CmsPublishOptions.PARAM_CONTENT);
        Locale locale = OpenCms.getWorkplaceManager().getWorkplaceLocale(cms);
        String title = Messages.get().getBundle(locale).key(Messages.GUI_CURRENTPAGE_PROJECT_0);
        CmsUUID structureIdForTitle;
        if ((pageId == null) && (elementId == null)) {
            return null;
        } else {
            structureIdForTitle = pageId != null ? new CmsUUID(pageId) : new CmsUUID(elementId);
        }

        CmsProjectBean bean = new CmsProjectBean(ID, 0, title, title);
        bean.setRank(100);
        bean.setDefaultGroupName("");
        try {
            CmsResource titleResource = cms.readResource(structureIdForTitle, CmsResourceFilter.IGNORE_EXPIRATION);
            CmsProperty titleProp = cms.readPropertyObject(titleResource, CmsPropertyDefinition.PROPERTY_TITLE, true);
            String rawName;
            if (titleProp.isNullProperty()) {
                rawName = cms.getSitePath(titleResource);
            } else {
                rawName = titleProp.getValue();
            }
            bean.setDefaultGroupName(Messages.get().getBundle(locale).key(Messages.GUI_PAGE_1, rawName));
        } catch (Exception e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
        return bean;
    }

    /**
     * @see org.opencms.ade.publish.I_CmsVirtualProject#getProjectId()
     */
    public CmsUUID getProjectId() {

        return ID;
    }

    /**
     * @see org.opencms.ade.publish.I_CmsVirtualProject#getRelatedResourceProvider(org.opencms.file.CmsObject, org.opencms.ade.publish.shared.CmsPublishOptions)
     */
    public I_CmsPublishRelatedResourceProvider getRelatedResourceProvider(
        final CmsObject cmsObject,
        final CmsPublishOptions options) {

        return new I_CmsPublishRelatedResourceProvider() {

            public Set<CmsResource> getAdditionalRelatedResources(CmsObject cms, CmsResource res) {

                Map<String, String> params = options.getParameters();

                String pageId = options.getParameters().get(CmsPublishOptions.PARAM_CONTAINERPAGE);
                String detailId = options.getParameters().get(CmsPublishOptions.PARAM_DETAIL);

                Set<CmsResource> result = Sets.newHashSet();

                if (res.getStructureId().toString().equals(detailId)) {
                    result.addAll(CmsDetailOnlyContainerUtil.getDetailOnlyResources(cms, res));
                }
                if (res.getStructureId().toString().equals(pageId)) {

                    try {
                        CmsProject online = cmsObject.readProject(CmsProject.ONLINE_PROJECT_ID);

                        CmsObject onlineCms = OpenCms.initCmsObject(cmsObject);
                        onlineCms.getRequestContext().setCurrentProject(online);
                        List<CmsRelation> relations = onlineCms.readRelations(
                            CmsRelationFilter.relationsFromStructureId(new CmsUUID(pageId)));
                        for (CmsRelation relation : relations) {
                            CmsResource offlineTarget = null;
                            CmsResource onlineTarget = null;
                            if (relation.getTargetId().isNullUUID()) {
                                continue;
                            }
                            try {
                                onlineTarget = onlineCms.readResource(
                                    relation.getTargetId(),
                                    CmsResourceFilter.IGNORE_EXPIRATION);

                                offlineTarget = cmsObject.readResource(relation.getTargetId(), CmsResourceFilter.ALL);
                                if (offlineTarget.getState().isDeleted()) {
                                    result.add(offlineTarget);
                                }
                            } catch (Exception e) {
                                LOG.error(e.getLocalizedMessage(), e);
                            }

                        }
                    } catch (CmsException e) {
                        LOG.error(e.getLocalizedMessage(), e);
                    }

                    I_CmsCollectorInfoFactory collectorInfoFactory = AutoBeanFactorySource.create(
                        I_CmsCollectorInfoFactory.class);
                    for (Map.Entry<String, String> entry : params.entrySet()) {
                        if (entry.getKey().startsWith(CmsPublishOptions.PARAM_COLLECTOR_INFO)) {
                            try {
                                AutoBean<I_CmsContentLoadCollectorInfo> autoBean = AutoBeanCodex.decode(
                                    collectorInfoFactory,
                                    I_CmsContentLoadCollectorInfo.class,
                                    entry.getValue());
                                String collectorName = autoBean.as().getCollectorName();
                                I_CmsCollectorPublishListProvider publishListProvider = null;
                                if (null != collectorName) { // registered collector
                                    publishListProvider = OpenCms.getResourceManager().getContentCollector(
                                        collectorName);
                                }
                                if (null == publishListProvider) { // unregistered collector
                                    String collectorClassName = autoBean.as().getCollectorClass();
                                    Class<?> collectorClass = Class.forName(collectorClassName);
                                    publishListProvider = (I_CmsCollectorPublishListProvider)collectorClass.newInstance();
                                }
                                if (publishListProvider == null) {
                                    continue;
                                }
                                result.addAll(publishListProvider.getPublishResources(cmsObject, autoBean.as()));
                            } catch (Exception e) {
                                LOG.error(e.getLocalizedMessage(), e);
{'fraction_non_alphanumeric': 0.08514816137093895, 'fraction_numerical': 0.0026181125788408903, 'mean_word_length': 2.2172664624808576, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '976129', 'n_tokens_mistral': 4524, 'n_tokens_neox': 4356, 'n_words': 926}