Search in sources :

Example 1 with MCRMetadataVersion

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

the class MCRXMLMetadataManager method retrieveContent.

/**
 * @param mcrid
 *            the id of the object to be retrieved
 * @param revision
 *            the revision to be returned, specify -1 if you want to
 *            retrieve the latest revision (includes deleted objects also)
 * @return a {@link MCRContent} representing the {@link MCRObject} of the
 *         given revision or <code>null</code> if there is no such object
 *         with the given revision
 */
public MCRContent retrieveContent(MCRObjectID mcrid, long revision) throws IOException {
    LOGGER.info("Getting object {} in revision {}", mcrid, revision);
    MCRMetadataVersion version = getMetadataVersion(mcrid, revision);
    if (version != null) {
        return version.retrieve();
    }
    return null;
}
Also used : MCRMetadataVersion(org.mycore.datamodel.ifs2.MCRMetadataVersion)

Example 2 with MCRMetadataVersion

use of org.mycore.datamodel.ifs2.MCRMetadataVersion 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 3 with MCRMetadataVersion

use of org.mycore.datamodel.ifs2.MCRMetadataVersion 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 MCRMetadataVersion

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

the class MCRMetadataHistoryCommands method buildDerivateHistory.

private static Stream<MCRMetaHistoryItem> buildDerivateHistory(MCRObjectID derId, List<MCRMetadataVersion> versions) throws IOException {
    boolean exist = false;
    LogManager.getLogger().debug("Complete history rebuild of {} should be possible", derId);
    ArrayList<MCRMetaHistoryItem> items = new ArrayList<>(100);
    for (MCRMetadataVersion version : versions) {
        String user = version.getUser();
        Instant revDate = version.getDate().toInstant();
        if (version.getType() == MCRMetadataVersion.DELETED) {
            if (exist) {
                items.add(delete(derId, user, revDate));
                exist = false;
            }
        } else {
            // created or updated
            int timeOffset = 0;
            if (version.getType() == MCRMetadataVersion.CREATED && !exist) {
                items.add(create(derId, user, revDate));
                timeOffset = 1;
                exist = true;
            }
            try {
                MCRDerivate derivate = new MCRDerivate(version.retrieve().asXML());
                boolean derivateIsHidden = !derivate.getDerivate().isDisplayEnabled();
                if (derivateIsHidden && exist) {
                    items.add(delete(derId, user, revDate.plusMillis(timeOffset)));
                    exist = false;
                } else if (!derivateIsHidden && !exist) {
                    items.add(create(derId, user, revDate.plusMillis(timeOffset)));
                    exist = true;
                }
            } catch (JDOMException | SAXException e) {
                LogManager.getLogger().error("Error while reading revision {} of {}", version.getRevision(), derId, e);
            }
        }
    }
    return items.stream();
}
Also used : MCRMetadataVersion(org.mycore.datamodel.ifs2.MCRMetadataVersion) Instant(java.time.Instant) ArrayList(java.util.ArrayList) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) JDOMException(org.jdom2.JDOMException) SAXException(org.xml.sax.SAXException)

Example 5 with MCRMetadataVersion

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

the class MCRMetadataHistoryCommands method buildObjectHistory.

private static Stream<MCRMetaHistoryItem> buildObjectHistory(MCRObjectID objId, List<MCRMetadataVersion> versions) throws IOException {
    boolean exist = false;
    LogManager.getLogger().debug("Complete history rebuild of {} should be possible", objId);
    ArrayList<MCRMetaHistoryItem> items = new ArrayList<>(100);
    for (MCRMetadataVersion version : versions) {
        String user = version.getUser();
        Instant revDate = version.getDate().toInstant();
        if (version.getType() == MCRMetadataVersion.DELETED) {
            if (exist) {
                items.add(delete(objId, user, revDate));
                exist = false;
            }
        } else {
            // created or updated
            int timeOffset = 0;
            if (version.getType() == MCRMetadataVersion.CREATED && !exist) {
                items.add(create(objId, user, revDate));
                timeOffset = 1;
                exist = true;
            }
            try {
                MCRObject obj = new MCRObject(version.retrieve().asXML());
                boolean objectIsHidden = MCRMetadataHistoryManager.objectIsHidden(obj);
                if (objectIsHidden && exist) {
                    items.add(delete(objId, user, revDate.plusMillis(timeOffset)));
                    exist = false;
                } else if (!objectIsHidden && !exist) {
                    items.add(create(objId, user, revDate.plusMillis(timeOffset)));
                    exist = true;
                }
            } catch (JDOMException | SAXException e) {
                LogManager.getLogger().error("Error while reading revision {} of {}", version.getRevision(), objId, e);
            }
        }
    }
    return items.stream();
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetadataVersion(org.mycore.datamodel.ifs2.MCRMetadataVersion) Instant(java.time.Instant) ArrayList(java.util.ArrayList) JDOMException(org.jdom2.JDOMException) SAXException(org.xml.sax.SAXException)

Aggregations

MCRMetadataVersion (org.mycore.datamodel.ifs2.MCRMetadataVersion)7 IOException (java.io.IOException)3 JDOMException (org.jdom2.JDOMException)3 MCRVersionedMetadata (org.mycore.datamodel.ifs2.MCRVersionedMetadata)3 SAXException (org.xml.sax.SAXException)3 Instant (java.time.Instant)2 ArrayList (java.util.ArrayList)2 MCRMetadataStore (org.mycore.datamodel.ifs2.MCRMetadataStore)2 MCRVersioningMetadataStore (org.mycore.datamodel.ifs2.MCRVersioningMetadataStore)2 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)2 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)2 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)2 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 MCRAccessException (org.mycore.access.MCRAccessException)1 MCRException (org.mycore.common.MCRException)1 MCRPersistenceException (org.mycore.common.MCRPersistenceException)1