001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 */
018
019package org.nuxeo.ecm.automation.jsf;
020
021import java.io.Serializable;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.jboss.seam.ScopeType;
026import org.jboss.seam.annotations.In;
027import org.jboss.seam.annotations.Name;
028import org.jboss.seam.annotations.Scope;
029import org.jboss.seam.faces.FacesMessages;
030import org.jboss.seam.international.StatusMessage;
031import org.nuxeo.ecm.automation.AutomationService;
032import org.nuxeo.ecm.automation.InvalidChainException;
033import org.nuxeo.ecm.automation.OperationChain;
034import org.nuxeo.ecm.automation.OperationContext;
035import org.nuxeo.ecm.automation.OperationException;
036import org.nuxeo.ecm.automation.jsf.operations.AddErrorMessage;
037import org.nuxeo.ecm.automation.jsf.operations.AddInfoMessage;
038import org.nuxeo.ecm.automation.jsf.operations.SeamOperation;
039import org.nuxeo.ecm.core.api.CoreSession;
040import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
041import org.nuxeo.ecm.platform.web.common.exceptionhandling.ExceptionHelper;
042import org.nuxeo.ecm.webapp.helpers.ResourcesAccessor;
043import org.nuxeo.runtime.api.Framework;
044
045/**
046 * @author <a href="mailto:td@nuxeo.com">Thierry Delprat</a>
047 */
048@Name("operationActionBean")
049@Scope(ScopeType.EVENT)
050public class OperationActionBean implements Serializable {
051
052    private static final Log log = LogFactory.getLog(OperationActionBean.class);
053
054    private static final long serialVersionUID = 1L;
055
056    @In(create = true, required = false)
057    protected transient NavigationContext navigationContext;
058
059    @In(create = true, required = false)
060    protected CoreSession documentManager;
061
062    @In(create = true, required = false)
063    protected FacesMessages facesMessages;
064
065    @In(create = true)
066    protected ResourcesAccessor resourcesAccessor;
067
068    public String doOperation(String chainId) throws OperationException {
069        return runOperation(chainId);
070    }
071
072    protected void showError(OperationContext ctx, String chain, Throwable cause) {
073        String msg = (String) ctx.get(AddErrorMessage.ID);
074        if (msg == null) {
075            msg = "An error occured while executing the chain '" + chain + "': " + cause.getMessage();
076        }
077        facesMessages.add(StatusMessage.Severity.ERROR, msg);
078    }
079
080    protected void showSuccess(OperationContext ctx, String chain) {
081        String msg = (String) ctx.get(AddInfoMessage.ID);
082        if (msg != null) {
083            facesMessages.add(StatusMessage.Severity.INFO, msg);
084        }
085    }
086
087    protected String runOperation(Object chain) throws OperationException {
088        AutomationService os = Framework.getService(AutomationService.class);
089        try (OperationContext ctx = new OperationContext(documentManager)) {
090            ctx.setInput(navigationContext.getCurrentDocument());
091
092            if (chain instanceof String) {
093                try {
094                    os.run(ctx, (String) chain);
095                    showSuccess(ctx, (String) chain);
096                } catch (InvalidChainException e) {
097                    facesMessages.add(StatusMessage.Severity.ERROR, "Unknown chain: " + chain);
098                    return null;
099                } catch (OperationException e) {
100                    log.error("Failed to execute action: ", e);
101                    Throwable cause = ExceptionHelper.unwrapException(e);
102                    showError(ctx, (String) chain, cause);
103                    return null;
104                }
105            } else {
106                os.run(ctx, (OperationChain) chain);
107                showSuccess(ctx, ((OperationChain) chain).getId());
108            }
109
110            return (String) ctx.get(SeamOperation.OUTCOME);
111        }
112    }
113}