Example #1
0
  public static TestUser getInstance() {
    if (instance != null) return instance;

    EntityManager entityManager =
        Persistence.createEntityManagerFactory(UserPersistenceUnit.NAME).createEntityManager();

    entityManager.getTransaction().begin();

    Address address =
        new Address("21 Blue street", "Chicago", "78801", "John", "Doe", "M.", null, "FRA");
    user1 =
        new User(
            "*****@*****.**", "test", "John", "Doe", "+33616161616", null, yesterday, "fr_FR", null);
    user1.setGender("M.");

    entityManager.persist(address);
    entityManager.persist(user1);

    Role adminRole = new Role(RoleName.admin);
    Role userRole = new Role(RoleName.user);
    entityManager.persist(adminRole);
    entityManager.persist(userRole);

    entityManager.getTransaction().commit();

    instance = new TestUser();
    entityManager.close();
    return instance;
  }
Example #2
0
  @Override
  public User authenticate(String username, String password) {
    try {
      DBConnectionFactory myFactory = DBConnectionFactory.getInstance(DAOFactory.MYSQL);
      Connection conn = myFactory.getConnection();
      PreparedStatement pstmt =
          conn.prepareStatement("select * from users where username = ? and userpassword = ?");
      pstmt.setString(1, username);
      pstmt.setString(2, password);
      ResultSet rs = pstmt.executeQuery();
      User result = null;

      while (rs.next()) {
        result = new User();
        result.setId(rs.getInt("idusers"));
        result.setUsername(rs.getString("username"));
        result.setUserpassword(rs.getString("userpassword"));
        result.setUsertypeid(rs.getInt("usertypeid"));
        result.setAge(rs.getInt("age"));
        result.setGender(rs.getString("gender"));
        result.setLocation(rs.getString("location"));
        result.setBirthday(rs.getString("birthday"));
      }

      conn.close();
      return result;
    } catch (SQLException ex) {
      Logger.getLogger(UserDAO_MySQL.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
  }
Example #3
0
  private static User getUser(Node node) {
    // XMLReaderDOM domReader = new XMLReaderDOM();
    User user = new User();
    if (node.getNodeType() == Node.ELEMENT_NODE) {
      Element element = (Element) node;
      user.setName(getTagValue("name", element));
      user.setAge(Integer.parseInt(getTagValue("age", element)));
      user.setGender(getTagValue("gender", element));
      user.setRole(getTagValue("role", element));
    }

    return user;
  }
Example #4
0
 private User addUser(
     String first, String last, int age, Address mainAddress, Address... addresses) {
   User user = new User(first, last, age, new Date());
   user.setGender(Gender.MALE);
   user.setMainAddress(mainAddress);
   for (Address address : addresses) {
     user.addAddress(address);
   }
   for (User u : users) {
     user.addFriend(u);
   }
   if (!users.isEmpty()) {
     user.setFriend(users.get(users.size() - 1));
   }
   ds.save(user);
   users.add(user);
   return user;
 }
Example #5
0
 public User getModel() {
   user.setGender(Gender.BOY);
   user.setUsername("mmmmm");
   user.setPassword("ppppp");
   return user;
 }
Example #6
0
  private boolean scanUserFriends(long id) throws IOException {

    long now = new Date().getTime();

    User user = dao.fetchUser(id);
    if (user != null) {
      Timestamp lastScan = user.getLastScan();
      if (lastScan != null) {
        long diff = now - lastScan.getTime();
        // week
        if (diff < 1000 * 7 * 86400) {
          System.out.println("User " + id + " was scanned recently, skip");
          return false;
        }
      }
    }
    System.out.println("Scan user " + id);

    List<Friend> friends = FriendFetcher.requestFriends(id, COOKIE);

    if (friends.size() == 0) {
      return true;
    }

    List<Long> ids = friends.stream().map((f) -> f.id).collect(Collectors.toList());
    List<Long> existing =
        dao.fetchUsers(ids).stream().map(User::getId).collect(Collectors.toList());

    List<User> toUpdate = new ArrayList<>();
    List<User> toInsert = new ArrayList<>();
    Timestamp current = new Timestamp(now);
    friends.forEach(
        (f) -> {
          User u = new User();
          u.setGender(f.gender);
          u.setId(f.id);
          u.setLastModified(current);
          u.setName(f.name);
          u.setPic(f.pic);
          u.setUniversityId(f.university);
          u.setWorkplace(f.workplace);
          if (existing.contains(f.id)) {
            toUpdate.add(u);
          } else {
            toInsert.add(u);
          }
        });

    if (toUpdate.size() > 0) {
      dao.updateUsers(toUpdate);
    }
    if (toInsert.size() > 0) {
      dao.insertUsers(toInsert);
    }

    List<Long> currentFriends = dao.fetchFriends(id);

    List<CommonFriend> friendsToUpdate = new ArrayList<>();
    List<CommonFriend> friendsToAdd = new ArrayList<>();

    List<Long> newFriends = friends.stream().map((f) -> f.id).collect(Collectors.toList());
    List<Long> friendsToDelete =
        currentFriends.stream().filter((f) -> !newFriends.contains(f)).collect(Collectors.toList());

    friends.forEach(
        (f) -> {
          CommonFriend common = new CommonFriend();
          common.setOwnerId(id);
          common.setFriendId(f.id);
          common.setGroupId(f.groupId);
          common.setLastUpdated(current);
          if (currentFriends.contains(f.id)) {
            friendsToUpdate.add(common);
          } else {
            friendsToAdd.add(common);
          }
        });

    if (friendsToDelete.size() > 0) {
      dao.deleteFriends(id, friendsToDelete);
    }
    if (friendsToAdd.size() > 0) {
      dao.insertFriends(friendsToAdd);
    }
    if (friendsToUpdate.size() > 0) {
      dao.updateFriends(friendsToUpdate);
    }

    if (user != null) {
      dao.updateLastScan(id, current);
    }

    return true;
  }