Search in sources :

Example 6 with Mets

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

the class MCRMetsMods2IIIFConverter method processDivContainer.

protected void processDivContainer(List<MCRIIIFRange> complete, LogicalDiv divContainer) {
    MCRIIIFRange range = new MCRIIIFRange(divContainer.getId());
    if (divContainer.getParent() == null) {
        range.setViewingHint(MCRIIIFViewingHint.top);
    }
    complete.add(range);
    range.setLabel(divContainer.getLabel());
    this.logicalIdIdentifiersMap.get(divContainer.getId()).stream().map(refId -> refId).forEach(canvasRef -> range.canvases.add(canvasRef));
    divContainer.getChildren().stream().forEach(div -> {
        processDivContainer(complete, div);
        range.ranges.add(div.getId());
    });
    range.metadata = extractMedataFromLogicalDiv(mets, divContainer);
}
Also used : MCRIIIFRange(org.mycore.iiif.presentation.model.basic.MCRIIIFRange) 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)

Example 7 with Mets

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

the class MCRMetsSave method updateFiles.

/**
 * Call this method to update the mets.xml if files of the derivate have changed. Files will be added or removed
 * from the mets:fileSec and mets:StructMap[@type=PHYSICAL]. The mets:structLink part will be rebuild after.
 *
 * <p>This method takes care of the group assignment. For example: image files will be added to the MASTER
 * group and ALTO files to the ALTO group. It will also bundle files with the same name e.g. sample1.tiff and
 * alto/sample1.xml to the same physical struct map div.</p>
 *
 * <p><b>Important:</b> This method does not update the mets.xml in the derivate, its just updating the given mets
 * instance.</p>
 *
 * @param mets the mets to update
 * @param derivatePath path to the derivate -&gt; required for looking up new files
 * @throws IOException derivate couldn't be read
 */
