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.nuxeo.common.utils.i18n.I18NUtils; 033import org.nuxeo.ecm.automation.AutomationService; 034import org.nuxeo.ecm.automation.OperationContext; 035import org.nuxeo.ecm.automation.OperationException; 036import org.nuxeo.ecm.automation.core.Constants; 037import org.nuxeo.ecm.automation.core.annotations.Context; 038import org.nuxeo.ecm.automation.core.annotations.Operation; 039import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 040import org.nuxeo.ecm.automation.core.annotations.Param; 041import org.nuxeo.ecm.core.api.Blob; 042import org.nuxeo.ecm.core.api.Blobs; 043import org.nuxeo.ecm.core.api.CoreSession; 044import org.nuxeo.ecm.core.api.DocumentModel; 045import org.nuxeo.ecm.core.api.NuxeoPrincipal; 046import org.nuxeo.ecm.platform.actions.Action; 047import org.nuxeo.ecm.platform.actions.ActionContext; 048import org.nuxeo.ecm.platform.actions.ELActionContext; 049import org.nuxeo.ecm.platform.actions.ejb.ActionManager; 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 public 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(); 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}