Search in sources :

Example 61 with Format

use of org.jdom2.output.Format in project mycore by MyCoRe-Org.

the class MCRQLSearchUtils method buildFormQuery.

/**
 * Build MCRQuery from editor XML input
 */
public static MCRQuery buildFormQuery(Element root) {
    Element conditions = root.getChild("conditions");
    if (conditions.getAttributeValue("format", "xml").equals("xml")) {
        Element condition = conditions.getChildren().get(0);
        renameElements(condition);
        // Remove conditions without values
        List<Element> empty = new ArrayList<>();
        for (Iterator<Element> it = conditions.getDescendants(new ElementFilter("condition")); it.hasNext(); ) {
            Element cond = it.next();
            if (cond.getAttribute("value") == null) {
                empty.add(cond);
            }
        }
        // Remove empty sort conditions
        Element sortBy = root.getChild("sortBy");
        if (sortBy != null) {
            for (Element field : sortBy.getChildren("field")) {
                if (field.getAttributeValue("name", "").length() == 0) {
                    empty.add(field);
                }
            }
        }
        for (int i = empty.size() - 1; i >= 0; i--) {
            empty.get(i).detach();
        }
        if (sortBy != null && sortBy.getChildren().size() == 0) {
            sortBy.detach();
        }
        // Remove empty returnFields
        Element returnFields = root.getChild("returnFields");
        if (returnFields != null && returnFields.getText().length() == 0) {
            returnFields.detach();
        }
    }
    return MCRQuery.parseXML(root.getDocument());
}
Also used : Element(org.jdom2.Element) ElementFilter(org.jdom2.filter.ElementFilter) ArrayList(java.util.ArrayList)

Example 62 with Format

use of org.jdom2.output.Format in project mycore by MyCoRe-Org.

the class MCRQLSearchUtilsTest method getMCRQuery.

private MCRQuery getMCRQuery(String mcrql) {
    LogManager.getLogger(getClass()).info("Building query from condition: {}", mcrql);
    Element query = new Element("query").setAttribute("numPerPage", "20");
    Element conditions = new Element("conditions").setAttribute("format", "xml");
    MCRCondition<Void> condition = new MCRQueryParser().parse(mcrql);
    query.addContent(conditions);
    conditions.addContent(condition.toXML());
    Document queryDoc = new Document(query);
    return MCRQLSearchUtils.buildFormQuery(queryDoc.getRootElement());
}
Also used : Element(org.jdom2.Element) MCRQueryParser(org.mycore.services.fieldquery.MCRQueryParser) Document(org.jdom2.Document)

Example 63 with Format

use of org.jdom2.output.Format in project mycore by MyCoRe-Org.

the class MCROAIObjectManager method getRecord.

public Record getRecord(Header header, MetadataFormat format) {
    Element recordElement;
    if (header.isDeleted()) {
        return new Record(header);
    }
    try {
        recordElement = getJDOMRecord(getMyCoReId(header.getId()), format);
    } catch (Exception exc) {
        LOGGER.error("unable to get record {} ({})", header.getId(), format.getPrefix(), exc);
        return null;
    }
    Record record = new Record(header);
    if (recordElement.getNamespace().equals(OAIConstants.NS_OAI)) {
        Element metadataElement = recordElement.getChild("metadata", OAIConstants.NS_OAI);
        if (metadataElement != null && !metadataElement.getChildren().isEmpty()) {
            Element metadataChild = metadataElement.getChildren().get(0);
            record.setMetadata(new SimpleMetadata(metadataChild.detach()));
        }
        Element aboutElement = recordElement.getChild("about", OAIConstants.NS_OAI);
        if (aboutElement != null) {
            for (Element aboutChild : aboutElement.getChildren()) {
                record.getAboutList().add(aboutChild.detach());
            }
        }
    } else {
        // handle as metadata
        record.setMetadata(new SimpleMetadata(recordElement));
    }
    return record;
}
Also used : Element(org.jdom2.Element) Record(org.mycore.oai.pmh.Record) SimpleMetadata(org.mycore.oai.pmh.SimpleMetadata)

Example 64 with Format

use of org.jdom2.output.Format in project mycore by MyCoRe-Org.

the class MCRDOMContent method sendTo.

@Override
public void sendTo(OutputStream out) throws IOException {
    org.jdom2.Document jdom;
    try {
        jdom = asXML();
    } catch (JDOMException ex) {
        throw new IOException(ex);
    }
    new XMLOutputter(format).output(jdom, out);
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException)

Example 65 with Format

use of org.jdom2.output.Format in project mycore by MyCoRe-Org.

