Esempio n. 1
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();
 }