Search in sources :

Example 11 with Publication

use of cz.metacentrum.perun.cabinet.model.Publication in project perun by CESNET.

the class PublicationManagerIntegrationTest method stripPublicationParams.

@Test
public void stripPublicationParams() throws Exception {
    System.out.println("PublicationManagerIntegrationTest.stripPublicationParams");
    Publication p = new Publication();
    p.setCategoryId(publicationOne.getCategoryId());
    p.setCreatedBy(sess.getPerunPrincipal().getActor());
    p.setCreatedDate(new Date());
    p.setExternalId(999);
    //MU
    p.setPublicationSystemId(publicationOne.getPublicationSystemId());
    p.setYear(2010);
    p.setRank(0.0);
    p.setLocked(false);
    // set long params
    String title = RandomStringUtils.random(1100, true, false);
    String main = RandomStringUtils.random(4020, true, false);
    String isbn = RandomStringUtils.random(40, true, false);
    String doi = RandomStringUtils.random(300, true, false);
    p.setIsbn(isbn);
    p.setMain(main);
    p.setTitle(title);
    p.setDoi(doi);
    p.setCreatedByUid(sess.getPerunPrincipal().getUserId());
    p = getCabinetManager().createPublication(sess, p);
    // if stripping works, must have been created
    assertTrue("Returned ID shouldn't be < 0.", p.getId() > 0);
    assertTrue("Doi shouldn't be longer than 256.", p.getDoi().length() <= 256);
    assertTrue("Isbn shouldn't be longer than 32.", p.getIsbn().length() <= 32);
    assertTrue("Title shouldn't be longer than 1024.", p.getTitle().length() <= 1024);
    assertTrue("Main shouldn't be longer than 4000.", p.getMain().length() <= 4000);
}
Also used : Publication(cz.metacentrum.perun.cabinet.model.Publication) Date(java.util.Date) Test(org.junit.Test)

Example 12 with Publication

use of cz.metacentrum.perun.cabinet.model.Publication in project perun by CESNET.

the class PublicationManagerIntegrationTest method createPublicationTest.

// ------------- TESTS --------------------------------------------
@Test
public void createPublicationTest() throws Exception {
    System.out.println("PublicationManagerIntegrationTest.createPublicationTest");
    Publication p = new Publication();
    p.setCategoryId(publicationOne.getCategoryId());
    // Pepa id 10, userId 1
    p.setCreatedBy(sess.getPerunPrincipal().getActor());
    p.setCreatedDate(new Date());
    p.setExternalId(999);
    p.setIsbn("isbn 123-4556-899");
    p.setMain("KERBEROS main zaznam.");
    //MU
    p.setPublicationSystemId(publicationOne.getPublicationSystemId());
    p.setTitle("Kerberos");
    p.setYear(2010);
    p.setRank(0.0);
    p.setLocked(false);
    p.setDoi("DOI");
    p.setCreatedByUid(sess.getPerunPrincipal().getUserId());
    p = getCabinetManager().createPublication(sess, p);
    assertTrue("Returned ID shouldn't be < 0.", p.getId() > 0);
}
Also used : Publication(cz.metacentrum.perun.cabinet.model.Publication) Date(java.util.Date) Test(org.junit.Test)

Example 13 with Publication

use of cz.metacentrum.perun.cabinet.model.Publication in project perun by CESNET.

the class MUStrategy method parseResponse.

/**
	 * Parse String response as XML document and retrieve Publications from it.
	 * @param xml XML response from MU Prezentator
	 * @return List of Publications
	 * @throws CabinetException If anything fails
	 */