public static void updateFiles(Mets mets, final MCRPath derivatePath) throws IOException {
    List<String> metsFiles = mets.getFileSec().getFileGroups().stream().flatMap(g -> g.getFileList().stream()).map(File::getFLocat).map(FLocat::getHref).collect(Collectors.toList());
    List<String> derivateFiles = Files.walk(derivatePath).filter(MCRStreamUtils.not(Files::isDirectory)).map(MCRPath::toMCRPath).map(MCRPath::getOwnerRelativePath).map(path -> path.substring(1)).filter(href -> !"mets.xml".equals(href)).collect(Collectors.toList());
    ArrayList<String> removedFiles = new ArrayList<>(metsFiles);
    removedFiles.removeAll(derivateFiles);
    ArrayList<String> addedFiles = new ArrayList<>(derivateFiles);
    Collections.sort(addedFiles);
    addedFiles.removeAll(metsFiles);
    StructLink structLink = mets.getStructLink();
    PhysicalStructMap physicalStructMap = mets.getPhysicalStructMap();
    List<String> unlinkedLogicalIds = new ArrayList<>();
    // remove files
    PhysicalDiv physicalDiv = physicalStructMap.getDivContainer();
    removedFiles.forEach(href -> {
        File file = null;
        // remove from fileSec
        for (FileGrp grp : mets.getFileSec().getFileGroups()) {
            file = grp.getFileByHref(href);
            if (file != null) {
                grp.removeFile(file);
                break;
            }
        }
        if (file == null) {
            return;
        }
        // remove from physical
        PhysicalSubDiv physicalSubDiv = physicalDiv.byFileId(file.getId());
        physicalSubDiv.remove(physicalSubDiv.getFptr(file.getId()));
        if (physicalSubDiv.getChildren().isEmpty()) {
            physicalDiv.remove(physicalSubDiv);
        }
        // remove from struct link
        structLink.getSmLinkByTo(physicalSubDiv.getId()).forEach(smLink -> {
            structLink.removeSmLink(smLink);
            if (structLink.getSmLinkByFrom(smLink.getFrom()).isEmpty()) {
                unlinkedLogicalIds.add(smLink.getFrom());
            }
        });
    });
    // fix unlinked logical divs
    if (!unlinkedLogicalIds.isEmpty()) {
        // get first physical div
        List<PhysicalSubDiv> physicalChildren = physicalStructMap.getDivContainer().getChildren();
        String firstPhysicalID = physicalChildren.isEmpty() ? physicalStructMap.getDivContainer().getId() : physicalChildren.get(0).getId();
        // a logical div is not linked anymore -> link with first physical div
        unlinkedLogicalIds.forEach(from -> structLink.addSmLink(new SmLink(from, firstPhysicalID)));
    }
    // get last logical div
    LogicalDiv divContainer = mets.getLogicalStructMap().getDivContainer();
    List<LogicalDiv> descendants = divContainer.getDescendants();
    LogicalDiv lastLogicalDiv = descendants.isEmpty() ? divContainer : descendants.get(descendants.size() - 1);
    // add files
    addedFiles.forEach(href -> {
        MCRPath filePath = (MCRPath) derivatePath.resolve(href);
        String fileBase = getFileBase(href);
        try {
            String fileGroupUse = MCRMetsSave.getFileGroupUse(filePath);
            // build file
            String mimeType = MCRContentTypes.probeContentType(filePath);
            String fileId = fileGroupUse.toLowerCase(Locale.ROOT) + "_" + fileBase;
            File file = new File(fileId, mimeType);
            file.setFLocat(new FLocat(LOCTYPE.URL, href));
            // fileSec
            FileGrp fileGroup = mets.getFileSec().getFileGroup(fileGroupUse);
            if (fileGroup == null) {
                fileGroup = new FileGrp(fileGroupUse);
                mets.getFileSec().addFileGrp(fileGroup);
            }
            fileGroup.addFile(file);
            // structMap physical
            String existingFileID = mets.getFileSec().getFileGroups().stream().filter(grp -> !grp.getUse().equals(fileGroupUse)).flatMap(grp -> grp.getFileList().stream()).filter(brotherFile -> fileBase.equals(getFileBase(brotherFile.getFLocat().getHref()))).map(File::getId).findAny().orElse(null);
            PhysicalSubDiv physicalSubDiv;
            if (existingFileID != null) {
                // there is a file (e.g. img or alto) which the same file base -> add the file to this mets:div
                physicalSubDiv = physicalDiv.byFileId(existingFileID);
                physicalSubDiv.add(new Fptr(file.getId()));
            } else {
                // there is no mets:div with this file
                physicalSubDiv = new PhysicalSubDiv(PhysicalSubDiv.ID_PREFIX + fileBase, PhysicalSubDiv.TYPE_PAGE);
                physicalSubDiv.add(new Fptr(file.getId()));
                physicalDiv.add(physicalSubDiv);
            }
            // add to struct link
            structLink.addSmLink(new SmLink(lastLogicalDiv.getId(), physicalSubDiv.getId()));
        } catch (Exception exc) {
            LOGGER.error("Unable to add file {} to mets.xml of {}", href, derivatePath.getOwner(), exc);
        }
    });
}
Also used : Arrays(java.util.Arrays) URLDecoder(java.net.URLDecoder) StructLink(org.mycore.mets.model.struct.StructLink) URISyntaxException(java.net.URISyntaxException) LOCTYPE(org.mycore.mets.model.struct.LOCTYPE) MCRMarkManager(org.mycore.datamodel.common.MCRMarkManager) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) Locale(java.util.Locale) FileGrp(org.mycore.mets.model.files.FileGrp) Map(java.util.Map) MCRMetsFileUse(org.mycore.mets.model.simple.MCRMetsFileUse) File(org.mycore.mets.model.files.File) Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) PhysicalStructMap(org.mycore.mets.model.struct.PhysicalStructMap) Format(org.jdom2.output.Format) MCRPath(org.mycore.datamodel.niofs.MCRPath) Collection(java.util.Collection) MCRPersistenceException(org.mycore.common.MCRPersistenceException) Set(java.util.Set) Mets(org.mycore.mets.model.Mets) Collectors(java.util.stream.Collectors) LogicalDiv(org.mycore.mets.model.struct.LogicalDiv) XPathExpression(org.jdom2.xpath.XPathExpression) SmLink(org.mycore.mets.model.struct.SmLink) Objects(java.util.Objects) FileVisitResult(java.nio.file.FileVisitResult) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) LogicalStructMap(org.mycore.mets.model.struct.LogicalStructMap) SAXException(org.xml.sax.SAXException) MCRStreamUtils(org.mycore.common.MCRStreamUtils) MCRPathContent(org.mycore.common.content.MCRPathContent) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Element(org.jdom2.Element) PhysicalDiv(org.mycore.mets.model.struct.PhysicalDiv) XPathFactory(org.jdom2.xpath.XPathFactory) MCRConstants(org.mycore.common.MCRConstants) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) MCRConfiguration(org.mycore.common.config.MCRConfiguration) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MCRXMLFunctions(org.mycore.common.xml.MCRXMLFunctions) MCRDirectory(org.mycore.datamodel.ifs2.MCRDirectory) OutputStream(java.io.OutputStream) MCRContentTypes(org.mycore.datamodel.niofs.MCRContentTypes) Files(java.nio.file.Files) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) PhysicalSubDiv(org.mycore.mets.model.struct.PhysicalSubDiv) XMLOutputter(org.jdom2.output.XMLOutputter) Attribute(org.jdom2.Attribute) Fptr(org.mycore.mets.model.struct.Fptr) Paths(java.nio.file.Paths) FLocat(org.mycore.mets.model.files.FLocat) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) Filters(org.jdom2.filter.Filters) PhysicalStructMap(org.mycore.mets.model.struct.PhysicalStructMap) ArrayList(java.util.ArrayList) StructLink(org.mycore.mets.model.struct.StructLink) FileGrp(org.mycore.mets.model.files.FileGrp) 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) LogicalDiv(org.mycore.mets.model.struct.LogicalDiv) SmLink(org.mycore.mets.model.struct.SmLink) FLocat(org.mycore.mets.model.files.FLocat) Files(java.nio.file.Files) MCRPath(org.mycore.datamodel.niofs.MCRPath) File(org.mycore.mets.model.files.File)

