All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.camunda.service.FormServices Maven / Gradle / Ivy

The newest version!
/*
 * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under
 * one or more contributor license agreements. See the NOTICE file distributed
 * with this work for additional information regarding copyright ownership.
 * Licensed under the Camunda License 1.0. You may not use this file
 * except in compliance with the Camunda License 1.0.
 */
package io.camunda.service;

import io.camunda.search.clients.FormSearchClient;
import io.camunda.search.entities.FormEntity;
import io.camunda.search.exception.CamundaSearchException;
import io.camunda.search.exception.NotFoundException;
import io.camunda.search.query.FormQuery;
import io.camunda.search.query.SearchQueryBuilders;
import io.camunda.search.query.SearchQueryResult;
import io.camunda.security.auth.Authentication;
import io.camunda.security.auth.SecurityContext;
import io.camunda.security.configuration.SecurityConfiguration;
import io.camunda.service.search.core.SearchQueryService;
import io.camunda.zeebe.broker.client.api.BrokerClient;
import java.util.Optional;

public final class FormServices extends SearchQueryService {

  private final FormSearchClient formSearchClient;

  public FormServices(
      final BrokerClient brokerClient,
      final SecurityConfiguration securityConfiguration,
      final FormSearchClient formSearchClient,
      final Authentication authentication) {
    super(brokerClient, securityConfiguration, authentication);
    this.formSearchClient = formSearchClient;
  }

  @Override
  public FormServices withAuthentication(final Authentication authentication) {
    return new FormServices(brokerClient, securityConfiguration, formSearchClient, authentication);
  }

  @Override
  public SearchQueryResult search(final FormQuery query) {
    return formSearchClient.searchForms(
        query, SecurityContext.of(s -> s.withAuthentication(authentication)));
  }

  public FormEntity getByKey(final Long key) {
    final SearchQueryResult result =
        search(SearchQueryBuilders.formSearchQuery().filter(f -> f.formKeys(key)).build());

    if (result.total() < 1) {
      throw new NotFoundException(String.format("Form with formKey=%d not found", key));
    } else if (result.total() > 1) {
      throw new CamundaSearchException(String.format("Found form with key %d more than once", key));
    } else {
      return result.items().stream().findFirst().orElseThrow();
    }
  }

  public Optional getLatestVersionByFormId(final String formId) {
    final Optional result =
        search(
                SearchQueryBuilders.formSearchQuery()
                    .filter(f -> f.formIds(formId))
                    .sort(s -> s.version().desc())
                    .build())
            .items()
            .stream()
            .findFirst();

    if (result.isPresent()) {
      return result;
    } else {
      return Optional.empty();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy