/**
   * 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/xml;charset=UTF-8");
    response.setHeader("Content-Disposition", "attachment; filename=export.xml");
    PrintWriter out = response.getWriter();
    try {
      DatabaseWrapper db = new DatabaseWrapper();

      // fill database wrapper with data
      db.setUsers(userMgr.findAll());
      db.setBooks(bookMgr.findAll());
      db.setAuthors(authorMgr.findAll());
      db.setBookings(bookingMgr.findAll());
      db.setBorrows(borrowMgr.findAll());
      db.setExemplars(exemplarMgr.findAll());
      db.setGenres(genreMgr.findAll());
      db.setPublishers(publisherMgr.findAll());

      // Step 2 - convert to xml
      JAXBContext jaxbContext = JAXBContext.newInstance(DatabaseWrapper.class);

      Marshaller marshaller = jaxbContext.createMarshaller();
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

      marshaller.marshal(db, out);

    } catch (Exception ex) {
      ex.printStackTrace(out);
    } finally {
      out.close();
    }
  }
Esempio n. 2
0
  /**
   * File upload listener
   *
   * @param event
   * @throws Exception
   */
  public void listener(FileUploadEvent event) throws Exception {
    UploadedFile file = event.getUploadedFile();

    try {

      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
      Document doc = dBuilder.parse(file.getInputStream());
      doc.getDocumentElement().normalize();

      processUsers(doc.getElementsByTagName("users"));
      processAuthors(doc.getElementsByTagName("authors"));
      processPublishers(doc.getElementsByTagName("publishers"));
      processGenres(doc.getElementsByTagName("genres"));
      processBooks(doc.getElementsByTagName("books"));
      processExemplars(doc.getElementsByTagName("exemplars"));
      processBookings(doc.getElementsByTagName("bookings"));
      processBorrows(doc.getElementsByTagName("borrows"));

      userMgr.flush();
      bookMgr.flush();
      authorMgr.flush();
      bookingMgr.flush();
      exemplarMgr.flush();
      genreMgr.flush();
      publisherMgr.flush();
      borrowMgr.flush();

      userMgr.clear();
      bookMgr.clear();
      authorMgr.clear();
      bookingMgr.clear();
      exemplarMgr.clear();
      genreMgr.clear();
      publisherMgr.clear();
      borrowMgr.clear();

    } catch (Exception e) {
      FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Import se nezdařil."));
      e.printStackTrace(System.out);

      return;
    }

    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Import proběhl úspěšně."));
  }