Search in sources :

Example 1 with File

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

the class MCRMetsMods2IIIFConverter method getIIIFIdentifier.

protected String getIIIFIdentifier(PhysicalSubDiv subDiv) {
    File file = subDiv.getChildren().stream().map(fptr -> imageGrp.getFileById(fptr.getFileId())).filter(Objects::nonNull).findAny().get();
    String cleanHref = file.getFLocat().getHref();
    cleanHref = cleanHref.substring(cleanHref.indexOf(this.identifier), cleanHref.length());
    return cleanHref;
}
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) Objects(java.util.Objects) File(org.mycore.mets.model.files.File)

Example 2 with File

use of org.mycore.mets.model.files.File 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 3 with File

use of org.mycore.mets.model.files.File 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 4 with File

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

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

the class MCRMetsSave method updateURNsInMetsDocument.

/**
 * Inserts the given URNs into the {@link Mets} Object.
 * @param mets the {@link Mets} object were the URNs should be inserted.
 * @param fileUrnMap a {@link Map} wich contains the file as key and the urn as  as value
 */
public static void updateURNsInMetsDocument(Mets mets, Map<String, String> fileUrnMap) throws UnsupportedEncodingException {
    // put all files of the mets in a list
    List<FileGrp> fileGroups = mets.getFileSec().getFileGroups();
    List<File> files = new ArrayList<>();
    for (FileGrp fileGrp : fileGroups) {
        files.addAll(fileGrp.getFileList());
    }
    // combine the filename and the id in a map
    Map<String, String> idFileMap = new HashMap<>();
    for (File file : files) {
        idFileMap.put(file.getId(), file.getFLocat().getHref());
    }
    List<PhysicalSubDiv> childs = ((PhysicalStructMap) mets.getStructMap(PhysicalStructMap.TYPE)).getDivContainer().getChildren();
    for (PhysicalSubDiv divChild : childs) {
        String idMets = divChild.getChildren().get(0).getFileId();
        // check if there is a URN for the file
        String file = "/" + URLDecoder.decode(idFileMap.get(idMets), "UTF-8");
        if (fileUrnMap.containsKey(file)) {
            divChild.setContentids(fileUrnMap.get(file));
        }
    }
}
Also used : HashMap(java.util.HashMap) FileGrp(org.mycore.mets.model.files.FileGrp) ArrayList(java.util.ArrayList) File(org.mycore.mets.model.files.File) PhysicalSubDiv(org.mycore.mets.model.struct.PhysicalSubDiv)

Aggregations

File (org.mycore.mets.model.files.File)11 FileGrp (org.mycore.mets.model.files.FileGrp)8 FLocat (org.mycore.mets.model.files.FLocat)7 PhysicalSubDiv (org.mycore.mets.model.struct.PhysicalSubDiv)7 Fptr (org.mycore.mets.model.struct.Fptr)6 PhysicalDiv (org.mycore.mets.model.struct.PhysicalDiv)6 PhysicalStructMap (org.mycore.mets.model.struct.PhysicalStructMap)6 URISyntaxException (java.net.URISyntaxException)5 ArrayList (java.util.ArrayList)5 MCRPersistenceException (org.mycore.common.MCRPersistenceException)5 IOException (java.io.IOException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 HashMap (java.util.HashMap)4 JDOMException (org.jdom2.JDOMException)4 Mets (org.mycore.mets.model.Mets)4 SAXException (org.xml.sax.SAXException)4 Collections (java.util.Collections)3 List (java.util.List)3 Map (java.util.Map)3 Objects (java.util.Objects)3