Search in sources :

Example 36 with MCRDerivate

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

the class MCRPIRegistrationService method register.

/**
 * Validates everything, registers a new Identifier, inserts the identifier to object metadata and writes a
 * information to the Database.
 *
 * @param obj the object which has to be identified
 * @param additional additional information for the persistent identifier
 * @param updateObject if true this method calls {@link MCRMetadataManager#update(MCRBase)}
 * @return the assigned Identifier
 * @throws MCRAccessException               the current User doesn't have the rights to insert the Identifier to Metadata
 * @throws MCRActiveLinkException           the {@link MCRPersistentIdentifierMetadataManager} lets
 * {@link org.mycore.datamodel.metadata.MCRMetadataManager#update(MCRObject)} throw this
 * @throws MCRPersistentIdentifierException see {@link org.mycore.pi.exceptions}
 */
public T register(MCRBase obj, String additional, boolean updateObject) throws MCRAccessException, MCRActiveLinkException, MCRPersistentIdentifierException {
    this.validateRegistration(obj, additional);
    T identifier = this.registerIdentifier(obj, additional);
    this.getMetadataManager().insertIdentifier(identifier, obj, additional);
    MCRPI databaseEntry = insertIdentifierToDatabase(obj, additional, identifier);
    addFlagToObject(obj, databaseEntry);
    if (updateObject) {
        if (obj instanceof MCRObject) {
            MCRMetadataManager.update((MCRObject) obj);
        } else if (obj instanceof MCRDerivate) {
            MCRMetadataManager.update((MCRDerivate) obj);
        }
    }
    return identifier;
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRPI(org.mycore.pi.backend.MCRPI) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate)

Example 37 with MCRDerivate

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

the class MCRPICommands method migrateURNGranularToServiceID.

@MCRCommand(syntax = "migrate urn granular to service id {0}", help = "Used to migrate urn granular to MyCoRe-PI. " + "{0} should be your granular service id.")
public static void migrateURNGranularToServiceID(String serviceID) {
    Session session = MCRHIBConnection.instance().getSession();
    MCRXMLMetadataManager.instance().listIDsOfType("derivate").forEach(derivateID -> {
        MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivateID));
        String urn = derivate.getDerivate().getURN();
        if (urn != null) {
            LOGGER.info("Found URN in :{}", derivateID);
            MCRPI derivatePI = new MCRPI(urn, MCRDNBURN.TYPE, derivateID, "", serviceID, new Date());
            if (MCRPersistentIdentifierManager.getInstance().exist(derivatePI)) {
                LOGGER.warn("PI-Entry for {} already exist!", urn);
            } else {
                session.save(derivatePI);
                derivate.getUrnMap().forEach((file, fileURN) -> {
                    MCRPI filePI = new MCRPI(fileURN, MCRDNBURN.TYPE, derivateID, file, serviceID, new Date());
                    if (MCRPersistentIdentifierManager.getInstance().exist(filePI)) {
                        LOGGER.warn("PI-Entry for {} already exist!", fileURN);
                    } else {
                        session.save(fileURN);
                    }
                });
            }
        }
    });
}
Also used : MCRPI(org.mycore.pi.backend.MCRPI) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) Date(java.util.Date) Session(org.hibernate.Session) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 38 with MCRDerivate

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

the class MCRDOIRegistrationService method getMediaList.

/**
 * Builds a list with with right content types and media urls assigned of a specific Object
 * @param obj the object
 * @return a list of entrys Media-Type, URL
 */
public List<Map.Entry<String, URI>> getMediaList(MCRObject obj) {
    List<Map.Entry<String, URI>> entryList = new ArrayList<>();
    Optional<MCRObjectID> derivateIdOptional = MCRMetadataManager.getDerivateIds(obj.getId(), 1, TimeUnit.MINUTES).stream().findFirst();
    derivateIdOptional.ifPresent(derivateId -> {
        MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(derivateId);
        String mainDoc = derivate.getDerivate().getInternals().getMainDoc();
        MCRPath mainDocumentPath = MCRPath.getPath(derivateId.toString(), mainDoc);
        try {
            String contentType = Optional.ofNullable(MCRContentTypes.probeContentType(mainDocumentPath)).orElse("application/octet-stream");
            entryList.add(new AbstractMap.SimpleEntry<>(contentType, new URI(this.registerURL + MCRXMLFunctions.encodeURIPath("/servlets/MCRFileNodeServlet/" + derivateId + "/" + mainDoc))));
        } catch (IOException | URISyntaxException e) {
            LOGGER.error("Error while detecting the file to register!", e);
        }
    });
    return entryList;
}
Also used : ArrayList(java.util.ArrayList) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) AbstractMap(java.util.AbstractMap) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 39 with MCRDerivate

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

