@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);
     }
   };
 }
  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))))));
  }
  @SafeVarargs
  public static Either<String, Date> parseDate(
      Either<String, Date> date, F<String, Date>... funcList) {

    for (F<String, Date> func : funcList) {
      date = date.left().bind(eitherK(func));
    }
    return date;
  }
Exemple #5
0
 /**
  * Sequences through the left 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 <A, X> Either<List<A>, X> sequenceLeft(final List<Either<A, X>> a) {
   return a.isEmpty()
       ? Either.<List<A>, X>left(List.<A>nil())
       : a.head()
           .left()
           .bind(
               new Func<A, Either<List<A>, X>>() {
                 public Either<List<A>, X> f(final A aa) {
                   return sequenceLeft(a.tail()).left().map(cons_(aa));
                 }
               });
 }
Exemple #6
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));
                 }
               });
 }
  public static void main(String... args) {
    F<String, F<String, Date>> simpleDate =
        df ->
            s -> {
              try {
                return new SimpleDateFormat(df).parse(s);
              } catch (Exception e) {
                throw new RuntimeException(e);
              }
            };

    Either<String, Date> res =
        parseDate(
            Either.left(args[0]),
            s -> Date.from(LocalDateTime.parse(s).toInstant(ZoneOffset.UTC)),
            s -> Date.from(OffsetDateTime.parse(s).toInstant()),
            s -> Date.from(ZonedDateTime.parse(s).toInstant()),
            simpleDate.f("yyyy-MM-dd HH:mm:ss"),
            s -> "now".equals(s) ? new Date() : null);

    System.out.println("------");

    System.out.println(res);
  }
  /**
   * Entry point
   *
   * @param args - config_file source_key harvest_tech_id
   * @throws Exception
   */
  public static void main(final String[] args) {
    try {
      if (args.length < 1) {
        System.out.println("CLI: config_file");
        System.exit(-1);
      }
      System.out.println("Running with command line: " + Arrays.toString(args));
      final Config config = ConfigFactory.parseFile(new File(args[0]));

      final DataImportManagerModule app =
          ModuleUtils.initializeApplication(
              Arrays.asList(new Module()),
              Optional.of(config),
              Either.left(DataImportManagerModule.class));
      app.start();
    } catch (Throwable e) {
      _logger.error(ErrorUtils.getLongForm("Exception reached main(): {0}", e));
      try {
        e.printStackTrace();
      } catch (Exception e2) { // the exception failed!
      }
      System.exit(-1);
    }
  }
Exemple #9
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());
 }
Exemple #10
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();
 }
Exemple #11
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);
 }
Exemple #12
0
 /**
  * Returns the value of this projection or fails with the given error message.
  *
  * @param err The error message to fail with.
  * @return The value of this projection
  */
 public A valueE(final P1<String> err) {
   if (e.isLeft())
     //noinspection CastToConcreteClass
     return ((Left<A, B>) e).a;
   else throw error(err._1());
 }
Exemple #13
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<A, X> bind(final Func<B, Either<A, X>> f) {
   return isRight() ? f.f(value()) : new Left<A, X>(e.left().value());
 }
Exemple #14
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<A, X> map(final Func<B, X> f) {
   return isRight() ? new Right<A, X>(f.f(value())) : new Left<A, X>(e.left().value());
 }
Exemple #15
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 B on(final Func<A, B> f) {
   return isRight() ? value() : f.f(e.left().value());
 }
Exemple #16
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());
 }
Exemple #17
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());
 }
Exemple #18
0
 /**
  * Returns the value of this projection or fails with the given error message.
  *
  * @param err The error message to fail with.
  * @return The value of this projection
  */
 public B valueE(final P1<String> err) {
   if (e.isRight())
     //noinspection CastToConcreteClass
     return ((Right<A, B>) e).b;
   else throw error(err._1());
 }
Exemple #19
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]);
 }