private Set<HookResource> assembleSetOfEvents(final JsonArray eventsArray) {

    final Set<HookResource> allEvents = new HashSet<>();

    for (int i = 0; i < eventsArray.size(); i++) {

      final JsonObject eventElement = eventsArray.get(i).getAsJsonObject();

      final String entityName =
          this.fromApiJsonHelper.extractStringNamed(entityNameParamName, eventElement);
      final String actionName =
          this.fromApiJsonHelper.extractStringNamed(actionNameParamName, eventElement);
      final HookResource event = HookResource.createNewWithoutHook(entityName, actionName);
      allEvents.add(event);
    }

    return allEvents;
  }
  private Set<HookConfiguration> assembleConfig(
      final Map<String, String> hookConfig, final HookTemplate template) {

    final Set<HookConfiguration> configuration = new HashSet<>();
    final Set<Schema> fields = template.getSchema();

    for (final Entry<String, String> configEntry : hookConfig.entrySet()) {
      for (final Schema field : fields) {
        final String fieldName = field.getFieldName();
        if (fieldName.equalsIgnoreCase(configEntry.getKey())) {

          final HookConfiguration config =
              HookConfiguration.createNewWithoutHook(
                  field.getFieldType(), configEntry.getKey(), configEntry.getValue());
          configuration.add(config);
          break;
        }
      }
    }

    return configuration;
  }
  private void validateHookRules(
      final HookTemplate template, final Set<HookConfiguration> config, Set<HookResource> events) {

    final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
    final DataValidatorBuilder baseDataValidator =
        new DataValidatorBuilder(dataValidationErrors).resource("hook");

    if (!template.getName().equalsIgnoreCase(webTemplateName)
        && this.hookRepository.findOneByTemplateId(template.getId()) != null) {
      final String errorMessage = "multiple.non.web.template.hooks.not.supported";
      baseDataValidator.reset().failWithCodeNoParameterAddedToErrorCode(errorMessage);
    }

    for (final HookConfiguration conf : config) {
      final String fieldValue = conf.getFieldValue();
      if (conf.getFieldName().equals(contentTypeName)) {
        if (!(fieldValue.equalsIgnoreCase("json") || fieldValue.equalsIgnoreCase("form"))) {
          final String errorMessage = "content.type.must.be.json.or.form";
          baseDataValidator.reset().failWithCodeNoParameterAddedToErrorCode(errorMessage);
        }
      }

      if (conf.getFieldName().equals(payloadURLName)) {
        try {
          final WebHookService service = ProcessorHelper.createWebHookService(fieldValue);
          service.sendEmptyRequest();
        } catch (RetrofitError re) {
          // Swallow error if it's because of method not supported or
          // if url throws 404 - required for integration test,
          // url generated on 1st POST request
          if (re.getResponse() == null) {
            String errorMessage = "url.invalid";
            baseDataValidator.reset().failWithCodeNoParameterAddedToErrorCode(errorMessage);
          }
        }
      }
    }

    if (events == null || events.isEmpty()) {
      final String errorMessage = "registered.events.cannot.be.empty";
      baseDataValidator.reset().failWithCodeNoParameterAddedToErrorCode(errorMessage);
    }

    final Set<Schema> fields = template.getSchema();
    for (final Schema field : fields) {
      if (!field.isOptional()) {
        boolean found = false;
        for (final HookConfiguration conf : config) {
          if (field.getFieldName().equals(conf.getFieldName())) {
            found = true;
          }
        }
        if (!found) {
          final String errorMessage = "required.config.field." + "not.provided";
          baseDataValidator
              .reset()
              .value(field.getFieldName())
              .failWithCodeNoParameterAddedToErrorCode(errorMessage);
        }
      }
    }

    if (!dataValidationErrors.isEmpty()) {
      throw new PlatformApiDataValidationException(dataValidationErrors);
    }
  }