use of org.mycore.datamodel.metadata.MCRObjectStructure in project mycore by MyCoRe-Org.
the class MCRLinkTableManager method create.
/**
* Creates all references for the given object. You should call {@link #delete(MCRObjectID)} before using this
* method otherwise doublets could occur.
*
* @param obj the object to create the references
*/
public void create(MCRObject obj) {
MCRObjectID mcrId = obj.getId();
// set new entries
MCRObjectMetadata meta = obj.getMetadata();
// use Set for category collection to remove duplicates if there are any
Collection<MCRCategoryID> categories = new HashSet<>();
meta.stream().flatMap(MCRMetaElement::stream).forEach(inf -> {
if (inf instanceof MCRMetaClassification) {
String classId = ((MCRMetaClassification) inf).getClassId();
String categId = ((MCRMetaClassification) inf).getCategId();
categories.add(new MCRCategoryID(classId, categId));
} else if (inf instanceof MCRMetaLinkID) {
addReferenceLink(mcrId.toString(), ((MCRMetaLink) inf).getXLinkHref(), MCRLinkTableManager.ENTRY_TYPE_REFERENCE, "");
} else if (inf instanceof MCRMetaDerivateLink) {
addReferenceLink(mcrId.toString(), ((MCRMetaLink) inf).getXLinkHref(), MCRLinkTableManager.ENTRY_TYPE_DERIVATE_LINK, "");
}
});
MCRCategoryID state = obj.getService().getState();
if (state != null) {
categories.add(state);
}
if (categories.size() > 0) {
MCRCategLinkReference objectReference = new MCRCategLinkReference(mcrId);
MCRCategLinkServiceFactory.getInstance().setLinks(objectReference, categories);
}
// add derivate reference
MCRObjectStructure structure = obj.getStructure();
for (int i = 0; i < structure.getDerivates().size(); i++) {
MCRMetaLinkID lid = structure.getDerivates().get(i);
addReferenceLink(obj.getId(), lid.getXLinkHrefID(), MCRLinkTableManager.ENTRY_TYPE_DERIVATE, "");
}
// add parent reference
if (structure.getParentID() != null) {
addReferenceLink(mcrId, structure.getParentID(), MCRLinkTableManager.ENTRY_TYPE_PARENT, "");
}
}
use of org.mycore.datamodel.metadata.MCRObjectStructure in project mycore by MyCoRe-Org.
the class MCRMigrationCommands method fixMissingChildren.
@MCRCommand(syntax = "add missing children to {0}", help = "Adds missing children to structure of parent {0}. (MCR-1480)", order = 15)
public static void fixMissingChildren(String id) throws IOException, JDOMException, SAXException {
MCRObjectID parentId = MCRObjectID.getInstance(id);
Collection<String> children = MCRLinkTableManager.instance().getSourceOf(parentId, MCRLinkTableManager.ENTRY_TYPE_PARENT);
if (children.isEmpty()) {
return;
}
MCRObject parent = MCRMetadataManager.retrieveMCRObject(parentId);
MCRObjectStructure parentStructure = parent.getStructure();
int sizeBefore = parentStructure.getChildren().size();
children.stream().map(MCRObjectID::getInstance).filter(cid -> !parentStructure.getChildren().stream().anyMatch(candidate -> candidate.getXLinkHrefID().equals(cid))).sorted().map(MCRMigrationCommands::toLinkId).sequential().peek(lid -> LOGGER.info("Adding {} to {}", lid, parentId)).forEach(parentStructure::addChild);
if (parentStructure.getChildren().size() != sizeBefore) {
MCRMetadataManager.fireUpdateEvent(parent);
}
}
use of org.mycore.datamodel.metadata.MCRObjectStructure in project mycore by MyCoRe-Org.
the class MCRUpdateDerivateServlet method updateDerivateXML.
/**
* Updates derivate xml in the persistence backend
* @param editorSubmission
* MyCoRe derivate as XML
* @return
* MCRObjectID of the MyCoRe object
* @throws SAXParseException
* @throws MCRAccessException
*/
private MCRObjectID updateDerivateXML(Document editorSubmission) throws SAXParseException, IOException, MCRAccessException {
MCRObjectID objectID;
Element root = editorSubmission.getRootElement();
root.setAttribute("noNamespaceSchemaLocation", "datamodel-derivate.xsd", XSI_NAMESPACE);
root.addNamespaceDeclaration(XLINK_NAMESPACE);
root.addNamespaceDeclaration(XSI_NAMESPACE);
byte[] xml = new MCRJDOMContent(editorSubmission).asByteArray();
MCRDerivate der = new MCRDerivate(xml, true);
MCRObjectID derivateID = der.getId();
// store entry of derivate xlink:title in object
objectID = der.getDerivate().getMetaLink().getXLinkHrefID();
MCRObject obj = MCRMetadataManager.retrieveMCRObject(objectID);
MCRObjectStructure structure = obj.getStructure();
MCRMetaLinkID linkID = structure.getDerivateLink(derivateID);
linkID.setXLinkTitle(der.getLabel());
try {
MCRMetadataManager.update(obj);
MCRMetadataManager.update(der);
} catch (MCRPersistenceException | MCRAccessException e) {
throw new MCRPersistenceException("Can't store label of derivate " + derivateID + " in derivate list of object " + objectID + ".", e);
}
return objectID;
}
Aggregations