Skip to content

Instantly share code, notes, and snippets.

@nurv
Created April 21, 2014 11:03
Show Gist options
  • Save nurv/11139468 to your computer and use it in GitHub Desktop.
Save nurv/11139468 to your computer and use it in GitHub Desktop.
diff --git a/src/main/java/net/sourceforge/fenixedu/presentationTier/Action/externalServices/epfl/ExportEPFLPhdProgramCandidacies.java b/src/main/java/net/sourceforge/fenixedu/presentationTier/Action/externalServices/epfl/ExportEPFLPhdProgramCandidacies.java
index 5ec58a4..66a38fe 100644
--- a/src/main/java/net/sourceforge/fenixedu/presentationTier/Action/externalServices/epfl/ExportEPFLPhdProgramCandidacies.java
+++ b/src/main/java/net/sourceforge/fenixedu/presentationTier/Action/externalServices/epfl/ExportEPFLPhdProgramCandidacies.java
@@ -1,183 +1,183 @@
package net.sourceforge.fenixedu.presentationTier.Action.externalServices.epfl;
import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.util.List;
import net.sourceforge.fenixedu.domain.ExecutionYear;
import net.sourceforge.fenixedu.domain.Person;
import net.sourceforge.fenixedu.domain.Photograph;
import net.sourceforge.fenixedu.domain.person.Gender;
import net.sourceforge.fenixedu.domain.phd.PhdIndividualProgramCollaborationType;
import net.sourceforge.fenixedu.domain.phd.PhdIndividualProgramProcess;
import net.sourceforge.fenixedu.domain.phd.PhdIndividualProgramProcessState;
import pt.utl.ist.fenix.tools.predicates.Predicate;
import pt.utl.ist.fenix.tools.util.i18n.Language;
public class ExportEPFLPhdProgramCandidacies {
public static byte[] run() throws Exception {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, Charset.defaultCharset().name());
PrintWriter writer = new PrintWriter(outputStreamWriter);
try {
writer.println("<?xml version=\"1.0\" encoding=\"" + Charset.defaultCharset().name() + "\" ?>");
writer.println("<data>");
List<PhdIndividualProgramProcess> list =
PhdIndividualProgramProcess.search(ExecutionYear.readCurrentExecutionYear(),
new Predicate<PhdIndividualProgramProcess>() {
@Override
public boolean eval(PhdIndividualProgramProcess t) {
if (t.getExecutionYear() != ExecutionYear.readCurrentExecutionYear()) {
return false;
}
if (!PhdIndividualProgramCollaborationType.EPFL.equals(t.getCollaborationType())) {
return false;
}
if (!PhdIndividualProgramProcessState.CANDIDACY.equals(t.getActiveState())) {
return false;
}
- if (!t.getCandidacyProcess().getValidatedByCandidate()) {
- return false;
- }
+// if (!t.getCandidacyProcess().getValidatedByCandidate()) {
+// return false;
+// }
return true;
}
});
for (PhdIndividualProgramProcess process : list) {
writePersonInfo(process, writer);
}
writer.println("</data>");
} finally {
writer.close();
}
return outputStream.toByteArray();
}
/**
* Write each person's personal and process information
*
* @param process
* @param writer
*/
private static void writePersonInfo(PhdIndividualProgramProcess process, PrintWriter writer) {
writer.println(addTabs(1) + "<personne action=\"AUTO\">");
Person person = process.getPerson();
writer.println(addTabs(2) + String.format("<nom>%s</nom>", person.getGivenNames()));
writer.println(addTabs(2) + String.format("<prenom>%s</prenom>", person.getFamilyNames()));
writer.println(addTabs(2) + String.format("<sexe>%s</sexe>", Gender.MALE.equals(person.getGender()) ? "SEXH" : "SEXF"));
writer.println(addTabs(2)
+ String.format("<naissance>%s</naissance>", person.getDateOfBirthYearMonthDay().toString("dd.MM.yyyy")));
writer.println(addTabs(2) + "<detailPersonne action=\"AUTO\">");
writer.println(addTabs(3) + "<domaine>DOMAINEACADEMIQUE</domaine>");
writer.println(addTabs(3)
+ String.format("<datePersonne action=\"AUTO\" type=\"TYPE_DATE_ENTREE\">%s</datePersonne>", process
.getCandidacyProcess().getCandidacyDate().toString("dd.MM.yyyy")));
writer.println(addTabs(3)
+ String.format(
"<lieuPersonne action=\"AUTO\" type=\"LIEUNAI\" identificationLieu=\"iso\" iso=\"%s\" typeLieu=\"PAYS\">%s</lieuPersonne>",
person.getCountry().getCode(), person.getCountry().getCountryNationality().getContent(Language.en)));
writer.println(addTabs(3)
+ String.format("<lieuPersonne action=\"AUTO\" type=\"LIEUNAIETRA\" "
+ "typeLieu=\"LOCETRNONCON\" forceTo=\"LOCETRNONCON\">%s</lieuPersonne>",
person.getDistrictSubdivisionOfBirth()));
writer.println(addTabs(3)
+ String.format("<lieuPersonne action=\"AUTO\" type=\"LIEUORI\" identificationLieu=\"iso\" iso=\"%s\" "
+ "typeLieu=\"PAYS\">%s</lieuPersonne>", person.getCountry().getCode(), person.getCountry()
.getLocalizedName().getContent(Language.en)));
writer.println(addTabs(3) + "<adresse type=\"ADR_ECH\" action=\"AUTO\">");
writer.println(addTabs(4) + String.format("<ligne n=\"1\">%s</ligne>", person.getAddress()));
writer.println(addTabs(4)
+ String.format(
"<localite typeLieu=\"LOCALITE;LOCETRNONCON\" identificationLieu=\"zip\" zip=\"%s\" b_returnfirst=\"1\">%s</localite>",
person.getAreaCode(), person.getArea()));
if (person.getCountryOfResidence() != null) {
writer.println(addTabs(4)
+ String.format("<pays identificationLieu=\"iso\" iso=\"%s\" b_returnfirst=\"1\">%s</pays>", person
.getCountryOfResidence().getCode(),
person.getCountryOfResidence().getLocalizedName().getContent(Language.en)));
} else {
writer.println(addTabs(4) + "<pays identificationLieu=\"iso\" iso=\"\" b_returnfirst=\"1\"></pays>");
}
writer.println(addTabs(4) + String.format("<moyen action=\"AUTO\" type=\"EMAIL\">%s</moyen>", person.getEmail()));
writer.println(addTabs(4) + String.format("<moyen action=\"AUTO\" type=\"PORTABLE\">%s</moyen>", person.getMobile()));
writer.println(addTabs(3) + "</adresse>");
writer.println(addTabs(2) + "</detailPersonne>");
writer.println(addTabs(2) + "<inscription action=\"AUTO\">");
writer.println(addTabs(3) + "<gps domaine=\"DOMAINEACADEMIQUE\">");
writer.println(addTabs(4) + "<modelegps>CDOC</modelegps>");
writer.println(addTabs(4) + "<unite type=\"ACAD\" format=\"LIBELLE\">IST-EPFL</unite>");
writer.println(addTabs(4) + "<periode type=\"PEDAGO\" format=\"LIBCOU\">Eval sep</periode>");
writer.println(addTabs(4) + "<periode type=\"ACAD\">2010</periode>");
writer.println(addTabs(3) + "</gps>");
writer.println(addTabs(3) + String.format(" <detail type=\"URL_IST-EPFL\">%s</detail>", getUrlForProcess(process)));
writer.println(addTabs(3) + String.format(" <detail type=\"URL_IST-EPFL_DOCUMENTS\">%s</detail>", getUrlForProcessDocs(process)));
final Photograph photo = person.getPersonalPhotoEvenIfPending();
if (photo != null) {
writer.println(addTabs(3) + String.format(" <detail type=\"URL_IST-EPFL_PHOTO\">%s</detail>", getUrlForPhoto(photo)));
}
if (process.getExternalPhdProgram() != null) {
writer.println(addTabs(3)
+ String.format("<detail type=\"PDOC_AT_EPFL\" format=\"COURTU\">%s</detail>", process.getExternalPhdProgram()
.getAcronym()));
}
writer.println(addTabs(3)
+ String.format(" <detail type=\"GPSDOMFOCUS\" conversion=\"IMPORT_IST:GPSDOMFOCUS\">%s</detail> ", process
.getPhdProgramFocusArea().getName()));
writer.println(addTabs(2) + "</inscription>");
writer.println(addTabs(1) + "</personne>");
}
private static String getUrlForProcess(PhdIndividualProgramProcess process) {
return String.format("https://fenix.ist.utl.pt/phd/epfl/applications/show?process=%s", process
.getCandidacyProcessHashCode().getValue());
}
private static String getUrlForProcessDocs(PhdIndividualProgramProcess process) {
return String.format("https://fenix.ist.utl.pt/phd/epfl/applications/candidateDocuments?candidateOid=%s", process
.getCandidacyProcessHashCode().getExternalId());
}
private static String getUrlForPhoto(final Photograph photo) {
return String.format("https://fenix.ist.utl.pt/phd/epfl/applications/photo?photoOid=%s", photo.getExternalId());
}
private static String addTabs(int level) {
String returnString = "";
for (int i = 1; i <= level; i++) {
returnString += '\t';
}
return returnString;
}
}
diff --git a/src/main/java/net/sourceforge/fenixedu/presentationTier/Action/externalServices/epfl/ExportPhdIndividualProgramProcessesInHtml.java b/src/main/java/net/sourceforge/fenixedu/presentationTier/Action/externalServices/epfl/ExportPhdIndividualProgramProcessesInHtml.java
index 03f05a3..7845a94 100644
--- a/src/main/java/net/sourceforge/fenixedu/presentationTier/Action/externalServices/epfl/ExportPhdIndividualProgramProcessesInHtml.java
+++ b/src/main/java/net/sourceforge/fenixedu/presentationTier/Action/externalServices/epfl/ExportPhdIndividualProgramProcessesInHtml.java
@@ -1,628 +1,628 @@
package net.sourceforge.fenixedu.presentationTier.Action.externalServices.epfl;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import net.sourceforge.fenixedu.domain.ExecutionYear;
import net.sourceforge.fenixedu.domain.Person;
import net.sourceforge.fenixedu.domain.Photograph;
import net.sourceforge.fenixedu.domain.PublicCandidacyHashCode;
import net.sourceforge.fenixedu.domain.Qualification;
import net.sourceforge.fenixedu.domain.QualificationType;
import net.sourceforge.fenixedu.domain.organizationalStructure.Unit;
import net.sourceforge.fenixedu.domain.phd.PhdIndividualProgramCollaborationType;
import net.sourceforge.fenixedu.domain.phd.PhdIndividualProgramProcess;
import net.sourceforge.fenixedu.domain.phd.PhdIndividualProgramProcessState;
import net.sourceforge.fenixedu.domain.phd.PhdParticipant;
import net.sourceforge.fenixedu.domain.phd.PhdProgramFocusArea;
import net.sourceforge.fenixedu.domain.phd.PhdProgramProcessDocument;
import net.sourceforge.fenixedu.domain.phd.ThesisSubjectOrder;
import net.sourceforge.fenixedu.domain.phd.candidacy.PhdCandidacyReferee;
import net.sourceforge.fenixedu.domain.phd.candidacy.PhdCandidacyRefereeLetter;
import net.sourceforge.fenixedu.domain.phd.candidacy.PhdProgramPublicCandidacyHashCode;
import org.apache.commons.lang.StringUtils;
import org.fenixedu.bennu.core.domain.Bennu;
import org.joda.time.Partial;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pt.utl.ist.fenix.tools.util.i18n.Language;
public class ExportPhdIndividualProgramProcessesInHtml {
private static final Logger logger = LoggerFactory.getLogger(ExportPhdIndividualProgramProcessesInHtml.class);
// TODO: IST-<Collaboration>: collaboration must be added as argument
static final private String APPLICATION_NAME = "Application to FCT Doctoral Programmes";
static final private String APPLICATION_PREFIX_LINK = "";
static byte[] exportPresentationPage() throws IOException {
List<PhdProgramPublicCandidacyHashCode> unfocusAreaCandidates = new ArrayList<PhdProgramPublicCandidacyHashCode>();
final Page page = new Page();
page.h2(APPLICATION_NAME);
for (final Entry<PhdProgramFocusArea, Set<PhdProgramPublicCandidacyHashCode>> entry : getApplicants(unfocusAreaCandidates)
.entrySet()) {
page.h(3, getFocusAreaTitle(entry), "mtop2");
page.ulStart();
for (final PhdProgramPublicCandidacyHashCode code : entry.getValue()) {
final String url = APPLICATION_PREFIX_LINK + "/phd/epfl/applications/show?process=" + code.getValue();
page.liStart().link(url, code.getPerson().getName()).liEnd();
}
page.ulEnd();
}
page.close();
return page.toByteArray();
}
private static String getFocusAreaTitle(final Entry<PhdProgramFocusArea, Set<PhdProgramPublicCandidacyHashCode>> entry) {
return entry.getKey().getName().getContent() + " (" + entry.getValue().size() + " applications)";
}
private static Map<PhdProgramFocusArea, Set<PhdProgramPublicCandidacyHashCode>> getApplicants(
final List<PhdProgramPublicCandidacyHashCode> unfocusAreaCandidates) {
final Map<PhdProgramFocusArea, Set<PhdProgramPublicCandidacyHashCode>> candidates =
new TreeMap<PhdProgramFocusArea, Set<PhdProgramPublicCandidacyHashCode>>(PhdProgramFocusArea.COMPARATOR_BY_NAME);
for (final PublicCandidacyHashCode hashCode : Bennu.getInstance().getCandidacyHashCodesSet()) {
if (hashCode.isFromPhdProgram() && hashCode.hasCandidacyProcess()) {
final PhdProgramPublicCandidacyHashCode phdHashCode = (PhdProgramPublicCandidacyHashCode) hashCode;
if (phdHashCode.getIndividualProgramProcess().getExecutionYear() != ExecutionYear.readCurrentExecutionYear()) {
continue;
}
if (!PhdIndividualProgramCollaborationType.EPFL.equals(phdHashCode.getIndividualProgramProcess()
.getCollaborationType())) {
continue;
}
if (!PhdIndividualProgramProcessState.CANDIDACY
.equals(phdHashCode.getIndividualProgramProcess().getActiveState())) {
continue;
}
- if (phdHashCode.getPhdProgramCandidacyProcess().isValidatedByCandidate()) {
+// if (phdHashCode.getPhdProgramCandidacyProcess().isValidatedByCandidate()) {
addCandidate(unfocusAreaCandidates, candidates, phdHashCode);
- }
+// }
}
}
return candidates;
}
private static void addCandidate(final List<PhdProgramPublicCandidacyHashCode> unfocusAreaCandidates,
final Map<PhdProgramFocusArea, Set<PhdProgramPublicCandidacyHashCode>> candidates,
final PhdProgramPublicCandidacyHashCode hashCode) {
final PhdProgramFocusArea focusArea = hashCode.getIndividualProgramProcess().getPhdProgramFocusArea();
if (focusArea == null) {
unfocusAreaCandidates.add(hashCode);
return;
}
if (!candidates.containsKey(focusArea)) {
candidates.put(focusArea, new TreeSet<PhdProgramPublicCandidacyHashCode>(
new Comparator<PhdProgramPublicCandidacyHashCode>() {
@Override
public int compare(PhdProgramPublicCandidacyHashCode o1, PhdProgramPublicCandidacyHashCode o2) {
return o1.getPerson().getName().compareTo(o2.getPerson().getName());
}
}));
}
candidates.get(focusArea).add(hashCode);
}
static byte[] drawCandidatePage(final PhdProgramPublicCandidacyHashCode hashCode) throws IOException {
final String email = hashCode.getEmail().substring(0, hashCode.getEmail().indexOf("@"));
final Page page = new Page();
page.h2(APPLICATION_NAME);
drawPersonalInformation(page, hashCode, email);
drawPhdIndividualProgramInformation(page, hashCode);
drawGuidings(page, hashCode);
drawQualifications(page, hashCode);
drawCandidacyReferees(page, hashCode, email);
drawDocuments(page, hashCode, email);
drawThesisRanking(page, hashCode, email);
page.close();
return page.toByteArray();
}
private static void drawPersonalInformation(final Page page, final PhdProgramPublicCandidacyHashCode hashCode,
final String folderName) throws IOException {
final Person person = hashCode.getPerson();
page.h(3, "Personal Information", "mtop2");
page.tableStart("tstyle2 thwhite thnowrap thlight thleft thtop ulnomargin ");
page.rowStart("tdbold").headerStartWithStyle("width: 125px;").write("Name:").headerEnd().column(person.getName())
.rowEnd();
page.rowStart().header("Gender:").column(person.getGender().toLocalizedString(Locale.ENGLISH)).rowEnd();
page.rowStart().header("Identity card type:").column(person.getIdDocumentType().getLocalizedName()).rowEnd();
page.rowStart().header("Identity card #:").column(person.getDocumentIdNumber()).rowEnd();
page.rowStart().header("Issued by:").column(person.getEmissionLocationOfDocumentId()).rowEnd();
page.rowStart().header("Fiscal number:").column(string(person.getSocialSecurityNumber())).rowEnd();
page.rowStart().header("Date of birth:").column(person.getDateOfBirthYearMonthDay().toString("dd/MM/yyyy")).rowEnd();
page.rowStart().header("Birthplace:").column(person.getDistrictSubdivisionOfBirth()).rowEnd();
page.rowStart().header("Nationality:").column(person.getCountry().getCountryNationality().getContent()).rowEnd();
page.rowStart().header("Address:").column(person.getAddress()).rowEnd();
page.rowStart().header("City:").column(person.getArea()).rowEnd();
page.rowStart().header("Zip code:").column(person.getAreaCode()).rowEnd();
page.rowStart().header("Country:")
.column((person.getCountryOfResidence() != null ? person.getCountryOfResidence().getName() : "-")).rowEnd();
page.rowStart().header("Phone:").column(person.getDefaultPhoneNumber()).rowEnd();
page.rowStart().header("Mobile:").column(person.getDefaultMobilePhoneNumber()).rowEnd();
page.rowStart().header("Email:").column(person.getDefaultEmailAddressValue()).rowEnd();
page.tableEnd();
page.h(3, "Photo");
String photoUrl = APPLICATION_PREFIX_LINK + "/phd/epfl/applications/photo";
final Photograph photo = person.getPersonalPhotoEvenIfPending();
if (photo != null) {
photoUrl += "?photoOid=" + photo.getExternalId();
}
page.photo(photoUrl);
}
private static void drawPhdIndividualProgramInformation(final Page page, final PhdProgramPublicCandidacyHashCode hashCode)
throws IOException {
final PhdIndividualProgramProcess process = hashCode.getIndividualProgramProcess();
page.h(3, "Application information");
page.tableStart("tstyle2 thwhite thnowrap thlight thleft thtop ulnomargin ");
page.rowStart().headerStartWithStyle("width: 125px;").write("Candidacy Date:").headerEnd()
.column(process.getCandidacyDate().toString("dd/MM/yyyy")).rowEnd();
page.rowStart().header("Area:").column(process.getPhdProgramFocusArea().getName().getContent()).rowEnd();
page.rowStart().header(Unit.getInstitutionAcronym() + " Phd Program:")
.column(process.getPhdProgram().getName().getContent(Language.en)).rowEnd();
if (process.getExternalPhdProgram() != null) {
page.rowStart().header("EPFL Phd Program:").column(process.getExternalPhdProgram().getName().getContent(Language.en));
}
page.rowStart().header("Title:").column(string(process.getThesisTitle())).rowEnd();
page.rowStart().header("Collaboration:").column(process.getCollaborationTypeName()).rowEnd();
page.rowStart().header("Year:").column(process.getExecutionYear().getYear()).rowEnd();
page.tableEnd();
}
private static void drawDocuments(final Page page, final PhdProgramPublicCandidacyHashCode hashCode, final String folderName)
throws IOException {
page.h(3, "Documents", "mtop2");
final PhdIndividualProgramProcess process = hashCode.getIndividualProgramProcess();
if (!process.getCandidacyProcessDocuments().isEmpty()) {
final String documentName = folderName + "-documents.zip";
final String url =
APPLICATION_PREFIX_LINK + "/phd/epfl/applications/candidateDocuments?candidateOid="
+ hashCode.getExternalId();
page.pStart("mbottom0").link(url, documentName).pEnd();
page.tableStart("tstyle2 thwhite thnowrap thlight thleft thtop ulnomargin ");
page.rowStart().header("Document type").header("Upload time").header("Filename").rowEnd();
for (final PhdProgramProcessDocument document : process.getCandidacyProcessDocuments()) {
page.rowStart().column(document.getDocumentType().getLocalizedName());
page.column(document.getUploadTime().toString("dd/MM/yyyy HH:mm"));
page.column(document.getFilename()).rowEnd();
}
page.tableEnd();
}
}
private static void drawThesisRanking(Page page, PhdProgramPublicCandidacyHashCode hashCode, String email) throws IOException {
page.h(3, "Thesis Rank", "mtop2");
final PhdIndividualProgramProcess process = hashCode.getIndividualProgramProcess();
page.tableStart("tstyle2");
page.rowStart();
page.header("Rank");
page.header("Name");
page.header("Teacher");
page.header("Description");
page.rowEnd();
Collection<ThesisSubjectOrder> thesisSubjectOrders = process.getThesisSubjectOrdersSorted();
for (ThesisSubjectOrder thesisSubjectOrder : thesisSubjectOrders) {
page.rowStart();
page.column(thesisSubjectOrder.getSubjectOrder().toString());
page.column(thesisSubjectOrder.getThesisSubject().getName().getContent());
page.column(thesisSubjectOrder.getThesisSubject().hasTeacher() ? thesisSubjectOrder.getThesisSubject().getTeacher()
.getPerson().getName() : "");
page.rowEnd();
}
page.tableEnd();
}
static byte[] createZip(final PhdProgramPublicCandidacyHashCode hashCode) {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zip = null;
try {
zip = new ZipOutputStream(outputStream);
int count = 1;
for (final PhdProgramProcessDocument document : hashCode.getIndividualProgramProcess().getCandidacyProcessDocuments()) {
final ZipEntry zipEntry = new ZipEntry(count + "-" + document.getFilename());
zip.putNextEntry(zipEntry);
// TODO: use in local context copy(new ByteArrayInputStream(new
// byte[20]), zip);
copy(document.getStream(), zip);
zip.closeEntry();
count++;
}
} catch (FileNotFoundException e) {
logger.error(e.getMessage(), e);
} catch (IOException e) {
logger.error(e.getMessage(), e);
} finally {
if (zip != null) {
try {
zip.flush();
zip.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
return outputStream.toByteArray();
}
private static void drawCandidacyReferees(final Page page, final PhdProgramPublicCandidacyHashCode hashCode,
final String folderName) throws IOException {
final PhdIndividualProgramProcess process = hashCode.getIndividualProgramProcess();
page.h(3, "Reference letters (referees)", "mtop2");
if (!process.getPhdCandidacyReferees().isEmpty()) {
int count = 1;
for (final PhdCandidacyReferee referee : process.getPhdCandidacyReferees()) {
page.pStart("mbottom0").strong(String.valueOf(count) + ". ").pEnd();
drawReferee(page, referee, count, folderName);
count++;
}
} else {
page.pStart().write("Not defined").pEnd();
}
}
// "displayRefereePage"
private static void drawReferee(final Page page, final PhdCandidacyReferee referee, final int count, final String folderName)
throws IOException {
page.tableStart("tstyle2 thwhite thnowrap thlight thleft thtop ulnomargin ");
page.rowStart().headerStartWithStyle("width: 125px;").write("Name:").headerEnd().column(referee.getName()).rowEnd();
page.rowStart().header("Email:").column(referee.getEmail()).rowEnd();
page.rowStart().header("Institution:").column(referee.getInstitution()).rowEnd();
if (referee.isLetterAvailable()) {
page.rowStart().header("Referee form submitted:");
final String url =
APPLICATION_PREFIX_LINK + "/phd/epfl/applications/referee?refereeOid=" + referee.getExternalId()
+ "&amp;count=" + count;
page.columnStart().link(url, "Yes").columnEnd().rowEnd();
} else {
page.rowStart().header("Referee form submitted:").column("No").rowEnd();
}
page.tableEnd();
}
static byte[] drawLetter(final PhdCandidacyReferee referee, final int count) throws IOException {
final Page page = new Page();
page.h2(APPLICATION_NAME);
page.h(3, "Applicant", "mtop2");
candidateInformation(referee, page);
letterInformation(referee, page);
page.close();
return page.toByteArray();
}
private static void letterInformation(final PhdCandidacyReferee referee, final Page page) throws IOException {
final PhdCandidacyRefereeLetter letter = referee.getLetter();
page.h(3, "Reference Letter", "mtop2");
page.tableStart("tstyle2 thwhite thnowrap thlight thleft thtop ulnomargin");
page.rowStart().headerStartWithStyle("width: 200px;").write("How long have you known the applicant?").headerEnd()
.column(string(letter.getHowLongKnownApplicant()) + " months").rowEnd();
page.rowStart().header("In what capacity?").column(string(letter.getCapacity())).rowEnd();
page.rowStart().header("Comparison group:").column(string(letter.getComparisonGroup())).rowEnd();
page.rowStart().header("Rank in class (if applicable):").column(string(letter.getRankInClass())).rowEnd();
page.rowStart().header("Academic performance:").column(string(letter.getAcademicPerformance().getLocalizedName()))
.rowEnd();
page.rowStart().header("Social and Communication Skills:")
.column(string(letter.getSocialAndCommunicationSkills().getLocalizedName())).rowEnd();
page.rowStart().header("Potential to excel in a PhD:").column(string(letter.getPotencialToExcelPhd().getLocalizedName()))
.rowEnd();
page.rowStart().header("Recomendation letter:");
if (letter.hasFile()) {
page.column(letter.getFile().getDisplayName() + " (file is inside documents zip file)");
} else {
page.column("-");
}
page.rowEnd();
page.rowStart().header("Comments:").column(string(letter.getComments())).rowEnd();
page.rowStart().header("Name:").column(string(letter.getRefereeName())).rowEnd();
page.rowStart().header("Position/Title:").column(string(letter.getRefereePosition())).rowEnd();
page.rowStart().header("Institution:").column(string(letter.getRefereeInstitution())).rowEnd();
page.rowStart().header("Address:").column(string(letter.getRefereeInstitution())).rowEnd();
page.rowStart().header("City:").column(string(letter.getRefereeCity())).rowEnd();
page.rowStart().header("Zip code:").column(string(letter.getRefereeZipCode())).rowEnd();
page.rowStart().header("Country:")
.column(letter.getRefereeCountry() != null ? letter.getRefereeCountry().getLocalizedName().getContent() : "-")
.rowEnd();
page.rowStart().header("Email:").column(string(letter.getRefereeEmail())).rowEnd();
page.tableEnd();
}
private static void candidateInformation(final PhdCandidacyReferee referee, final Page page) throws IOException {
page.tableStart("tstyle2 thwhite thnowrap thlight thleft thtop ulnomargin ");
page.rowStart("tdbold").headerStartWithStyle("width: 200px;").write("Name: ").headerEnd()
.column(referee.getPhdProgramCandidacyProcess().getPerson().getName()).rowEnd();
page.tableEnd();
}
private static void drawQualifications(final Page page, final PhdProgramPublicCandidacyHashCode hashCode) throws IOException {
final PhdIndividualProgramProcess process = hashCode.getIndividualProgramProcess();
page.h(3, "Academic Degrees", "mtop2");
if (!process.getQualifications().isEmpty()) {
int count = 1;
for (final Qualification qualification : process.getQualifications()) {
page.pStart("mbottom0").strong(String.valueOf(count) + ". ").pEnd();
drawQualification(page, qualification);
count++;
}
} else {
page.pStart().write("Not defined").pEnd();
}
}
private static void drawQualification(final Page page, final Qualification qualification) throws IOException {
page.tableStart("tstyle2 thwhite thnowrap thlight thleft thtop ulnomargin ");
if (qualification != null) {
final QualificationType type = qualification.getType();
page.rowStart().header("Type:").column(type == null ? "-" : type.getLocalizedName()).rowEnd();
final String degree = qualification.getDegree();
page.rowStart().header("Scientific Field:").column(degree == null ? "-" : degree).rowEnd();
final String school = qualification.getSchool();
page.rowStart().header("Institution:").column(school == null ? "-" : school).rowEnd();
final String mark = qualification.getMark();
page.rowStart().header("Grade:").column(mark == null ? "-" : mark).rowEnd();
final Partial attendedBegin = qualification.getAttendedBegin();
page.rowStart().header("Attended from:").column(attendedBegin == null ? "-" : attendedBegin.toString("MM/yyyy"))
.rowEnd();
final Partial attendedEnd = qualification.getAttendedEnd();
page.rowStart().header("Attended to:").column(attendedEnd == null ? "-" : attendedEnd.toString("MM/yyyy")).rowEnd();
}
page.tableEnd();
}
private static void drawGuidings(final Page page, final PhdProgramPublicCandidacyHashCode hashCode) throws IOException {
final PhdIndividualProgramProcess process = hashCode.getIndividualProgramProcess();
page.h(3, "Phd supervisors (if applicable)", "mtop2");
if (process.hasAnyGuidings()) {
int count = 1;
for (final PhdParticipant guiding : process.getGuidings()) {
page.pStart("mbottom0").strong(String.valueOf(count) + ". ").pEnd();
drawGuiding(page, guiding);
count++;
}
} else {
page.pStart().write("Not defined").pEnd();
}
}
private static void drawGuiding(final Page page, final PhdParticipant guiding) throws IOException {
page.tableStart("tstyle2 thwhite thnowrap thlight thleft thtop ulnomargin ");
page.rowStart().headerStartWithStyle("width: 125px;").write("Name:").headerEnd().column(guiding.getName()).rowEnd();
page.rowStart().header("Affiliation:").column(guiding.getWorkLocation()).rowEnd();
page.rowStart().header("Email:").column(guiding.getEmail()).rowEnd();
page.tableEnd();
}
private static String string(final String value) {
return (value != null) ? value : StringUtils.EMPTY;
}
/*
* do not close output stream
*/
private static void copy(final InputStream inputStream, final OutputStream outputStream) throws IOException {
int BUFFER_SIZE = 1024 * 1024;
try {
final byte[] buffer = new byte[BUFFER_SIZE];
for (int numberOfBytesRead; (numberOfBytesRead = inputStream.read(buffer, 0, BUFFER_SIZE)) != -1; outputStream.write(
buffer, 0, numberOfBytesRead)) {
;
}
} finally {
inputStream.close();
}
}
static private class Page {
private ByteArrayOutputStream writer = new ByteArrayOutputStream();
Page() throws IOException {
write("<xhtml>");
write("<head>");
css(APPLICATION_PREFIX_LINK + "/CSS/iststyle.css");
css(APPLICATION_PREFIX_LINK + "/CSS/webservice.css");
write("</head>");
write("<body>");
}
public byte[] toByteArray() throws IOException {
write("</body>");
write("</xhtml>");
return writer.toByteArray();
}
public Page css(final String url) throws IOException {
return write(String.format("<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"%s\" />", url));
}
public Page h2(final String body) throws IOException {
return h(2, body);
}
public Page h(final int level, final String body) throws IOException {
return startTag("h" + level).write(body).endTag("h" + level);
}
public Page h(final int level, final String body, final String classes) throws IOException {
return write(String.format("<h%s class=\"%s\">", level, classes)).write(body).endTag("h" + level);
}
public Page link(final String path, final String name) throws IOException {
return write(String.format("<a href='%s'>%s</a>", path, name));
}
public Page strong(final String body) throws IOException {
return startTag("strong").write(body).endTag("strong");
}
public Page tableStart(String classes) throws IOException {
return write(String.format("<table class=\"%s\">", classes));
}
public Page rowStart() throws IOException {
return startTag("tr");
}
public Page rowStart(final String classes) throws IOException {
return write(String.format("<tr class=\"%s\"", classes));
}
public Page rowEnd() throws IOException {
return endTag("tr");
}
public Page headerStartWithStyle(final String style) throws IOException {
return write(String.format("<th style=\"%s\">", style));
}
public Page headerEnd() throws IOException {
return endTag("th");
}
public Page header(final String body) throws IOException {
return write(String.format("<th>%s</th>", body));
}
public Page columnStart() throws IOException {
return startTag("td");
}
public Page columnEnd() throws IOException {
return endTag("td");
}
public Page column(final String body) throws IOException {
return write(String.format("<td>%s</td>", body));
}
public Page tableEnd() throws IOException {
return endTag("table");
}
public Page ulStart() throws IOException {
return startTag("ul");
}
public Page ulEnd() throws IOException {
return endTag("ul");
}
public Page liStart() throws IOException {
return startTag("li");
}
public Page liEnd() throws IOException {
return endTag("li");
}
public Page pStart(final String classes) throws IOException {
return write(String.format("<p class=\"%s\">", classes));
}
public Page pStart() throws IOException {
return startTag("p");
}
public Page pEnd() throws IOException {
return endTag("p");
}
public Page photo(final String photoPath) throws IOException {
return write(String.format("<img src=\"%s\" />", photoPath));
}
private Page startTag(final String tagName) throws IOException {
return write("<" + tagName + ">");
}
private Page endTag(final String tagName) throws IOException {
return write("</" + tagName + ">");
}
public Page write(final String value) throws IOException {
writer.write(value.getBytes(Charset.defaultCharset().name()));
writer.write("\n".getBytes());
return this;
}
public void close() throws IOException {
if (writer != null) {
writer.flush();
writer.close();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment