throws RemoteException {
for (Person person : persons) {
BigDecimal grandTotalPoints = BigDecimal.ZERO;
boolean include = false;
ResultsRow row = new ResultsRow();
row.addCell(person.getNames());
row.addCell(person.getTown());
row.addCell(person.getSchool());
row.addCell(Integer.toString(person.getSchoolYear()));
for (Contest nextContest : selectedContests) {
boolean foundLoginForContest = false;
if (person.getLogins() != null) {
for (String login : person.getLogins()) {
Hashtable<String, UserContestData> contestData = reportData.get(nextContest
.getId());
if (contestData.containsKey(login)) {
row.append(contestData.get(login).getData());
grandTotalPoints = grandTotalPoints.add(contestData.get(login)
.getTotalPoints());
contestData.remove(login);
foundLoginForContest = true;
break;
}
}
}
if (foundLoginForContest) {
include = true;
} else {
row.addCell("-");
for (Task task : nextContest.getTasks()) {
for (int i = 0; i < task.getNumberOfTests(); ++i) {
row.addCell("-");
}
row.addCell("-");
}
row.addCell("0");
}
}
row.addCell(grandTotalPoints.toString());
if (include)
table.appendRow(row);
}
for (Contest contest : selectedContests) {
Hashtable<String, UserContestData> contestData = reportData.get(contest.getId());
for (String login : contestData.keySet()) {
BigDecimal total = BigDecimal.ZERO;
ResultsRow row = table.addRow();
row.addCell("-");
row.addCell("-");
row.addCell("-");
row.addCell("-");
for (Contest rowContest : selectedContests) {
if (rowContest == contest) {
row.append(contestData.get(login).getData());
total = contestData.get(login).getTotalPoints();
} else {
row.addCell("");
for (Task task : rowContest.getTasks()) {
for (int i = 0; i < task.getNumberOfTests(); ++i)
row.addCell("");
row.addCell("");
}
row.addCell("-");
}
}
row.addCell(total.toString());
}
}
Vector<ResultsRow> newRows = new Vector<ResultsRow>();
Vector<ResultsRow> tableRows = table.getRows();
boolean[] used = new boolean[tableRows.size()];
for (int i = 0; i < tableRows.size(); ++i) {
used[i] = false;
}
for (int i = 0; i < tableRows.size(); ++i) {
int index = -1;
BigDecimal max = BigDecimal.ZERO;
for (int j = 0; j < tableRows.size(); ++j) {
BigDecimal points = new BigDecimal(tableRows.get(j).getCells().lastElement()
.getValue());
if (!used[j] && max.compareTo(points) <= 0) {
max = points;
index = j;
}
}
used[index] = true;
newRows.add(tableRows.get(index));
}
tableRows = newRows;
newRows = new Vector<ResultsRow>();
for (int i = 0; i < tableRows.size(); ++i) {
BigDecimal points = new BigDecimal(tableRows.get(i).getCells().lastElement().getValue());
int count = 1;
for (; i + count < tableRows.size(); ++count) {
if (points.compareTo(new BigDecimal(tableRows.get(i + count).getCells()
.lastElement().getValue())) != 0)
break;
}
for (int j = 0; j < count; ++j) {
ResultsRow row = new ResultsRow();
if (count > 1) {
row.addCell(Integer.toString(i + 1) + "-" + Integer.toString(i + count));
} else {
row.addCell(Integer.toString(i + 1));
}
row.append(tableRows.get(i + j));
newRows.add(row);
}
i += count - 1;
}