Example 8 with Mets

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

the class MCRMetsSave method updateMetsOnUrnGenerate.

/**
 * Inserts the given URNs into the Mets document.
 * @param derivateID The {@link MCRObjectID} of the Derivate wich contains the METs file
 * @param fileUrnMap a {@link Map} which contains the file as key and the urn as  as value
 */
public static void updateMetsOnUrnGenerate(MCRObjectID derivateID, Map<String, String> fileUrnMap) throws JDOMException, SAXException, IOException {
    Document mets = getCurrentMets(derivateID.toString());
    if (mets == null) {
        LOGGER.info(MessageFormat.format("Derivate with id \"{0}\" has no mets file. Nothing to do", derivateID));
        return;
    }
    LOGGER.info(MessageFormat.format("Update {0} URNS in Mets.xml", fileUrnMap.size()));
    Mets metsObject = new Mets(mets);
    updateURNsInMetsDocument(metsObject, fileUrnMap);
    saveMets(metsObject.asDocument(), derivateID);
}
Also used : Mets(org.mycore.mets.model.Mets) Document(org.jdom2.Document)

Example 9 with Mets

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

the class MCRSimpleModelXMLConverter method toXML.

/**
 * Converts MetsSimpleModel to XML
 * @param msm the MetsSimpleModel which should be converted
 * @return xml
 */
public static Document toXML(MCRMetsSimpleModel msm) {
    Mets mets = new Mets();
    Hashtable<MCRMetsPage, String> pageIdMap = new Hashtable<>();
    Map<String, String> idToNewIDMap = new Hashtable<>();
    buildPhysicalPages(msm, mets, pageIdMap, idToNewIDMap);
    Hashtable<MCRMetsSection, String> sectionIdMap = new Hashtable<>();
    buildLogicalPages(msm, mets, sectionIdMap, idToNewIDMap);
    StructLink structLink = mets.getStructLink();
    msm.getSectionPageLinkList().stream().map((metsLink) -> {
        MCRMetsSection section = metsLink.getFrom();
        MCRMetsPage page = metsLink.getTo();
        String fromId = sectionIdMap.get(section);
        String toId = pageIdMap.get(page);
        return new SmLink(fromId, toId);
    }).forEach(structLink::addSmLink);
    return mets.asDocument();
}
Also used : PhysicalDiv(org.mycore.mets.model.struct.PhysicalDiv) MCRMetsPage(org.mycore.mets.model.simple.MCRMetsPage) StructLink(org.mycore.mets.model.struct.StructLink) LOCTYPE(org.mycore.mets.model.struct.LOCTYPE) Area(org.mycore.mets.model.struct.Area) MCRException(org.mycore.common.MCRException) Document(org.jdom2.Document) FileGrp(org.mycore.mets.model.files.FileGrp) Map(java.util.Map) MCRMetsFileUse(org.mycore.mets.model.simple.MCRMetsFileUse) File(org.mycore.mets.model.files.File) Hashtable(java.util.Hashtable) PhysicalStructMap(org.mycore.mets.model.struct.PhysicalStructMap) Mets(org.mycore.mets.model.Mets) UUID(java.util.UUID) Seq(org.mycore.mets.model.struct.Seq) LogicalDiv(org.mycore.mets.model.struct.LogicalDiv) PhysicalSubDiv(org.mycore.mets.model.struct.PhysicalSubDiv) SmLink(org.mycore.mets.model.struct.SmLink) List(java.util.List) Fptr(org.mycore.mets.model.struct.Fptr) LogicalStructMap(org.mycore.mets.model.struct.LogicalStructMap) FLocat(org.mycore.mets.model.files.FLocat) MCRMetsSection(org.mycore.mets.model.simple.MCRMetsSection) MCRMetsSimpleModel(org.mycore.mets.model.simple.MCRMetsSimpleModel) SmLink(org.mycore.mets.model.struct.SmLink) Mets(org.mycore.mets.model.Mets) Hashtable(java.util.Hashtable) MCRMetsPage(org.mycore.mets.model.simple.MCRMetsPage) StructLink(org.mycore.mets.model.struct.StructLink) MCRMetsSection(org.mycore.mets.model.simple.MCRMetsSection)

