Search in sources :

Example 6 with Fptr

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

the class MCRMetsSave method updateOnImageFile.

private static void updateOnImageFile(Document mets, String fileId, String path) {
    LOGGER.debug("FILE is a image!");
    // check if custom files are present and save the ids
    String[] customFileGroups = { MCRMetsFileUse.TRANSCRIPTION.toString(), MCRMetsFileUse.ALTO.toString(), MCRMetsFileUse.TRANSLATION.toString() };
    // add to structMap physical
    PhysicalSubDiv div = new PhysicalSubDiv(PhysicalSubDiv.ID_PREFIX + fileId, PhysicalSubDiv.TYPE_PAGE);
    div.add(new Fptr(fileId));
    Arrays.stream(customFileGroups).map(customFileGroup -> searchFileInGroup(mets, path, customFileGroup)).filter(Objects::nonNull).map(Fptr::new).forEach(div::add);
    // actually alter the mets document
    Element structMapPhys = getPhysicalStructmap(mets);
    structMapPhys.addContent(div.asElement());
    // add to structLink
    SmLink smLink = getDefaultSmLink(mets, div);
    Element structLink = getStructLink(mets);
    structLink.addContent(smLink.asElement());
}
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) SmLink(org.mycore.mets.model.struct.SmLink) Element(org.jdom2.Element) Objects(java.util.Objects) Fptr(org.mycore.mets.model.struct.Fptr) PhysicalSubDiv(org.mycore.mets.model.struct.PhysicalSubDiv)

Example 7 with Fptr

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

the class MCRDFGLinkServlet method getOrderNumber.

private static int getOrderNumber(Document metsDoc, String fileHref) {
    int orderNumber = -1;
    String fileID = null;
    try {
        Mets mets = new Mets(metsDoc);
        List<FileGrp> fileGroups = mets.getFileSec().getFileGroups();
        for (FileGrp fileGrp : fileGroups) {
            List<File> fileList = fileGrp.getFileList();
            for (File file : fileList) {
                FLocat fLocat = file.getFLocat();
                if (fLocat.getHref().equals(MCRXMLFunctions.encodeURIPath(fileHref)))
                    fileID = file.getId();
            }
        }
        if (fileID != null) {
            PhysicalStructMap structMap = (PhysicalStructMap) mets.getStructMap(PhysicalStructMap.TYPE);
            PhysicalDiv rootDiv = structMap.getDivContainer();
            List<PhysicalSubDiv> children = rootDiv.getChildren();
            for (int index = 0; index < children.size(); index++) {
                PhysicalSubDiv physicalSubDiv = children.get(index);
                List<Fptr> fptrList = physicalSubDiv.getChildren();
                for (Fptr fptr : fptrList) {
                    if (fptr.getFileId().equals(fileID))
                        orderNumber = index + 1;
                }
            }
        }
    } catch (Exception e) {
        throw new MCRPersistenceException("could not parse mets.xml", e);
    }
    return orderNumber;
}
Also used : PhysicalStructMap(org.mycore.mets.model.struct.PhysicalStructMap) FileGrp(org.mycore.mets.model.files.FileGrp) Fptr(org.mycore.mets.model.struct.Fptr) PhysicalDiv(org.mycore.mets.model.struct.PhysicalDiv) MCRPersistenceException(org.mycore.common.MCRPersistenceException) PhysicalSubDiv(org.mycore.mets.model.struct.PhysicalSubDiv) Mets(org.mycore.mets.model.Mets) FLocat(org.mycore.mets.model.files.FLocat) File(org.mycore.mets.model.files.File) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 8 with Fptr

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

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

the class MCRMetsSave method updateOnCustomFile.

private static void updateOnCustomFile(Document mets, String fileId, String path) {
    LOGGER.debug("FILE is a custom file (ALTO/TEI)!");
    String matchId = searchFileInGroup(mets, path, MCRMetsFileUse.DEFAULT.toString());
    if (matchId == null) {
        // there is no file wich belongs to the alto xml so just return
        LOGGER.warn("no file found wich belongs to the custom xml : {}", path);
        return;
    }
    // check if there is a physical file
    Element physPageElement = getPhysicalFile(mets, matchId);
    if (physPageElement != null) {
        physPageElement.addContent(new Fptr(fileId).asElement());
        LOGGER.warn("physical page found for file {}", matchId);
    } else {
        LOGGER.warn("no physical page found for file {}", matchId);
    }
}
Also used : Element(org.jdom2.Element) Fptr(org.mycore.mets.model.struct.Fptr)

Example 10 with Fptr

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

the class MCRSimpleModelXMLConverter method buildPhysicalPages.

private static void buildPhysicalPages(MCRMetsSimpleModel msm, Mets mets, Map<MCRMetsPage, String> pageIdMap, Map<String, String> idToNewIDMap) {
    List<MCRMetsPage> pageList = msm.getMetsPageList();
    PhysicalStructMap structMap = (PhysicalStructMap) mets.getStructMap(PhysicalStructMap.TYPE);
    structMap.setDivContainer(new PhysicalDiv(PHYSICAL_ID_PREFIX + UUID.randomUUID(), PhysicalDiv.TYPE_PHYS_SEQ));
    for (MCRMetsPage page : pageList) {
        String id = page.getId();
        PhysicalSubDiv physicalSubDiv = new PhysicalSubDiv(id, DEFAULT_PHYSICAL_TYPE);
        String orderLabel = page.getOrderLabel();
        if (orderLabel != null) {
            physicalSubDiv.setOrderLabel(orderLabel);
        }
        String contentIds = page.getContentIds();
        if (contentIds != null) {
            physicalSubDiv.setContentids(contentIds);
        }
        structMap.getDivContainer().add(physicalSubDiv);
        pageIdMap.put(page, id);
        page.getFileList().forEach((simpleFile) -> {
            String href = simpleFile.getHref();
            String fileID = simpleFile.getId();
            String mimeType = simpleFile.getMimeType();
            MCRMetsFileUse use = simpleFile.getUse();
            idToNewIDMap.put(simpleFile.getId(), fileID);
            File file = new File(fileID, mimeType);
            FLocat fLocat = new FLocat(LOCTYPE.URL, href);
            file.setFLocat(fLocat);
            FileGrp fileGrp = getFileGroup(mets, use);
            fileGrp.addFile(file);
            physicalSubDiv.add(new Fptr(fileID));
        });
    }
}
Also used : PhysicalStructMap(org.mycore.mets.model.struct.PhysicalStructMap) MCRMetsPage(org.mycore.mets.model.simple.MCRMetsPage) FileGrp(org.mycore.mets.model.files.FileGrp) Fptr(org.mycore.mets.model.struct.Fptr) FLocat(org.mycore.mets.model.files.FLocat) File(org.mycore.mets.model.files.File) PhysicalDiv(org.mycore.mets.model.struct.PhysicalDiv) MCRMetsFileUse(org.mycore.mets.model.simple.MCRMetsFileUse) PhysicalSubDiv(org.mycore.mets.model.struct.PhysicalSubDiv)

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