Search in sources :

Example 26 with MCRObject

use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.

the class MCRTransferPackagePacker method build.

/**
 * Builds the transfer package and returns it.
 *
 * @return the transfer package
 * @throws MCRAccessException
 */
private MCRTransferPackage build() throws MCRAccessException {
    MCRObject source = getSource();
    MCRTransferPackage transferPackage = new MCRTransferPackage(source);
    transferPackage.build();
    return transferPackage;
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject)

Example 27 with MCRObject

use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.

the class MCRTransferPackageUtil method importObjectCLI.

/**
 * Same as {@link #importObject(Path, String)} but returns a list of derivates which
 * should be imported afterwards.
 *
 * @param targetDirectory
 *                the directory where the *.tar was unpacked
 * @param objectId
 *                object id to import
 * @throws JDOMException
 *                coulnd't parse the import order xml
 * @throws IOException
 *                when an I/O error prevents a document from being fully parsed
 * @throws MCRActiveLinkException
 *                if object is created (no real update), see {@link MCRMetadataManager#create(MCRObject)}
 * @throws MCRAccessException
 *                if write permission is missing or see {@link MCRMetadataManager#create(MCRObject)}
 */
public static List<String> importObjectCLI(Path targetDirectory, String objectId) throws JDOMException, IOException, MCRActiveLinkException, MCRAccessException {
    SAXBuilder sax = new SAXBuilder();
    Path targetXML = targetDirectory.resolve(CONTENT_DIRECTORY).resolve(objectId + ".xml");
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(MessageFormat.format("Importing {0}", targetXML.toAbsolutePath().toString()));
    }
    Document objXML = sax.build(targetXML.toFile());
    MCRObject mcr = new MCRObject(objXML);
    mcr.setImportMode(true);
    List<String> derivates = new LinkedList<>();
    // one must copy the ids before updating the mcr objects
    for (MCRMetaLinkID id : mcr.getStructure().getDerivates()) {
        derivates.add(id.getXLinkHref());
    }
    // delete children & derivate -> will be added later
    mcr.getStructure().clearChildren();
    mcr.getStructure().clearDerivates();
    // update
    MCRMetadataManager.update(mcr);
    // return list of derivates
    return derivates;
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) SAXBuilder(org.jdom2.input.SAXBuilder) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) Document(org.jdom2.Document) LinkedList(java.util.LinkedList)

Example 28 with MCRObject

use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.

the class MCRDOICommands method synchronizeDatabase.

@MCRCommand(syntax = "repair registered dois {0}", help = "Contacts the Registration Service and inserts all registered DOIs to the Database. It also updates all media files. The Service ID{0} is the id from the configuration.", order = 10)
public static void synchronizeDatabase(String serviceID) {
    MCRDOIRegistrationService registrationService = new MCRDOIRegistrationService(serviceID);
    try {
        MCRDataciteClient dataciteClient = registrationService.getDataciteClient();
        List<MCRDigitalObjectIdentifier> doiList = dataciteClient.getDOIList();
        doiList.stream().filter(doi -> {
            boolean isTestDOI = doi.getPrefix().equals(MCRDigitalObjectIdentifier.TEST_DOI_PREFIX);
            return !isTestDOI || registrationService.usesTestPrefix();
        }).forEach(doi -> {
            try {
                URI uri = dataciteClient.resolveDOI(doi);
                if (uri.toString().startsWith(registrationService.getRegisterURL())) {
                    LOGGER.info("Checking DOI: {}", doi.asString());
                    MCRObjectID objectID = getObjectID(uri);
                    if (MCRMetadataManager.exists(objectID)) {
                        if (!registrationService.isRegistered(objectID, "")) {
                            LOGGER.info("DOI is not registered in MyCoRe. Add to Database: {}", doi.asString());
                            MCRPI databaseEntry = new MCRPI(doi.asString(), registrationService.getType(), objectID.toString(), "", serviceID, new Date());
                            MCRHIBConnection.instance().getSession().save(databaseEntry);
                        }
                        // Update main files
                        MCRObject obj = MCRMetadataManager.retrieveMCRObject(objectID);
                        List<Map.Entry<String, URI>> entryList = registrationService.getMediaList(obj);
                        dataciteClient.setMediaList(doi, entryList);
                    } else {
                        LOGGER.info("Could not find Object : {}", objectID);
                    }
                } else {
                    LOGGER.info("DOI/URL is not from this application: {}/{}", doi.asString(), uri);
                }
            } catch (MCRPersistentIdentifierException e) {
                LOGGER.error("Error occurred for DOI: {}", doi, e);
            }
        });
    } catch (MCRPersistentIdentifierException e) {
        LOGGER.error("Error while receiving DOI list from Registration-Service!", e);
    }
}
Also used : MCRDOIParser(org.mycore.pi.doi.MCRDOIParser) Date(java.util.Date) HashMap(java.util.HashMap) MCRException(org.mycore.common.MCRException) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) MCRPI(org.mycore.pi.backend.MCRPI) MCRIdentifierUnresolvableException(org.mycore.pi.exceptions.MCRIdentifierUnresolvableException) Map(java.util.Map) URI(java.net.URI) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) MCRAccessException(org.mycore.access.MCRAccessException) MCRDataciteClient(org.mycore.pi.doi.MCRDataciteClient) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRHIBConnection(org.mycore.backend.hibernate.MCRHIBConnection) Collectors(java.util.stream.Collectors) MCRDOIRegistrationService(org.mycore.pi.doi.MCRDOIRegistrationService) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRDigitalObjectIdentifier(org.mycore.pi.doi.MCRDigitalObjectIdentifier) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRPersistentIdentifierMetadataManager(org.mycore.pi.MCRPersistentIdentifierMetadataManager) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) MCRPI(org.mycore.pi.backend.MCRPI) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) URI(java.net.URI) Date(java.util.Date) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRDigitalObjectIdentifier(org.mycore.pi.doi.MCRDigitalObjectIdentifier) MCRDOIRegistrationService(org.mycore.pi.doi.MCRDOIRegistrationService) MCRDataciteClient(org.mycore.pi.doi.MCRDataciteClient) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 29 with MCRObject

