コード例 #1
0
  @GET
  @Produces(MediaType.APPLICATION_XML)
  @Path("isRegistered/{email}/{password}")
  public boolean isRegistered(
      @PathParam("email") String email, @PathParam("password") String password) {
    System.out.println("Method: isRegistered"); // test

    createEntityManager();

    try {
      // TO TRY
      // Query q = em.createQuery("select u from User u where u.email = ?1 and u.password = ?2");
      // q.setParameter(1, email);
      // q.setParameter(2, password);

      Query q =
          entityManager.createQuery(
              "select u from User u where u.email ='"
                  + email
                  + "' and u.password = '******'");
      User user = (User) q.getSingleResult();

      if (user.getEmail().equals(email) && user.getPassword().equals(password)) {
        return true;
      } else {
        return false;
      }
    } catch (Exception e) {
      // If there is no user
      return false;
    }
  }
コード例 #2
0
  @POST
  @Consumes(MediaType.APPLICATION_XML)
  public boolean registerNewUser(User user) {
    System.out.println("Method: registerNewUser"); // test

    createEntityManager();

    // Verify that the user doesn't exist already
    if (isRegistered(user.getEmail(), user.getPassword())) return false;

    try {
      entityManager.getTransaction().begin();
      entityManager.persist(user);
      entityManager.getTransaction().commit();
    } catch (Exception e) {
      return false;
    }

    return true;
  }