@Override
  public RandomList getRandomList(User user) {
    int year = Calendar.getInstance().get(Calendar.YEAR);
    int fromYear = Util.randInt(daoAlbum.findOldestAlbum(user.getLogin()), year);
    int toYear = fromYear + 10; // Util.randInt( fromYear, year );
    List<MDOAlbum> albums = daoAlbum.findAlbumsBasedOnDate(user.getLogin(), fromYear, toYear);

    if (albums != null && albums.size() > 0) {
      RandomList rl = new RandomList("RandomListName-Date", "RandomListTitle-Date");
      rl.addDetail(fromYear + " - " + toYear);

      for (int i = 0; i < albums.size() && rl.getSongs().size() < MAX_ELEMENTS; i++) {
        List<MDOSong> songs = albums.get(i).getSongs();
        for (int j = 0; j < songs.size() && rl.getSongs().size() < MAX_ELEMENTS; j++) {
          MDOSong mdoSong = songs.get(j);
          Song song = new Song(mdoSong, true, true);
          rl.addSong(song);
        }
      }

      long seed = System.nanoTime();
      if (rl.getSongs() != null) {
        Collections.shuffle(rl.getSongs(), new Random(seed));
      }
      return rl;
    }
    return null;
  }
Beispiel #2
0
 /**
  * Return the current user logged. Throws an exception if nobody logged depending on the parameter
  * completeUser, the function will return an {@link User} with only the username, or an {@link
  * User} with all the information.
  *
  * @param completeUser boolean flag to know if the returned user must be complete with all the
  *     user information or just the login information
  * @param daoUser {@link DAOUser} this param is only necessary when param completeUser is true. If
  *     not, it can be null
  * @return {@link User} user logged
  * @throws NotAuthorizedMessicRESTException
  */
 public static User getCurrentUser(boolean completeUser, DAOUser daoUser)
     throws NotAuthorizedMessicRESTException {
   Authentication auth = SecurityContextHolder.getContext().getAuthentication();
   if (auth != null && auth.getPrincipal().equals("anonymousUser")) {
     return null;
   }
   if (auth != null) {
     if (!completeUser) {
       User user = new User();
       user.setLogin(auth.getName());
       return user;
     } else {
       MDOUser mdoUser = daoUser.getUserByLogin(auth.getName());
       User user = new User(mdoUser);
       return user;
     }
   } else {
     return null;
   }
 }