Coverage Report - org.jtheque.films.view.impl.sort.ByKindSorter
 
Classes in this File Line Coverage Branch Coverage Complexity
ByKindSorter
0 %
0/14
0 %
0/10
3
 
 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.primary.od.able.Kind;
 23  
 import org.jtheque.primary.services.able.IKindsService;
 24  
 import org.jtheque.primary.view.impl.models.tree.Category;
 25  
 import org.jtheque.primary.view.impl.models.tree.JThequeTreeModel;
 26  
 import org.jtheque.primary.view.impl.models.tree.TreeElement;
 27  
 import org.jtheque.primary.view.impl.sort.Sorter;
 28  
 
 29  
 import javax.annotation.Resource;
 30  
 import java.util.ArrayList;
 31  
 import java.util.List;
 32  
 
 33  
 /**
 34  
  * a sorter to sort by kind.
 35  
  *
 36  
  * @author Baptiste Wicht
 37  
  */
 38  0
 public final class ByKindSorter implements Sorter {
 39  
     @Resource
 40  
     private IFilmController filmController;
 41  
 
 42  
     @Override
 43  
     public boolean canSort(String content, String sortType) {
 44  0
         return IFilmsService.DATA_TYPE.equals(content) && sortType.equals(IKindsService.DATA_TYPE);
 45  
     }
 46  
 
 47  
     @Override
 48  
     public void sort(JThequeTreeModel model) {
 49  0
         TreeElement root = model.getRoot();
 50  
 
 51  0
         List<Kind> groups = new ArrayList<Kind>(10);
 52  
 
 53  0
         for (Film film : filmController.getDisplayList()) {
 54  0
             for (Kind kind : film.getKinds()) {
 55  0
                 if (!groups.contains(kind)) {
 56  0
                     root.add(new Category(kind.getElementName()));
 57  0
                     groups.add(kind);
 58  
                 }
 59  
 
 60  0
                 int index = groups.indexOf(kind);
 61  
 
 62  0
                 root.getChild(index).add(film);
 63  0
             }
 64  
         }
 65  
 
 66  0
         model.setRootElement(root);
 67  0
     }
 68  
 }