Example 10 with Mets

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

the class MCRXMLSimpleModelConverter method buildPageList.

private static List<MCRMetsPage> buildPageList(Mets mets, Map<String, MCRMetsPage> idPageMap, Map<String, MCRMetsFile> idFileMap) {
    PhysicalStructMap physicalStructMap = (PhysicalStructMap) mets.getStructMap(PhysicalStructMap.TYPE);
    List<PhysicalSubDiv> physicalSubDivs = physicalStructMap.getDivContainer().getChildren();
    List<MCRMetsPage> result = new ArrayList<>();
    physicalSubDivs.stream().map((physicalSubDiv) -> {
        // Convert PhysicalSubDiv to MetsPage
        MCRMetsPage metsPage = new MCRMetsPage();
        metsPage.setId(physicalSubDiv.getId());
        metsPage.setOrderLabel(physicalSubDiv.getOrderLabel());
        metsPage.setContentIds(physicalSubDiv.getContentids());
        // Add all MetsFile to the MetsPage
        List<MCRMetsFile> fileList = metsPage.getFileList();
        physicalSubDiv.getChildren().stream().map(file -> idFileMap.get(file.getFileId())).forEachOrdered(fileList::add);
        // return a entry of physicalSubDiv.id and MetsPage
        return new AbstractMap.SimpleEntry<>(physicalSubDiv.getId(), metsPage);
    }).forEachOrdered((entry) -> {
        // Put page to list
        result.add(entry.getValue());
        // Put that generated entry to a Hashtable
        idPageMap.put(entry.getKey(), entry.getValue());
    });
    return result;
}
Also used : MCRMetsPage(org.mycore.mets.model.simple.MCRMetsPage) Mets(org.mycore.mets.model.Mets) MCRException(org.mycore.common.MCRException) IStructMap(org.mycore.mets.model.struct.IStructMap) LogicalDiv(org.mycore.mets.model.struct.LogicalDiv) PhysicalSubDiv(org.mycore.mets.model.struct.PhysicalSubDiv) ArrayList(java.util.ArrayList) Document(org.jdom2.Document) MCRMetsFile(org.mycore.mets.model.simple.MCRMetsFile) AbstractMap(java.util.AbstractMap) List(java.util.List) FileGrp(org.mycore.mets.model.files.FileGrp) LogicalStructMap(org.mycore.mets.model.struct.LogicalStructMap) Map(java.util.Map) MCRMetsAltoLink(org.mycore.mets.model.simple.MCRMetsAltoLink) MCRMetsFileUse(org.mycore.mets.model.simple.MCRMetsFileUse) MCRMetsSection(org.mycore.mets.model.simple.MCRMetsSection) MCRMetsSimpleModel(org.mycore.mets.model.simple.MCRMetsSimpleModel) Hashtable(java.util.Hashtable) MCRMetsLink(org.mycore.mets.model.simple.MCRMetsLink) PhysicalStructMap(org.mycore.mets.model.struct.PhysicalStructMap) AbstractMap(java.util.AbstractMap) PhysicalStructMap(org.mycore.mets.model.struct.PhysicalStructMap) MCRMetsPage(org.mycore.mets.model.simple.MCRMetsPage) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) PhysicalSubDiv(org.mycore.mets.model.struct.PhysicalSubDiv)

Aggregations

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