<commit_before>/*
 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * 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.
 *
 * Contributors:
 *     <a href="mailto:<EMAIL>">Guillaume</a>
 */
package org.nuxeo.ecm.collections.jsf.actions;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.core.Events;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.international.Messages;
import org.jboss.seam.international.StatusMessage;
import org.nuxeo.ecm.collections.api.CollectionConstants;
import org.nuxeo.ecm.collections.api.CollectionManager;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.IdRef;
import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
import org.nuxeo.ecm.webapp.documentsLists.DocumentsListsManager;
import org.nuxeo.ecm.webapp.helpers.EventNames;
import org.nuxeo.runtime.api.Framework;

/**
 * @since 5.9.3
 */
@Name("collectionActions")
@Scope(ScopeType.PAGE)
@BypassInterceptors
public class CollectionActionsBean implements Serializable {

    public static final String COLLECTION_CURRENT_SELECTION = "COLLECTION_CURRENT_SELECTION";

    public static final String DOCUMENT_ADDED_TO_COLLECTION_EVENT = "documentAddedToCollection";

    public static final String DOCUMENT_REMOVED_FROM_COLLECTION_EVENT = "documentRemovedFromCollection";

    private static final Log log = LogFactory.getLog(CollectionActionsBean.class);

    private static final long serialVersionUID = 6091077088147407371L;

    private String newDescription;

    private String newTitle;

    private DocumentModel selectedCollection;

    private String selectedCollectionUid;

    public void addCurrentDocumentToSelectedCollection() throws ClientException {
        final NavigationContext navigationContext = (NavigationContext) Component.getInstance(
                "navigationContext", true);
        final DocumentModel currentDocument = navigationContext.getCurrentDocument();
        if (currentDocument != null) {
            final CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class);
            final CoreSession session = (CoreSession) Component.getInstance(
                    "documentManager", true);
            if (isCreateNewCollection()) {
                collectionManager.addToNewCollection(getNewTitle(),
                        getNewDescription(), currentDocument, session);
            } else {
                collectionManager.addToCollection(getSelectedCollection(),
                        currentDocument, session);
            }

            Events.instance().raiseEvent(EventNames.DOCUMENT_CHANGED);

            addFacesMessage(StatusMessage.Severity.INFO,
                    "collection.addedToCollection",
                    isCreateNewCollection() ? getNewTitle()
                            : getSelectedCollection().getTitle());
        }
    }

    protected static void addFacesMessage(StatusMessage.Severity severity,
            String message, String arguments) {
        final FacesMessages facesMessages = (FacesMessages) Component.getInstance(
                "facesMessages", true);
        facesMessages.add(severity, Messages.instance().get(message),
                Messages.instance().get(arguments));
    }

    public void addCurrentSelectionToSelectedCollection()
            throws ClientException {
        final DocumentsListsManager documentsListsManager = getDocumentsListsManager();
        addToSelectedCollection(documentsListsManager.getWorkingList());
    }

    public void addToSelectedCollection(
            final List<DocumentModel> documentListToBeAdded)
            throws ClientException {
        if (documentListToBeAdded != null && !documentListToBeAdded.isEmpty()) {
            final CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class);
            final CoreSession session = (CoreSession) Component.getInstance(
                    "documentManager", true);
            if (isCreateNewCollection()) {
                collectionManager.addToNewCollection(getNewTitle(),
                        getNewDescription(), documentListToBeAdded, session);
            } else {
                collectionManager.addToCollection(getSelectedCollection(),
                        documentListToBeAdded, session);
            }
        }
    }

    public boolean canAddToSelectedCollection() throws ClientException {
        final boolean result = canAddToCollection(getSelectedCollection())
                || isCreateNewCollection();
        return result;
    }

    public boolean canAddToCollection(DocumentModel collection)
            throws ClientException {
        final CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class);
        final CoreSession session = (CoreSession) Component.getInstance(
                "documentManager", true);
        final boolean result = collection != null
                && collectionManager.isCollection(collection)
                && collectionManager.canAddToCollection(collection, session);
        return result;
    }

    public void cancel() {
        selectedCollectionUid = null;
        newDescription = null;
        newTitle = null;
    }

    public boolean canCurrentDocumentBeCollected() {
        final NavigationContext navigationContext = (NavigationContext) Component.getInstance(
                "navigationContext", true);
        final DocumentModel currentDocument = navigationContext.getCurrentDocument();
        final CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class);
        return collectionManager.isCollectable(currentDocument);
    }

    public boolean canAllSelectedDocumentBeCollected() {
        final DocumentsListsManager documentsListsManager = getDocumentsListsManager();
        List<DocumentModel> documents = documentsListsManager.getWorkingList();
        if (documents == null || documents.isEmpty()) {
            return false;
        }
        final CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class);
        for (DocumentModel doc : documents) {
            if (!collectionManager.isCollectable(doc)) {
                return false;
            }
        }
        return true;
    }

    public boolean canRemoveFromCollection() throws ClientException {
        final DocumentsListsManager documentsListsManager = getDocumentsListsManager();
        final List<DocumentModel> doccumentListToBeRemoved = documentsListsManager.getWorkingList(COLLECTION_CURRENT_SELECTION);
        if (doccumentListToBeRemoved == null
                || doccumentListToBeRemoved.isEmpty()) {
            return false;
        }
        final NavigationContext navigationContext = (NavigationContext) Component.getInstance(
                "navigationContext", true);
        final DocumentModel currentDocument = navigationContext.getCurrentDocument();
        return canAddToCollection(currentDocument);
    }
<commit_msg>NXP-13793: use CURRENT_SELECTION for multiple add to collection<commit_after>/*
 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * 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.
 *
 * Contributors:
 *     <a href="mailto:<EMAIL>">Guillaume</a>
 */
