001/*
002 * (C) Copyright 2006-2018 Nuxeo (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 *     Nuxeo - initial API and implementation
018 *
019 */
020
021package org.nuxeo.ecm.automation.core.operations.services;
022
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.LinkedHashMap;
026import java.util.List;
027import java.util.Locale;
028import java.util.Map;
029
030import org.apache.commons.lang3.LocaleUtils;
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.platform.actions.Action;
045import org.nuxeo.ecm.platform.actions.ActionContext;
046import org.nuxeo.ecm.platform.actions.ELActionContext;
047import org.nuxeo.ecm.platform.actions.ejb.ActionManager;
048
049/**
050 * Queries {@link ActionManager} for available actions in the given context
051 *
052 * @author Tiry (tdelprat@nuxeo.com)
053 */
054@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)
055public class GetActions {
056
057    public static final String SEAM_ACTION_CONTEXT = "seamActionContext";
058
059    public static final String ID = "Actions.GET";
060
061    @Context
062    protected CoreSession session;
063
064    @Context
065    protected OperationContext ctx;
066
067    @Context
068    protected AutomationService automation;
069
070    @Context
071    protected ActionManager actionService;
072
073    @Param(name = "category", required = true)
074    protected String category;
075
076    @Param(name = "lang", required = false)
077    protected String lang;
078
079    protected DocumentModel getCurrentDocumentFromContext() throws OperationException {
080        String cdRef = (String) ctx.get("currentDocument");
081        return automation.getAdaptedValue(ctx, cdRef, DocumentModel.class);
082    }
083
084    protected ActionContext getActionContext(DocumentModel currentDocument) throws OperationException {
085        if (ctx.containsKey(SEAM_ACTION_CONTEXT)) {
086            // if Seam Context has been initialized, use it
087            return (ActionContext) ctx.get(SEAM_ACTION_CONTEXT);
088        }
089        ActionContext actionContext = new ELActionContext();
090        actionContext.setDocumentManager(session);
091        actionContext.setCurrentPrincipal(session.getPrincipal());
092        if (currentDocument != null) {
093            actionContext.setCurrentDocument(currentDocument);
094        } else {
095            actionContext.setCurrentDocument(getCurrentDocumentFromContext());
096        }
097        actionContext.putAllLocalVariables(ctx);
098        return actionContext;
099    }
100
101    protected Locale getLocale() {
102        if (lang == null) {
103            lang = (String) ctx.get("lang");
104        }
105        if (lang == null) {
106            lang = "en";
107        }
108        return LocaleUtils.toLocale(lang);
109    }
110
111    protected String translate(String key) {
112        if (key == null) {
113            return "";
114        }
115        return I18NUtils.getMessageString("messages", key, new Object[0], getLocale());
116    }
117
118    @OperationMethod
119    public Blob run() throws IOException, OperationException {
120        return run(null);
121    }
122
123    @OperationMethod
124    public Blob run(DocumentModel currentDocument) throws IOException, OperationException {
125
126        ActionContext actionContext = getActionContext(currentDocument);
127        List<Action> actions = actionService.getActions(category, actionContext);
128
129        List<Map<String, Object>> rows = new ArrayList<>();
130        for (Action action : actions) {
131            Map<String, Object> obj = new LinkedHashMap<>();
132
133            obj.put("id", action.getId());
134            obj.put("link", action.getLink());
135            obj.put("icon", action.getIcon());
136
137            String label = translate(action.getLabel());
138            obj.put("label", label);
139            String help = translate(action.getHelp());
140            obj.put("help", help);
141
142            Map<String, Object> properties = new LinkedHashMap<>(action.getProperties());
143            obj.put("properties", properties);
144            rows.add(obj);
145        }
146        return Blobs.createJSONBlobFromValue(rows);
147    }
148}