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;
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.automation.core.annotations.Param;
021import org.nuxeo.ecm.core.api.CoreSession;
022import org.nuxeo.ecm.core.api.DocumentModelList;
023import org.nuxeo.ecm.core.api.DocumentRef;
024import org.nuxeo.ecm.core.api.DocumentRefList;
025import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
026
027/**
028 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
029 */
030@Operation(id = RestoreDocumentsInput.ID, category = Constants.CAT_EXECUTION, label = "Restore Documents Input", description = "Restore the document list input from a context variable given its name. Return the document list.")
031public class RestoreDocumentsInput {
032
033    public static final String ID = "Context.RestoreDocumentsInput";
034
035    @Context
036    protected OperationContext ctx;
037
038    @Param(name = "name")
039    protected String name;
040
041    @OperationMethod
042    public DocumentModelList run() throws OperationException {
043        Object obj = ctx.get(name);
044        if (obj instanceof DocumentModelList) {
045            return (DocumentModelList) obj;
046        } else if (obj instanceof DocumentRefList) {
047            CoreSession session = ctx.getCoreSession();
048            DocumentRefList refs = (DocumentRefList) obj;
049            DocumentModelListImpl list = new DocumentModelListImpl((int) refs.totalSize());
050            for (DocumentRef ref : refs) {
051                list.add(session.getDocument(ref));
052            }
053            // FIXME: variable list is never used!
054        }
055        throw new OperationException(
056                "Illegal state error for restore documents operation. The context stack doesn't contains a documents variable with the name "
057                        + name);
058    }
059
060}