Search in sources :

Example 1 with LogicalDiv

use of org.mycore.mets.model.struct.LogicalDiv in project mycore by MyCoRe-Org.

the class MCRMetsMods2IIIFConverter method convert.

public MCRIIIFManifest convert() {
    MCRIIIFManifest manifest = new MCRIIIFManifest();
    // root chapter ^= manifest metadata
    LogicalStructMap logicalStructMap = (LogicalStructMap) mets.getStructMap("LOGICAL");
    LogicalDiv divContainer = logicalStructMap.getDivContainer();
    List<MCRIIIFMetadata> metadata = extractMedataFromLogicalDiv(mets, divContainer);
    manifest.metadata = metadata;
    manifest.setId(this.identifier);
    PhysicalStructMap physicalStructMap = (PhysicalStructMap) mets.getStructMap("PHYSICAL");
    PhysicalDiv physicalDivContainer = physicalStructMap.getDivContainer();
    String id = physicalDivContainer.getId();
    MCRIIIFSequence sequence = new MCRIIIFSequence(id);
    List<PhysicalSubDiv> children = physicalDivContainer.getChildren();
    MCRIIIFImageImpl imageImpl = MCRIIIFImageImpl.getInstance(getImageImplName());
    MCRIIIFImageProfile profile = imageImpl.getProfile();
    profile.setId(MCRIIIFImageUtil.getProfileLink(imageImpl));
    sequence.canvases = children.stream().map(physicalSubDiv -> {
        String order = physicalSubDiv.asElement().getAttributeValue("ORDER");
        String orderLabel = physicalSubDiv.getOrderLabel();
        String contentids = physicalSubDiv.getContentids();
        String label = Stream.of(order, orderLabel, contentids).filter(o -> o != null && !o.isEmpty()).collect(Collectors.joining(" - "));
        String identifier = this.physicalIdentifierMap.get(physicalSubDiv);
        try {
            MCRIIIFImageInformation information = imageImpl.getInformation(identifier);
            MCRIIIFCanvas canvas = new MCRIIIFCanvas(identifier, label, information.width, information.height);
            MCRIIIFAnnotation annotation = new MCRIIIFAnnotation(identifier, canvas);
            canvas.images.add(annotation);
            MCRIIIFResource resource = new MCRIIIFResource(information.getId(), MCRDCMIType.Image);
            resource.setWidth(information.width);
            resource.setHeight(information.height);
            MCRIIIFService service = new MCRIIIFService(information.getId(), profile.getContext());
            service.profile = MCRIIIFImageProfile.IIIF_PROFILE_2_0;
            resource.setService(service);
            annotation.setResource(resource);
            return canvas;
        } catch (MCRIIIFImageNotFoundException | MCRIIIFImageProvidingException e) {
            throw new MCRException("Error while providing ImageInfo for " + identifier, e);
        } catch (MCRAccessException e) {
            LOGGER.warn("User has no access to {}", identifier);
            return null;
        }
    }).filter(Objects::nonNull).collect(Collectors.toList());
    manifest.sequences.add(sequence);
    List<MCRIIIFRange> complete = new ArrayList<>();
    processDivContainer(complete, divContainer);
    manifest.structures.addAll(complete);
    manifest.setLabel(metadata.stream().filter(m -> m.getLabel().equals("title")).findFirst().get().getStringValue().get());
    return manifest;
}
Also used : PhysicalDiv(org.mycore.mets.model.struct.PhysicalDiv) MCRIIIFCanvas(org.mycore.iiif.presentation.model.basic.MCRIIIFCanvas) MCRIIIFImageUtil(org.mycore.iiif.image.MCRIIIFImageUtil) MCRIIIFImageImpl(org.mycore.iiif.image.impl.MCRIIIFImageImpl) MCRConstants(org.mycore.common.MCRConstants) MCRIIIFImageProvidingException(org.mycore.iiif.image.impl.MCRIIIFImageProvidingException) MCRIIIFImageProfile(org.mycore.iiif.image.model.MCRIIIFImageProfile) HashMap(java.util.HashMap) MCRIIIFSequence(org.mycore.iiif.presentation.model.basic.MCRIIIFSequence) MCRException(org.mycore.common.MCRException) ArrayList(java.util.ArrayList) Document(org.jdom2.Document) MCRIIIFService(org.mycore.iiif.presentation.model.attributes.MCRIIIFService) FileGrp(org.mycore.mets.model.files.FileGrp) Map(java.util.Map) MCRDCMIType(org.mycore.iiif.presentation.model.attributes.MCRDCMIType) MCRIIIFResource(org.mycore.iiif.presentation.model.attributes.MCRIIIFResource) MCRAccessException(org.mycore.access.MCRAccessException) File(org.mycore.mets.model.files.File) PhysicalStructMap(org.mycore.mets.model.struct.PhysicalStructMap) MCRIIIFAnnotation(org.mycore.iiif.presentation.model.additional.MCRIIIFAnnotation) MCRIIIFViewingHint(org.mycore.iiif.presentation.model.attributes.MCRIIIFViewingHint) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MCRIIIFManifest(org.mycore.iiif.presentation.model.basic.MCRIIIFManifest) Mets(org.mycore.mets.model.Mets) Collectors(java.util.stream.Collectors) MCRIIIFRange(org.mycore.iiif.presentation.model.basic.MCRIIIFRange) LogicalDiv(org.mycore.mets.model.struct.LogicalDiv) MDTYPE(org.mycore.mets.model.struct.MDTYPE) PhysicalSubDiv(org.mycore.mets.model.struct.PhysicalSubDiv) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) MCRIIIFImageNotFoundException(org.mycore.iiif.image.impl.MCRIIIFImageNotFoundException) MCRIIIFImageInformation(org.mycore.iiif.image.model.MCRIIIFImageInformation) LogicalStructMap(org.mycore.mets.model.struct.LogicalStructMap) DmdSec(org.mycore.mets.model.sections.DmdSec) MdWrap(org.mycore.mets.model.struct.MdWrap) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) MCRIIIFMetadata(org.mycore.iiif.presentation.model.attributes.MCRIIIFMetadata) PhysicalStructMap(org.mycore.mets.model.struct.PhysicalStructMap) ArrayList(java.util.ArrayList) MCRIIIFSequence(org.mycore.iiif.presentation.model.basic.MCRIIIFSequence) PhysicalSubDiv(org.mycore.mets.model.struct.PhysicalSubDiv) MCRIIIFRange(org.mycore.iiif.presentation.model.basic.MCRIIIFRange) LogicalStructMap(org.mycore.mets.model.struct.LogicalStructMap) LogicalDiv(org.mycore.mets.model.struct.LogicalDiv) MCRIIIFCanvas(org.mycore.iiif.presentation.model.basic.MCRIIIFCanvas) MCRIIIFMetadata(org.mycore.iiif.presentation.model.attributes.MCRIIIFMetadata) MCRIIIFService(org.mycore.iiif.presentation.model.attributes.MCRIIIFService) MCRIIIFImageImpl(org.mycore.iiif.image.impl.MCRIIIFImageImpl) MCRException(org.mycore.common.MCRException) MCRIIIFManifest(org.mycore.iiif.presentation.model.basic.MCRIIIFManifest) MCRIIIFImageInformation(org.mycore.iiif.image.model.MCRIIIFImageInformation) MCRIIIFResource(org.mycore.iiif.presentation.model.attributes.MCRIIIFResource) MCRAccessException(org.mycore.access.MCRAccessException) PhysicalDiv(org.mycore.mets.model.struct.PhysicalDiv) MCRIIIFImageProfile(org.mycore.iiif.image.model.MCRIIIFImageProfile) MCRIIIFAnnotation(org.mycore.iiif.presentation.model.additional.MCRIIIFAnnotation) Objects(java.util.Objects)

