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 */
011
012package org.nuxeo.ecm.automation.jsf;
013
014import java.io.Serializable;
015
016import org.apache.commons.logging.Log;
017import org.apache.commons.logging.LogFactory;
018import org.jboss.seam.ScopeType;
019import org.jboss.seam.annotations.In;
020import org.jboss.seam.annotations.Name;
021import org.jboss.seam.annotations.Scope;
022import org.jboss.seam.faces.FacesMessages;
023import org.jboss.seam.international.StatusMessage;
024import org.nuxeo.ecm.automation.AutomationService;
025import org.nuxeo.ecm.automation.InvalidChainException;
026import org.nuxeo.ecm.automation.OperationChain;
027import org.nuxeo.ecm.automation.OperationContext;
028import org.nuxeo.ecm.automation.OperationException;
029import org.nuxeo.ecm.automation.jsf.operations.AddErrorMessage;
030import org.nuxeo.ecm.automation.jsf.operations.AddInfoMessage;
031import org.nuxeo.ecm.automation.jsf.operations.SeamOperation;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
034import org.nuxeo.ecm.platform.web.common.exceptionhandling.ExceptionHelper;
035import org.nuxeo.ecm.webapp.helpers.ResourcesAccessor;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * @author <a href="mailto:td@nuxeo.com">Thierry Delprat</a>
040 */
041@Name("operationActionBean")
042@Scope(ScopeType.EVENT)
043public class OperationActionBean implements Serializable {
044
045    private static final Log log = LogFactory.getLog(OperationActionBean.class);
046
047    private static final long serialVersionUID = 1L;
048
049    @In(create = true, required = false)
050    protected transient NavigationContext navigationContext;
051
052    @In(create = true, required = false)
053    protected CoreSession documentManager;
054
055    @In(create = true, required = false)
056    protected FacesMessages facesMessages;
057
058    @In(create = true)
059    protected ResourcesAccessor resourcesAccessor;
060
061    public String doOperation(String chainId) throws OperationException {
062        return runOperation(chainId);
063    }
064
065    protected void showError(OperationContext ctx, String chain, Throwable cause) {
066        String msg = (String) ctx.get(AddErrorMessage.ID);
067        if (msg == null) {
068            msg = "An error occured while executing the chain '" + chain + "': " + cause.getMessage();
069        }
070        facesMessages.add(StatusMessage.Severity.ERROR, msg);
071    }
072
073    protected void showSuccess(OperationContext ctx, String chain) {
074        String msg = (String) ctx.get(AddInfoMessage.ID);
075        if (msg != null) {
076            facesMessages.add(StatusMessage.Severity.INFO, msg);
077        }
078    }
079
080    protected String runOperation(Object chain) throws OperationException {
081        AutomationService os = Framework.getService(AutomationService.class);
082        OperationContext ctx = new OperationContext(documentManager);
083        ctx.setInput(navigationContext.getCurrentDocument());
084
085        if (chain instanceof String) {
086            try {
087                os.run(ctx, (String) chain);
088                showSuccess(ctx, (String) chain);
089            } catch (InvalidChainException e) {
090                facesMessages.add(StatusMessage.Severity.ERROR, "Unknown chain: " + chain);
091                return null;
092            } catch (OperationException e) {
093                log.error("Failed to execute action: ", e);
094                Throwable cause = ExceptionHelper.unwrapException(e);
095                showError(ctx, (String) chain, cause);
096                return null;
097            }
098        } else {
099            os.run(ctx, (OperationChain) chain);
100            showSuccess(ctx, ((OperationChain) chain).getId());
101        }
102
103        return (String) ctx.get(SeamOperation.OUTCOME);
104    }
105}