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;
}
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();
}
}
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);
}
}
}
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();
}
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();
}
Aggregations