Search in sources :

Example 1 with MCRVersionedMetadata

use of org.mycore.datamodel.ifs2.MCRVersionedMetadata in project mycore by MyCoRe-Org.

the class MCRMetadataHistoryCommands method buildDerivateHistory.

private static Stream<MCRMetaHistoryItem> buildDerivateHistory(MCRObjectID derId) {
    try {
        List<MCRMetadataVersion> versions = Collections.emptyList();
        MCRMetadataStore store = MCRXMLMetadataManager.instance().getStore(derId);
        if (store instanceof MCRVersioningMetadataStore) {
            MCRVersionedMetadata versionedMetadata = ((MCRVersioningMetadataStore) store).retrieve(derId.getNumberAsInteger());
            versions = versionedMetadata.listVersions();
        }
        if (versions.isEmpty()) {
            return buildSimpleDerivateHistory(derId);
        } else {
            return buildDerivateHistory(derId, versions);
        }
    } catch (IOException e) {
        LogManager.getLogger().error("Error while getting history of {}", derId);
        return Stream.empty();
    }
}
Also used : MCRVersionedMetadata(org.mycore.datamodel.ifs2.MCRVersionedMetadata) MCRMetadataVersion(org.mycore.datamodel.ifs2.MCRMetadataVersion) MCRVersioningMetadataStore(org.mycore.datamodel.ifs2.MCRVersioningMetadataStore) IOException(java.io.IOException) MCRMetadataStore(org.mycore.datamodel.ifs2.MCRMetadataStore)

Example 2 with MCRVersionedMetadata

use of org.mycore.datamodel.ifs2.MCRVersionedMetadata in project mycore by MyCoRe-Org.

the class MCRXMLMetadataManagerTest method retrieve.

@Test
public void retrieve() throws JDOMException, IOException, SAXException {
    assertEquals("Stored document ID do not match:", MyCoRe_document_00000001.id.toString(), SAX_BUILDER.build(new ByteArrayInputStream(MyCoRe_document_00000001.blob)).getRootElement().getAttributeValue("id"));
    getStore().create(MyCoRe_document_00000001.id, new MCRByteContent(MyCoRe_document_00000001.blob, MCR_document_00000001.lastModified.getTime()), MyCoRe_document_00000001.lastModified);
    MCRVersionedMetadata data = getStore().getVersionedMetaData(MyCoRe_document_00000001.id);
    assertFalse(data.isDeleted());
    assertFalse(data.isDeletedInRepository());
    Document doc = getStore().retrieveXML(MyCoRe_document_00000001.id);
    assertEquals("Stored document ID do not match:", MyCoRe_document_00000001.id.toString(), doc.getRootElement().getAttributeValue("id"));
    try {
        doc = getStore().retrieveXML(MCR_document_00000001.id);
        if (doc != null) {
            fail("Requested " + MCR_document_00000001.id + ", retrieved wrong document:\n" + new XMLOutputter(Format.getPrettyFormat()).outputString(doc));
        }
    } catch (Exception e) {
    // this is ok
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) MCRVersionedMetadata(org.mycore.datamodel.ifs2.MCRVersionedMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) MCRByteContent(org.mycore.common.content.MCRByteContent) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) Test(org.junit.Test)

Example 3 with MCRVersionedMetadata

use of org.mycore.datamodel.ifs2.MCRVersionedMetadata in project mycore by MyCoRe-Org.

the class MCRMigrationCommands method addServFlags.

