it.unitn.elisco.servlet.admin.DownloadQuestionsServlet.java Source code

Java tutorial

Introduction

Here is the source code for it.unitn.elisco.servlet.admin.DownloadQuestionsServlet.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 it.unitn.elisco.servlet.admin;

import it.unitn.elisco.bean.hibernate.Context;
import it.unitn.elisco.bean.hibernate.Question;
import it.unitn.elisco.dao.ContextDAO;
import it.unitn.elisco.dao.QuestionDAO;
import it.unitn.elisco.utils.Utilities;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Andrea Marcolin
 */
@WebServlet(name = "DownloadQuestionsServlet", urlPatterns = { "/admin/download_questions/*" })
public class DownloadQuestionsServlet extends HttpServlet {

    private static final Logger LOG = LoggerFactory.getLogger(DownloadQuestionsServlet.class);

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        ContextDAO ctxdao = new ContextDAO();
        QuestionDAO qdao = new QuestionDAO();
        long context_id;

        try {
            context_id = Long.parseLong(request.getParameter("id"));
        } catch (NumberFormatException ex) {
            LOG.warn("Invalid request parameter passed to servlet: " + ex.getMessage());
            request.setAttribute("error", "Invalid request parameter");
            request.getRequestDispatcher("/error.jsp").forward(request, response);
            return;
        }

        // Check if the context hidden field sent a valid context_id
        if (context_id != -1) {
            Context context = ctxdao.getContext(context_id);
            // Check if the context exists
            if (context != null) {

                // Get student questions
                List<Question> questions = qdao.getQuestions(context);

                // Get excel file for questions
                Workbook excel = Utilities.getExcelFromQuestionList(
                        context.getCourse().getName() + " - " + context.getTitle() + " - Domande degli studenti",
                        questions);

                // Set response header
                response.setHeader("Content-Type",
                        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
                response.setHeader("Content-Disposition",
                        "attachment; filename=" + context.getCourse().getName() + " - " + "Domande.xlsx");

                // Write to response stream
                ServletOutputStream out = null;
                try {
                    out = response.getOutputStream();
                    excel.write(out);
                    out.flush();
                } finally {
                    if (out != null)
                        try {
                            out.close();
                        } catch (IOException ignore) {
                        }
                }
            } else {
                request.setAttribute("error", "Errore inatteso nei dati della richiesta");
                request.getRequestDispatcher("/error.jsp").forward(request, response);
            }
        } else {
            request.setAttribute("error", "Errore inatteso nei dati della richiesta");
            request.getRequestDispatcher("/error.jsp").forward(request, response);
        }
    }
}