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