private ContainerShape getParentContainer(
      String flowElementId, Process process, Diagram diagram) {
    Lane foundLane = null;
    for (Lane lane : process.getLanes()) {
      if (lane.getFlowReferences().contains(flowElementId)) {
        foundLane = lane;
        break;
      }
    }

    if (foundLane != null) {
      final IFeatureProvider featureProvider = getDiagramTypeProvider().getFeatureProvider();
      return (ContainerShape) featureProvider.getPictogramElementForBusinessObject(foundLane);
    } else {
      return diagram;
    }
  }
  public void convertToBpmnModel(
      JsonNode elementNode,
      JsonNode modelNode,
      ActivityProcessor processor,
      BaseElement parentElement,
      Map<String, JsonNode> shapeMap,
      BpmnModel bpmnModel) {

    this.processor = processor;
    this.model = bpmnModel;

    BaseElement baseElement = convertJsonToElement(elementNode, modelNode, shapeMap);
    baseElement.setId(BpmnJsonConverterUtil.getElementId(elementNode));

    if (baseElement instanceof FlowElement) {
      FlowElement flowElement = (FlowElement) baseElement;
      flowElement.setName(getPropertyValueAsString(PROPERTY_NAME, elementNode));
      flowElement.setDocumentation(getPropertyValueAsString(PROPERTY_DOCUMENTATION, elementNode));

      BpmnJsonConverterUtil.convertJsonToListeners(elementNode, flowElement);

      if (baseElement instanceof Activity) {
        Activity activity = (Activity) baseElement;
        activity.setAsynchronous(getPropertyValueAsBoolean(PROPERTY_ASYNCHRONOUS, elementNode));
        activity.setNotExclusive(!getPropertyValueAsBoolean(PROPERTY_EXCLUSIVE, elementNode));

        String multiInstanceType =
            getPropertyValueAsString(PROPERTY_MULTIINSTANCE_TYPE, elementNode);
        String multiInstanceCardinality =
            getPropertyValueAsString(PROPERTY_MULTIINSTANCE_CARDINALITY, elementNode);
        String multiInstanceCollection =
            getPropertyValueAsString(PROPERTY_MULTIINSTANCE_COLLECTION, elementNode);
        String multiInstanceCondition =
            getPropertyValueAsString(PROPERTY_MULTIINSTANCE_CONDITION, elementNode);

        if (StringUtils.isNotEmpty(multiInstanceType)
            && "none".equalsIgnoreCase(multiInstanceType) == false) {

          String multiInstanceVariable =
              getPropertyValueAsString(PROPERTY_MULTIINSTANCE_VARIABLE, elementNode);

          MultiInstanceLoopCharacteristics multiInstanceObject =
              new MultiInstanceLoopCharacteristics();
          if ("sequential".equalsIgnoreCase(multiInstanceType)) {
            multiInstanceObject.setSequential(true);
          } else {
            multiInstanceObject.setSequential(false);
          }
          multiInstanceObject.setLoopCardinality(multiInstanceCardinality);
          multiInstanceObject.setInputDataItem(multiInstanceCollection);
          multiInstanceObject.setElementVariable(multiInstanceVariable);
          multiInstanceObject.setCompletionCondition(multiInstanceCondition);
          activity.setLoopCharacteristics(multiInstanceObject);
        }

      } else if (baseElement instanceof Gateway) {
        JsonNode flowOrderNode = getProperty(PROPERTY_SEQUENCEFLOW_ORDER, elementNode);
        if (flowOrderNode != null) {
          flowOrderNode = BpmnJsonConverterUtil.validateIfNodeIsTextual(flowOrderNode);
          JsonNode orderArray = flowOrderNode.get("sequenceFlowOrder");
          if (orderArray != null && orderArray.size() > 0) {
            for (JsonNode orderNode : orderArray) {
              ExtensionElement orderElement = new ExtensionElement();
              orderElement.setName("EDITOR_FLOW_ORDER");
              orderElement.setElementText(orderNode.asText());
              flowElement.addExtensionElement(orderElement);
            }
          }
        }
      }
    }

    if (baseElement instanceof FlowElement) {
      FlowElement flowElement = (FlowElement) baseElement;
      if (flowElement instanceof SequenceFlow) {
        ExtensionElement idExtensionElement = new ExtensionElement();
        idExtensionElement.setName("EDITOR_RESOURCEID");
        idExtensionElement.setElementText(elementNode.get(EDITOR_SHAPE_ID).asText());
        flowElement.addExtensionElement(idExtensionElement);
      }

      if (parentElement instanceof Process) {
        ((Process) parentElement).addFlowElement(flowElement);

      } else if (parentElement instanceof SubProcess) {
        ((SubProcess) parentElement).addFlowElement(flowElement);

      } else if (parentElement instanceof Lane) {
        Lane lane = (Lane) parentElement;
        lane.getFlowReferences().add(flowElement.getId());
        lane.getParentProcess().addFlowElement(flowElement);
      }

    } else if (baseElement instanceof Artifact) {
      Artifact artifact = (Artifact) baseElement;
      if (parentElement instanceof Process) {
        ((Process) parentElement).addArtifact(artifact);

      } else if (parentElement instanceof SubProcess) {
        ((SubProcess) parentElement).addArtifact(artifact);

      } else if (parentElement instanceof Lane) {
        Lane lane = (Lane) parentElement;
        lane.getFlowReferences().add(artifact.getId());
        lane.getParentProcess().addArtifact(artifact);
      }
    }
  }
  public static void writeBPMNDI(BpmnModel model, XMLStreamWriter xtw) throws Exception {
    // BPMN DI information
    xtw.writeStartElement(BPMNDI_PREFIX, ELEMENT_DI_DIAGRAM, BPMNDI_NAMESPACE);

    String processId = null;
    if (model.getPools().size() > 0) {
      processId = "Collaboration";
    } else {
      processId = model.getMainProcess().getId();
    }

    xtw.writeAttribute(ATTRIBUTE_ID, "BPMNDiagram_" + processId);

    xtw.writeStartElement(BPMNDI_PREFIX, ELEMENT_DI_PLANE, BPMNDI_NAMESPACE);
    xtw.writeAttribute(ATTRIBUTE_DI_BPMNELEMENT, processId);
    xtw.writeAttribute(ATTRIBUTE_ID, "BPMNPlane_" + processId);

    // System.out.println("DI EXPORT");
    for (String elementId : model.getLocationMap().keySet()) {

      if (model.getFlowElement(elementId) != null
          || model.getArtifact(elementId) != null
          || model.getPool(elementId) != null
          || model.getLane(elementId) != null) {

        xtw.writeStartElement(BPMNDI_PREFIX, ELEMENT_DI_SHAPE, BPMNDI_NAMESPACE);
        xtw.writeAttribute(ATTRIBUTE_DI_BPMNELEMENT, elementId);
        xtw.writeAttribute(ATTRIBUTE_ID, "BPMNShape_" + elementId);

        GraphicInfo graphicInfo = model.getGraphicInfo(elementId);
        FlowElement flowElement = model.getFlowElement(elementId);
        if (flowElement != null
            && flowElement instanceof SubProcess
            && graphicInfo.getExpanded() != null) {
          xtw.writeAttribute(ATTRIBUTE_DI_IS_EXPANDED, String.valueOf(graphicInfo.getExpanded()));
        }

        if (flowElement == null) {
          boolean foundLane = false;
          for (Pool pool : model.getPools()) {
            if (foundLane) {
              break;
            }
            if (elementId.equals(pool.getId())) {
              if (graphicInfo.getHorizontal() != null) {
                xtw.writeAttribute(
                    ATTRIBUTE_DI_IS_HORIZONTAL, String.valueOf(graphicInfo.getHorizontal()));
              }
            } else {
              Process process = model.getProcess(pool.getId());
              if (process != null) {
                for (Lane lane : process.getLanes()) {
                  if (elementId.equals(lane.getId())) {
                    foundLane = true;
                    if (graphicInfo.getHorizontal() != null) {
                      xtw.writeAttribute(
                          ATTRIBUTE_DI_IS_HORIZONTAL, String.valueOf(graphicInfo.getHorizontal()));
                    }
                    break;
                  }
                }
              }
            }
          }
        }

        xtw.writeStartElement(OMGDC_PREFIX, ELEMENT_DI_BOUNDS, OMGDC_NAMESPACE);
        xtw.writeAttribute(ATTRIBUTE_DI_HEIGHT, "" + graphicInfo.getHeight());
        xtw.writeAttribute(ATTRIBUTE_DI_WIDTH, "" + graphicInfo.getWidth());
        xtw.writeAttribute(ATTRIBUTE_DI_X, "" + graphicInfo.getX());
        xtw.writeAttribute(ATTRIBUTE_DI_Y, "" + graphicInfo.getY());
        xtw.writeEndElement();

        xtw.writeEndElement();
      }
    }

    for (String elementId : model.getFlowLocationMap().keySet()) {

      if (model.getFlowElement(elementId) != null || model.getArtifact(elementId) != null) {

        xtw.writeStartElement(BPMNDI_PREFIX, ELEMENT_DI_EDGE, BPMNDI_NAMESPACE);
        xtw.writeAttribute(ATTRIBUTE_DI_BPMNELEMENT, elementId);
        xtw.writeAttribute(ATTRIBUTE_ID, "BPMNEdge_" + elementId);

        List<GraphicInfo> graphicInfoList = model.getFlowLocationGraphicInfo(elementId);
        for (GraphicInfo graphicInfo : graphicInfoList) {
          xtw.writeStartElement(OMGDI_PREFIX, ELEMENT_DI_WAYPOINT, OMGDI_NAMESPACE);
          xtw.writeAttribute(ATTRIBUTE_DI_X, "" + graphicInfo.getX());
          xtw.writeAttribute(ATTRIBUTE_DI_Y, "" + graphicInfo.getY());
          xtw.writeEndElement();
        }

        GraphicInfo labelGraphicInfo = model.getLabelGraphicInfo(elementId);
        FlowElement flowElement = model.getFlowElement(elementId);
        if (labelGraphicInfo != null
            && flowElement != null
            && StringUtils.isNotEmpty(flowElement.getName())) {
          xtw.writeStartElement(BPMNDI_PREFIX, ELEMENT_DI_LABEL, BPMNDI_NAMESPACE);
          xtw.writeStartElement(OMGDC_PREFIX, ELEMENT_DI_BOUNDS, OMGDC_NAMESPACE);
          xtw.writeAttribute(ATTRIBUTE_DI_HEIGHT, "" + labelGraphicInfo.getHeight());
          xtw.writeAttribute(ATTRIBUTE_DI_WIDTH, "" + labelGraphicInfo.getWidth());
          xtw.writeAttribute(ATTRIBUTE_DI_X, "" + labelGraphicInfo.getX());
          xtw.writeAttribute(ATTRIBUTE_DI_Y, "" + labelGraphicInfo.getY());
          xtw.writeEndElement();
          xtw.writeEndElement();
        }

        xtw.writeEndElement();
      }
    }

    // end BPMN DI elements
    xtw.writeEndElement();
    xtw.writeEndElement();
  }
  protected Point getMinXAndMinY(BpmnModel bpmnModel) {
    // We need to calculate maximum values to know how big the image will be in its entirety
    double theMinX = java.lang.Double.MAX_VALUE;
    double theMaxX = 0;
    double theMinY = java.lang.Double.MAX_VALUE;
    double theMaxY = 0;

    for (Pool pool : bpmnModel.getPools()) {
      GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(pool.getId());
      theMinX = graphicInfo.getX();
      theMaxX = graphicInfo.getX() + graphicInfo.getWidth();
      theMinY = graphicInfo.getY();
      theMaxY = graphicInfo.getY() + graphicInfo.getHeight();
    }

    List<FlowNode> flowNodes = gatherAllFlowNodes(bpmnModel);

    for (FlowNode flowNode : flowNodes) {
      GraphicInfo flowNodeGraphicInfo = bpmnModel.getGraphicInfo(flowNode.getId());

      // width
      if ((flowNodeGraphicInfo.getX() + flowNodeGraphicInfo.getWidth()) > theMaxX) {
        theMaxX = flowNodeGraphicInfo.getX() + flowNodeGraphicInfo.getWidth();
      }

      if (flowNodeGraphicInfo.getX() < theMinX) {
        theMinX = flowNodeGraphicInfo.getX();
      }

      // height
      if ((flowNodeGraphicInfo.getY() + flowNodeGraphicInfo.getHeight()) > theMaxY) {
        theMaxY = flowNodeGraphicInfo.getY() + flowNodeGraphicInfo.getHeight();
      }

      if (flowNodeGraphicInfo.getY() < theMinY) {
        theMinY = flowNodeGraphicInfo.getY();
      }

      for (SequenceFlow sequenceFlow : flowNode.getOutgoingFlows()) {
        List<GraphicInfo> graphicInfoList =
            bpmnModel.getFlowLocationGraphicInfo(sequenceFlow.getId());

        for (GraphicInfo graphicInfo : graphicInfoList) {
          // width
          if (graphicInfo.getX() > theMaxX) {
            theMaxX = graphicInfo.getX();
          }

          if (graphicInfo.getX() < theMinX) {
            theMinX = graphicInfo.getX();
          }

          // height
          if (graphicInfo.getY() > theMaxY) {
            theMaxY = graphicInfo.getY();
          }

          if (graphicInfo.getY() < theMinY) {
            theMinY = graphicInfo.getY();
          }
        }
      }
    }

    List<Artifact> artifacts = gatherAllArtifacts(bpmnModel);

    for (Artifact artifact : artifacts) {
      GraphicInfo artifactGraphicInfo = bpmnModel.getGraphicInfo(artifact.getId());

      if (artifactGraphicInfo != null) {
        // width
        if ((artifactGraphicInfo.getX() + artifactGraphicInfo.getWidth()) > theMaxX) {
          theMaxX = artifactGraphicInfo.getX() + artifactGraphicInfo.getWidth();
        }

        if (artifactGraphicInfo.getX() < theMinX) {
          theMinX = artifactGraphicInfo.getX();
        }

        // height
        if ((artifactGraphicInfo.getY() + artifactGraphicInfo.getHeight()) > theMaxY) {
          theMaxY = artifactGraphicInfo.getY() + artifactGraphicInfo.getHeight();
        }

        if (artifactGraphicInfo.getY() < theMinY) {
          theMinY = artifactGraphicInfo.getY();
        }
      }

      List<GraphicInfo> graphicInfoList = bpmnModel.getFlowLocationGraphicInfo(artifact.getId());

      if (graphicInfoList != null) {
        for (GraphicInfo graphicInfo : graphicInfoList) {
          // width
          if (graphicInfo.getX() > theMaxX) {
            theMaxX = graphicInfo.getX();
          }

          if (graphicInfo.getX() < theMinX) {
            theMinX = graphicInfo.getX();
          }

          // height
          if (graphicInfo.getY() > theMaxY) {
            theMaxY = graphicInfo.getY();
          }

          if (graphicInfo.getY() < theMinY) {
            theMinY = graphicInfo.getY();
          }
        }
      }
    }

    int nrOfLanes = 0;

    for (org.activiti.bpmn.model.Process process : bpmnModel.getProcesses()) {
      for (Lane l : process.getLanes()) {
        nrOfLanes++;

        GraphicInfo graphicInfo = bpmnModel.getGraphicInfo(l.getId());

        // // width
        if ((graphicInfo.getX() + graphicInfo.getWidth()) > theMaxX) {
          theMaxX = graphicInfo.getX() + graphicInfo.getWidth();
        }

        if (graphicInfo.getX() < theMinX) {
          theMinX = graphicInfo.getX();
        }

        // height
        if ((graphicInfo.getY() + graphicInfo.getHeight()) > theMaxY) {
          theMaxY = graphicInfo.getY() + graphicInfo.getHeight();
        }

        if (graphicInfo.getY() < theMinY) {
          theMinY = graphicInfo.getY();
        }
      }
    }

    // Special case, see http://jira.codehaus.org/browse/ACT-1431
    if ((flowNodes.size() == 0) && (bpmnModel.getPools().size() == 0) && (nrOfLanes == 0)) {
      // Nothing to show
      theMinX = 0;
      theMinY = 0;
    }

    return new Point((int) theMinX, (int) theMinY);
  }