Example 2 with LogicalDiv

use of org.mycore.mets.model.struct.LogicalDiv in project mycore by MyCoRe-Org.

the class MCRMETSHierarchyGenerator method newLogicalStructMap.

protected LogicalStructMap newLogicalStructMap() {
    LogicalStructMap lstr = new LogicalStructMap();
    MCRObjectID objId = this.rootObj.getId();
    // create main div
    String amdId = this.amdSection.getId();
    String dmdId = this.dmdSection.getId();
    LogicalDiv logicalDiv = new LogicalDiv(objId.toString(), getType(this.rootObj), getLabel(this.rootObj), amdId, dmdId);
    lstr.setDivContainer(logicalDiv);
    // run through all children
    newLogicalStructMap(this.rootObj, logicalDiv);
    // remove not linked logical divs
    logicalDiv.getChildren().removeIf(child -> !validateLogicalStruct(child));
    return lstr;
}
Also used : LogicalStructMap(org.mycore.mets.model.struct.LogicalStructMap) LogicalDiv(org.mycore.mets.model.struct.LogicalDiv) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Example 3 with LogicalDiv

use of org.mycore.mets.model.struct.LogicalDiv in project mycore by MyCoRe-Org.

the class MCRMETSHierarchyGenerator method mergeOldLogicalStructMap.

