use of org.jdom2.filter.Filter in project mycore by MyCoRe-Org.
the class MCRMODSMetadataShareAgent method distributeMetadata.
/* (non-Javadoc)
* @see org.mycore.datamodel.metadata.share.MCRMetadataShareAgent#inheritMetadata(org.mycore.datamodel.metadata.MCRObject)
*/
@Override
public void distributeMetadata(MCRObject holder) throws MCRPersistenceException, MCRAccessException {
MCRMODSWrapper holderWrapper = new MCRMODSWrapper(holder);
List<MCRMetaLinkID> children = holder.getStructure().getChildren();
if (!children.isEmpty()) {
LOGGER.info("Update inherited metadata");
for (MCRMetaLinkID childIdRef : children) {
MCRObjectID childId = childIdRef.getXLinkHrefID();
if (MCRMODSWrapper.isSupported(childId)) {
LOGGER.info("Update: {}", childIdRef);
MCRObject child = MCRMetadataManager.retrieveMCRObject(childId);
MCRMODSWrapper childWrapper = new MCRMODSWrapper(child);
inheritToChild(holderWrapper, childWrapper);
LOGGER.info("Saving: {}", childIdRef);
try {
MCRMetadataManager.update(child);
} catch (MCRPersistenceException | MCRAccessException e) {
throw new MCRPersistenceException("Error while updating inherited metadata", e);
}
}
}
}
Collection<String> recipientIds = MCRLinkTableManager.instance().getSourceOf(holder.getId(), MCRLinkTableManager.ENTRY_TYPE_REFERENCE);
for (String rId : recipientIds) {
MCRObjectID recipientId = MCRObjectID.getInstance(rId);
if (MCRMODSWrapper.isSupported(recipientId)) {
LOGGER.info("distribute metadata to {}", rId);
MCRObject recipient = MCRMetadataManager.retrieveMCRObject(recipientId);
MCRMODSWrapper recipientWrapper = new MCRMODSWrapper(recipient);
for (Element relatedItem : recipientWrapper.getLinkedRelatedItems()) {
String holderId = relatedItem.getAttributeValue("href", MCRConstants.XLINK_NAMESPACE);
if (holder.getId().toString().equals(holderId)) {
@SuppressWarnings("unchecked") Filter<Content> sharedMetadata = (Filter<Content>) Filters.element("part", MCRConstants.MODS_NAMESPACE).negate();
relatedItem.removeContent(sharedMetadata);
relatedItem.addContent(holderWrapper.getMODS().cloneContent());
LOGGER.info("Saving: {}", recipientId);
try {
MCRMetadataManager.update(recipient);
} catch (MCRPersistenceException | MCRAccessException e) {
throw new MCRPersistenceException("Error while updating shared metadata", e);
}
}
}
}
}
}
use of org.jdom2.filter.Filter in project mycore by MyCoRe-Org.
the class MCRMODSWrapper method setElement.
/**
* Sets or adds an element with target name and value. The element name and attributes are used as xpath expression
* to filter for an element. The attributes are used with and operation if present.
*/
public Optional<Element> setElement(String elementName, String elementValue, Map<String, String> attributes) {
boolean isAttributeDataPresent = attributes != null && !attributes.isEmpty();
boolean isValuePresent = elementValue != null && !elementValue.isEmpty();
if (!isValuePresent && !isAttributeDataPresent) {
return Optional.empty();
}
StringBuilder xPath = new StringBuilder("mods:");
xPath.append(elementName);
// add attributes to xpath with and operator
if (isAttributeDataPresent) {
xPath.append('[');
Iterator<Map.Entry<String, String>> attributeIterator = attributes.entrySet().iterator();
while (attributeIterator.hasNext()) {
Map.Entry<String, String> attribute = attributeIterator.next();
xPath.append('@').append(attribute.getKey()).append("='").append(attribute.getValue()).append('\'');
if (attributeIterator.hasNext()) {
xPath.append(" and ");
}
}
xPath.append(']');
}
Element element = getElement(xPath.toString());
if (element == null) {
element = addElement(elementName);
if (isAttributeDataPresent) {
for (Map.Entry<String, String> entry : attributes.entrySet()) {
element.setAttribute(entry.getKey(), entry.getValue());
}
}
}
if (isValuePresent) {
element.setText(elementValue.trim());
}
return Optional.of(element);
}
use of org.jdom2.filter.Filter in project mycore by MyCoRe-Org.
the class MCRClassificationMappingEventHandler method createMapping.
private void createMapping(MCRObject obj) {
MCRMODSWrapper mcrmodsWrapper = new MCRMODSWrapper(obj);
if (mcrmodsWrapper.getMODS() == null) {
return;
}
// vorher alle mit generator *-mycore löschen
mcrmodsWrapper.getElements("mods:classification[contains(@generator, '" + GENERATOR_SUFFIX + "')]").stream().forEach(Element::detach);
LOGGER.info("check mappings {}", obj.getId());
mcrmodsWrapper.getMcrCategoryIDs().stream().map(categoryId -> DAO.getCategory(categoryId, 0)).filter(Objects::nonNull).map(MCRClassificationMappingEventHandler::getMappings).flatMap(Collection::stream).distinct().forEach(mapping -> {
String taskMessage = String.format(Locale.ROOT, "add mapping from '%s' to '%s'", mapping.getKey().toString(), mapping.getValue().toString());
LOGGER.info(taskMessage);
Element mappedClassification = mcrmodsWrapper.addElement("classification");
String generator = getGenerator(mapping.getKey(), mapping.getValue());
mappedClassification.setAttribute("generator", generator);
MCRClassMapper.assignCategory(mappedClassification, mapping.getValue());
});
LOGGER.debug("mapping complete.");
}
use of org.jdom2.filter.Filter in project mycore by MyCoRe-Org.
the class MCRMODSURNPersistentIdentifierMetadataManager method getIdentifier.
@Override
public Optional<MCRPersistentIdentifier> getIdentifier(MCRBase obj, String additional) throws MCRPersistentIdentifierException {
MCRObject object = checkObject(obj);
MCRMODSWrapper wrapper = new MCRMODSWrapper(object);
Element element = wrapper.getElement(MODS_IDENTIFIER_TYPE_URN);
if (element == null) {
return Optional.empty();
}
String urnText = element.getTextNormalize();
return new MCRDNBURNParser().parse(urnText).filter(Objects::nonNull).map(MCRPersistentIdentifier.class::cast);
}
use of org.jdom2.filter.Filter in project mycore by MyCoRe-Org.
the class MCRAccessCondition method lookupCategoryID.
/* (non-Javadoc)
* @see org.mycore.mods.classification.MCRAuthorityInfo#lookupCategoryID()
*/
@Override
protected MCRCategoryID lookupCategoryID() {
Collection<MCRCategory> categoryByURI = MCRAuthorityWithURI.getCategoryByURI(href);
if (categoryByURI.size() > 1) {
throw new MCRException(href + " is ambigous: " + categoryByURI.stream().map(MCRCategory::getId).collect(Collectors.toList()));
}
if (!categoryByURI.isEmpty()) {
return categoryByURI.iterator().next().getId();
}
// maybe href is in form {authorityURI}#{categId}
String categId, authorityURI = null;
try {
authorityURI = href.substring(0, href.lastIndexOf("#"));
categId = href.substring(authorityURI.length() + 1);
} catch (RuntimeException e) {
LOGGER.warn("authorityURI:{}, valueURI:{}", authorityURI, href);
return null;
}
int internalStylePos = authorityURI.indexOf(MCRAuthorityWithURI.CLASS_URI_PART);
if (internalStylePos > 0) {
String rootId = authorityURI.substring(internalStylePos + MCRAuthorityWithURI.CLASS_URI_PART.length());
MCRCategoryID catId = new MCRCategoryID(rootId, categId);
if (DAO.exist(catId)) {
return catId;
}
}
Collection<MCRCategory> classes = MCRAuthorityWithURI.getCategoryByURI(authorityURI);
return classes.stream().map(cat -> new MCRCategoryID(cat.getId().getRootID(), categId)).filter(DAO::exist).findFirst().orElse(null);
}
Aggregations