Example #1
0
 public List<Movie> getSearchResults() {
   String[] tokens = query.split(" ");
   List<Movie> movies = em.createNamedQuery("Movie.findAll").getResultList();
   HashSet<String> removeDups = new HashSet<>();
   for (String s : tokens) {
     boolean skip = false;
     for (String skipWord : skipWords) {
       if (skipWord.equalsIgnoreCase(s)) {
         skip = true;
         break;
       }
     }
     if (!skip) {
       removeDups.add(s);
     }
   }
   List<String> searchTokens = new LinkedList<>();
   for (String s : removeDups) {
     searchTokens.add(s);
   }
   PriorityQueue<MovieValueTuple> returnList = new PriorityQueue<>();
   for (Movie m : movies) {
     MovieValueTuple movieValue = new MovieValueTuple(m, 0);
     for (String s : searchTokens) {
       if (m.getName().toLowerCase().contains(s.toLowerCase())) {
         movieValue.two(movieValue.two() + 2);
       }
       for (Actor a : m.getActors()) {
         if (a.getName().toLowerCase().contains(s.toLowerCase())) {
           movieValue.two(movieValue.two() + 1);
         }
       }
       if (m.getType().toLowerCase().contains(s.toLowerCase())) {
         movieValue.two(movieValue.two() + 1);
       }
     }
     if (movieValue.two() > 0) {
       returnList.offer(movieValue);
     }
   }
   List<Movie> retVal = new LinkedList<>();
   for (MovieValueTuple mvt : returnList) {
     retVal.add(mvt.one());
   }
   return retVal;
 }