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.DocumentModel;
022import org.nuxeo.ecm.core.api.DocumentRef;
023
024/**
025 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
026 */
027@Operation(id = RestoreDocumentInput.ID, category = Constants.CAT_EXECUTION, label = "Restore Document Input", description = "Restore the document input from a context variable given its name. Return the document.")
028public class RestoreDocumentInput {
029
030    public static final String ID = "Context.RestoreDocumentInput";
031
032    @Context
033    protected OperationContext ctx;
034
035    @Param(name = "name")
036    protected String name;
037
038    @OperationMethod
039    public DocumentModel run() throws OperationException {
040        Object obj = ctx.get(name);
041        if (obj instanceof DocumentModel) {
042            return (DocumentModel) obj;
043        } else if (obj instanceof DocumentRef) {
044            return ctx.getCoreSession().getDocument((DocumentRef) obj);
045        }
046        throw new OperationException(
047                "Illegal state error for restore document operation. The context map doesn't contains a document variable with name "
048                        + name);
049    }
050
051}