package org.nuxeo.ecm.collections.jsf.actions;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.core.Events;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.international.Messages;
import org.jboss.seam.international.StatusMessage;
import org.nuxeo.ecm.collections.api.CollectionConstants;
import org.nuxeo.ecm.collections.api.CollectionManager;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.IdRef;
import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
import org.nuxeo.ecm.webapp.documentsLists.DocumentsListsManager;
import org.nuxeo.ecm.webapp.helpers.EventNames;
import org.nuxeo.runtime.api.Framework;

/**
 * @since 5.9.3
 */
@Name("collectionActions")
@Scope(ScopeType.PAGE)
@BypassInterceptors
public class CollectionActionsBean implements Serializable {

    public static final String COLLECTION_CURRENT_SELECTION = "COLLECTION_CURRENT_SELECTION";

    public static final String DOCUMENT_ADDED_TO_COLLECTION_EVENT = "documentAddedToCollection";

    public static final String DOCUMENT_REMOVED_FROM_COLLECTION_EVENT = "documentRemovedFromCollection";

    private static final Log log = LogFactory.getLog(CollectionActionsBean.class);

    private static final long serialVersionUID = 6091077088147407371L;

    private String newDescription;

    private String newTitle;

    private DocumentModel selectedCollection;

    private String selectedCollectionUid;

    public void addCurrentDocumentToSelectedCollection() throws ClientException {
        final NavigationContext navigationContext = (NavigationContext) Component.getInstance(
                "navigationContext", true);
        final DocumentModel currentDocument = navigationContext.getCurrentDocument();
        if (currentDocument != null) {
            final CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class);
            final CoreSession session = (CoreSession) Component.getInstance(
                    "documentManager", true);
            if (isCreateNewCollection()) {
                collectionManager.addToNewCollection(getNewTitle(),
                        getNewDescription(), currentDocument, session);
            } else {
                collectionManager.addToCollection(getSelectedCollection(),
                        currentDocument, session);
            }

            Events.instance().raiseEvent(EventNames.DOCUMENT_CHANGED);

            addFacesMessage(StatusMessage.Severity.INFO,
                    "collection.addedToCollection",
                    isCreateNewCollection() ? getNewTitle()
                            : getSelectedCollection().getTitle());
        }
    }

    protected static void addFacesMessage(StatusMessage.Severity severity,
            String message, String arguments) {
        final FacesMessages facesMessages = (FacesMessages) Component.getInstance(
                "facesMessages", true);
        facesMessages.add(severity, Messages.instance().get(message),
                Messages.instance().get(arguments));
    }

    public void addCurrentSelectionToSelectedCollection()
            throws ClientException {
        final DocumentsListsManager documentsListsManager = getDocumentsListsManager();
        addToSelectedCollection(documentsListsManager.getWorkingList());
    }

    public void addToSelectedCollection(
            final List<DocumentModel> documentListToBeAdded)
            throws ClientException {
        if (documentListToBeAdded != null && !documentListToBeAdded.isEmpty()) {
            final CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class);
            final CoreSession session = (CoreSession) Component.getInstance(
                    "documentManager", true);
            if (isCreateNewCollection()) {
                collectionManager.addToNewCollection(getNewTitle(),
                        getNewDescription(), documentListToBeAdded, session);
            } else {
                collectionManager.addToCollection(getSelectedCollection(),
                        documentListToBeAdded, session);
            }
        }
    }

    public boolean canAddToSelectedCollection() throws ClientException {
        final boolean result = canAddToCollection(getSelectedCollection())
                || isCreateNewCollection();
        return result;
    }

    public boolean canAddToCollection(DocumentModel collection)
            throws ClientException {
        final CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class);
        final CoreSession session = (CoreSession) Component.getInstance(
                "documentManager", true);
        final boolean result = collection != null
                && collectionManager.isCollection(collection)
                && collectionManager.canAddToCollection(collection, session);
        return result;
    }

    public void cancel() {
        selectedCollectionUid = null;
        newDescription = null;
        newTitle = null;
    }

    public boolean canCurrentDocumentBeCollected() {
        final NavigationContext navigationContext = (NavigationContext) Component.getInstance(
                "navigationContext", true);
        final DocumentModel currentDocument = navigationContext.getCurrentDocument();
        final CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class);
        return collectionManager.isCollectable(currentDocument);
    }

    public boolean canAllSelectedDocumentBeCollected() {
        final DocumentsListsManager documentsListsManager = getDocumentsListsManager();
        List<DocumentModel> documents = documentsListsManager.getWorkingList("CURRENT_SELECTION");
        if (documents == null || documents.isEmpty()) {
            return false;
        }
        final CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class);
        for (DocumentModel doc : documents) {
            if (!collectionManager.isCollectable(doc)) {
                return false;
            }
        }
        return true;
    }

    public boolean canRemoveFromCollection() throws ClientException {
        final DocumentsListsManager documentsListsManager = getDocumentsListsManager();
        final List<DocumentModel> doccumentListToBeRemoved = documentsListsManager.getWorkingList(COLLECTION_CURRENT_SELECTION);
        if (doccumentListToBeRemoved == null
                || doccumentListToBeRemoved.isEmpty()) {
            return false;
        }
        final NavigationContext navigationContext = (NavigationContext) Component.getInstance(
                "navigationContext", true);
        final DocumentModel currentDocument = navigationContext.getCurrentDocument();
        return canAddToCollection(currentDocument);
    }

{'fraction_non_alphanumeric': 0.07890287431899305, 'fraction_numerical': 0.004070386373598848, 'mean_word_length': 3.2575313249800053, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '5049978', 'n_tokens_mistral': 4029, 'n_tokens_neox': 3769, 'n_words': 857}