serwlety.dodaj.DokumentServlet.java Source code

Java tutorial

Introduction

Here is the source code for serwlety.dodaj.DokumentServlet.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 serwlety.dodaj;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.stream.Collectors;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import org.bson.types.ObjectId;

/**
 *
 * @author mat-bi
 */
@MultipartConfig
public class DokumentServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet DokumentServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet DokumentServlet at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * 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 {
        request.getRequestDispatcher("/WEB-INF/dodaj/dokument.jsp").forward(request, response);
    }

    /**
     * Handles the HTTP <code>POST</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 doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        DBCollection db = baza.FabrykaPolaczenia.getInstance().getConnection().getCollection("Osoba");
        BasicDBObject obiekt = new BasicDBObject();
        obiekt.append("nazwa", request.getParameter("nazwa")).append("tagi", request.getParameterValues("tagi"));
        ArrayList<String> pliki = new ArrayList<>();
        for (Part plik : request.getParts().stream().filter(part -> "plik".equals(part.getName()))
                .collect(Collectors.toList())) {
            String nazwa = plik.getName() + Math.random();
            //Files.copy(plik.getInputStream(), new File("/home/mat-bi/NetBeansProjects/MongoTAB2/web/pliki",nazwa).toPath());
            Files.copy(plik.getInputStream(), new File("C:\\Users\\JG\\Desktop", nazwa).toPath());
            pliki.add(nazwa);
        }
        obiekt.append("pliki", pliki);
        BasicDBObject findOne = (BasicDBObject) db
                .findOne(new BasicDBObject("_id", new ObjectId(request.getParameter("id"))));
        if (findOne.get("dokumenty") == null) {
            findOne.append("dokumenty", new BasicDBList());
        }

        if (request.getParameterValues("dodatkowe") != null) {
            ArrayList<BasicDBObject> lista = new ArrayList<>();
            int i = 0;
            for (String dodatkowe : request.getParameterValues("dodatkowe")) {
                lista.add(new BasicDBObject(dodatkowe, request.getParameterValues("dodatkowe_wartosc")[i++]));
            }
            obiekt.append("dodatkowe", lista);
        }
        ((BasicDBList) findOne.get("dokumenty")).add(obiekt);
        db.update(new BasicDBObject("_id", new ObjectId(request.getParameter("id"))), findOne);
        request.getRequestDispatcher("/WEB-INF/dodaj/dokument_dodany.jsp").forward(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}