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