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());
}
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());
}
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;
}
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);
}
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()));
}
}
}
Aggregations