/**
 * Runs through the logical part of the old mets and copies the ALTO part (mets:fptr/mets:seq/mets:area)
 * to the newly created logical struct map. This is done by comparing the mets:div @ID's of the old and the new
 * logical struct map. If two @ID's are equal, we can assume that it is the same mets:div and we just copy all
 * the old mets:fptr's.
 *
 * @param logicalStructMap the logical struct map to enhance
 */
protected void mergeOldLogicalStructMap(LogicalStructMap logicalStructMap) {
    if (!this.getOldMets().isPresent()) {
        return;
    }
    Mets oldMets = this.getOldMets().get();
    LogicalStructMap oldLsm = oldMets.getLogicalStructMap();
    FileGrp oldAltoGroup = oldMets.getFileSec().getFileGroup("ALTO");
    FileGrp newAltoGroup = this.fileSection.getFileGroup("ALTO");
    List<LogicalDiv> descendants = oldLsm.getDivContainer().getDescendants();
    descendants.stream().filter(div -> !div.getFptrList().isEmpty()).forEach(oldDiv -> {
        String id = oldDiv.getId();
        LogicalDiv newDiv = logicalStructMap.getDivContainer().getLogicalSubDiv(id);
        if (newDiv != null) {
            for (Fptr fptr : oldDiv.getFptrList()) {
                copyFptr(oldAltoGroup, newAltoGroup, fptr).ifPresent(newFptr -> newDiv.getFptrList().add(newFptr));
            }
        }
        updateStructLinkMapUsingALTO(newDiv);
    });
}
Also used : PhysicalDiv(org.mycore.mets.model.struct.PhysicalDiv) StructLink(org.mycore.mets.model.struct.StructLink) URISyntaxException(java.net.URISyntaxException) LOCTYPE(org.mycore.mets.model.struct.LOCTYPE) HashMap(java.util.HashMap) MCRMetaElement(org.mycore.datamodel.metadata.MCRMetaElement) Area(org.mycore.mets.model.struct.Area) MCRException(org.mycore.common.MCRException) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) ArrayList(java.util.ArrayList) FileGrp(org.mycore.mets.model.files.FileGrp) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) MCRXMLFunctions(org.mycore.common.xml.MCRXMLFunctions) MCRMetaDerivateLink(org.mycore.datamodel.metadata.MCRMetaDerivateLink) MCRMetsFileUse(org.mycore.mets.model.simple.MCRMetsFileUse) File(org.mycore.mets.model.files.File) PhysicalStructMap(org.mycore.mets.model.struct.PhysicalStructMap) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) FileSec(org.mycore.mets.model.files.FileSec) MCRContentTypes(org.mycore.datamodel.niofs.MCRContentTypes) Iterator(java.util.Iterator) MCRPath(org.mycore.datamodel.niofs.MCRPath) IOException(java.io.IOException) Seq(org.mycore.mets.model.struct.Seq) Instant(java.time.Instant) LogicalDiv(org.mycore.mets.model.struct.LogicalDiv) PhysicalSubDiv(org.mycore.mets.model.struct.PhysicalSubDiv) SmLink(org.mycore.mets.model.struct.SmLink) MCRObjectUtils(org.mycore.datamodel.metadata.MCRObjectUtils) Objects(java.util.Objects) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Fptr(org.mycore.mets.model.struct.Fptr) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) LogicalStructMap(org.mycore.mets.model.struct.LogicalStructMap) MCRObject(org.mycore.datamodel.metadata.MCRObject) FLocat(org.mycore.mets.model.files.FLocat) MetsHdr(org.mycore.mets.model.header.MetsHdr) Optional(java.util.Optional) DmdSec(org.mycore.mets.model.sections.DmdSec) MCRMetsSave(org.mycore.mets.tools.MCRMetsSave) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) AmdSec(org.mycore.mets.model.sections.AmdSec) LogicalStructMap(org.mycore.mets.model.struct.LogicalStructMap) LogicalDiv(org.mycore.mets.model.struct.LogicalDiv) FileGrp(org.mycore.mets.model.files.FileGrp) Fptr(org.mycore.mets.model.struct.Fptr)

