<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="Definitions_0co5an7" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="2.0.3">
  <bpmn:collaboration id="Collaboration">
    <bpmn:participant id="Sentiment_Analysis" name="Sentiment Analysis" processRef="sentiment-analysis" />
  </bpmn:collaboration>
  <bpmn:process id="sentiment-analysis" name="Sentiment Analysis" isExecutable="true" camunda:versionTag="0.0.1">
    <bpmn:laneSet id="LaneSet_1u08y0e" />
    <bpmn:sequenceFlow id="SequenceFlow_14nqu0e" sourceRef="service-report" targetRef="Task_1e7059p" />
    <bpmn:sequenceFlow id="SequenceFlow_0jebdb9" sourceRef="Task_1e7059p" targetRef="EndEvent_0bmuv13" />
    <bpmn:sequenceFlow id="SequenceFlow_15dip2q" sourceRef="StartEvent_1t6nxib" targetRef="terms_loop" />
    <bpmn:sequenceFlow id="SequenceFlow_1j2y6tv" sourceRef="terms_loop" targetRef="service-report" />
    <bpmn:startEvent id="StartEvent_1t6nxib" camunda:formKey="embedded:deployment:input-terms.html">
      <bpmn:extensionElements>
        <camunda:executionListener event="start">
          <camunda:script scriptFormat="javascript">// make variable global
execution.setVariable("results", S('[]'))</camunda:script>
        </camunda:executionListener>
      </bpmn:extensionElements>
      <bpmn:outgoing>SequenceFlow_15dip2q</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:subProcess id="terms_loop">
      <bpmn:extensionElements>
        <camunda:executionListener event="start">
          <camunda:script scriptFormat="javascript">// make variables local to loop
execution.setVariable("analysis", 0, "terms_loop");
execution.setVariable('tweets', S('[]'), "terms_loop");</camunda:script>
        </camunda:executionListener>
        <camunda:executionListener event="end">
          <camunda:script scriptFormat="javascript">var results = execution.getVariable("results");
var term = execution.getVariable("term");
var analysis = execution.getVariable("analysis");
var termStr = term.prop('term').value();

// this is for dictionary
//results.prop(termStr, analysis);
// this is for list
var item = {};
item["name"] = termStr;
item["sentiment"] = analysis;
results.append(item)

execution.setVariable("results", S(results))</camunda:script>
        </camunda:executionListener>
      </bpmn:extensionElements>
      <bpmn:incoming>SequenceFlow_15dip2q</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_1j2y6tv</bpmn:outgoing>
      <bpmn:multiInstanceLoopCharacteristics camunda:collection="${terms.elements()}" camunda:elementVariable="term" />
      <bpmn:serviceTask id="service-analysis" name="Analysis Service">
        <bpmn:extensionElements>
          <camunda:connector>
            <camunda:inputOutput>
              <camunda:inputParameter name="url">http://analysis:8081/</camunda:inputParameter>
              <camunda:inputParameter name="method">POST</camunda:inputParameter>
              <camunda:inputParameter name="headers">
                <camunda:map>
                  <camunda:entry key="Accept">application/json</camunda:entry>
                  <camunda:entry key="Content-Type">application/json</camunda:entry>
                </camunda:map>
              </camunda:inputParameter>
              <camunda:inputParameter name="payload">${tweets.toString()}</camunda:inputParameter>
              <camunda:outputParameter name="analysis">
                <camunda:script scriptFormat="Javascript">var response = connector.getVariable("response");
response.trim()</camunda:script>
              </camunda:outputParameter>
            </camunda:inputOutput>
            <camunda:connectorId>http-connector</camunda:connectorId>
          </camunda:connector>
        </bpmn:extensionElements>
        <bpmn:incoming>SequenceFlow_0g6bfz6</bpmn:incoming>
        <bpmn:outgoing>SequenceFlow_1bz7zx2</bpmn:outgoing>
      </bpmn:serviceTask>
      <bpmn:serviceTask id="service-twitter" name="Twitter Service">
        <bpmn:extensionElements>
          <camunda:connector>
            <camunda:inputOutput>
              <camunda:inputParameter name="url">
                <camunda:script scriptFormat="Javascript">var term = execution.getVariable("term");
'http://twitter:8084/search/tweets.json?q='+term.prop('term').value()</camunda:script>
              </camunda:inputParameter>
              <camunda:inputParameter name="method">GET</camunda:inputParameter>
              <camunda:inputParameter name="headers">
                <camunda:map>
                  <camunda:entry key="Accept">application/json</camunda:entry>
                </camunda:map>
              </camunda:inputParameter>
              <camunda:outputParameter name="tweets">
                <camunda:script scriptFormat="Javascript">var response = connector.getVariable("response");
