Search in sources :

Example 1 with MCRBase

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);
        }
    }
}
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 2 with MCRBase

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);
}
Also used : HashMap(java.util.HashMap) MCRBaseContent(org.mycore.common.content.MCRBaseContent) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRContent(org.mycore.common.content.MCRContent)

Example 3 with MCRBase

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);
}
Also used : MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) XPathFactory(org.jdom2.xpath.XPathFactory) MCRPersistentIdentifier(org.mycore.pi.MCRPersistentIdentifier) MCRException(org.mycore.common.MCRException) Text(org.jdom2.Text) XPathExpression(org.jdom2.xpath.XPathExpression) Document(org.jdom2.Document) List(java.util.List) MCRObject(org.mycore.datamodel.metadata.MCRObject) Optional(java.util.Optional) MCRPersistentIdentifierMetadataManager(org.mycore.pi.MCRPersistentIdentifierMetadataManager) Filters(org.jdom2.filter.Filters) MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) Element(org.jdom2.Element) XPathFactory(org.jdom2.xpath.XPathFactory) Element(org.jdom2.Element) Document(org.jdom2.Document)

Example 4 with MCRBase

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;
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRBase(org.mycore.datamodel.metadata.MCRBase) Arrays(java.util.Arrays) Date(java.util.Date) Session(org.hibernate.Session) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) ArrayList(java.util.ArrayList) MCRPI(org.mycore.pi.backend.MCRPI) MCRFileCollectingFileVisitor(org.mycore.pi.MCRFileCollectingFileVisitor) MCRAccessException(org.mycore.access.MCRAccessException) Path(java.nio.file.Path) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) MCRObjectDerivate(org.mycore.datamodel.metadata.MCRObjectDerivate) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRHIBConnection(org.mycore.backend.hibernate.MCRHIBConnection) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) MCRPIRegistrationService(org.mycore.pi.MCRPIRegistrationService) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MCRPersistentIdentifierManager(org.mycore.pi.MCRPersistentIdentifierManager) Pattern(java.util.regex.Pattern) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) LogManager(org.apache.logging.log4j.LogManager) Pattern(java.util.regex.Pattern) MCRPI(org.mycore.pi.backend.MCRPI) IOException(java.io.IOException) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) Date(java.util.Date) Predicate(java.util.function.Predicate) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRFileCollectingFileVisitor(org.mycore.pi.MCRFileCollectingFileVisitor) Session(org.hibernate.Session)

Example 5 with MCRBase

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();
}
Also used : MCRException(org.mycore.common.MCRException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRAccessException(org.mycore.access.MCRAccessException) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPersistentIdentifier(org.mycore.pi.MCRPersistentIdentifier) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Aggregations

MCRBase (org.mycore.datamodel.metadata.MCRBase)18 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)13 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)9 IRI (org.apache.abdera.i18n.iri.IRI)5 MCRException (org.mycore.common.MCRException)5 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)5 MCRSwordCollectionProvider (org.mycore.sword.application.MCRSwordCollectionProvider)5 MCRAccessException (org.mycore.access.MCRAccessException)4 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)4 MCRPersistentIdentifier (org.mycore.pi.MCRPersistentIdentifier)4 MCRPI (org.mycore.pi.backend.MCRPI)4 MCRMetadataManager (org.mycore.datamodel.metadata.MCRMetadataManager)3 IOException (java.io.IOException)2 Date (java.util.Date)2 List (java.util.List)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 Document (org.jdom2.Document)2 Element (org.jdom2.Element)2 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)2