Search in sources :

Example 1 with MCRObjectStructure

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, "");
    }
}
Also used : MCRMetaClassification(org.mycore.datamodel.metadata.MCRMetaClassification) MCRMetaDerivateLink(org.mycore.datamodel.metadata.MCRMetaDerivateLink) MCRObjectMetadata(org.mycore.datamodel.metadata.MCRObjectMetadata) MCRMetaLink(org.mycore.datamodel.metadata.MCRMetaLink) MCRObjectStructure(org.mycore.datamodel.metadata.MCRObjectStructure) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference) HashSet(java.util.HashSet)

Example 2 with MCRObjectStructure

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);
    }
}
Also used : MCRObjectService(org.mycore.datamodel.metadata.MCRObjectService) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) MCRObjectStructure(org.mycore.datamodel.metadata.MCRObjectStructure) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRDNBURN(org.mycore.pi.urn.MCRDNBURN) Document(org.jdom2.Document) MCRPI(org.mycore.pi.backend.MCRPI) JDOMException(org.jdom2.JDOMException) MCRTileJob(org.mycore.iview2.services.MCRTileJob) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) Path(java.nio.file.Path) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) MCRLinkTableManager(org.mycore.datamodel.common.MCRLinkTableManager) MCRPath(org.mycore.datamodel.niofs.MCRPath) Collection(java.util.Collection) MCRPersistenceException(org.mycore.common.MCRPersistenceException) XPathExpression(org.jdom2.xpath.XPathExpression) MCREntityManagerProvider(org.mycore.backend.jpa.MCREntityManagerProvider) MCRVersionedMetadata(org.mycore.datamodel.ifs2.MCRVersionedMetadata) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) SAXException(org.xml.sax.SAXException) Optional(java.util.Optional) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRDNBURNParser(org.mycore.pi.urn.MCRDNBURNParser) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) Element(org.jdom2.Element) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRLINKHREF(org.mycore.backend.jpa.links.MCRLINKHREF) XPathFactory(org.jdom2.xpath.XPathFactory) MCRConstants(org.mycore.common.MCRConstants) TypedQuery(javax.persistence.TypedQuery) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) MCRLINKHREFPK_(org.mycore.backend.jpa.links.MCRLINKHREFPK_) MCRXMLFunctions(org.mycore.common.xml.MCRXMLFunctions) MCRMetaDerivateLink(org.mycore.datamodel.metadata.MCRMetaDerivateLink) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) MCRAccessException(org.mycore.access.MCRAccessException) Root(javax.persistence.criteria.Root) MCRMetadataVersion(org.mycore.datamodel.ifs2.MCRMetadataVersion) Files(java.nio.file.Files) IOException(java.io.IOException) MCRLINKHREF_(org.mycore.backend.jpa.links.MCRLINKHREF_) EntityManager(javax.persistence.EntityManager) Paths(java.nio.file.Paths) EntityTransaction(javax.persistence.EntityTransaction) MCRSessionMgr(org.mycore.common.MCRSessionMgr) MCRObject(org.mycore.datamodel.metadata.MCRObject) LogManager(org.apache.logging.log4j.LogManager) Filters(org.jdom2.filter.Filters) MCRObjectStructure(org.mycore.datamodel.metadata.MCRObjectStructure) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 3 with MCRObjectStructure

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;
}
Also used : MCRObjectStructure(org.mycore.datamodel.metadata.MCRObjectStructure) MCRObject(org.mycore.datamodel.metadata.MCRObject) Element(org.jdom2.Element) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRAccessException(org.mycore.access.MCRAccessException) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Aggregations

MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)3 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)3 MCRObjectStructure (org.mycore.datamodel.metadata.MCRObjectStructure)3 Element (org.jdom2.Element)2 MCRAccessException (org.mycore.access.MCRAccessException)2 MCRPersistenceException (org.mycore.common.MCRPersistenceException)2 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)2 MCRMetaDerivateLink (org.mycore.datamodel.metadata.MCRMetaDerivateLink)2 MCRObject (org.mycore.datamodel.metadata.MCRObject)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Optional (java.util.Optional)1