@MCRCommand(syntax = "migrate author servflags for {0}", help = "Create missing servflags for createdby and modifiedby for object {0}. (MCR-786)", order = 10)
public static void addServFlags(String id) throws IOException, MCRPersistenceException, MCRActiveLinkException, MCRAccessException {
    MCRObjectID objectID = MCRObjectID.getInstance(id);
    MCRBase obj = MCRMetadataManager.retrieve(objectID);
    MCRObjectService service = obj.getService();
    if (!service.isFlagTypeSet(MCRObjectService.FLAG_TYPE_CREATEDBY)) {
        // the egg
        MCRVersionedMetadata versionedMetadata = MCRXMLMetadataManager.instance().getVersionedMetaData(objectID);
        String createUser = null, modifyUser = null;
        if (versionedMetadata == null) {
            LOGGER.warn("Cannot restore author servflags as there are no versions available. Setting to current user.");
            createUser = MCRSessionMgr.getCurrentSession().getUserInformation().getUserID();
            modifyUser = createUser;
        } else {
            List<MCRMetadataVersion> versions = versionedMetadata.listVersions();
            MCRMetadataVersion firstVersion = versions.get(0);
            for (MCRMetadataVersion version : versions) {
                if (version.getType() == 'A') {
                    // get last 'added'
                    firstVersion = version;
                }
            }
            MCRMetadataVersion lastVersion = versions.get(versions.size() - 1);
            createUser = firstVersion.getUser();
            modifyUser = lastVersion.getUser();
        }
        service.addFlag(MCRObjectService.FLAG_TYPE_CREATEDBY, createUser);
        LOGGER.info("{}, created by: {}", objectID, createUser);
        if (!service.isFlagTypeSet(MCRObjectService.FLAG_TYPE_MODIFIEDBY)) {
            // the chicken
            // have to restore also modifiedby from version history.
            LOGGER.info("{}, modified by: {}", objectID, modifyUser);
            service.addFlag(MCRObjectService.FLAG_TYPE_CREATEDBY, modifyUser);
        }
        obj.setImportMode(true);
        if (obj instanceof MCRDerivate) {
            MCRMetadataManager.updateMCRDerivateXML((MCRDerivate) obj);
        } else {
            MCRMetadataManager.update((MCRObject) obj);
        }
    }
}
Also used : MCRVersionedMetadata(org.mycore.datamodel.ifs2.MCRVersionedMetadata) MCRObjectService(org.mycore.datamodel.metadata.MCRObjectService) MCRMetadataVersion(org.mycore.datamodel.ifs2.MCRMetadataVersion) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 4 with MCRVersionedMetadata

use of org.mycore.datamodel.ifs2.MCRVersionedMetadata in project mycore by MyCoRe-Org.

the class MCRMetadataHistoryCommands method buildObjectHistory.

private static Stream<MCRMetaHistoryItem> buildObjectHistory(MCRObjectID objId) {
    try {
        List<MCRMetadataVersion> versions = Collections.emptyList();
        MCRMetadataStore store = MCRXMLMetadataManager.instance().getStore(objId);
        if (store instanceof MCRVersioningMetadataStore) {
            MCRVersionedMetadata versionedMetadata = ((MCRVersioningMetadataStore) store).retrieve(objId.getNumberAsInteger());
            versions = versionedMetadata.listVersions();
        }
        if (versions.isEmpty()) {
            return buildSimpleObjectHistory(objId);
        } else {
            return buildObjectHistory(objId, versions);
        }
    } catch (IOException e) {
        LogManager.getLogger().error("Error while getting history of {}", objId);
        return Stream.empty();
    }
}
Also used : MCRVersionedMetadata(org.mycore.datamodel.ifs2.MCRVersionedMetadata) MCRMetadataVersion(org.mycore.datamodel.ifs2.MCRMetadataVersion) MCRVersioningMetadataStore(org.mycore.datamodel.ifs2.MCRVersioningMetadataStore) IOException(java.io.IOException) MCRMetadataStore(org.mycore.datamodel.ifs2.MCRMetadataStore)

Example 5 with MCRVersionedMetadata

use of org.mycore.datamodel.ifs2.MCRVersionedMetadata in project mycore by MyCoRe-Org.

the class MCRXMLMetadataManager method getVersionedMetaData.

public MCRVersionedMetadata getVersionedMetaData(MCRObjectID id) throws IOException {
    if (id == null) {
        return null;
    }
    MCRMetadataStore metadataStore = getStore(id);
    if (!(metadataStore instanceof MCRVersioningMetadataStore)) {
        return null;
    }
    MCRVersioningMetadataStore verStore = (MCRVersioningMetadataStore) metadataStore;
    return verStore.retrieve(id.getNumberAsInteger());
}
Also used : MCRVersioningMetadataStore(org.mycore.datamodel.ifs2.MCRVersioningMetadataStore) MCRMetadataStore(org.mycore.datamodel.ifs2.MCRMetadataStore)

Aggregations

MCRVersionedMetadata (org.mycore.datamodel.ifs2.MCRVersionedMetadata)4 IOException (java.io.IOException)3 MCRMetadataStore (org.mycore.datamodel.ifs2.MCRMetadataStore)3 MCRMetadataVersion (org.mycore.datamodel.ifs2.MCRMetadataVersion)3 MCRVersioningMetadataStore (org.mycore.datamodel.ifs2.MCRVersioningMetadataStore)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Document (org.jdom2.Document)1 JDOMException (org.jdom2.JDOMException)1 XMLOutputter (org.jdom2.output.XMLOutputter)1 Test (org.junit.Test)1 MCRByteContent (org.mycore.common.content.MCRByteContent)1 MCRBase (org.mycore.datamodel.metadata.MCRBase)1 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)1 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)1 MCRObjectService (org.mycore.datamodel.metadata.MCRObjectService)1 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)1 SAXException (org.xml.sax.SAXException)1