예제 #1
0
 private boolean userExist(String registerKey, UserType userType) {
   String[] authenticateKeysForLog = {registerKey, userType.name()};
   Logger.getLogger(UsersResource.class.getName())
       .log(
           Level.INFO,
           "######### Check if user exists: registerKey: {0}, userType: {1}.",
           authenticateKeysForLog);
   EntityManager em = emf.createEntityManager();
   if (UserType.MOBILE.equals(userType)) {
     Query queryQ = em.createNamedQuery("User.findByPhoneNumber");
     queryQ.setParameter("phoneNumber", registerKey);
     return !queryQ.getResultList().isEmpty();
   }
   if (UserType.DUDU.equals(userType)) {
     Query queryQ = em.createNamedQuery("User.findByUserId");
     queryQ.setParameter("userId", registerKey);
     return !queryQ.getResultList().isEmpty();
   }
   if (UserType.QQ.equals(userType)) {
     Query queryQ = em.createNamedQuery("User.findByQQ");
     queryQ.setParameter("qq", registerKey);
     return !queryQ.getResultList().isEmpty();
   }
   if (UserType.WEIBO.equals(userType)) {
     Query queryQ = em.createNamedQuery("User.findByWeibo");
     queryQ.setParameter("weibo", registerKey);
     return !queryQ.getResultList().isEmpty();
   }
   if (UserType.EMAIL.equals(userType)) {
     Query queryQ = em.createNamedQuery("User.findByEmail");
     queryQ.setParameter("email", registerKey);
     return !queryQ.getResultList().isEmpty();
   }
   return true;
 }
예제 #2
0
  /**
   * Method for user register
   *
   * @param user
   * @return
   */
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  public Response postUser(final User user) {
    String[] userKeysForLog = {
      user.getQq(), user.getWeibo(), user.getEmail(), user.getPhoneNumber()
    };
    Logger.getLogger(UsersResource.class.getName())
        .log(
            Level.INFO,
            "######### User registeration: QQ: {0}, Weibo: {1}, Mail: {2}, Mobile: {3}.",
            userKeysForLog);
    boolean userExist = false;

    switch (user.getUserType()) {
      case QQ:
        userExist = userExist(user.getQq(), user.getUserType());
        break;
      case WEIBO:
        userExist = userExist(user.getWeibo(), user.getUserType());
        break;
      case MOBILE:
        userExist = userExist(user.getPhoneNumber(), user.getUserType());
        break;
      case EMAIL:
        userExist = userExist(user.getEmail(), user.getUserType());
        break;
      default:
        break;
    }
    if (userExist) {
      Logger.getLogger(UsersResource.class.getName())
          .log(
              Level.WARNING,
              "######### User registeration failed: QQ: {0}, Weibo: {1}, Mail: {2}, Mobile: {3}.",
              userKeysForLog);
      return Response.ok(AuthenticationConstants.DUPLICATEUSERID).entity(user).build();
    }
    if (user.getUserType() == null || UserType.DUDU.equals(user.getUserType())) {
      Logger.getLogger(UsersResource.class.getName())
          .log(
              Level.WARNING,
              "######### User registeration failed: QQ: {0}, Weibo: {1}, Mail: {2}, Mobile: {3}.",
              userKeysForLog);
      return Response.ok(AuthenticationConstants.USERTYPEWRONG).entity(user).build();
    }

    if (user.getUserName() != null) {
      user.setUserName(user.getUserName().trim());
    }
    final EntityManager em = emf.createEntityManager();
    TransactionManager.manage(
        new Transactional(em) {
          @Override
          public void transact() {
            em.persist(user);
            Calendar today = Calendar.getInstance();
            today.setTime(new Date());
            Query queryCD = em.createNamedQuery("CouponDefinition.findValidCoupon");
            queryCD.setParameter("expiryDate", today);
            List<CouponDefinition> couponDefinitions = queryCD.getResultList();
            for (CouponDefinition cd : couponDefinitions) {
              if (cd.isForAllUser()) {
                UserCoupon userCoupon = new UserCoupon(user.getUserId());
                userCoupon.setUser(user);
                userCoupon.setCouponDefinitionNumber(cd.getCouponDefinitionNumber());
                userCoupon.setCouponDefinition(cd);
                em.persist(userCoupon);
              }
            }
          }
        });
    return Response.ok(AuthenticationConstants.REGISTERSUCCESS).entity(user).build();
  }