001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors: Antoine Taillefer
010 */
011
012package org.nuxeo.ecm.automation.jsf.operations;
013
014import java.util.List;
015
016import org.apache.commons.lang.StringUtils;
017import org.nuxeo.ecm.automation.OperationContext;
018import org.nuxeo.ecm.automation.OperationException;
019import org.nuxeo.ecm.automation.core.Constants;
020import org.nuxeo.ecm.automation.core.annotations.Context;
021import org.nuxeo.ecm.automation.core.annotations.Operation;
022import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
023import org.nuxeo.ecm.automation.core.annotations.Param;
024import org.nuxeo.ecm.automation.jsf.OperationHelper;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.DocumentModelList;
027import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
028import org.nuxeo.ecm.webapp.documentsLists.DocumentsListsManager;
029import org.nuxeo.ecm.webapp.documentsLists.DocumentsListsPersistenceManager;
030
031/**
032 * Gets selected documents from the selection list passed as a parameter.
033 *
034 * @author Antoine Taillefer (ataillefer@nuxeo.com)
035 * @since 5.7
036 */
037@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.")
038public class GetDocumentsFromSelectionList {
039
040    public static final String ID = "Seam.GetDocumentsFromSelectionList";
041
042    @Context
043    protected OperationContext ctx;
044
045    @Param(name = "listName", required = false)
046    protected String listName;
047
048    @OperationMethod
049    public DocumentModelList run() throws OperationException {
050
051        String workingListName = listName;
052        if (StringUtils.isEmpty(workingListName)) {
053            workingListName = DocumentsListsManager.CURRENT_DOCUMENT_SELECTION;
054        }
055
056        List<DocumentModel> res = null;
057        if (OperationHelper.isSeamContextAvailable()) {
058            res = OperationHelper.getDocumentListManager().getWorkingList(workingListName);
059        } else {
060            if (OperationHelper.getDocumentListManager().getWorkingListDescriptor(workingListName).getPersistent()) {
061                DocumentsListsPersistenceManager pm = new DocumentsListsPersistenceManager();
062                res = pm.loadPersistentDocumentsLists(ctx.getCoreSession(), ctx.getPrincipal().getName(),
063                        workingListName);
064            } else {
065                throw new OperationException(
066                        String.format(
067                                "Cannot get selected documents from selection list '%s' because the Seam context is not available and this list is not persisted.",
068                                workingListName));
069            }
070        }
071        return new DocumentModelListImpl(res);
072    }
073
074}