/**
* Copyright 1999-2009 The Pegadi Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pegadi.articlelist;
import org.pegadi.model.Article;
import org.pegadi.model.Section;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;
public class DepartmentGrouper implements Grouper {
private String name;
private final Logger log = LoggerFactory.getLogger(getClass());
public boolean equals(Object obj) {
return obj instanceof DepartmentGrouper;
}
public DepartmentGrouper(String name) {
this.name = name;
}
public DepartmentGrouper() {
this("Section");
}
public String toString() {
return this.getName();
}
public String getName() {
return name;
}
public GroupedArticles group(Article[] articles, Comparator comparator) {
// first, lets find all departments in the incoming set.
// we use TreeSet, since this class doesn't permit duplicates,
// and also automagically sorts the objects for us, for the given comparator.
ArrayList[] tempArrayListOut;
String[] tempHeaders;
ArticleSum[] tempSums;
TreeSet actualSortObjects = new TreeSet();
boolean hasNulls = false;
GroupedArticles groupedArticles = new GroupedArticles();
ArrayList groupIndexes;
for (Article article1 : articles) {
if (article1 != null) {
if (article1.getSection() != null)
actualSortObjects.add(article1.getSection());
else
hasNulls = true;
}
}
// then, we convert the TreeSet to a Vector
// so that we get a sorted and indexed list of departments.
// if there are articles without Section, create a "empty"
// folder at the end.
groupIndexes = new ArrayList(actualSortObjects);
if (hasNulls)
groupIndexes.add(new Section(0, "No department", ""));
// initalize the ArrayLists
int numberOfGroups = groupIndexes.size();
groupedArticles.setNumberOfGroups(numberOfGroups);
tempArrayListOut = new ArrayList[numberOfGroups];
for (int i = 0; i < tempArrayListOut.length; i++)
tempArrayListOut[i] = new ArrayList();
// now, iterate over all the articles, and duplicating
// them into the correct ArrayList
for (Article article : articles) {
if (article != null) {
Section tempSection = article.getSection();
int tempIndex = groupIndexes.indexOf(tempSection);
if (tempIndex == -1) // no such department, put it in "empt dep."
tempArrayListOut[numberOfGroups - 1].add(article);
else
tempArrayListOut[tempIndex].add(article);
}
}
// Sort all the arraylists
for (ArrayList aTempArrayListOut : tempArrayListOut) Collections.sort(aTempArrayListOut, comparator);
// update the group headers
tempHeaders = new String[numberOfGroups];
for (int i = 0; i < numberOfGroups; i++) {
Section tempSection2 = (Section) groupIndexes.get(i);
tempHeaders[i] = tempSection2.getName();
}
groupedArticles.setGroupHeaders(tempHeaders);
// update the group sums
tempSums = new ArticleSum[numberOfGroups];
for (int i = 0; i < numberOfGroups; i++) {
tempSums[i] = new ArticleSum();
for (Object o : tempArrayListOut[i]) {
Article tempArticle = (Article) o;
int tempInt = tempArticle.getWantedNumberOfCharacters();
tempSums[i].add(tempInt);
}
}
// update, and return the results.
groupedArticles.setGroupedArticles(tempArrayListOut);
groupedArticles.setGroupHeaders(tempHeaders);
groupedArticles.setGroupSums(tempSums);
return groupedArticles;
}
}