components/src/main/java/eu/internetofus/common/components/WeNetValidateContext.java
/**
   * Check if a field contains a defined model.
   *
   * @param name   of the field to validate.
   * @param ids    identifiers of the models.
   * @param type   of model to search.
   * @param search function to check if the models exist.
   * @param future to compose if the models are defined or not.
   *
   * @return the future with the validation result.
   */
Check if a field contains a defined model.
@param name   of the field to validate.
@param ids    identifiers of the models.
@param type   of model to search.
@param search function to check if the models exist.
@param future to compose if the models are defined or not.

@return the future with the validation result.
Check if a field contains a defined model . @param name of the field to validate . @param ids identifiers of the models . @param type of model to search . @param search function to check if the models exist . @param future to compose if the models are defined or not . @return the future with the validation result .
public Future<Void> validateDefinedIdsField(final String name, final Iterable<String> ids, final Class<?> type,
      final Function<String, Future<Boolean>> search, final Future<Void> future) {
    @SuppressWarnings("rawtypes")
    final List<Future> futures = new ArrayList<>();
    final var alreadyDefined = new HashMap<String, Integer>();
    final var iter = ids.iterator();
    for (var i = 0; iter.hasNext(); i++) {
      final var id = iter.next();
      if (id == null) {
        iter.remove();
      } else if (alreadyDefined.containsKey(id)) {
        final var index = alreadyDefined.get(id);
        return this.failFieldElement(name, i, "The '" + id + "' is already defied at' " + index + "'.");
      } else {
        alreadyDefined.put(id, i);
        final var key = this.generateKey(id, type);
        if (!this.idsCache.contains(key)) {
          final var index = i;
          futures.add(search.apply(id).transform(defined -> {
            if (defined.failed()) {
              return this.failFieldElement(name, index, defined.cause());
            } else if (defined.result() == true) {
              this.idsCache.add(key);
              return Future.succeededFuture();
            } else {
              return this.failFieldElement(name, index, "The '" + id + "' is not associated to any model.");
            }
          }));
        }
      }
    }
    if (futures.isEmpty()) {
      return future;
    } else {
      futures.add(0, future);
      return CompositeFuture.all(futures).map(any -> null);
    }
  }
public Future < Void > validateDefinedIdsField ( final String name , final Iterable < String > ids , final Class < ? > type , final Function < String , Future < Boolean > > search , final Future < Void > future ) { @ SuppressWarnings ( "rawtypes" ) final List < Future > futures = new ArrayList < > ( ) ; final var alreadyDefined = new HashMap < String , Integer > ( ) ; final var iter = ids . iterator ( ) ; for ( var i = 0 ; iter . hasNext ( ) ; i ++ ) { final var id = iter . next ( ) ; if ( id == null ) { iter . remove ( ) ; } else if ( alreadyDefined . containsKey ( id ) ) { final var index = alreadyDefined . get ( id ) ; return this . failFieldElement ( name , i , "The '" + id + "' is already defied at' " + index + "'." ) ; } else { alreadyDefined . put ( id , i ) ; final var key = this . generateKey ( id , type ) ; if ( ! this . idsCache . contains ( key ) ) { final var index = i ; futures . add ( search . apply ( id ) . transform ( defined -> { if ( defined . failed ( ) ) { return this . failFieldElement ( name , index , defined . cause ( ) ) ; } else if ( defined . result ( ) == true ) { this . idsCache . add ( key ) ; return Future . succeededFuture ( ) ; } else { return this . failFieldElement ( name , index , "The '" + id + "' is not associated to any model." ) ; } } ) ) ; } } } if ( futures . isEmpty ( ) ) { return future ; } else { futures . add ( 0 , future ) ; return CompositeFuture . all ( futures ) . map ( any -> null ) ; } }
Check if a field contains a defined model.
Check if a field contains a defined model .