S(response);</camunda:script>
              </camunda:outputParameter>
            </camunda:inputOutput>
            <camunda:connectorId>http-connector</camunda:connectorId>
          </camunda:connector>
        </bpmn:extensionElements>
        <bpmn:incoming>SequenceFlow_0f6v4j4</bpmn:incoming>
        <bpmn:outgoing>SequenceFlow_0g6bfz6</bpmn:outgoing>
      </bpmn:serviceTask>
      <bpmn:startEvent id="StartEvent_parsing_term">
        <bpmn:outgoing>SequenceFlow_0f6v4j4</bpmn:outgoing>
      </bpmn:startEvent>
      <bpmn:sequenceFlow id="SequenceFlow_0f6v4j4" sourceRef="StartEvent_parsing_term" targetRef="service-twitter" />
      <bpmn:sequenceFlow id="SequenceFlow_0g6bfz6" sourceRef="service-twitter" targetRef="service-analysis" />
      <bpmn:sequenceFlow id="SequenceFlow_1bz7zx2" sourceRef="service-analysis" targetRef="EndEvent_parsing_term" />
      <bpmn:endEvent id="EndEvent_parsing_term">
        <bpmn:incoming>SequenceFlow_1bz7zx2</bpmn:incoming>
      </bpmn:endEvent>
    </bpmn:subProcess>
    <bpmn:endEvent id="EndEvent_0bmuv13">
      <bpmn:incoming>SequenceFlow_0jebdb9</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:serviceTask id="service-report" name="Report Service">
      <bpmn:extensionElements>
        <camunda:connector>
          <camunda:inputOutput>
            <camunda:inputParameter name="url">http://reporting:8083/generatePDF/</camunda:inputParameter>
            <camunda:inputParameter name="method">POST</camunda:inputParameter>
            <camunda:inputParameter name="headers">
              <camunda:map>
                <camunda:entry key="Accept">application/base64</camunda:entry>
                <camunda:entry key="Content-Type">application/json</camunda:entry>
              </camunda:map>
            </camunda:inputParameter>
            <camunda:inputParameter name="payload">${results.toString()}</camunda:inputParameter>
            <camunda:outputParameter name="reportPDF">
              <camunda:script scriptFormat="javascript">var response = connector.getVariable("response");
// from nodejs base64 package				  
var lookup = []
var revLookup = []
var javaByteArray = Java.type('byte[]')

var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
for (var i = 0, len = code.length; i &lt; len; ++i) {
  lookup[i] = code[i]
  revLookup[code.charCodeAt(i)] = i
}

// Support decoding URL-safe base64 strings, as Node.js does.
// See: https://en.wikipedia.org/wiki/Base64#URL_applications
revLookup['-'.charCodeAt(0)] = 62
revLookup['_'.charCodeAt(0)] = 63

function getLens (b64) {
  var len = b64.length

  if (len % 4 &gt; 0) {
    throw new Error('Invalid string. Length must be a multiple of 4')
  }

  // Trim off extra bytes after placeholder bytes are found
  // See: https://github.com/beatgammit/base64-js/issues/42
  var validLen = b64.indexOf('=')
  if (validLen === -1) validLen = len

  var placeHoldersLen = validLen === len
    ? 0
    : 4 - (validLen % 4)

  return [validLen, placeHoldersLen]
}

function _byteLength (b64, validLen, placeHoldersLen) {
  return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
}

function toByteArray (b64) {
  var tmp
  var lens = getLens(b64)
  var validLen = lens[0]
  var placeHoldersLen = lens[1]

  var arr = new javaByteArray(_byteLength(b64, validLen, placeHoldersLen))

  var curByte = 0

  // if there are placeholders, only get up to the last complete 4 chars
  var len = placeHoldersLen &gt; 0
    ? validLen - 4
    : validLen

  for (var i = 0; i &lt; len; i += 4) {
    tmp =
      (revLookup[b64.charCodeAt(i)] &lt;&lt; 18) |
      (revLookup[b64.charCodeAt(i + 1)] &lt;&lt; 12) |
      (revLookup[b64.charCodeAt(i + 2)] &lt;&lt; 6) |
      revLookup[b64.charCodeAt(i + 3)]
    arr[curByte++] = (tmp &gt;&gt; 16) &amp; 0xFF
    arr[curByte++] = (tmp &gt;&gt; 8) &amp; 0xFF
    arr[curByte++] = tmp &amp; 0xFF
  }

  if (placeHoldersLen === 2) {
    tmp =
      (revLookup[b64.charCodeAt(i)] &lt;&lt; 2) |
      (revLookup[b64.charCodeAt(i + 1)] &gt;&gt; 4)
    arr[curByte++] = tmp &amp; 0xFF
  }

  if (placeHoldersLen === 1) {
    tmp =
      (revLookup[b64.charCodeAt(i)] &lt;&lt; 10) |
      (revLookup[b64.charCodeAt(i + 1)] &lt;&lt; 4) |
      (revLookup[b64.charCodeAt(i + 2)] &gt;&gt; 2)
    arr[curByte++] = (tmp &gt;&gt; 8) &amp; 0xFF
    arr[curByte++] = tmp &amp; 0xFF
  }

  return arr
}

