private static Either<String, RegistrationsUpdate> registrationsUpdate(JsonNode jsonNode) {
    Either<String, BillingCompany> billingCompany =
        ArgoUtils.stringValue(jsonNode, "billingCompany").right().map(BillingCompany::new);
    Either<String, Badge> badge = ArgoUtils.stringValue(jsonNode, "badge").right().map(Badge::new);
    Either<String, String> dietaryRequirements =
        ArgoUtils.stringValue(jsonNode, "dietaryRequirements");
    Either<String, Option<String>> bundle =
        ArgoUtils.stringValue(jsonNode, "bundle")
            .right()
            .map(Option::fromNull)
            .right()
            .map(r -> r.filter(Strings.isNotNullOrEmpty));

    return bundle
        .right()
        .apply(
            dietaryRequirements
                .right()
                .apply(
                    badge
                        .right()
                        .apply(
                            billingCompany
                                .right()
                                .apply(Either.right(Function.curry(RegistrationsUpdate::new))))));
  }
  @POST
  @Path("/{registrationId}")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Response postUpdate(
      @Context HttpServletRequest request, @PathParam("registrationId") UUID id, String body)
      throws Exception {
    authService.guardAuthenticatedUser(request);
    Either<String, RegistrationsUpdate> map =
        ArgoUtils.parseEither(body)
            .right()
            .map(x -> x.getNode("tuple"))
            .right()
            .bind(RegistrationsRestService::registrationsUpdate);

    if (map.isLeft()) return Response.serverError().build();
    RegistrationsUpdate ru = map.right().value();

    try (Connection c = dataSource.getConnection()) {
      c.setAutoCommit(false);
      if (!registrationsSqlMapper.one(c, id).isSome())
        return Response.status(Status.NOT_FOUND).build();
      registrationsSqlMapper.update(
          c, id, ru.billingCompany, ru.badge, ru.dietaryRequirements, ru.bundle);
      c.commit();
    }
    return Response.ok().build();
  }
 private static <S, T> F<S, Either<S, T>> eitherK(final F<S, T> func) {
   return s -> {
     try {
       T res = func.f(s);
       return (res == null) ? Either.left(s) : Either.right(res);
     } catch (Exception ex) {
       System.out.println("* " + ex.getMessage());
       return Either.left(s);
     }
   };
 }
Beispiel #4
0
 /**
  * Sequences through the right side of the either monad with a list of values.
  *
  * @param a The list of values to sequence with the either monad.
  * @return A sequenced value.
  */
 public static <B, X> Either<X, List<B>> sequenceRight(final List<Either<X, B>> a) {
   return a.isEmpty()
       ? Either.<X, List<B>>right(List.<B>nil())
       : a.head()
           .right()
           .bind(
               new Func<B, Either<X, List<B>>>() {
                 public Either<X, List<B>> f(final B bb) {
                   return sequenceRight(a.tail()).right().map(cons_(bb));
                 }
               });
 }
Beispiel #5
0
 /**
  * Takes an <code>Either</code> to its contained value within left or right.
  *
  * @param e The either to reduce.
  * @return An <code>Either</code> to its contained value within left or right.
  */
 public static <A> A reduce(final Either<A, A> e) {
   return e.isLeft() ? e.left().value() : e.right().value();
 }
Beispiel #6
0
 /**
  * Binds the given function across this projection's value if it has one.
  *
  * @param f The function to bind across this projection.
  * @return A new either value after binding.
  */
 public <X> Either<X, B> bind(final Func<A, Either<X, B>> f) {
   return isLeft() ? f.f(value()) : new Right<X, B>(e.right().value());
 }
Beispiel #7
0
 /**
  * Map the given function across this projection's value if it has one.
  *
  * @param f The function to map across this projection.
  * @return A new either value after mapping.
  */
 public <X> Either<X, B> map(final Func<A, X> f) {
   return isLeft() ? new Left<X, B>(f.f(value())) : new Right<X, B>(e.right().value());
 }
Beispiel #8
0
 /**
  * The value of this projection or the result of the given function on the opposing projection's
  * value.
  *
  * @param f The function to execute if this projection has no value.
  * @return The value of this projection or the result of the given function on the opposing
  *     projection's value.
  */
 public A on(final Func<B, A> f) {
   return isLeft() ? value() : f.f(e.right().value());
 }
Beispiel #9
0
 /**
  * Returns an either projection of this optional value; the given argument in <code>Left</code> if
  * no value, or the value in <code>Right</code>.
  *
  * @param x The value to return in left if this optional value has no value.
  * @return An either projection of this optional value.
  */
 public <X> Either<X, A> toEither(final X x) {
   return isSome() ? Either.<X, A>right(some()) : Either.<X, A>left(x);
 }
Beispiel #10
0
 /**
  * Returns an either projection of this array; the given argument in <code>Left</code> if empty,
  * or the first element in <code>Right</code>.
  *
  * @param x The value to return in left if this array is empty.
  * @return An either projection of this array.
  */
 @SuppressWarnings("unchecked")
 public <X> Either<X, A> toEither(final P1<X> x) {
   return a.length == 0 ? Either.<X, A>left(x._1()) : Either.<X, A>right((A) a[0]);
 }