the class MCRRestAPIObjectsHelper method retrieveMCRObject.

private static MCRObject retrieveMCRObject(String idString) throws MCRRestAPIException {
    // the default value for the key
    String key = "mcr";
    if (idString.contains(":")) {
        int pos = idString.indexOf(":");
        key = idString.substring(0, pos);
        idString = idString.substring(pos + 1);
        if (!key.equals("mcr")) {
            try {
                idString = URLDecoder.decode(idString, "UTF-8");
            } catch (UnsupportedEncodingException e) {
            // will not happen
            }
        // ToDo - Shall we restrict the key set with a property?
        // throw new MCRRestAPIException(MCRRestAPIError.create(Response.Status.BAD_REQUEST,
        // "The ID is not valid.", "The prefix is unkown. Only 'mcr' is allowed."));
        }
    }
    if (key.equals("mcr")) {
        MCRObjectID mcrID = null;
        try {
            mcrID = MCRObjectID.getInstance(idString);
        } catch (Exception e) {
            throw new MCRRestAPIException(Response.Status.BAD_REQUEST, new MCRRestAPIError(MCRRestAPIError.CODE_WRONG_ID, "The MyCoRe ID '" + idString + "' is not valid. - Did you use the proper format: '{project}_{type}_{number}'?", e.getMessage()));
        }
        if (!MCRMetadataManager.exists(mcrID)) {
            throw new MCRRestAPIException(Response.Status.NOT_FOUND, new MCRRestAPIError(MCRRestAPIError.CODE_NOT_FOUND, "There is no object with the given MyCoRe ID '" + idString + "'.", null));
        }
        return MCRMetadataManager.retrieveMCRObject(mcrID);
    } else {
        SolrClient solrClient = MCRSolrClientFactory.getSolrClient();
        SolrQuery query = new SolrQuery();
        query.setQuery(key + ":" + idString);
        try {
            QueryResponse response = solrClient.query(query);
            SolrDocumentList solrResults = response.getResults();
            if (solrResults.getNumFound() == 1) {
                String id = solrResults.get(0).getFieldValue("returnId").toString();
                return retrieveMCRObject(id);
            } else {
                if (solrResults.getNumFound() == 0) {
                    throw new MCRRestAPIException(Response.Status.NOT_FOUND, new MCRRestAPIError(MCRRestAPIError.CODE_NOT_FOUND, "There is no object with the given ID '" + key + ":" + idString + "'.", null));
                } else {
                    throw new MCRRestAPIException(Response.Status.NOT_FOUND, new MCRRestAPIError(MCRRestAPIError.CODE_NOT_FOUND, "The ID is not unique. There are " + solrResults.getNumFound() + " objecst fore the given ID '" + key + ":" + idString + "'.", null));
                }
            }
        } catch (SolrServerException | IOException e) {
            LOGGER.error(e);
            throw new MCRRestAPIException(Response.Status.BAD_REQUEST, new MCRRestAPIError(MCRRestAPIError.CODE_INTERNAL_ERROR, "Internal server error.", e.getMessage()));
        }
    }
}
Also used : MCRRestAPIException(org.mycore.restapi.v1.errors.MCRRestAPIException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) MCRRestAPIError(org.mycore.restapi.v1.errors.MCRRestAPIError) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SolrDocumentList(org.apache.solr.common.SolrDocumentList) IOException(java.io.IOException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) JDOMException(org.jdom2.JDOMException) ParseException(java.text.ParseException) MCRRestAPIException(org.mycore.restapi.v1.errors.MCRRestAPIException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MCRException(org.mycore.common.MCRException) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SolrClient(org.apache.solr.client.solrj.SolrClient) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Aggregations

Element (org.jdom2.Element)56 Document (org.jdom2.Document)20 XMLOutputter (org.jdom2.output.XMLOutputter)19 IOException (java.io.IOException)15 StringWriter (java.io.StringWriter)9 Attribute (org.jdom2.Attribute)9 Format (org.jdom2.output.Format)7 File (java.io.File)5 ArrayList (java.util.ArrayList)5 MCRRestAPIError (org.mycore.restapi.v1.errors.MCRRestAPIError)5 MCRRestAPIException (org.mycore.restapi.v1.errors.MCRRestAPIException)5 JsonWriter (com.google.gson.stream.JsonWriter)4 Date (java.util.Date)4 SAXBuilder (org.jdom2.input.SAXBuilder)4 SimpleDateFormat (java.text.SimpleDateFormat)3 JDOMException (org.jdom2.JDOMException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 MalformedURLException (java.net.MalformedURLException)2 List (java.util.List)2 JComponent (javax.swing.JComponent)2