/**
* 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 no.dusken.common.model.Person;
import org.pegadi.model.Article;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;
public class JournalistGrouper implements Grouper {
private String name;
public JournalistGrouper(String name) {
this.name = name;
}
public JournalistGrouper() {
this("Journalist");
}
public String toString() {
return this.getName();
}
public String getName() {
return name;
}
public boolean equals(Object obj) {
return obj instanceof JournalistGrouper;
}
public GroupedArticles group(Article[] articles, Comparator comparator) {
// first, lets find all journalists 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.getJournalist() != null)
actualSortObjects.add(article1.getJournalist());
else
hasNulls = true;
}
// then, we convert the TreeSet to a Vector
// so that we get a sorted and indexed list of journalists.
// if there are articles without Journalist, create a "empty"
// folder at the end.
groupIndexes = new ArrayList(actualSortObjects);
if (hasNulls) {
// FIXME: I18N
Person u = new Person();
u.setFirstname("No journalist");
groupIndexes.add(u);
}
// 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) {
Person tempJournalist = article.getJournalist();
int tempIndex = groupIndexes.indexOf(tempJournalist);
if (tempIndex == -1) // no such journalist, 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++) {
Person tempJournalist2 = (Person) groupIndexes.get(i);
tempHeaders[i] = tempJournalist2.getFullName();
}
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;
}
}