use of org.mycore.datamodel.metadata.MCRObjectService 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.metadata.MCRObjectService in project mycore by MyCoRe-Org.
the class MCRSwordUtil method addDatesToEntry.
public static void addDatesToEntry(Entry entry, MCRObject mcrObject) {
MCRObjectService serviceElement = mcrObject.getService();
ArrayList<String> flags = serviceElement.getFlags(MCRObjectService.FLAG_TYPE_CREATEDBY);
flags.addAll(serviceElement.getFlags(MCRObjectService.FLAG_TYPE_MODIFIEDBY));
Set<String> clearedFlags = new LinkedHashSet<>(flags);
clearedFlags.forEach(entry::addAuthor);
Date modifyDate = serviceElement.getDate(MCRObjectService.DATE_TYPE_MODIFYDATE);
Date createDate = serviceElement.getDate(MCRObjectService.DATE_TYPE_CREATEDATE);
entry.setEdited(modifyDate);
entry.setPublished(createDate);
}
use of org.mycore.datamodel.metadata.MCRObjectService in project mycore by MyCoRe-Org.
the class MCRPIRegistrationService method updateFlag.
public void updateFlag(MCRObjectID id, String additional, MCRPI mcrpi) {
MCRBase obj = MCRMetadataManager.retrieve(id);
MCRObjectService service = obj.getService();
ArrayList<String> flags = service.getFlags(MCRPIRegistrationService.PI_FLAG);
Gson gson = getGson();
String stringFlag = flags.stream().filter(_stringFlag -> {
MCRPI flag = gson.fromJson(_stringFlag, MCRPI.class);
return flag.getAdditional().equals(additional) && flag.getIdentifier().equals(mcrpi.getIdentifier());
}).findAny().orElseThrow(() -> new MCRException(new MCRPersistentIdentifierException("Could find flag to update (" + id + "," + additional + "," + mcrpi.getIdentifier() + ")")));
int flagIndex = service.getFlagIndex(stringFlag);
service.removeFlag(flagIndex);
addFlagToObject(obj, mcrpi);
try {
MCRMetadataManager.update(obj);
} catch (Exception e) {
throw new MCRException("Could not update flags of object " + id, e);
}
}
use of org.mycore.datamodel.metadata.MCRObjectService in project mycore by MyCoRe-Org.
the class MCRPIRegistrationService method removeFlagFromObject.
/**
* Removes a flag from a {@link MCRObject}
* @param obj the object
* @param databaseEntry the database entry
* @return the remove entry parsed from json or null
*/
public static MCRPI removeFlagFromObject(MCRBase obj, MCRPI databaseEntry) {
MCRObjectService service = obj.getService();
ArrayList<String> flags = service.getFlags(MCRPIRegistrationService.PI_FLAG);
int flagCount = flags.size();
for (int flagIndex = 0; flagIndex < flagCount; flagIndex++) {
String flag = flags.get(flagIndex);
MCRPI pi = getGson().fromJson(flag, MCRPI.class);
if (pi.getIdentifier().equals(databaseEntry.getIdentifier()) && pi.getAdditional().equals(databaseEntry.getAdditional()) && pi.getService().equals(databaseEntry.getService()) && pi.getType().equals(databaseEntry.getType())) {
service.removeFlag(flagIndex);
return databaseEntry;
}
}
return null;
}
Aggregations