@Override
public ActionForward execute(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request,
final HttpServletResponse response) throws FenixActionException, FenixServiceException {
final SearchExecutionCourseAttendsBean executionCourseAttendsBean = getRenderedObject("downloadViewState");
executionCourseAttendsBean.getExecutionCourse().searchAttends(executionCourseAttendsBean);
final List<Attends> attendsResult = new ArrayList<Attends>(executionCourseAttendsBean.getAttendsResult());
Collections.sort(attendsResult, Attends.COMPARATOR_BY_STUDENT_NUMBER);
String fileContents = new String();
// building the table header
fileContents += STUDENT_USERNAME + SEPARATOR;
fileContents += STUDENT_NUMBER + SEPARATOR;
fileContents += NUMBER_OF_ENROLLMENTS + SEPARATOR;
fileContents += ATTENDACY_TYPE + SEPARATOR;
fileContents += COURSE + SEPARATOR;
fileContents += NAME + SEPARATOR;
final List<Grouping> groupings = new ArrayList<Grouping>(executionCourseAttendsBean.getExecutionCourse().getGroupings());
Collections.sort(groupings, Grouping.COMPARATOR_BY_ENROLMENT_BEGIN_DATE);
if (!groupings.isEmpty()) {
for (final Grouping grouping : groupings) {
fileContents += GROUP + grouping.getName() + SEPARATOR;
}
}
fileContents += EMAIL + SEPARATOR;
final List<ShiftType> shiftTypes =
new ArrayList<ShiftType>(executionCourseAttendsBean.getExecutionCourse().getShiftTypes());
Collections.sort(shiftTypes);
for (final ShiftType shiftType : shiftTypes) {
fileContents += SHIFT + BundleUtil.getString(Bundle.ENUMERATION, shiftType.getName()) + SEPARATOR;
}
fileContents += NEWLINE;
// building each line
for (final Attends attends : attendsResult) {
fileContents += attends.getRegistration().getStudent().getPerson().getUsername() + SEPARATOR;
fileContents += attends.getRegistration().getStudent().getNumber() + SEPARATOR;
if (attends.getEnrolment() == null) {
fileContents += NULL + SEPARATOR;
} else {
fileContents +=
attends.getEnrolment()
.getNumberOfTotalEnrolmentsInThisCourse(attends.getEnrolment().getExecutionPeriod()) + SEPARATOR;
}
fileContents +=
BundleUtil.getString(Bundle.ENUMERATION, attends.getAttendsStateType().getQualifiedName()) + SEPARATOR;
fileContents += attends.getStudentCurricularPlanFromAttends().getDegreeCurricularPlan().getName() + SEPARATOR;
fileContents += attends.getRegistration().getStudent().getPerson().getName() + SEPARATOR;
for (final Grouping grouping : groupings) {
final StudentGroup studentGroup = attends.getStudentGroupByGrouping(grouping);
if (studentGroup == null) {
fileContents += NOT_AVAILABLE + SEPARATOR;
} else {
fileContents += studentGroup.getGroupNumber() + SEPARATOR;
}
}
final String email = attends.getRegistration().getStudent().getPerson().getEmail();
fileContents += (email != null ? email : "") + SEPARATOR;
for (final ShiftType shiftType : shiftTypes) {
final Shift shift =
attends.getRegistration().getShiftFor(executionCourseAttendsBean.getExecutionCourse(), shiftType);
if (shift == null) {
fileContents += NOT_AVAILABLE + SEPARATOR;
} else {
fileContents += shift.getNome() + SEPARATOR;
}
}
fileContents += NEWLINE;
}
fileContents += NEWLINE;
fileContents += SUMMARY + NEWLINE;
fileContents += NUMBER_ENROLLMENTS + SEPARATOR + NUMBER_STUDENTS + NEWLINE;
final SortedSet<Integer> keys = new TreeSet<Integer>(executionCourseAttendsBean.getEnrolmentsNumberMap().keySet());
for (final Integer key : keys) {
fileContents += key + SEPARATOR + executionCourseAttendsBean.getEnrolmentsNumberMap().get(key) + NEWLINE;
}
try {
response.setContentType("plain/text");
final ServletOutputStream writer = response.getOutputStream();
// final PrintWriter printWriter = response.getWriter();
final StringBuilder fileName = new StringBuilder();
final YearMonthDay currentDate = new YearMonthDay();
fileName.append("listaDeAlunos_");
fileName.append(executionCourseAttendsBean.getExecutionCourse().getSigla()).append("_")
.append(currentDate.getDayOfMonth());
fileName.append("-").append(currentDate.getMonthOfYear()).append("-").append(currentDate.getYear());
fileName.append(".tsv");
response.setHeader("Content-disposition", "attachment; filename=" + StringNormalizer.normalize(fileName.toString()));
// printWriter.print(fileContents);