the class MCREventHandlerBase method doHandleEvent.

/**
 * This method handle all calls for EventHandler for the event types
 * MCRObject, MCRDerivate and MCRFile.
 *
 * @param evt
 *            The MCREvent object
 */
public void doHandleEvent(MCREvent evt) {
    if (evt.getObjectType().equals(MCREvent.OBJECT_TYPE)) {
        MCRObject obj = (MCRObject) evt.get("object");
        if (obj != null) {
            logger.debug("{} handling {} {}", getClass().getName(), obj.getId(), evt.getEventType());
            switch(evt.getEventType()) {
                case MCREvent.CREATE_EVENT:
                    handleObjectCreated(evt, obj);
                    break;
                case MCREvent.UPDATE_EVENT:
                    handleObjectUpdated(evt, obj);
                    break;
                case MCREvent.DELETE_EVENT:
                    handleObjectDeleted(evt, obj);
                    break;
                case MCREvent.REPAIR_EVENT:
                    handleObjectRepaired(evt, obj);
                    break;
                case MCREvent.INDEX_EVENT:
                    handleObjectIndex(evt, obj);
                    break;
                default:
                    logger.warn("Can't find method for an object data handler for event type {}", evt.getEventType());
                    break;
            }
            return;
        }
        logger.warn("Can't find method for " + MCREvent.OBJECT_TYPE + " for event type {}", evt.getEventType());
        return;
    }
    if (evt.getObjectType().equals(MCREvent.DERIVATE_TYPE)) {
        MCRDerivate der = (MCRDerivate) evt.get("derivate");
        if (der != null) {
            logger.debug("{} handling {} {}", getClass().getName(), der.getId(), evt.getEventType());
            switch(evt.getEventType()) {
                case MCREvent.CREATE_EVENT:
                    handleDerivateCreated(evt, der);
                    break;
                case MCREvent.UPDATE_EVENT:
                    handleDerivateUpdated(evt, der);
                    break;
                case MCREvent.DELETE_EVENT:
                    handleDerivateDeleted(evt, der);
                    break;
                case MCREvent.REPAIR_EVENT:
                    handleDerivateRepaired(evt, der);
                    break;
                case MCREvent.INDEX_EVENT:
                    updateDerivateFileIndex(evt, der);
                    break;
                default:
                    logger.warn("Can't find method for a derivate data handler for event type {}", evt.getEventType());
                    break;
            }
            return;
        }
        logger.warn("Can't find method for " + MCREvent.DERIVATE_TYPE + " for event type {}", evt.getEventType());
        return;
    }
    if (evt.getObjectType().equals(MCREvent.PATH_TYPE)) {
        Path path = (Path) evt.get(MCREvent.PATH_KEY);
        if (path != null) {
            if (!path.isAbsolute()) {
                logger.warn("Cannot handle path events on non absolute paths: {}", path);
            }
            logger.debug("{} handling {} {}", getClass().getName(), path, evt.getEventType());
            BasicFileAttributes attrs = (BasicFileAttributes) evt.get(MCREvent.FILEATTR_KEY);
            if (attrs == null && !evt.getEventType().equals(MCREvent.DELETE_EVENT)) {
                logger.warn("BasicFileAttributes for {} was not given. Resolving now.", path);
                try {
                    attrs = Files.getFileAttributeView(path, BasicFileAttributeView.class).readAttributes();
                } catch (IOException e) {
                    logger.error("Could not get BasicFileAttributes from path: {}", path, e);
                }
            }
            switch(evt.getEventType()) {
                case MCREvent.CREATE_EVENT:
                    handlePathCreated(evt, path, attrs);
                    break;
                case MCREvent.UPDATE_EVENT:
                    handlePathUpdated(evt, path, attrs);
                    break;
                case MCREvent.DELETE_EVENT:
                    handlePathDeleted(evt, path, attrs);
                    break;
                case MCREvent.REPAIR_EVENT:
                    handlePathRepaired(evt, path, attrs);
                    break;
                case MCREvent.INDEX_EVENT:
                    updatePathIndex(evt, path, attrs);
                    break;
                default:
                    logger.warn("Can't find method for Path data handler for event type {}", evt.getEventType());
                    break;
            }
            return;
        }
        logger.warn("Can't find method for " + MCREvent.PATH_TYPE + " for event type {}", evt.getEventType());
        return;
    }
    if (evt.getObjectType().equals(MCREvent.CLASS_TYPE)) {
        MCRCategory cl = (MCRCategory) evt.get("class");
        if (cl != null) {
            logger.debug("{} handling {} {}", getClass().getName(), cl.getId(), evt.getEventType());
            switch(evt.getEventType()) {
                case MCREvent.CREATE_EVENT:
                    handleClassificationCreated(evt, cl);
                    break;
                case MCREvent.UPDATE_EVENT:
                    handleClassificationUpdated(evt, cl);
                    break;
                case MCREvent.DELETE_EVENT:
                    handleClassificationDeleted(evt, cl);
                    break;
                case MCREvent.REPAIR_EVENT:
                    handleClassificationRepaired(evt, cl);
                    break;
                default:
                    logger.warn("Can't find method for a classification data handler for event type {}", evt.getEventType());
                    break;
            }
            return;
        }
        logger.warn("Can't find method for " + MCREvent.CLASS_TYPE + " for event type {}", evt.getEventType());
    }
}
Also used : Path(java.nio.file.Path) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 40 with MCRDerivate

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