Example 4 with LogicalDiv

use of org.mycore.mets.model.struct.LogicalDiv in project mycore by MyCoRe-Org.

the class MCRMetsSave method updateOnFileDelete.

/**
 * @param mets
 * @param file
 * @return
 */
private static Document updateOnFileDelete(Document mets, MCRPath file) {
    Mets modifiedMets;
    try {
        modifiedMets = new Mets(mets);
        String href = file.getOwnerRelativePath().substring(1);
        PhysicalStructMap physStructMap = (PhysicalStructMap) modifiedMets.getStructMap(PhysicalStructMap.TYPE);
        PhysicalDiv divContainer = physStructMap.getDivContainer();
        // search the right group and remove the file from the group
        List<FileGrp> fileGroups = modifiedMets.getFileSec().getFileGroups();
        for (FileGrp fileGrp : fileGroups) {
            if (fileGrp.contains(href)) {
                org.mycore.mets.model.files.File fileToRemove = fileGrp.getFileByHref(href);
                fileGrp.removeFile(fileToRemove);
                ArrayList<PhysicalSubDiv> physicalSubDivsToRemove = new ArrayList<>();
                // remove file from mets:mets/mets:structMap[@TYPE='PHYSICAL']
                for (PhysicalSubDiv physicalSubDiv : divContainer.getChildren()) {
                    ArrayList<Fptr> fptrsToRemove = new ArrayList<>();
                    for (Fptr fptr : physicalSubDiv.getChildren()) {
                        if (fptr.getFileId().equals(fileToRemove.getId())) {
                            if (fileGrp.getUse().equals(FileGrp.USE_MASTER)) {
                                physicalSubDivsToRemove.add(physicalSubDiv);
                            } else {
                                fptrsToRemove.add(fptr);
                            }
                        }
                    }
                    for (Fptr fptrToRemove : fptrsToRemove) {
                        LOGGER.warn(String.format(Locale.ROOT, "remove fptr \"%s\" from mets.xml of \"%s\"", fptrToRemove.getFileId(), file.getOwner()));
                        physicalSubDiv.remove(fptrToRemove);
                    }
                }
                for (PhysicalSubDiv physicalSubDivToRemove : physicalSubDivsToRemove) {
                    // remove links in mets:structLink section
                    List<SmLink> list = modifiedMets.getStructLink().getSmLinkByTo(physicalSubDivToRemove.getId());
                    LogicalStructMap logicalStructMap = (LogicalStructMap) modifiedMets.getStructMap(LogicalStructMap.TYPE);
                    for (SmLink linkToRemove : list) {
                        LOGGER.warn(String.format(Locale.ROOT, "remove smLink from \"%s\" to \"%s\"", linkToRemove.getFrom(), linkToRemove.getTo()));
                        modifiedMets.getStructLink().removeSmLink(linkToRemove);
                        // modify logical struct Map
                        String logID = linkToRemove.getFrom();
                        // the deleted file was not directly assigned to a structure
                        if (logicalStructMap.getDivContainer().getId().equals(logID)) {
                            continue;
                        }
                        LogicalDiv logicalDiv = logicalStructMap.getDivContainer().getLogicalSubDiv(logID);
                        if (logicalDiv == null) {
                            LOGGER.error("Could not find {} with id {}", LogicalDiv.class.getSimpleName(), logID);
                            LOGGER.error("Mets document remains unchanged");
                            return mets;
                        }
                        // there are still files for this logical sub div, nothing to do
                        if (modifiedMets.getStructLink().getSmLinkByFrom(logicalDiv.getId()).size() > 0) {
                            continue;
                        }
                        // the logical div has other divs included, nothing to do
                        if (logicalDiv.getChildren().size() > 0) {
                            continue;
                        }
                        /*
                             * the log div might be in a hierarchy of divs, which may now be empty
                             * (only containing empty directories), if so the parent of the log div
                             * must be deleted
                             * */
                        handleParents(logicalDiv, modifiedMets);
                        logicalStructMap.getDivContainer().remove(logicalDiv);
                    }
                    divContainer.remove(physicalSubDivToRemove);
                }
            }
        }
    } catch (Exception ex) {
        LOGGER.error("Error occured while removing file {} from the existing mets file", file, ex);
        return null;
    }
    return modifiedMets.asDocument();
}
Also used : PhysicalStructMap(org.mycore.mets.model.struct.PhysicalStructMap) FileGrp(org.mycore.mets.model.files.FileGrp) ArrayList(java.util.ArrayList) Fptr(org.mycore.mets.model.struct.Fptr) PhysicalDiv(org.mycore.mets.model.struct.PhysicalDiv) URISyntaxException(java.net.URISyntaxException) JDOMException(org.jdom2.JDOMException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) PhysicalSubDiv(org.mycore.mets.model.struct.PhysicalSubDiv) LogicalStructMap(org.mycore.mets.model.struct.LogicalStructMap) LogicalDiv(org.mycore.mets.model.struct.LogicalDiv) SmLink(org.mycore.mets.model.struct.SmLink) Mets(org.mycore.mets.model.Mets) File(org.mycore.mets.model.files.File)

