package pt.utl.ist.scripts.runOnce.teacher; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.Teacher; import net.sourceforge.fenixedu.domain.person.RoleType; import net.sourceforge.fenixedu.domain.research.Researcher; import net.sourceforge.fenixedu.domain.research.activity.JournalIssue; import net.sourceforge.fenixedu.domain.research.result.ResearchResult; import net.sourceforge.fenixedu.domain.research.result.ResultParticipation; import net.sourceforge.fenixedu.domain.research.result.ResultParticipation.ResultParticipationRole; import net.sourceforge.fenixedu.domain.research.result.publication.Article; import net.sourceforge.fenixedu.domain.research.result.publication.Book; import net.sourceforge.fenixedu.domain.research.result.publication.BookPart; import net.sourceforge.fenixedu.domain.research.result.publication.Inproceedings; import net.sourceforge.fenixedu.domain.research.result.publication.Manual; import net.sourceforge.fenixedu.domain.research.result.publication.OtherPublication; import net.sourceforge.fenixedu.domain.research.result.publication.Proceedings; import net.sourceforge.fenixedu.domain.research.result.publication.ResearchResultPublication; import net.sourceforge.fenixedu.domain.research.result.publication.TechnicalReport; import net.sourceforge.fenixedu.domain.research.result.publication.Thesis; import net.sourceforge.fenixedu.domain.research.result.publication.Unstructured; import org.apache.commons.lang.StringUtils; import pt.utl.ist.fenix.tools.util.excel.StyledExcelSpreadsheet; import pt.utl.ist.scripts.commons.AtomicScript; public class TeacherPublications extends AtomicScript { protected static final String SINGLE_SPACE = ", "; @Override protected void run() throws Exception { Set researchers = new HashSet(); for (Teacher teacher : rootDomainObject.getTeachersSet()) { if (teacher.isActive()) { researchers.add(teacher.getPerson()); } } for (Researcher researcher : rootDomainObject.getResearchersSet()) { if (researcher.hasPerson() && researcher.getPerson().hasRole(RoleType.RESEARCHER)) { researchers.add(researcher.getPerson()); } } List participations = new ArrayList(); for (final Person researcher : researchers) { for (ResultParticipation participation : researcher.getResultParticipationsSet()) { ResearchResult result = participation.getResult(); if (participation.getRole().equals(ResultParticipationRole.Author) && result instanceof ResearchResultPublication) { if (!(result instanceof Manual || result instanceof TechnicalReport || result instanceof OtherPublication || result instanceof Unstructured)) { participations.add(participation); } } } } StyledExcelSpreadsheet sheet = new StyledExcelSpreadsheet("Publicações"); sheet.newHeaderRow(); sheet.addHeader("Author"); sheet.addHeader("Preference"); sheet.addHeader("PublicationID"); sheet.addHeader("Type"); sheet.addHeader("Year"); sheet.addHeader("Authors"); sheet.addHeader("Title"); sheet.addHeader("Scope"); sheet.addHeader("Journal"); sheet.addHeader("Volume/Edition"); sheet.addHeader("Publisher"); sheet.addHeader("Pages"); for (ResultParticipation participation : participations) { sheet.newRow(); Person author = participation.getPerson(); ResearchResultPublication publication = (ResearchResultPublication) participation.getResult(); sheet.addCell(author.hasTeacher() ? author.getTeacher().getPerson().getEmployee().getEmployeeNumber() : ""); sheet.addCell(publication.getPreferredLevel(author).name()); sheet.addCell(publication.getExternalId()); sheet.addCell(publication.getClass().getSimpleName()); sheet.addCell(publication.getYear() != null ? publication.getYear() : ""); List parts = new ArrayList(); for (ResultParticipation participant : publication.getOrderedAuthorsResultParticipations()) { parts.add(participant.getPerson().getNickname()); } sheet.addCell(filter(StringUtils.join(parts, ", "))); sheet.addCell(filter(publication.getTitle())); // nº Tipo Ano Autor Título Revista_Livro Volume Paginas if (publication instanceof Article) { Article article = (Article) publication; sheet.addCell(article.getScientificJournal().getLocationType().name()); JournalIssue issue = article.getArticleAssociation().getJournalIssue(); sheet.addCell(filter(issue.getScientificJournal().getName())); sheet.addCell(filter(issue.getVolume() + (StringUtils.isNotBlank(issue.getNumber()) ? " (" + issue.getNumber() + ")" : ""))); sheet.addCell(filter(issue.getPublisher())); sheet.addCell(renderPages(article.getFirstPage(), article.getLastPage())); } else if (publication instanceof Book) { Book book = (Book) publication; sheet.addCell(""); sheet.addCell(""); sheet.addCell(filter(book.getEdition())); sheet.addCell(filter(book.getPublisher())); sheet.addCell(""); } else if (publication instanceof BookPart) { BookPart bookPart = (BookPart) publication; sheet.addCell(""); sheet.addCell(filter(bookPart.getBookTitle())); sheet.addCell(filter(bookPart.getEdition())); sheet.addCell(filter(bookPart.getPublisher())); sheet.addCell(""); } else if (publication instanceof Inproceedings) { Inproceedings inproceedings = (Inproceedings) publication; sheet.addCell(inproceedings.getEvent().getLocationType().name()); sheet.addCell(filter(inproceedings.getEventEdition().getFullName())); sheet.addCell(""); sheet.addCell(filter(inproceedings.getPublisher())); sheet.addCell(renderPages(inproceedings.getFirstPage(), inproceedings.getLastPage())); } else if (publication instanceof Proceedings) { Proceedings proceedings = (Proceedings) publication; sheet.addCell(proceedings.getEvent().getLocationType().name()); sheet.addCell(filter(proceedings.getEventEdition().getFullName())); sheet.addCell(""); sheet.addCell(""); sheet.addCell(""); } else if (publication instanceof Thesis) { sheet.addCell(""); sheet.addCell(""); sheet.addCell(""); sheet.addCell(""); sheet.addCell(""); } } sheet.getWorkbook().write(new FileOutputStream(new File(EXPORT_DIR_PATH + File.separator + "teacherPublications.xls"))); } private String filter(String... text) { Set parts = new HashSet(); for (String string : text) { if (StringUtils.isNotBlank(string)) { parts.add(string.replace('\n', ' ').replace('\r', ' ').trim()); } } return StringUtils.join(parts, ", "); } private String renderPages(Integer start, Integer end) { if (start != null) { if (end != null) { return start + "-" + end; } else { return start.toString(); } } else { return ""; } } public static void main(String[] args) { process(new TeacherPublications()); System.exit(0); } }