var source = "kP/+kA==";
var decoded = toByteArray(source);
//decoded
var file = Java.type('org.camunda.bpm.engine.variable.Variables').fileValue("pdfTest").file(toByteArray(response)).mimeType('application/pdf').create()
file
</camunda:script>
            </camunda:outputParameter>
          </camunda:inputOutput>
          <camunda:connectorId>http-connector</camunda:connectorId>
        </camunda:connector>
      </bpmn:extensionElements>
      <bpmn:incoming>SequenceFlow_1j2y6tv</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_14nqu0e</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:userTask id="Task_1e7059p" name="download pdf" camunda:formKey="embedded:deployment:download-pdf.html">
      <bpmn:incoming>SequenceFlow_14nqu0e</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_0jebdb9</bpmn:outgoing>
    </bpmn:userTask>
  </bpmn:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Collaboration">
      <bpmndi:BPMNShape id="Participant_0gzc3m9_di" bpmnElement="Sentiment_Analysis">
        <dc:Bounds x="0" y="0" width="1084" height="202" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="SubProcess_1skl2rb_di" bpmnElement="terms_loop" isExpanded="true">
        <dc:Bounds x="135" y="19" width="443" height="163" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="ServiceTask_01bq7a6_di" bpmnElement="service-analysis">
        <dc:Bounds x="381" y="60" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="ServiceTask_1o2usc9_di" bpmnElement="service-twitter">
        <dc:Bounds x="230" y="60" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="StartEvent_1nx6b6f_di" bpmnElement="StartEvent_parsing_term">
        <dc:Bounds x="156" y="82" width="36" height="36" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_0f6v4j4_di" bpmnElement="SequenceFlow_0f6v4j4">
        <di:waypoint x="192" y="100" />
        <di:waypoint x="230" y="100" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_0g6bfz6_di" bpmnElement="SequenceFlow_0g6bfz6">
        <di:waypoint x="330" y="100" />
        <di:waypoint x="381" y="100" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_1bz7zx2_di" bpmnElement="SequenceFlow_1bz7zx2">
        <di:waypoint x="481" y="100" />
        <di:waypoint x="516" y="100" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="StartEvent_1t6nxib_di" bpmnElement="StartEvent_1t6nxib">
        <dc:Bounds x="56" y="83" width="36" height="36" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="ServiceTask_1gaqtzh_di" bpmnElement="service-report">
        <dc:Bounds x="619" y="61" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="UserTask_0iooc2g_di" bpmnElement="Task_1e7059p">
        <dc:Bounds x="766" y="61" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="EndEvent_0bmuv13_di" bpmnElement="EndEvent_0bmuv13">
        <dc:Bounds x="1024" y="83" width="36" height="36" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_14nqu0e_di" bpmnElement="SequenceFlow_14nqu0e">
        <di:waypoint x="719" y="101" />
        <di:waypoint x="766" y="101" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_0jebdb9_di" bpmnElement="SequenceFlow_0jebdb9">
        <di:waypoint x="866" y="101" />
        <di:waypoint x="1024" y="101" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_15dip2q_di" bpmnElement="SequenceFlow_15dip2q">
        <di:waypoint x="92" y="101" />
        <di:waypoint x="135" y="101" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_1j2y6tv_di" bpmnElement="SequenceFlow_1j2y6tv">
        <di:waypoint x="578" y="101" />
        <di:waypoint x="619" y="101" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="EndEvent_07g109m_di" bpmnElement="EndEvent_parsing_term">
        <dc:Bounds x="516" y="82" width="36" height="36" />
      </bpmndi:BPMNShape>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn:definitions>