Example 5 with LogicalDiv

use of org.mycore.mets.model.struct.LogicalDiv in project mycore by MyCoRe-Org.

the class MCRMetsSave method handleParents.

/**
 * @param logDiv
 * @param mets
 */
private static void handleParents(LogicalDiv logDiv, Mets mets) {
    LogicalDiv parent = logDiv.getParent();
    // there are files for the parent of the log div, thus nothing to do
    if (mets.getStructLink().getSmLinkByFrom(parent.getId()).size() > 0) {
        return;
    }
    // no files associated to the parent of the log div
    LogicalDiv logicalDiv = ((LogicalStructMap) mets.getStructMap(LogicalStructMap.TYPE)).getDivContainer();
    if (parent.getParent() == logicalDiv) {
        // the parent the log div container itself, thus we quit here and remove the log div
        logicalDiv.remove(parent);
    } else {
        handleParents(parent, mets);
    }
}
Also used : LogicalDiv(org.mycore.mets.model.struct.LogicalDiv) LogicalStructMap(org.mycore.mets.model.struct.LogicalStructMap)

Aggregations

LogicalDiv (org.mycore.mets.model.struct.LogicalDiv)15 LogicalStructMap (org.mycore.mets.model.struct.LogicalStructMap)12 PhysicalStructMap (org.mycore.mets.model.struct.PhysicalStructMap)8 FileGrp (org.mycore.mets.model.files.FileGrp)7 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 PhysicalDiv (org.mycore.mets.model.struct.PhysicalDiv)6 PhysicalSubDiv (org.mycore.mets.model.struct.PhysicalSubDiv)6 HashMap (java.util.HashMap)5 List (java.util.List)5 MCRException (org.mycore.common.MCRException)5 Mets (org.mycore.mets.model.Mets)5 File (org.mycore.mets.model.files.File)5 Collections (java.util.Collections)4 Objects (java.util.Objects)4 LogManager (org.apache.logging.log4j.LogManager)4 Logger (org.apache.logging.log4j.Logger)4 Document (org.jdom2.Document)4 DmdSec (org.mycore.mets.model.sections.DmdSec)4 Fptr (org.mycore.mets.model.struct.Fptr)4