package com.chenyang.nse.bussiness.engine.infa.basic.webservice;

import com.chenyang.nse.bussiness.engine.infa.InfoProperty;
import com.chenyang.nse.bussiness.engine.infa.basic.wsclient.DIServiceInfo;
import com.chenyang.nse.bussiness.engine.infa.basic.wsclient.DataIntegrationInterface;
import com.chenyang.nse.bussiness.engine.infa.basic.wsclient.DataIntegrationService;
import com.chenyang.nse.bussiness.engine.infa.basic.wsclient.ETaskRunMode;
import com.chenyang.nse.bussiness.engine.infa.basic.wsclient.SessionHeader;
import com.chenyang.nse.bussiness.engine.infa.basic.wsclient.WorkflowRequest;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.Service.Mode;
import org.w3c.dom.Document;

public class MultiGetWorkflowLogWebService {
   public static String returnStatus(String sessionId, InfoProperty engine, String workflowname, String mpname, String fdname, String wf_run_id) throws Exception {
      String ns = engine.getWebserviceurl();
      String wsdlUrl = engine.getWebserviceserverurl();
      URL url = new URL(wsdlUrl);
      QName sname = new QName(ns, "DataIntegrationService");
      Service service = Service.create(url, sname);
      Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns, "DataIntegration"), SOAPMessage.class, Mode.MESSAGE);
      SOAPMessage msg = MessageFactory.newInstance("SOAP 1.1 Protocol").createMessage();
      SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
      SOAPHeader head = envelope.getHeader();
      QName hename = new QName(ns, "Context", "ns0");
      SOAPHeaderElement hele = head.addHeaderElement(hename);
      hele.addChildElement("SessionId").setValue(sessionId);
      SOAPBody body = envelope.getBody();
      QName ename = new QName(ns, "GetWorkflowLog", "ns0");
      SOAPBodyElement ele = body.addBodyElement(ename);
      SOAPElement di = ele.addChildElement("DIServiceInfo");
      di.addChildElement("DomainName").setValue(engine.getDomainname());
      di.addChildElement("ServiceName").setValue(engine.getInteservicename());
      ele.addChildElement("FolderName").setValue(fdname);
      ele.addChildElement("WorkflowName").setValue(workflowname);
      ele.addChildElement("WorkflowRunInstanceName").setValue("s_M_" + mpname);
      ele.addChildElement("WorkflowRunId").setValue(wf_run_id);
      ele.addChildElement("Timeout").setValue("1000");
      SOAPMessage response = (SOAPMessage)dispatch.invoke(msg);
      Document doc = response.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
      if (doc.getElementsByTagName("Buffer") == null) {
         return "FAIL";
      } else {
         String str = doc.getElementsByTagName("Buffer").item(0).getTextContent();
         return str;
      }
   }

   public static String stopWorkflow(String sessionId, InfoProperty engine, String workflowname, String fdname, String wf_run_id, String mpname) throws Exception {
      String ns = engine.getWebserviceurl();
      String wsdlUrl = engine.getWebserviceserverurl();
      URL url = new URL(wsdlUrl);
      QName sname = new QName(ns, "DataIntegrationService");
      Service service = Service.create(url, sname);
      Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns, "DataIntegration"), SOAPMessage.class, Mode.MESSAGE);
      SOAPMessage msg = MessageFactory.newInstance("SOAP 1.1 Protocol").createMessage();
      SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
      SOAPHeader head = envelope.getHeader();
      QName hename = new QName(ns, "Context", "ns0");
      SOAPHeaderElement hele = head.addHeaderElement(hename);
      hele.addChildElement("SessionId").setValue(sessionId);
      SOAPBody body = envelope.getBody();
      QName ename = new QName(ns, "StopWorkflow", "ns0");
      SOAPBodyElement ele = body.addBodyElement(ename);
      SOAPElement di = ele.addChildElement("DIServiceInfo");
      di.addChildElement("DomainName").setValue(engine.getDomainname());
      di.addChildElement("ServiceName").setValue(engine.getInteservicename());
      ele.addChildElement("FolderName").setValue(fdname);
      ele.addChildElement("WorkflowName").setValue(workflowname);
      ele.addChildElement("WorkflowRunInstanceName").setValue("s_M_" + mpname);
      ele.addChildElement("WorkflowRunId").setValue(wf_run_id);
      SOAPMessage response = (SOAPMessage)dispatch.invoke(msg);
      Document doc = response.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
      return doc.getElementsByTagName("Buffer") == null ? "FAIL" : "SUCCESS";
   }

   public static String recoverWorkflow(InfoProperty engine, String folderName, String workflowName, String wf_run_id, String sessionId) {
      if (!"".equals(workflowName) && workflowName != null && !workflowName.startsWith("wf_")) {
         workflowName = "wf_" + workflowName;
      }

      String recoverStatus = "FAIL";
      DataIntegrationInterface webService = (new DataIntegrationService()).getDataIntegration();
      DIServiceInfo dIServiceInfo = new DIServiceInfo();
      dIServiceInfo.setDomainName(engine.getDomainname());
      dIServiceInfo.setServiceName(engine.getInteservicename());
      WorkflowRequest workflowRequest = new WorkflowRequest();
      workflowRequest.setDIServiceInfo(dIServiceInfo);
      workflowRequest.setFolderName(folderName);
      workflowRequest.setWorkflowName(workflowName);
      workflowRequest.setWorkflowRunId(Integer.valueOf(wf_run_id));
      workflowRequest.setRequestMode(ETaskRunMode.NORMAL);

      try {
         SessionHeader sessionHeader = new SessionHeader();
         sessionHeader.setSessionId(sessionId);
         webService.recoverWorkflow(workflowRequest, sessionHeader);
         recoverStatus = "SUCCESS";
         return recoverStatus;
      } catch (Exception e) {
         System.out.println("工作流重新启动失败！");
         e.printStackTrace();
         return recoverStatus;
      }
   }
}
