info.toegepaste.controller.PdfController.java Source code

Java tutorial

Introduction

Here is the source code for info.toegepaste.controller.PdfController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package info.toegepaste.controller;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import info.toegepaste.model.Course;
import info.toegepaste.model.Exam;
import info.toegepaste.model.Score;
import info.toegepaste.model.Student;
import info.toegepaste.service.CourseService;
import info.toegepaste.service.PdfService;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.activation.DataSource;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.apache.commons.mail.*;

//import sun.jdbc.odbc.ee.DataSource;
//import org.apache.poi.poifs.nio.DataSource;
/**
 *
 * @author Steffi
 */
@ManagedBean(name = "pdfController")
public class PdfController implements Serializable {

    @EJB
    private PdfService pdfService;
    @EJB
    private CourseService courseService;

    public PdfService getPdfService() {
        return pdfService;
    }

    public void setPdfService(PdfService pdfService) {
        this.pdfService = pdfService;
    }

    public void CreatePdf(long studentId) {
        //Fonts
        Font header = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD);
        Font normal = new Font(Font.FontFamily.HELVETICA, 12, Font.NORMAL);

        //Declaration
        Student student = new Student();
        List<Course> courses;
        List<Exam> exams = null;
        Score score;
        int totaal = 0; //totaal van een vak berekenen
        int maxScore = 0; //max soore van een test
        int aantalVakken = 0; //aantal vakken waaraan student heeft deelgenomen
        int klasgemiddelde = 0; //hulpvariabele om klasgemiddelde op te vragen
        Double totaalGemiddelde = 0.0; //eindpercentage
        Double vermenigvuldigfactor = 0.0; //om score op 20 weer te geven
        ArrayList<Integer> gemiddeldes = new ArrayList<Integer>(); //gemiddeldes per vak
        ArrayList<Integer> gemiddeldesKlas = new ArrayList<Integer>(); //gemiddelde klas per vak

        PdfPCell cell;

        //Document
        Document document = new Document();

        //student opvullen
        student = pdfService.getStudent(studentId);

        //Tempfix
        //student.getClassgroup();

        //cursussen opvullen
        courses = courseService.getWithClassgroup(student.getClassgroup());

        for (int i = 0; i < courses.size(); i++) {
            //testen opvullen
            exams = pdfService.getExamsByCourse(courses.get(i));
            for (int j = 0; j < exams.size(); j++) {
                score = pdfService.getScoreByExamAndStudent(exams.get(j), student);
                //nagaan of de student heeft deelgenomen aan de test
                if (score != null) {
                    // score op 20 weergeven
                    vermenigvuldigfactor = 20 / (double) exams.get(j).getTotal();
                    totaal += (int) (score.getScore() * vermenigvuldigfactor);
                }
                //klasgemiddelde voor een vak
                klasgemiddelde = pdfService.getAverageScoreByExam(exams.get(j));
                gemiddeldesKlas.add(klasgemiddelde);
            }
            if (exams.size() > 0) {
                gemiddeldes.add(totaal / exams.size());
                totaalGemiddelde += totaal / exams.size();
                aantalVakken++;
            }
            totaal = 0;
            exams.clear();
        }

        totaalGemiddelde = totaalGemiddelde / aantalVakken * 5;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            PdfWriter.getInstance(document, outputStream);

            document.open();
            //header
            document.add(new Paragraph("Studenten Rapport", header));
            document.add(new Paragraph("Student:" + student.getFirstname() + " " + student.getLastname(), normal));
            document.add(new Paragraph("Klas:" + student.getClassgroup(), normal));
            document.add(new Paragraph("Alle scores worden op 20 weergegeven", normal));

            //table
            PdfPTable table = new PdfPTable(3);

            //create tableheaders
            PdfPCell c1 = new PdfPCell(new Phrase("Vak"));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);

            c1 = new PdfPCell(new Phrase("Score"));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);

            c1 = new PdfPCell(new Phrase("Gemiddelde"));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);
            table.setHeaderRows(1);

            for (int i = 0; i < aantalVakken; i++) {
                cell = new PdfPCell(new Phrase(gemiddeldes.get(i).toString()));
                if (gemiddeldes.get(i) < 10) {
                    cell.setBackgroundColor(BaseColor.RED);
                }

                table.addCell(courses.get(i).getName());
                table.addCell(cell);
                table.addCell(gemiddeldesKlas.get(i).toString());
            }

            document.add(table);

            //end
            document.add(new Paragraph("Totaal behaald percentage:" + totaalGemiddelde, normal));
            document.close();

        } catch (DocumentException e) {
            e.printStackTrace();
            //  } catch (FileNotFoundException e) {
            //   e.printStackTrace();
        }
        MultiPartEmail email = new MultiPartEmail();
        email.setHostName("smtp.googlemail.com");
        email.setSmtpPort(587);
        email.setSSLOnConnect(true);
        email.setAuthenticator(new DefaultAuthenticator("bitmescoretracker@gmail.com", "bitmeScore"));
        try {

            email.addTo(student.getEmail());
            email.setFrom("bitmescoretracker@gmail.com");
            email.setSubject("Requested scores , scoretracker");
            email.setMsg("Here are the scores you requested!");

            //  EmailAttachment attachment = new EmailAttachment();
            //attachment.setDescription("PDF met uw punten");
            // attachment.setName(student.getFirstname() + " " + student.getLastname() + " scores");
            byte[] bytes = outputStream.toByteArray();

            DataSource source = new javax.mail.util.ByteArrayDataSource(bytes, "application/pdf");
            System.out.println("Stuff");

            /* try{
             source = outputStream.write(bytes);
             }catch(IOException e){
                 e.printStackTrace();
             }*/
            email.attach(source, student.getFirstname() + " " + student.getLastname() + " scores", "stuff");

            email.send();
        } catch (EmailException ex) {
            ex.printStackTrace();
        }

    }

    public void sendPdf(Student student) {

    }

    public PdfController() {
    }

    @PostConstruct
    public void init() {
    }
}