Ejemplo n.º 1
0
  /**
   * 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();
    }
  }
Ejemplo n.º 2
0
  /**
   * Import users
   *
   * @param nodes list of users nodes (wrapper node, thus only one)
   * @return
   */
  private Boolean processUsers(NodeList nodes) {
    NodeList users = nodes.item(0).getChildNodes();

    for (int i = 0; i < users.getLength(); i++) {
      if (users.item(i).getNodeType() == Node.ELEMENT_NODE) {
        Element userNode = (Element) users.item(i);

        User user = new User();

        user.setIduser(Integer.parseInt(getTagValue("iduser", userNode)));
        user.setForename(getTagValue("forename", userNode));
        user.setSurname(getTagValue("surname", userNode));
        user.setPermitNumber(getTagValue("permitNumber", userNode));
        user.setAddress(getTagValue("address", userNode));
        user.setEmail(getTagValue("email", userNode));
        user.setRegistered(
            DatatypeConverter.parseDateTime(getTagValue("registered", userNode)).getTime());
        user.setExpire(DatatypeConverter.parseDateTime(getTagValue("expire", userNode)).getTime());
        user.setPassword(getTagValue("password", userNode));
        user.setLevel(getTagValue("level", userNode));

        try {
          userMgr.Save(user);
        } catch (EJBException ex) {
          ex.printStackTrace(System.out);
        }
      }
    }
    return true;
  }
Ejemplo n.º 3
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ě."));
  }
Ejemplo n.º 4
0
  /**
   * Import borrows
   *
   * @param nodes
   * @return
   */
  public Boolean processBorrows(NodeList nodes) {
    NodeList borrows = nodes.item(0).getChildNodes();

    for (int i = 0; i < borrows.getLength(); i++) {
      if (borrows.item(i).getNodeType() == Node.ELEMENT_NODE) {
        Element borrowNode = (Element) borrows.item(i);

        Borrow borrow = new Borrow();

        borrow.setIdborrow(Integer.parseInt(getTagValue("idborrow", borrowNode)));
        borrow.setProlongations(Integer.parseInt(getTagValue("prolongations", borrowNode)));
        borrow.setBorrowed(
            DatatypeConverter.parseDateTime(getTagValue("borrowed", borrowNode)).getTime());

        // set returned date (can be null)
        try {
          if (getTagValue("returned", borrowNode) != null) {
            borrow.setReturned(
                DatatypeConverter.parseDateTime(getTagValue("returned", borrowNode)).getTime());
          }
        } catch (NullPointerException e) {
        }

        User user = userMgr.findByIduser(Integer.parseInt(getTagValue("user", borrowNode)));
        if (user == null) {
          continue;
        }

        borrow.setUser(user);

        Exemplar exemplar =
            exemplarMgr.findByIdexemplar(Integer.parseInt(getTagValue("exemplar", borrowNode)));
        if (exemplar == null) {
          continue;
        }

        borrow.setExemplar(exemplar);

        try {
          borrowMgr.Save(borrow);
        } catch (EJBException ex) {
          ex.printStackTrace(System.out);
        }
      }
    }
    return true;
  }
Ejemplo n.º 5
0
  /**
   * Import bookings
   *
   * @param nodes
   * @return
   */
  private Boolean processBookings(NodeList nodes) {
    NodeList bookings = nodes.item(0).getChildNodes();

    for (int i = 0; i < bookings.getLength(); i++) {
      if (bookings.item(i).getNodeType() == Node.ELEMENT_NODE) {
        Element bookingNode = (Element) bookings.item(i);

        Booking booking = new Booking();

        booking.setIdbooking(Integer.parseInt(getTagValue("idbooking", bookingNode)));
        booking.setState(Integer.parseInt(getTagValue("state", bookingNode)));
        booking.setDate(
            DatatypeConverter.parseDateTime(getTagValue("date", bookingNode)).getTime());

        Book book = bookMgr.findByIdbook(Integer.parseInt(getTagValue("book", bookingNode)));
        if (book == null) {
          continue;
        }

        booking.setBook(book);

        User user = userMgr.findByIduser(Integer.parseInt(getTagValue("user", bookingNode)));
        if (user == null) {
          continue;
        }

        booking.setUser(user);

        try {
          bookingMgr.Save(booking);
        } catch (EJBException ex) {
          ex.printStackTrace(System.out);
        }
      }
    }
    return true;
  }