1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.services.cms.actions.impl;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
22 import java.util.Map;
23
24 import javax.jcr.Node;
25 import javax.jcr.PropertyType;
26 import javax.jcr.Value;
27 import javax.jcr.nodetype.NodeType;
28 import javax.jcr.nodetype.PropertyDefinition;
29
30 import org.apache.commons.lang.StringUtils;
31 import org.exoplatform.container.component.ComponentPlugin;
32 import org.exoplatform.container.xml.InitParams;
33 import org.exoplatform.services.cms.actions.activation.ScriptActionActivationJob;
34 import org.exoplatform.services.cms.scripts.CmsScript;
35 import org.exoplatform.services.cms.scripts.ScriptService;
36 import org.exoplatform.services.jcr.RepositoryService;
37 import org.exoplatform.services.jcr.core.ManageableRepository;
38 import org.exoplatform.services.jcr.ext.common.SessionProvider;
39 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
40
41 public class ScriptActionPlugin extends BaseActionPlugin implements ComponentPlugin {
42
43 public static final String ACTION_TYPE = "exo:scriptAction";
44
45 private ScriptService scriptService_;
46 private RepositoryService repositoryService_;
47 private ActionConfig config_;
48 private String desc_ = "";
49
50 public ScriptActionPlugin(ScriptService scriptService, InitParams params,
51 RepositoryService repositoryService) throws Exception {
52 scriptService_ = scriptService;
53 repositoryService_ = repositoryService;
54 config_ = params.getObjectParamValues(ActionConfig.class).get(0);
55 }
56
57 public Collection<String> getActionExecutables() throws Exception {
58 Collection<String> actionScriptNames = new ArrayList<String>();
59 SessionProvider provider = SessionProvider.createSystemProvider();
60 List<Node> actionScriptList = scriptService_.getECMActionScripts(provider) ;
61 String baseScriptPath = scriptService_.getBaseScriptPath() ;
62 for(Node script:actionScriptList) {
63 String actionScriptName = StringUtils.substringAfter(script.getPath(),baseScriptPath + "/") ;
64 actionScriptNames.add(actionScriptName) ;
65 }
66 provider.close();
67 return actionScriptNames;
68 }
69
70 public String getActionExecutableLabel() { return "exo:scriptLabel"; }
71
72 public String getExecutableDefinitionName() { return "exo:script"; }
73 protected String getWorkspaceName() { return config_.getWorkspace() ; }
74
75 protected ManageableRepository getRepository() throws Exception {
76 return repositoryService_.getCurrentRepository();
77 }
78 protected String getActionType() { return ACTION_TYPE; }
79 protected List getActions() { return config_.getActions(); }
80
81 protected ECMEventListener createEventListener(String actionName, String actionExecutable,
82 String repository, String srcWorkspace, String srcPath, Map variables, String actiontype) throws Exception {
83 return new ScriptActionLauncherListener(actionName, actionExecutable, repository, srcWorkspace,
84 srcPath, variables);
85 }
86
87 public String getName() { return ACTION_TYPE; }
88 public void setName(String s) { }
89
90 public String getDescription() { return desc_; }
91 public void setDescription(String desc) {
92 desc_ = desc;
93 }
94
95 @SuppressWarnings("unchecked")
96 public void executeAction(String userId, Node actionNode, Map variables) throws Exception {
97 String script = null;
98 if(actionNode.hasProperty("exo:script")) {
99 script = actionNode.getProperty("exo:script").getString();
100 } else {
101 NodeType nodeType = actionNode.getPrimaryNodeType();
102 for(PropertyDefinition propertyDefinition : nodeType.getPropertyDefinitions()) {
103 if(propertyDefinition.getName().equals("exo:script")) {
104 script = getDefaultValue(propertyDefinition);
105 }
106 }
107 }
108 variables.put("actionNode", actionNode);
109 variables.put("repository",WCMCoreUtils.getRepository().getConfiguration().getName()) ;
110 executeAction(userId, script, variables);
111 }
112
113 private String getDefaultValue(PropertyDefinition proDef) throws Exception {
114 StringBuilder defaultValue = new StringBuilder() ;
115 Value[] values = proDef.getDefaultValues() ;
116 if(values == null || values.length < 0) return "" ;
117 for(Value value : values) {
118 if(value == null) continue ;
119 if(defaultValue.length() > 0) defaultValue.append(",") ;
120 defaultValue.append(getPropertyValue(value)) ;
121 }
122 return defaultValue.toString() ;
123 }
124
125 private String getPropertyValue(Value value) throws Exception{
126 switch(value.getType()) {
127 case PropertyType.BINARY: return Integer.toString(PropertyType.BINARY) ;
128 case PropertyType.BOOLEAN :return Boolean.toString(value.getBoolean()) ;
129 case PropertyType.DATE : return value.getDate().getTime().toString() ;
130 case PropertyType.DOUBLE : return Double.toString(value.getDouble()) ;
131 case PropertyType.LONG : return Long.toString(value.getLong()) ;
132 case PropertyType.NAME : return value.getString() ;
133 case PropertyType.STRING : return value.getString() ;
134 }
135 return null ;
136 }
137
138 public void executeAction(String userId, String executable, Map variables) throws Exception {
139 if (!variables.containsKey("userId")) {
140 variables.put("userId", userId);
141 }
142 ScriptService scriptService = WCMCoreUtils.getService(ScriptService.class);
143 CmsScript cmsScript = scriptService.getScript(executable);
144 cmsScript.execute(variables);
145 }
146
147 public class ScriptActionLauncherListener extends BaseActionLauncherListener {
148
149 public ScriptActionLauncherListener(String actionName, String script, String repository, String srcWorkspace,
150 String srcPath, Map actionVariables) throws Exception {
151 super(actionName, script, repository, srcWorkspace, srcPath, actionVariables);
152 }
153
154 public void triggerAction(String userId, Map variables, String repository) throws Exception {
155 executeAction(userId, super.executable_, variables);
156 }
157 }
158
159 @SuppressWarnings("unchecked")
160 public void activateAction(String userId, String executable, Map variables) throws Exception {
161 executeAction(userId,executable,variables) ;
162 }
163
164 protected Class createActivationJob() throws Exception {
165 return ScriptActionActivationJob.class ;
166 }
167
168
169 }