SagaImporter.prototype.import = function()

in saga/seata-saga-statemachine-designer/src/modeling/SagaImporter.js [112:225]


SagaImporter.prototype.import = function (definitions) {
  let error = [];
  const warnings = [];

  this.eventBus.fire('import.start', { definitions });

  try {
    const root = this.sagaFactory.create('StateMachine');
    root.importJson(definitions);
    this.root(root);
    // Add start state
    let start = this.sagaFactory.create('StartState');
    let stateArrayList = new Map();
    start.importJson(definitions);
    let begin = start;
    start = this.add(start);
    const edges = [];
    const catches = [];
    forEach(definitions.States, (semantic) => {
      const state = this.sagaFactory.create(semantic.Type);

      if (semantic.style === undefined) {
        stateArrayList = addArrayList(definitions);
        state.importStates(definitions, semantic, begin, stateArrayList);
      } else {
        state.importJson(semantic);
        begin = state;
      }
      const host = this.add(state);

      if (semantic.edge === undefined) {
        state.importEdges(definitions, semantic);
        if (semantic.edge) {
          edges.push(...Object.values(semantic.edge));
        }
      } else {
        edges.push(...Object.values(semantic.edge));
      }

      if (semantic.Catch) {
        let source;
        if (semantic.catch === undefined) {
          const node = this.sagaFactory.create('Catch');
          const catchList = addCatchList(definitions, semantic);
          node.addCatch(definitions, semantic, catchList, stateArrayList);
          source = this.add(node);
        } else {
          const node = this.sagaFactory.create('Catch');
          node.importJson(semantic.catch);
          source = this.add(node);
        }

        if (semantic.catch.edge === undefined) {
          state.importCatchesEdges(definitions, semantic);
        }
        if (semantic.catch.edge) {
          semantic.Catch.forEach((exceptionMatch) => {
            if (semantic.catch.edge[exceptionMatch.Next]) {
              semantic.catch.edge[exceptionMatch.Next].Exceptions = exceptionMatch.Exceptions;
            }
          });

          this.modeling.updateAttachment(source, host);
          catches.push({
            source,
            edges: Object.values(semantic.catch.edge),
          });
        }
      }
    });

    if ((definitions.edge === undefined) && (definitions.States)) {
      start = this.sagaFactory.create('StartState');
      definitions.edge = {};
      start.importJsonEdges(definitions);
      if (definitions.edge) {
        const startEdge = this.sagaFactory.create('Transition');
        startEdge.importJson(definitions.edge);
        this.add(startEdge, { source: start });
      }
    }
    if (definitions.edge) {
      const startEdge = this.sagaFactory.create('Transition');
      startEdge.importJson(definitions.edge);
      this.add(startEdge, { source: start });
    }

    forEach(edges, (semantic) => {
      const transition = this.sagaFactory.create(semantic.Type);
      transition.importJson(semantic);
      this.add(transition);
    });

    forEach(catches, (oneCatch) => {
      const {
        source,
        edges: exceptionMatches,
      } = oneCatch;
      forEach(exceptionMatches, (semantic) => {
        const exceptionMatch = this.sagaFactory.create(semantic.Type);
        exceptionMatch.importJson(semantic);
        this.add(exceptionMatch, { source });
      });
    });
  } catch (e) {
    error = e;
    console.error(error);
  }

  this.eventBus.fire('import.done', {
    error,
    warnings,
  });
};