Ejemplo n.º 1
0
  /**
   * Import books
   *
   * @param nodes
   * @return
   */
  private Boolean processBooks(NodeList nodes) {
    NodeList books = nodes.item(0).getChildNodes();

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

        Book book = new Book();

        book.setIdbook(Integer.parseInt(getTagValue("idbook", bookNode)));
        book.setCode(getTagValue("code", bookNode));
        book.setEdition(Integer.parseInt(getTagValue("edition", bookNode)));
        book.setPages(Integer.parseInt(getTagValue("pages", bookNode)));
        book.setPlace(getTagValue("place", bookNode));
        book.setYear(DatatypeConverter.parseDateTime(getTagValue("year", bookNode)).getTime());
        book.setType(getTagValue("type", bookNode));
        book.setName(getTagValue("name", bookNode));

        // find and set publisher
        Publisher publisher =
            publisherMgr.findByIdpublisher(Integer.parseInt(getTagValue("publisher", bookNode)));
        if (publisher == null) {
          continue;
        }

        book.setPublisher(publisher);

        // find and set genre
        Genre genre = genreMgr.findByIdgenre(Integer.parseInt(getTagValue("genre", bookNode)));
        if (genre == null) {
          continue;
        }

        book.setGenre(genre);

        // setup book authors
        List<String> authors = getTagsValues("authorCollection", bookNode);

        if (book.getAuthorCollection() == null) {
          book.setAuthorCollection(new ArrayList<Author>());
        }

        for (String authorId : authors) {
          Author author = authorMgr.findByIdauthor(Integer.parseInt(authorId));
          if (author != null) {
            //						book.getAuthorCollection().add(author);
            author.getBooksCollection().add(book);
            authorMgr.save(author);
          }
        }

        try {
          bookMgr.save(book);
        } catch (EJBException ex) {
          ex.printStackTrace(System.out);
        }
      }
    }
    return true;
  }
Ejemplo n.º 2
0
  public static void main(String[] args) {
    ArrayList<String> List = new ArrayList<String>();
    Scanner sc = new Scanner(System.in);

    // SETTING THE CONDITION FOR CONTINUATION
    String choice = "y";
    while (choice.equalsIgnoreCase("y")) {

      // SELECTION OF PRODUCT TYPE
      System.out.println("Product type:(Book/Software)?");
      String type = sc.nextLine();

      // FOR BOOK, ENTER THE REQUIRED INFORMATION AND STORE TO DESIGNATED LOCATION
      if (type.equalsIgnoreCase("Book")) {
        System.out.println("Enter the book description:");
        String descript = sc.nextLine();
        System.out.println("Enter the book price:");
        double price = sc.nextDouble();
        sc.nextLine();
        System.out.println("Enter the book author:");
        String author = sc.nextLine();
        Book a = new Book();
        a.setAuthor(author);
        a.setDescription(descript);
        a.setPrice(price);
        a.setCode(type);
        List.add(a.toString("detail"));
      } else {

        // FOR SOFTWARE, ENTER THE REQUIRED INFORMATION AND STORE TO DESIGNATED LOCATION
        if (type.equalsIgnoreCase("Software")) {
          System.out.println("Enter the software description:");
          String descript = sc.nextLine();
          System.out.println("Enter the software price:");
          double price = sc.nextDouble();
          sc.nextLine();
          System.out.println("Enter the software version:");
          String version = sc.nextLine();
          Software s = new Software();
          s.setVersion(version);
          s.setDescription(descript);
          s.setPrice(price);
          s.setCode(type);
          List.add(s.toString("detail"));
        }
      }
      System.out.println("Do you wish to continue?(y/n)");
      choice = sc.nextLine();
    }
    System.out.println("PRODUCT DETAILS DISPLAYED BELOW");
    System.out.println("\n");
    Iterator<String> itr = List.iterator();
    while (itr.hasNext()) {
      String in = itr.next();
      System.out.println(in);
    }
    sc.close();
  }
  public void testClob() throws Exception {
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    Book b = new Book();
    b.setShortDescription("Hibernate Bible");
    b.setFullText("Hibernate in Action aims to...");
    b.setCode(new Character[] {'a', 'b', 'c'});
    b.setCode2(new char[] {'a', 'b', 'c'});
    s.persist(b);
    tx.commit();
    s.close();

    s = openSession();
    tx = s.beginTransaction();
    Book b2 = (Book) s.get(Book.class, b.getId());
    assertNotNull(b2);
    assertEquals(b2.getFullText(), b.getFullText());
    assertEquals(b2.getCode()[1].charValue(), b.getCode()[1].charValue());
    assertEquals(b2.getCode2()[2], b.getCode2()[2]);
    tx.commit();
    s.close();
  }