Search in sources :

Example 1 with Format

use of org.talend.esb.examples.ebook.model.Format in project tesb-rt-se by Talend.

the class BookService method sendBook.

@POST
@Path("{id}")
public Response sendBook(@PathParam("id") String id) {
    String recipient = "root@localhost";
    LOG.info("Sending book {} to {}.", id, recipient);
    Book book = bookRepo.getBook(id);
    Format format = getMobiFormat(book);
    if (format == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    URI uri = format.getFile();
    new MailSender().send(recipient, uri);
    LOG.info("Mail sent successfully");
    return Response.ok().build();
}
Also used : Format(org.talend.esb.examples.ebook.model.Format) Book(org.talend.esb.examples.ebook.model.Book) URI(java.net.URI) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 2 with Format

use of org.talend.esb.examples.ebook.model.Format in project tesb-rt-se by Talend.

the class BookParser method parse.

public Book parse(Document doc) throws XPathExpressionException, URISyntaxException {
    Book book = new Book();
    String id = xpath.evaluate("//pgterms:ebook/@rdf:about", doc);
    book.setId(id);
    book.setTitle(xpath.evaluate("//dcterms:title/text()", doc));
    book.setCreator(xpath.evaluate("//dcterms:creator//pgterms:name/text()", doc));
    NodeList files = (NodeList) xpath.evaluate("//pgterms:file", doc, XPathConstants.NODESET);
    // NodeList subjects = (NodeList)xpath.evaluate("//dcterms:subject/rdf:Description/rdf:value", doc, XPathConstants.NODESET);
    for (int c = 0; c < files.getLength(); c++) {
        Format format = parseFormat((Element) files.item(c));
        if (format.getMediaType().startsWith("image")) {
            book.setCover(format.getFile());
        }
        book.getFormats().add(format);
    }
    return book;
}
Also used : Format(org.talend.esb.examples.ebook.model.Format) Book(org.talend.esb.examples.ebook.model.Book) NodeList(org.w3c.dom.NodeList)

Example 3 with Format

use of org.talend.esb.examples.ebook.model.Format in project tesb-rt-se by Talend.

the class BookParser method parseFormat.

private Format parseFormat(Element file) throws XPathExpressionException {
    Format format = new Format();
    String about = xpath.evaluate("@rdf:about", file);
    try {
        format.setFile(new URI(about));
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
    }
    format.setExtent(new Integer(xpath.evaluate("dcterms:extent", file)));
    format.setMediaType(xpath.evaluate("dcterms:format/rdf:Description/rdf:value", file));
    format.setModified(xpath.evaluate("dcterms:modified", file));
    return format;
}
Also used : Format(org.talend.esb.examples.ebook.model.Format) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 4 with Format

use of org.talend.esb.examples.ebook.model.Format in project tesb-rt-se by Talend.

the class BookRepositoryTest method testEMLifecycleWithCoordination.

/**
 * Test EntityManager life cycle can be extended by outer Coordination
 */
@Test
public void testEMLifecycleWithCoordination() throws Exception {
    Coordination coordination = coordinator.begin("coord", 10000);
    Book book = createBook("My title");
    try {
        bookRepository.add(book);
        Book book2 = bookRepository.getBook(book.getId());
        Format format = book2.getFormats().get(0);
        Assert.assertEquals(book.getFormats().get(0).getFile().toString(), format.getFile().toString());
    } finally {
        bookRepository.delete(book.getId());
        coordination.end();
    }
}
Also used : Format(org.talend.esb.examples.ebook.model.Format) Coordination(org.osgi.service.coordinator.Coordination) Book(org.talend.esb.examples.ebook.model.Book) Test(org.junit.Test)

Example 5 with Format

use of org.talend.esb.examples.ebook.model.Format in project tesb-rt-se by Talend.

the class BookRepositoryImpl method add.

@Override
public void add(Book book) {
    if (getBook(book.getId()) != null) {
        LOG.info("Ignoring book {} as it already exists.", book.getId());
        return;
    }
    for (Format fomat : book.getFormats()) {
        em.persist(fomat);
    }
    persistSubjects(book);
    em.persist(book);
    if ("error1".equals(book.getTitle())) {
        throw new RuntimeException("Test for error handling. Should cause tx rollback");
    }
}
Also used : Format(org.talend.esb.examples.ebook.model.Format)

Aggregations

Format (org.talend.esb.examples.ebook.model.Format)7 Book (org.talend.esb.examples.ebook.model.Book)5 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)2 Test (org.junit.Test)2 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Coordination (org.osgi.service.coordinator.Coordination)1 Document (org.w3c.dom.Document)1 NodeList (org.w3c.dom.NodeList)1