Search in sources :

Example 1 with Fptr

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

the class MCRMETSDefaultGenerator method buildPhysDivs.

private void buildPhysDivs(MCRPath dir, PhysicalDiv physicalDiv, String fileID, final String physicalID, String fileName) {
    if (!fileName.isEmpty() && hrefIdMap.containsKey(fileName) && isInExcludedRootFolder(dir)) {
        for (PhysicalSubDiv physSubDiv : physicalDiv.getChildren()) {
            if (physSubDiv.getId().contains(hrefIdMap.get(fileName))) {
                physSubDiv.add(new Fptr(fileID));
            }
        }
    } else {
        PhysicalSubDiv pyhsicalPage = new PhysicalSubDiv(physicalID, "page");
        Fptr fptr = new Fptr(fileID);
        pyhsicalPage.add(fptr);
        physicalDiv.add(pyhsicalPage);
    }
}
Also used : Fptr(org.mycore.mets.model.struct.Fptr) PhysicalSubDiv(org.mycore.mets.model.struct.PhysicalSubDiv)

Example 2 with Fptr

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

the class MCRMETSHierarchyGenerator method createPhysicalStruct.

/**
 * This method creates the physical structure map.
 *
 * @return generated pyhiscal struct map secion.
 */
protected PhysicalStructMap createPhysicalStruct() {
    PhysicalStructMap pstr = new PhysicalStructMap();
    PhysicalDiv physicalDiv = new PhysicalDiv("phys_" + this.mcrDer.getId(), PhysicalDiv.TYPE_PHYS_SEQ);
    pstr.setDivContainer(physicalDiv);
    // run through file references
    for (FileRef ref : this.files) {
        String physId = ref.toPhysId();
        PhysicalSubDiv page = physicalDiv.get(physId);
        if (page == null) {
            page = new PhysicalSubDiv(physId, PhysicalSubDiv.TYPE_PAGE);
            getOrderLabel(ref.toId()).ifPresent(page::setOrderLabel);
            physicalDiv.add(page);
        }
        page.add(new Fptr(ref.toId()));
    }
    return pstr;
}
Also used : PhysicalStructMap(org.mycore.mets.model.struct.PhysicalStructMap) Fptr(org.mycore.mets.model.struct.Fptr) PhysicalDiv(org.mycore.mets.model.struct.PhysicalDiv) PhysicalSubDiv(org.mycore.mets.model.struct.PhysicalSubDiv)

Example 3 with Fptr

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

the class MCRMETSHierarchyGenerator method copyFptr.

private Optional<Fptr> copyFptr(FileGrp oldGrp, FileGrp newGrp, Fptr oldFptr) {
    Fptr newFptr = new Fptr();
    for (Seq oldSeq : oldFptr.getSeqList()) {
        Seq newSeq = new Seq();
        for (Area oldArea : oldSeq.getAreaList()) {
            if (oldArea.getBetype() == null) {
                continue;
            }
            String oldFileID = oldArea.getFileId();
            File oldFile = oldGrp.getFileById(oldFileID);
            String href = oldFile.getFLocat().getHref();
            File newFile = newGrp.getFileByHref(href);
            Area newArea = new Area();
            newArea.setBegin(oldArea.getBegin());
            newArea.setEnd(oldArea.getEnd());
            newArea.setFileId(newFile.getId());
            newArea.setBetype("IDREF");
            newSeq.getAreaList().add(newArea);
        }
        if (!newSeq.getAreaList().isEmpty()) {
            newFptr.getSeqList().add(newSeq);
        }
    }
    return newFptr.getSeqList().isEmpty() ? Optional.empty() : Optional.of(newFptr);
}
Also used : Area(org.mycore.mets.model.struct.Area) Fptr(org.mycore.mets.model.struct.Fptr) File(org.mycore.mets.model.files.File) Seq(org.mycore.mets.model.struct.Seq)

Example 4 with Fptr

use of org.mycore.mets.model.struct.Fptr 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 5 with Fptr

use of org.mycore.mets.model.struct.Fptr 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)

Aggregations

Fptr (org.mycore.mets.model.struct.Fptr)11 PhysicalSubDiv (org.mycore.mets.model.struct.PhysicalSubDiv)8 File (org.mycore.mets.model.files.File)7 PhysicalDiv (org.mycore.mets.model.struct.PhysicalDiv)7 PhysicalStructMap (org.mycore.mets.model.struct.PhysicalStructMap)7 FileGrp (org.mycore.mets.model.files.FileGrp)6 FLocat (org.mycore.mets.model.files.FLocat)5 LogicalDiv (org.mycore.mets.model.struct.LogicalDiv)5 IOException (java.io.IOException)4 URISyntaxException (java.net.URISyntaxException)4 ArrayList (java.util.ArrayList)4 MCRMetsFileUse (org.mycore.mets.model.simple.MCRMetsFileUse)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Collections (java.util.Collections)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 Objects (java.util.Objects)3 LogManager (org.apache.logging.log4j.LogManager)3 Logger (org.apache.logging.log4j.Logger)3