css.controller.AdmissionResultsController.java Source code

Java tutorial

Introduction

Here is the source code for css.controller.AdmissionResultsController.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 css.controller;

import com.css.admission.AdmissionContestSolver;
import com.css.admission.PdfPublisher;
import com.css.database.dao.Student;
import com.css.database.entitymanager.EntityManager;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * @author Radu
 */
public class AdmissionResultsController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView model = new ModelAndView("resultscomputed");
        String resultsFile = "Rezultate.pdf";

        EntityManager em = new EntityManager();
        List<Student> students = em.getAll(Student.class);

        int ghs = Integer.parseInt(httpServletRequest.getParameter("hsproc"));
        int gbac = Integer.parseInt(httpServletRequest.getParameter("bacproc"));
        int gadm = Integer.parseInt(httpServletRequest.getParameter("examproc"));
        int bdNo = Integer.parseInt(httpServletRequest.getParameter("budgetedNo"));
        int taxNo = Integer.parseInt(httpServletRequest.getParameter("taxNo"));

        AdmissionContestSolver acs = new AdmissionContestSolver(students, bdNo, taxNo, ghs, gbac, gadm);
        acs.compute();
        PdfPublisher pdfpub = new PdfPublisher(students, resultsFile, "Rezultate Admitere");
        pdfpub.exportPDF();
        downloadFile(resultsFile, httpServletResponse);

        return model;
    }

    private void downloadFile(String file, HttpServletResponse httpServletResponse) throws Exception {
        OutputStream out = httpServletResponse.getOutputStream();
        FileInputStream in = new FileInputStream(file);
        byte[] buffer = new byte[4096];
        int length;
        while ((length = in.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }
        in.close();
        out.flush();
    }
}