use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.

the class MCRPersistentIdentifierEventHandler method handleObjectRepaired.

@Override
protected void handleObjectRepaired(MCREvent evt, MCRObject obj) {
    /* Add PIs to DB if they are not there */
    MCRPersistentIdentifierManager.getInstance().getRegistered(obj).forEach(pi -> MCRPersistentIdentifierManager.getInstance().delete(pi.getMycoreID(), pi.getAdditional(), pi.getType(), pi.getService()));
    Gson gson = new Gson();
    obj.getService().getFlags(MCRPIRegistrationService.PI_FLAG).stream().map(piFlag -> gson.fromJson(piFlag, MCRPI.class)).filter(entry -> !MCRPersistentIdentifierManager.getInstance().exist(entry)).forEach(entry -> {
        // TODO: disabled for MCR-1393
        // entry.setMcrRevision(MCRCoreVersion.getRevision());
        entry.setMcrVersion(MCRCoreVersion.getVersion());
        entry.setMycoreID(obj.getId().toString());
        LOGGER.info("Add PI : {} with service {} to database!", entry.getIdentifier(), entry.getService());
        MCRHIBConnection.instance().getSession().save(entry);
    });
    handleObjectUpdated(evt, obj);
}
Also used : MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRHIBConnection(org.mycore.backend.hibernate.MCRHIBConnection) MCREvent(org.mycore.common.events.MCREvent) MCRException(org.mycore.common.MCRException) MCREventHandlerBase(org.mycore.common.events.MCREventHandlerBase) MCRPI(org.mycore.pi.backend.MCRPI) MCRCoreVersion(org.mycore.common.MCRCoreVersion) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Gson(com.google.gson.Gson) MCRObject(org.mycore.datamodel.metadata.MCRObject) BiConsumer(java.util.function.BiConsumer) LogManager(org.apache.logging.log4j.LogManager) Gson(com.google.gson.Gson) MCRPI(org.mycore.pi.backend.MCRPI)

Example 30 with MCRObject

use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.

the class MCRDOIRegistrationService method registerJob.

@Override
public void registerJob(Map<String, String> parameters) throws MCRPersistentIdentifierException {
    MCRDigitalObjectIdentifier doi = getDOIFromJob(parameters);
    String idString = parameters.get(CONTEXT_OBJ);
    MCRObjectID objectID = MCRObjectID.getInstance(idString);
    this.validateJobUserRights(objectID);
    MCRObject object = MCRMetadataManager.retrieveMCRObject(objectID);
    MCRObject mcrBase = MCRMetadataManager.retrieveMCRObject(objectID);
    Document dataciteDocument = transformToDatacite(doi, mcrBase);
    MCRDataciteClient dataciteClient = getDataciteClient();
    dataciteClient.storeMetadata(dataciteDocument);
    URI registeredURI;
    try {
        registeredURI = getRegisteredURI(object);
        dataciteClient.mintDOI(doi, registeredURI);
    } catch (URISyntaxException e) {
        throw new MCRException("Base-URL seems to be invalid!", e);
    }
    List<Map.Entry<String, URI>> entryList = getMediaList((MCRObject) object);
    dataciteClient.setMediaList(doi, entryList);
    this.updateRegistrationDate(objectID, "", new Date());
}
Also used : MCRException(org.mycore.common.MCRException) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) URISyntaxException(java.net.URISyntaxException) Document(org.jdom2.Document) URI(java.net.URI) Date(java.util.Date)

Aggregations

MCRObject (org.mycore.datamodel.metadata.MCRObject)71 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)33 Document (org.jdom2.Document)18 Element (org.jdom2.Element)17 MCRException (org.mycore.common.MCRException)16 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)14 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)14 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)12 IOException (java.io.IOException)11 MCRAccessException (org.mycore.access.MCRAccessException)11 MCRMODSWrapper (org.mycore.mods.MCRMODSWrapper)9 MCRPersistenceException (org.mycore.common.MCRPersistenceException)7 Date (java.util.Date)6 JDOMException (org.jdom2.JDOMException)6 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)6 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)6 SAXException (org.xml.sax.SAXException)6 URI (java.net.URI)5 URISyntaxException (java.net.URISyntaxException)5 ArrayList (java.util.ArrayList)5