Coverage Report - org.jtheque.films.view.impl.sort.ByYearSorter
 
Classes in this File Line Coverage Branch Coverage Complexity
ByYearSorter
0 %
0/14
0 %
0/8
2.5
 
 1  
 package org.jtheque.films.view.impl.sort;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.films.controllers.able.IFilmController;
 20  
 import org.jtheque.films.persistence.od.able.Film;
 21  
 import org.jtheque.films.services.able.IFilmsService;
 22  
 import org.jtheque.films.utils.Constants;
 23  
 import org.jtheque.primary.view.impl.models.tree.Category;
 24  
 import org.jtheque.primary.view.impl.models.tree.JThequeTreeModel;
 25  
 import org.jtheque.primary.view.impl.models.tree.TreeElement;
 26  
 import org.jtheque.primary.view.impl.sort.Sorter;
 27  
 
 28  
 import javax.annotation.Resource;
 29  
 import java.util.HashMap;
 30  
 import java.util.Map;
 31  
 
 32  
 /**
 33  
  * A sorter to sort by year.
 34  
  *
 35  
  * @author Baptiste Wicht
 36  
  */
 37  0
 public final class ByYearSorter implements Sorter {
 38  
     @Resource
 39  
     private IFilmController filmController;
 40  
 
 41  
     @Override
 42  
     public boolean canSort(String content, String sortType) {
 43  0
         return content.equals(IFilmsService.DATA_TYPE) && sortType.equals(Constants.YEAR);
 44  
     }
 45  
 
 46  
     @Override
 47  
     public void sort(JThequeTreeModel model) {
 48  0
         TreeElement root = model.getRoot();
 49  
 
 50  0
         Map<Integer, Category> groups = new HashMap<Integer, Category>(10);
 51  
 
 52  0
         for (Film film : filmController.getDisplayList()) {
 53  0
             int year = film.getYear();
 54  
 
 55  0
             if (!groups.containsKey(year)) {
 56  0
                 Category category = new Category(Integer.toString(year));
 57  
 
 58  0
                 root.add(category);
 59  0
                 groups.put(year, category);
 60  
             }
 61  
 
 62  0
             groups.get(year).add(film);
 63  0
         }
 64  
 
 65  0
         model.setRootElement(root);
 66  0
     }
 67  
 }