use of org.mycore.datamodel.metadata.MCRBase 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.MCRBase in project mycore by MyCoRe-Org.
the class MCRSolrIndexHandlerFactory method getIndexHandler.
public MCRSolrIndexHandler getIndexHandler(MCRBase... derOrObjs) {
if (derOrObjs.length == 1) {
MCRBaseContent content = new MCRBaseContent(derOrObjs[0]);
return getIndexHandler(content, derOrObjs[0].getId());
}
HashMap<MCRObjectID, MCRContent> contentMap = new HashMap<>();
for (MCRBase derOrObj : derOrObjs) {
MCRBaseContent content = new MCRBaseContent(derOrObj);
contentMap.put(derOrObj.getId(), content);
}
return getIndexHandler(contentMap);
}
use of org.mycore.datamodel.metadata.MCRBase in project mycore by MyCoRe-Org.
the class MCRURNObjectXPathMetadataManager method removeIdentifier.
@Override
public void removeIdentifier(MCRDNBURN identifier, MCRBase obj, String additional) {
String xpath = getProperties().get("Xpath");
Document xml = obj.createXML();
XPathFactory xPathFactory = XPathFactory.instance();
XPathExpression<Element> xp = xPathFactory.compile(xpath, Filters.element());
List<Element> elements = xp.evaluate(xml);
elements.stream().filter(element -> element.getTextTrim().equals(identifier.asString())).forEach(Element::detach);
}
use of org.mycore.datamodel.metadata.MCRBase in project mycore by MyCoRe-Org.
the class MCRURNGranularOAIRegistrationService method registerURNsDerivate.
private MCRDNBURN registerURNsDerivate(MCRBase obj, String additional, MCRObjectDerivate derivate) throws MCRPersistentIdentifierException {
LOGGER.info("Add URNs to all files of {}", obj.getId());
Session session = MCRHIBConnection.instance().getSession();
Path path = MCRPath.getPath(obj.getId().toString(), "/");
MCRFileCollectingFileVisitor<Path> collectingFileVisitor = new MCRFileCollectingFileVisitor<>();
try {
Files.walkFileTree(path, collectingFileVisitor);
} catch (IOException e) {
throw new MCRPersistentIdentifierException("Could not walk derivate file tree!", e);
}
List<String> ignoreFileNamesList = getIgnoreFileList();
List<Predicate<String>> predicateList = ignoreFileNamesList.stream().map(Pattern::compile).map(Pattern::asPredicate).collect(Collectors.toList());
List<MCRPath> pathList = collectingFileVisitor.getPaths().stream().filter(file -> predicateList.stream().noneMatch(p -> p.test(file.toString().split(":")[1]))).map(p -> (MCRPath) p).sorted().collect(Collectors.toList());
MCRDNBURN newURN = getNewIdentifier(obj.getId(), additional);
String setID = obj.getId().getNumberAsString();
for (int pathListIndex = 0; pathListIndex < pathList.size(); pathListIndex++) {
MCRDNBURN subURN = newURN.toGranular(setID, pathListIndex + 1, pathList.size());
derivate.getOrCreateFileMetadata(pathList.get(pathListIndex), subURN.asString()).setUrn(subURN.asString());
MCRPI databaseEntry = new MCRPI(subURN.asString(), getType(), obj.getId().toString(), pathList.get(pathListIndex).getOwnerRelativePath(), this.getRegistrationServiceID(), null);
session.save(databaseEntry);
}
derivate.setURN(newURN.asString());
MCRPI databaseEntry = new MCRPI(newURN.asString(), getType(), obj.getId().toString(), "", this.getRegistrationServiceID(), new Date());
session.save(databaseEntry);
return newURN;
}
use of org.mycore.datamodel.metadata.MCRBase in project mycore by MyCoRe-Org.
the class MCRPersistentIdentifierRegistrationResource method register.
@POST
@Path("service/{serviceName}/{mycoreId}")
@Produces(MediaType.APPLICATION_JSON)
public Response register(@PathParam("serviceName") String serviceName, @PathParam("mycoreId") String mycoreId, @DefaultValue("") @QueryParam("additional") String additional) {
if (!MCRPIRegistrationServiceManager.getInstance().getServiceIDList().contains(serviceName)) {
return Response.status(Response.Status.BAD_REQUEST).entity(buildErrorJSON("No Registration Service found for " + serviceName)).build();
}
MCRPIRegistrationService<MCRPersistentIdentifier> registrationService = MCRPIRegistrationServiceManager.getInstance().getRegistrationService(serviceName);
MCRObjectID mycoreIDObject;
try {
mycoreIDObject = MCRObjectID.getInstance(mycoreId);
} catch (MCRException e) {
LOGGER.error(e);
return Response.status(Response.Status.BAD_REQUEST).entity(buildErrorJSON("The provided id " + mycoreId + " seems to be invalid!", e)).build();
}
MCRPersistentIdentifier identifier;
MCRBase object = MCRMetadataManager.retrieve(mycoreIDObject);
try {
identifier = registrationService.register(object, additional, true);
} catch (MCRPersistentIdentifierException | MCRActiveLinkException e) {
LOGGER.error(e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(buildErrorJSON("Error while register new identifier!", e)).build();
} catch (MCRAccessException e) {
LOGGER.error(e);
return Response.status(Response.Status.FORBIDDEN).entity(buildErrorJSON("Error while register new identifier!", e)).build();
}
return Response.status(Response.Status.CREATED).entity(buildIdentifierObject(identifier)).build();
}
Aggregations