]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/server/client/impl/SlcServerHttpClientImpl.java
@update:79; Simplify the execution of flows
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / server / client / impl / SlcServerHttpClientImpl.java
index e640c5c697d11f08059ceb07c8233ece408e3360..20c938301dd0de2f3d1605a58553f144305eb0e2 100644 (file)
@@ -1,6 +1,8 @@
 package org.argeo.slc.server.client.impl;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 
@@ -9,6 +11,7 @@ import org.apache.commons.logging.LogFactory;
 import org.argeo.slc.Condition;
 import org.argeo.slc.SlcException;
 import org.argeo.slc.execution.ExecutionFlowDescriptor;
+import org.argeo.slc.execution.ExecutionModuleDescriptor;
 import org.argeo.slc.msg.ExecutionAnswer;
 import org.argeo.slc.msg.MsgConstants;
 import org.argeo.slc.msg.ObjectList;
@@ -19,9 +22,8 @@ import org.argeo.slc.server.client.SlcServerHttpClient;
 
 public class SlcServerHttpClientImpl extends AbstractHttpServicesClient
                implements SlcServerHttpClient {
-       public final static String LIST_AGENTS = "listAgents.service";
-       public final static String IS_SERVER_READY = "isServerReady.service";
-       public final static String NEW_SLC_EXECUTION = "newSlcExecution.service";
+
+       protected final static String PARAM_AGENT_ID = "agentId";
 
        private final static Log log = LogFactory
                        .getLog(SlcServerHttpClientImpl.class);
@@ -29,19 +31,10 @@ public class SlcServerHttpClientImpl extends AbstractHttpServicesClient
        private Long retryTimeout = 60 * 1000l;
        private Long serverReadyTimeout = 120 * 1000l;
 
-       public ExecutionAnswer startFlow(String agentId, String moduleName,
-                       String version, String flowName) {
+       public ExecutionAnswer startFlow(String agentId, RealizedFlow realizedFlow) {
                SlcExecution slcExecution = new SlcExecution();
                slcExecution.setUuid(UUID.randomUUID().toString());
 
-               RealizedFlow realizedFlow = new RealizedFlow();
-               realizedFlow.setModuleName(moduleName);
-               realizedFlow.setModuleVersion(version);
-
-               ExecutionFlowDescriptor flowDescriptor = new ExecutionFlowDescriptor();
-               flowDescriptor.setName(flowName);
-
-               realizedFlow.setFlowDescriptor(flowDescriptor);
                slcExecution.getRealizedFlows().add(realizedFlow);
 
                Map<String, String> parameters = new HashMap<String, String>();
@@ -51,6 +44,83 @@ public class SlcServerHttpClientImpl extends AbstractHttpServicesClient
                return answer;
        }
 
+       public ExecutionAnswer startFlowDefault(String moduleName, String flowName,
+                       Map<String, Object> args) {
+               SlcAgentDescriptor agentDescriptor = waitForOneAgent();
+               List<ExecutionModuleDescriptor> lst = listModuleDescriptors(agentDescriptor
+                               .getUuid());
+               ExecutionModuleDescriptor moduleDescMinimal = findModule(lst,
+                               moduleName);
+               String moduleVersion = moduleDescMinimal.getVersion();
+
+               ExecutionModuleDescriptor moduleDesc = getModuleDescriptor(
+                               agentDescriptor.getUuid(), moduleName, moduleVersion);
+
+               RealizedFlow realizedFlow = new RealizedFlow();
+               realizedFlow.setModuleName(moduleName);
+               realizedFlow.setModuleVersion(moduleDesc.getVersion());
+
+               ExecutionFlowDescriptor flowDescriptor = findFlow(moduleDesc, flowName);
+               if (args != null) {
+                       for (String key : args.keySet()) {
+                               if (flowDescriptor.getValues().containsKey(key)) {
+                                       flowDescriptor.getValues().put(key, args.get(key));
+                               }
+                       }
+               }
+               realizedFlow.setFlowDescriptor(flowDescriptor);
+
+               return startFlow(agentDescriptor.getUuid(), realizedFlow);
+       }
+
+       public static ExecutionModuleDescriptor findModule(
+                       List<ExecutionModuleDescriptor> lst, String moduleName) {
+               ExecutionModuleDescriptor moduleDesc = null;
+               for (ExecutionModuleDescriptor desc : lst) {
+                       if (desc.getName().equals(moduleName)) {
+                               if (moduleDesc != null)
+                                       throw new SlcException(
+                                                       "There is more than one module named " + moduleName
+                                                                       + " (versions: " + moduleDesc + " and "
+                                                                       + desc.getVersion() + ")");
+                               moduleDesc = desc;
+                       }
+               }
+               return moduleDesc;
+       }
+
+       public static ExecutionFlowDescriptor findFlow(
+                       ExecutionModuleDescriptor moduleDesc, String flowName) {
+               ExecutionFlowDescriptor flowDesc = null;
+               for (ExecutionFlowDescriptor desc : moduleDesc.getExecutionFlows()) {
+                       if (desc.getName().equals(flowName)) {
+                               flowDesc = desc;
+                       }
+               }
+               return flowDesc;
+       }
+
+       public List<ExecutionModuleDescriptor> listModuleDescriptors(String agentId) {
+               Map<String, String> parameters = new HashMap<String, String>();
+               parameters.put(PARAM_AGENT_ID, agentId);
+
+               List<ExecutionModuleDescriptor> moduleDescriptors = new ArrayList<ExecutionModuleDescriptor>();
+               ObjectList ol = callService(LIST_MODULE_DESCRIPTORS, parameters);
+               ol.fill(moduleDescriptors);
+               return moduleDescriptors;
+       }
+
+       public ExecutionModuleDescriptor getModuleDescriptor(String agentId,
+                       String moduleName, String version) {
+               Map<String, String> parameters = new HashMap<String, String>();
+               parameters.put(PARAM_AGENT_ID, agentId);
+               parameters.put("moduleName", moduleName);
+               parameters.put("version", version);
+               ExecutionModuleDescriptor moduleDescriptor = callService(
+                               GET_MODULE_DESCRIPTOR, parameters);
+               return moduleDescriptor;
+       }
+
        public SlcAgentDescriptor waitForOneAgent() {
                ObjectList objectList = callServiceSafe(LIST_AGENTS, null,
                                new Condition<ObjectList>() {