001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors: Antoine Taillefer
017 */
018
019package org.nuxeo.ecm.automation.jsf.operations;
020
021import java.util.List;
022
023import org.apache.commons.lang.StringUtils;
024import org.nuxeo.ecm.automation.OperationContext;
025import org.nuxeo.ecm.automation.OperationException;
026import org.nuxeo.ecm.automation.core.Constants;
027import org.nuxeo.ecm.automation.core.annotations.Context;
028import org.nuxeo.ecm.automation.core.annotations.Operation;
029import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
030import org.nuxeo.ecm.automation.core.annotations.Param;
031import org.nuxeo.ecm.automation.jsf.OperationHelper;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.DocumentModelList;
034import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
035import org.nuxeo.ecm.webapp.documentsLists.DocumentsListsManager;
036import org.nuxeo.ecm.webapp.documentsLists.DocumentsListsPersistenceManager;
037
038/**
039 * Gets selected documents from the selection list passed as a parameter.
040 *
041 * @author Antoine Taillefer (ataillefer@nuxeo.com)
042 * @since 5.7
043 */
044@Operation(id = GetDocumentsFromSelectionList.ID, category = Constants.CAT_FETCH, requires = Constants.SEAM_CONTEXT, label = "UI Selected documents from list", description = "Fetch the documents selected in the selection list passed as a parameter. If the list name is empty, the current folder selection list is used.")
045public class GetDocumentsFromSelectionList {
046
047    public static final String ID = "Seam.GetDocumentsFromSelectionList";
048
049    @Context
050    protected OperationContext ctx;
051
052    @Param(name = "listName", required = false)
053    protected String listName;
054
055    @OperationMethod
056    public DocumentModelList run() throws OperationException {
057
058        String workingListName = listName;
059        if (StringUtils.isEmpty(workingListName)) {
060            workingListName = DocumentsListsManager.CURRENT_DOCUMENT_SELECTION;
061        }
062
063        List<DocumentModel> res = null;
064        if (OperationHelper.isSeamContextAvailable()) {
065            res = OperationHelper.getDocumentListManager().getWorkingList(workingListName);
066        } else {
067            if (OperationHelper.getDocumentListManager().getWorkingListDescriptor(workingListName).getPersistent()) {
068                DocumentsListsPersistenceManager pm = new DocumentsListsPersistenceManager();
069                res = pm.loadPersistentDocumentsLists(ctx.getCoreSession(), ctx.getPrincipal().getName(),
070                        workingListName);
071            } else {
072                throw new OperationException(
073                        String.format(
074                                "Cannot get selected documents from selection list '%s' because the Seam context is not available and this list is not persisted.",
075                                workingListName));
076            }
077        }
078        return new DocumentModelListImpl(res);
079    }
080
081}