protected List<Publication> parseResponse(String xml) throws CabinetException {
    assert xml != null;
    List<Publication> result = new ArrayList<Publication>();
    //hook for titles with &
    xml = xml.replace("&", "&amp;");
    //log.debug("RESPONSE: "+xml);
    //Create new document factory builder
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        throw new CabinetException("Error when creating newDocumentBuilder.", ex);
    }
    Document doc;
    try {
        doc = builder.parse(new InputSource(new StringReader(xml)));
    } catch (SAXParseException ex) {
        throw new CabinetException("Error when parsing uri by document builder.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
    } catch (SAXException ex) {
        throw new CabinetException("Problem with parsing is more complex, not only invalid characters.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
    } catch (IOException ex) {
        throw new CabinetException("Error when parsing uri by document builder. Problem with input or output.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
    }
    //Prepare xpath expression
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression publicationsQuery;
    try {
        publicationsQuery = xpath.compile("/P/UL/publication");
    } catch (XPathExpressionException ex) {
        throw new CabinetException("Error when compiling xpath query.", ex);
    }
    NodeList nodeList;
    try {
        nodeList = (NodeList) publicationsQuery.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException ex) {
        throw new CabinetException("Error when evaluate xpath query on document.", ex);
    }
    //Test if there is any nodeset in result
    if (nodeList.getLength() == 0) {
        //There is no results, return empty subjects
        return result;
    }
    //Iterate through nodes and convert them to Map<String,String>
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node singleNode = nodeList.item(i);
        // remove node from original structure in order to keep access time constant (otherwise is exp.)
        singleNode.getParentNode().removeChild(singleNode);
        try {
            Publication publication = convertNodeToPublication(singleNode);
            result.add(publication);
        } catch (InternalErrorException ex) {
            log.error("Unable to parse Publication:", ex);
        }
    }
    return result;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Publication(cz.metacentrum.perun.cabinet.model.Publication) IOException(java.io.IOException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 14 with Publication

use of cz.metacentrum.perun.cabinet.model.Publication in project perun by CESNET.

the class MUStrategy method convertNodeToPublication.

/**
	 * Convert node of response to Publication
	 *
	 * @param node XML node to convert
	 * @return Publication instance
	 * @throws InternalErrorException
	 */
private Publication convertNodeToPublication(Node node) throws InternalErrorException {
    Publication publication = new Publication();
    publication.setExternalId(((Double) getValueFromXpath(node, "./id/text()", XPathConstants.NUMBER)).intValue());
    publication.setMain(((String) getValueFromXpath(node, "./main/text()", XPathConstants.STRING)));
    //optional properties
    String isbn = (String) getValueFromXpath(node, "./isbn/text()", XPathConstants.STRING);
    publication.setIsbn((isbn != null) ? isbn : "");
    String title = (String) getValueFromXpath(node, "./title/text()", XPathConstants.STRING);
    publication.setTitle((title != null) ? title : "");
    String doi = (String) getValueFromXpath(node, "./doi/text()", XPathConstants.STRING);
    publication.setDoi((doi != null) ? doi : "");
    int year = ((Double) getValueFromXpath(node, "./year/text()", XPathConstants.NUMBER)).intValue();
    publication.setYear(year);
    // parse authors
    List<Author> authors = new ArrayList<Author>();
    String[] names = ((String) getValueFromXpath(node, "./authors/names/text()", XPathConstants.STRING)).split("#");
    for (String name : names) {
        try {
            Author author = new Author();
            Map<String, String> commonName = Utils.parseCommonName(name);
            author.setFirstName(commonName.get("firstName"));
            author.setLastName(WordUtils.capitalize(commonName.get("lastName")));
            authors.add(author);
        } catch (Exception ex) {
            log.error("Exception [{}] caught while processing authors of response: [{}]", ex, node);
        }
    }
    publication.setAuthors(authors);
    return publication;
}
Also used : ArrayList(java.util.ArrayList) Publication(cz.metacentrum.perun.cabinet.model.Publication) Author(cz.metacentrum.perun.cabinet.model.Author) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 15 with Publication

use of cz.metacentrum.perun.cabinet.model.Publication in project perun by CESNET.

the class OBD30Strategy method convertNodeToPublication.

/**
	 * Convert node of response to Publication
	 *
	 * @param node XML node to convert
	 * @return Publication instance
	 * @throws InternalErrorException
	 */
private Publication convertNodeToPublication(Node node) throws InternalErrorException {
    Publication publication = new Publication();
    publication.setExternalId(Integer.valueOf(node.getAttributes().getNamedItem("id").getNodeValue()));
    NodeList titleList = (NodeList) getValueFromXpath(node, "./titul_list/titul", XPathConstants.NODESET);
    for (int i = 0; i < titleList.getLength(); i++) {
        Node singleNode = titleList.item(i);
        String original = (String) getValueFromXpath(singleNode, "./original/text()", XPathConstants.STRING);
        // use original name (language) of publication
        if ("ano".equalsIgnoreCase(original)) {
            String title = (String) getValueFromXpath(singleNode, "./nazev/text()", XPathConstants.STRING);
            publication.setTitle((title != null) ? title : "");
        }
    }
    String isbn = (String) getValueFromXpath(node, "./isbn/text()", XPathConstants.STRING);
    isbn = (isbn != null) ? isbn : "";
    String issn = (String) getValueFromXpath(node, "./issn/text()", XPathConstants.STRING);
    issn = (issn != null) ? issn : "";
    if (!issn.isEmpty())
        publication.setIsbn(issn);
    if (!isbn.isEmpty())
        publication.setIsbn(isbn);
    int year = ((Double) getValueFromXpath(node, "./rok/text()", XPathConstants.NUMBER)).intValue();
    publication.setYear(year);
    String source = (String) getValueFromXpath(node, "./zdroj_nazev/text()", XPathConstants.STRING);
    source = (source != null) ? source : "";
    String source_year = (String) getValueFromXpath(node, "./rocnik/text()", XPathConstants.STRING);
    source_year = (source_year != null) ? source_year : "";
    String pages = (String) getValueFromXpath(node, "./strany/text()", XPathConstants.STRING);
    pages = (pages != null) ? pages : "";
    String issue = (String) getValueFromXpath(node, "./cislo/text()", XPathConstants.STRING);
    issue = (issue != null) ? issue : "";
    // parse authors
    List<Author> authors = new ArrayList<Author>();
    NodeList authorList = (NodeList) getValueFromXpath(node, "./autor_list/autor", XPathConstants.NODESET);
    for (int i = 0; i < authorList.getLength(); i++) {
        Node singleNode = authorList.item(i);
        Author author = new Author();
        author.setTitleBefore((String) getValueFromXpath(singleNode, "./titul_pred/text()", XPathConstants.STRING));
        author.setTitleAfter((String) getValueFromXpath(singleNode, "./titul_za/text()", XPathConstants.STRING));
        author.setFirstName((String) getValueFromXpath(singleNode, "./jmeno/text()", XPathConstants.STRING));
        author.setLastName((String) getValueFromXpath(singleNode, "./prijmeni/text()", XPathConstants.STRING));
        authors.add(author);
    }
    publication.setAuthors(authors);
    // create main entry
    String main = "";
    for (int i = 0; i < authors.size(); i++) {
        if (i == 0) {
            main += authors.get(i).getLastName().toUpperCase() + " " + authors.get(i).getFirstName();
        } else {
            main += authors.get(i).getFirstName() + " " + authors.get(i).getLastName().toUpperCase();
        }
        main += " a ";
    }
    if (main.length() > 3) {
        main = main.substring(0, main.length() - 3) + ". ";
    }
    main += publication.getTitle() + ". ";
    main += (publication.getYear() != 0) ? publication.getYear() + ". " : "";
    main += (!source.isEmpty()) ? source + "," : "";
    main += (!source_year.isEmpty()) ? " roč. " + source_year + "," : "";
    main += (!issue.isEmpty()) ? " č. " + issue + "," : "";
    main += (!pages.isEmpty()) ? " s. " + pages + "," : "";
    main += (!isbn.isEmpty()) ? " ISBN: " + isbn + "." : "";
    main += (!issn.isEmpty()) ? " ISSN: " + issn + "." : "";
    publication.setMain(main);
    return publication;
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Publication(cz.metacentrum.perun.cabinet.model.Publication) Author(cz.metacentrum.perun.cabinet.model.Author)

Aggregations

Publication (cz.metacentrum.perun.cabinet.model.Publication)20 CabinetException (cz.metacentrum.perun.cabinet.bl.CabinetException)9 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)6 Author (cz.metacentrum.perun.cabinet.model.Author)5 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)5 Date (java.util.Date)5 Category (cz.metacentrum.perun.cabinet.model.Category)3 PublicationSystem (cz.metacentrum.perun.cabinet.model.PublicationSystem)3 IOException (java.io.IOException)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 XPathExpressionException (javax.xml.xpath.XPathExpressionException)3 Node (org.w3c.dom.Node)3 NodeList (org.w3c.dom.NodeList)3 SAXException (org.xml.sax.SAXException)3 SAXParseException (org.xml.sax.SAXParseException)3 Authorship (cz.metacentrum.perun.cabinet.model.Authorship)2 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)2 StringReader (java.io.StringReader)2 HashSet (java.util.HashSet)2