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:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.core.operations.stack;
013
014import org.nuxeo.ecm.automation.OperationContext;
015import org.nuxeo.ecm.automation.OperationException;
016import org.nuxeo.ecm.automation.core.Constants;
017import org.nuxeo.ecm.automation.core.annotations.Context;
018import org.nuxeo.ecm.automation.core.annotations.Operation;
019import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
020import org.nuxeo.ecm.core.api.CoreSession;
021import org.nuxeo.ecm.core.api.DocumentModelList;
022import org.nuxeo.ecm.core.api.DocumentRef;
023import org.nuxeo.ecm.core.api.DocumentRefList;
024import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
025
026/**
027 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
028 */
029@Operation(id = PullDocumentList.ID, category = Constants.CAT_EXECUTION_STACK, label = "Pull Document List", description = "Restore the first saved input document list in the context input stack", aliases = { "Document.PullList" })
030public class PullDocumentList {
031
032    public static final String ID = "Context.PullDocumentList";
033
034    @Context
035    protected OperationContext ctx;
036
037    @OperationMethod
038    public DocumentModelList run() throws OperationException {
039        Object obj = ctx.pull(Constants.O_DOCUMENTS);
040        if (obj instanceof DocumentModelList) {
041            return (DocumentModelList) obj;
042        } else if (obj instanceof DocumentRefList) {
043            CoreSession session = ctx.getCoreSession();
044            DocumentRefList refs = (DocumentRefList) obj;
045            DocumentModelListImpl list = new DocumentModelListImpl((int) refs.totalSize());
046            for (DocumentRef ref : refs) {
047                list.add(session.getDocument(ref));
048            }
049            // FIXME: variable list is never used!
050        }
051        throw new OperationException(
052                "Illegal state error for pull document operation. The context stack doesn't contains a document list on its bottom");
053    }
054
055}