Esempio n. 1
0
 @GET
 @Path("/{userId}")
 public UserVO findByUserId(@PathParam("userId") Integer userId) {
   UserVO u = new UserVO();
   User user = null;
   EntityManager em = EntityManagerLoaderListener.createEntityManager();
   try {
     TypedQuery<User> query = em.createQuery("FROM User WHERE userId=:userId", User.class);
     query.setParameter("userId", userId);
     user = query.getSingleResult();
     BeanUtils.populate(u, BeanUtils.describe(user));
     if (user.getAccount() != null) {
       u.setAccountId(user.getAccount().getAccountId());
     } else {
       System.out.println("ERROR a user MUST be bound to an existing account");
     }
   } catch (NoResultException nre) {
     System.out.println("No result found for userId=" + userId);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     em.close();
   }
   return u;
 }
Esempio n. 2
0
  /**
   * Execute search request on User entity and return the ValueObject list
   *
   * @param queryString jpql query
   * @param parameters jpql parameters
   * @return list of UserVO
   */
  private List<UserVO> executeUserRequestList(
      String queryString, HashMap<String, Object> parameters) {
    List<User> users = null;
    List<UserVO> vos = new ArrayList<UserVO>();
    EntityManager em = EntityManagerLoaderListener.createEntityManager();
    try {

      TypedQuery<User> query = em.createQuery(queryString, User.class);
      if (parameters != null) {
        Iterator<String> it = parameters.keySet().iterator();
        while (it.hasNext()) {
          String pName = it.next();
          query.setParameter(pName, parameters.get(pName));
        }
      }
      query.setMaxResults(Constants.MAX_RESULT);
      users = query.getResultList();

      /* Populate the value object */
      for (User user : users) {
        UserVO u = new UserVO();
        BeanUtils.populate(u, BeanUtils.describe(user));
        u.setAccountId(user.getAccount().getAccountId());
        vos.add(u);
      }
    } catch (NoResultException nre) {
      System.out.println("No result found while search users " + nre.getMessage());
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      em.close();
    }
    return vos;
  }
Esempio n. 3
0
 @POST
 @Consumes(MediaType.APPLICATION_JSON)
 public Response registerUser(UserVO user) {
   System.out.println("Register " + user);
   EntityManager em = EntityManagerLoaderListener.createEntityManager();
   try {
     User u = new User();
     Account account = null;
     BeanUtils.populate(u, BeanUtils.describe(user));
     u.setUserId(null); // forced
     em.getTransaction().begin();
     if (user.getAccountId() == null) {
       System.out.println("Create a new account.");
       user.setNewUser(true);
       account = new Account();
       em.persist(account);
     } else {
       TypedQuery<Account> tQ =
           em.createQuery("FROM Account where accountId=:accountId", Account.class);
       tQ.setParameter("accountId", user.getAccountId());
       account = tQ.getSingleResult();
     }
     u.setAccount(account);
     em.persist(u);
     em.getTransaction().commit();
     user.setAccountId(u.getAccount().getAccountId());
     user.setUserId(u.getUserId());
   } catch (NoResultException nre) {
     System.out.println("ERROR no account found with accountId: " + user.getAccountId());
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     em.close();
   }
   return Response.created(URI.create("/services/users/" + user.getUserId())).entity(user).build();
 }