예제 #1
0
  @Override
  public FlowSnippetDTO instantiateTemplate(
      String groupId, Double originX, Double originY, String templateId, String idGenerationSeed) {
    ProcessGroup group = locateProcessGroup(flowController, groupId);

    // get the template id and find the template
    Template template = getTemplate(templateId);

    // ensure the template could be found
    if (template == null) {
      throw new ResourceNotFoundException(
          String.format("Unable to locate template with id '%s'.", templateId));
    }

    try {
      // copy the template which pre-processes all ids
      TemplateDTO templateDetails = template.getDetails();
      FlowSnippetDTO snippet =
          snippetUtils.copy(templateDetails.getSnippet(), group, idGenerationSeed);

      // calculate scaling factors based on the template encoding version
      // attempt to parse the encoding version
      final FlowEncodingVersion templateEncodingVersion =
          FlowEncodingVersion.parse(templateDetails.getEncodingVersion());
      // get the major version, or 0 if no version could be parsed
      int templateEncodingMajorVersion =
          templateEncodingVersion != null ? templateEncodingVersion.getMajorVersion() : 0;
      // based on the major version < 1, use the default scaling factors.  Otherwise, don't scale
      // (use factor of 1.0)
      double factorX =
          templateEncodingMajorVersion < 1 ? FlowController.DEFAULT_POSITION_SCALE_FACTOR_X : 1.0;
      double factorY =
          templateEncodingMajorVersion < 1 ? FlowController.DEFAULT_POSITION_SCALE_FACTOR_Y : 1.0;

      // reposition and scale the template contents
      org.apache.nifi.util.SnippetUtils.moveAndScaleSnippet(
          snippet, originX, originY, factorX, factorY);

      // find all the child process groups in each process group in the top level of this snippet
      final List<ProcessGroupDTO> childProcessGroups =
          org.apache.nifi.util.SnippetUtils.findAllProcessGroups(snippet);
      // scale (but don't reposition) child process groups
      childProcessGroups
          .stream()
          .forEach(
              processGroup ->
                  org.apache.nifi.util.SnippetUtils.scaleSnippet(
                      processGroup.getContents(), factorX, factorY));

      // instantiate the template into this group
      flowController.instantiateSnippet(group, snippet);

      return snippet;
    } catch (ProcessorInstantiationException pie) {
      throw new NiFiCoreException(
          String.format(
              "Unable to instantiate template because processor type '%s' is unknown to this NiFi.",
              StringUtils.substringAfterLast(pie.getMessage(), ".")));
    }
  }
예제 #2
0
  @Override
  public FlowSnippetDTO instantiateTemplate(
      String groupId, Double originX, Double originY, String templateId) {
    ProcessGroup group = locateProcessGroup(flowController, groupId);

    // get the template id and find the template
    Template template = flowController.getTemplate(templateId);

    // ensure the template could be found
    if (template == null) {
      throw new ResourceNotFoundException(
          String.format("Unable to locate template with id '%s'.", templateId));
    }

    try {
      // copy the template which pre-processes all ids
      TemplateDTO templateDetails = template.getDetails();
      FlowSnippetDTO snippet = snippetUtils.copy(templateDetails.getSnippet(), group);

      // reposition the template contents
      org.apache.nifi.util.SnippetUtils.moveSnippet(snippet, originX, originY);

      // instantiate the template into this group
      flowController.instantiateSnippet(group, snippet);

      return snippet;
    } catch (ProcessorInstantiationException pie) {
      throw new NiFiCoreException(
          String.format(
              "Unable to instantiate template because processor type '%s' is unknown to this NiFi.",
              StringUtils.substringAfterLast(pie.getMessage(), ".")));
    }
  }