001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 */
018
019package org.nuxeo.ecm.automation.core.operations.services;
020
021import java.io.Serializable;
022import java.util.List;
023import java.util.Locale;
024import java.util.Map;
025
026import net.sf.json.JSONArray;
027import net.sf.json.JSONObject;
028
029import org.apache.commons.lang.LocaleUtils;
030import org.jboss.el.ExpressionFactoryImpl;
031import org.nuxeo.common.utils.i18n.I18NUtils;
032import org.nuxeo.ecm.automation.AutomationService;
033import org.nuxeo.ecm.automation.OperationContext;
034import org.nuxeo.ecm.automation.OperationException;
035import org.nuxeo.ecm.automation.core.Constants;
036import org.nuxeo.ecm.automation.core.annotations.Context;
037import org.nuxeo.ecm.automation.core.annotations.Operation;
038import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
039import org.nuxeo.ecm.automation.core.annotations.Param;
040import org.nuxeo.ecm.core.api.Blob;
041import org.nuxeo.ecm.core.api.Blobs;
042import org.nuxeo.ecm.core.api.CoreSession;
043import org.nuxeo.ecm.core.api.DocumentModel;
044import org.nuxeo.ecm.core.api.NuxeoPrincipal;
045import org.nuxeo.ecm.platform.actions.Action;
046import org.nuxeo.ecm.platform.actions.ActionContext;
047import org.nuxeo.ecm.platform.actions.ELActionContext;
048import org.nuxeo.ecm.platform.actions.ejb.ActionManager;
049import org.nuxeo.ecm.platform.el.ExpressionContext;
050
051/**
052 * Queries {@link ActionManager} for available actions in the given context
053 *
054 * @author Tiry (tdelprat@nuxeo.com)
055 */
056@Operation(id = GetActions.ID, category = Constants.CAT_SERVICES, label = "List available actions", description = "Retrieve list of available actions for a given category. Action context is built based on the Operation context (currentDocument will be fetched from Context if not provided as input). If this operation is executed in a chain that initialized the Seam context, it will be used for Action context", addToStudio = false)
057public class GetActions {
058
059    private static final String SEAM_ACTION_CONTEXT = "seamActionContext";
060
061    public static final String ID = "Actions.GET";
062
063    @Context
064    protected CoreSession session;
065
066    @Context
067    protected OperationContext ctx;
068
069    @Context
070    protected AutomationService automation;
071
072    @Context
073    protected ActionManager actionService;
074
075    @Param(name = "category", required = true)
076    protected String category;
077
078    @Param(name = "lang", required = false)
079    protected String lang;
080
081    protected DocumentModel getCurrentDocumentFromContext() throws OperationException {
082        String cdRef = (String) ctx.get("currentDocument");
083        return automation.getAdaptedValue(ctx, cdRef, DocumentModel.class);
084    }
085
086    protected ActionContext getActionContext(DocumentModel currentDocument) throws OperationException {
087        if (ctx.containsKey(SEAM_ACTION_CONTEXT)) {
088            // if Seam Context has been initialized, use it
089            return (ActionContext) ctx.get(SEAM_ACTION_CONTEXT);
090        }
091        ActionContext actionContext = new ELActionContext(new ExpressionContext(), new ExpressionFactoryImpl());
092        actionContext.setDocumentManager(session);
093        actionContext.setCurrentPrincipal((NuxeoPrincipal) session.getPrincipal());
094        if (currentDocument != null) {
095            actionContext.setCurrentDocument(currentDocument);
096        } else {
097            actionContext.setCurrentDocument(getCurrentDocumentFromContext());
098        }
099        actionContext.putAllLocalVariables(ctx);
100        return actionContext;
101    }
102
103    protected Locale getLocale() {
104        if (lang == null) {
105            lang = (String) ctx.get("lang");
106        }
107        if (lang == null) {
108            lang = "en";
109        }
110        return LocaleUtils.toLocale(lang);
111    }
112
113    protected String translate(String key) {
114        if (key == null) {
115            return "";
116        }
117        return I18NUtils.getMessageString("messages", key, new Object[0], getLocale());
118    }
119
120    @OperationMethod
121    public Blob run() throws OperationException {
122        return run(null);
123    }
124
125    @OperationMethod
126    public Blob run(DocumentModel currentDocument) throws OperationException {
127
128        ActionContext actionContext = getActionContext(currentDocument);
129        List<Action> actions = actionService.getActions(category, actionContext);
130
131        JSONArray rows = new JSONArray();
132        for (Action action : actions) {
133            JSONObject obj = new JSONObject();
134
135            obj.element("id", action.getId());
136            obj.element("link", action.getLink());
137            obj.element("icon", action.getIcon());
138
139            String label = translate(action.getLabel());
140            obj.element("label", label);
141            String help = translate(action.getHelp());
142            obj.element("help", help);
143
144            JSONObject properties = new JSONObject();
145            Map<String, Serializable> actionProperties = action.getProperties();
146            for (Map.Entry<String, Serializable> entry : actionProperties.entrySet()) {
147                properties.element(entry.getKey(), entry.getValue());
148            }
149            obj.element("properties", properties);
150            rows.add(obj);
151        }
152        return Blobs.createBlob(rows.toString(), "application/json");
153    }
154}