the class MCRTransferPackage method getContent.

/**
 * Returns the content for this transfer package. You have to call {@link #build()}
 * before you can retrieve this data.
 *
 * @return a map where key = filename; value = MCRContent
 */
public Map<String, MCRContent> getContent() throws IOException {
    Map<String, MCRContent> content = new HashMap<>();
    // config
    content.put(IMPORT_CONFIG_FILENAME, new MCRJDOMContent(buildImportConfiguration()));
    // objects
    for (MCRObject object : this.objects) {
        String fileName = CONTENT_PATH + object.getId() + ".xml";
        Document xml = object.createXML();
        content.put(fileName, new MCRJDOMContent(xml));
    }
    // file containers
    for (MCRTransferPackageFileContainer fc : this.fileContainers) {
        // derivate
        MCRObjectID derivateId = fc.getDerivateId();
        MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(derivateId);
        Document derivateXML = derivate.createXML();
        String folder = CONTENT_PATH + fc.getName();
        String derivateFileName = folder + "/" + fc.getName() + ".xml";
        content.put(derivateFileName, new MCRJDOMContent(derivateXML));
        // files of derivate
        for (MCRFile file : fc.getFiles()) {
            content.put(folder + file.getAbsolutePath(), file.getContent());
        }
    }
    // classifications
    for (String classId : this.classifications) {
        Document classification = MCRClassificationUtils.asDocument(classId);
        String path = CLASS_PATH + classId + ".xml";
        content.put(path, new MCRJDOMContent(classification));
    }
    return content;
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) MCRObject(org.mycore.datamodel.metadata.MCRObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent)

Aggregations

MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)53 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)27 IOException (java.io.IOException)22 MCRPath (org.mycore.datamodel.niofs.MCRPath)19 MCRObject (org.mycore.datamodel.metadata.MCRObject)13 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)10 MCRAccessException (org.mycore.access.MCRAccessException)9 Path (java.nio.file.Path)8 MCRException (org.mycore.common.MCRException)8 MCRRestAPIException (org.mycore.restapi.v1.errors.MCRRestAPIException)8 Document (org.jdom2.Document)7 MCRPersistenceException (org.mycore.common.MCRPersistenceException)7 Date (java.util.Date)6 JDOMException (org.jdom2.JDOMException)6 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)6 ArrayList (java.util.ArrayList)5 MCRMetaIFS (org.mycore.datamodel.metadata.MCRMetaIFS)5 MCRObjectDerivate (org.mycore.datamodel.metadata.MCRObjectDerivate)5 SAXException (org.xml.sax.SAXException)5 StringWriter (java.io.StringWriter)4