Search in sources :

Example 86 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath 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 87 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRMigrationCommands method tryRawPath.

private static boolean tryRawPath(MCRObjectID objectID, Element derivateLinkElement, String href, MCRMetaDerivateLink link, String owner) {
    String rawPath = link.getRawPath();
    MCRPath mcrPath = MCRPath.getPath(owner, rawPath);
    if (Files.exists(mcrPath)) {
        // path exists -> do URI encoding for href
        try {
            String encodedHref = MCRXMLFunctions.encodeURIPath(rawPath);
            derivateLinkElement.setAttribute("href", owner + encodedHref, MCRConstants.XLINK_NAMESPACE);
            return true;
        } catch (URISyntaxException uriEncodeException) {
            LOGGER.error("Unable to encode {} for object {}", rawPath, objectID, uriEncodeException);
            return false;
        }
    }
    return false;
}
Also used : URISyntaxException(java.net.URISyntaxException) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 88 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRDFGLinkServlet method doGetPost.

@Override
protected void doGetPost(MCRServletJob job) throws Exception {
    HttpServletRequest request = job.getRequest();
    HttpServletResponse response = job.getResponse();
    String filePath = request.getParameter("file") == null ? "" : request.getParameter("file");
    String derivateID = request.getParameter("deriv") == null ? "" : request.getParameter("deriv");
    if (derivateID.equals("")) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Derivate is not set");
    }
    String encodedMetsURL = URLEncoder.encode(MCRServlet.getServletBaseURL() + "MCRMETSServlet/" + derivateID + "?XSL.Style=dfg", "UTF-8");
    LOGGER.info(request.getPathInfo());
    MCRPath rootPath = MCRPath.getPath(derivateID, "/");
    if (!Files.isDirectory(rootPath)) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, MessageFormat.format("Derivate {0} does not exist.", derivateID));
        return;
    }
    request.setAttribute("XSL.derivateID", derivateID);
    Collection<String> linkList = MCRLinkTableManager.instance().getSourceOf(derivateID);
    if (linkList.isEmpty()) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageFormat.format("Derivate {0} is not linked with a MCRObject. Please contact an administrator.", derivateID));
        return;
    }
    // TODO: this seems very very wrong
    if (filePath == "") {
        MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivateID));
        filePath = derivate.getDerivate().getInternals().getMainDoc();
    }
    MCRPath metsPath = (MCRPath) rootPath.resolve("mets.xml");
    int imageNumber = -2;
    if (Files.exists(metsPath)) {
        imageNumber = getOrderNumber(new MCRPathContent(metsPath).asXML(), filePath);
    } else {
        MCRContent metsContent = getMetsSource(job, useExistingMets(request), derivateID);
        imageNumber = getOrderNumber(metsContent.asXML(), filePath);
    }
    String dfgURL = "";
    switch(imageNumber) {
        case -1:
            response.sendError(HttpServletResponse.SC_CONFLICT, MessageFormat.format("Image \"{0}\" not found in the MCRDerivate. Please contact an administrator.", filePath));
            return;
        case -2:
            dfgURL = "http://dfg-viewer.de/show/?set[mets]=" + encodedMetsURL;
            break;
        default:
            dfgURL = "http://dfg-viewer.de/show/?set[mets]=" + encodedMetsURL + "&set[image]=" + imageNumber;
            break;
    }
    response.sendRedirect(dfgURL);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MCRPathContent(org.mycore.common.content.MCRPathContent) HttpServletResponse(javax.servlet.http.HttpServletResponse) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRContent(org.mycore.common.content.MCRContent)

Example 89 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRDFGLinkServlet method getMetsSource.

/**
 * Returns the mets document wrapped in a {@link MCRContent} object.
 *
 * @param job
 * @param useExistingMets
 * @return
 * @throws Exception
 */
private static MCRContent getMetsSource(MCRServletJob job, boolean useExistingMets, String derivate) throws Exception {
    MCRPath metsFile = MCRPath.getPath(derivate, "/mets.xml");
    try {
        job.getRequest().setAttribute("XSL.derivateID", derivate);
        job.getRequest().setAttribute("XSL.objectID", MCRLinkTableManager.instance().getSourceOf(derivate).iterator().next());
    } catch (Exception x) {
        LOGGER.warn("Unable to set \"XSL.objectID\" attribute to current request", x);
    }
    boolean metsExists = Files.exists(metsFile);
    if (metsExists && useExistingMets) {
        MCRContent content = new MCRPathContent(metsFile);
        content.setDocType("mets");
        return content;
    } else {
        Document mets = MCRMETSGeneratorFactory.create(metsFile.getParent()).generate().asDocument();
        return new MCRJDOMContent(mets);
    }
}
Also used : MCRPathContent(org.mycore.common.content.MCRPathContent) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 90 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRMETSServlet method getMetsSource.

/**
 * Returns the mets document wrapped in a {@link MCRContent} object.
 */
static MCRContent getMetsSource(MCRServletJob job, boolean useExistingMets, String derivate) throws Exception {
    MCRPath metsPath = MCRPath.getPath(derivate, "/mets.xml");
    try {
        job.getRequest().setAttribute("XSL.derivateID", derivate);
        String objectid = MCRLinkTableManager.instance().getSourceOf(derivate).iterator().next();
        if (objectid == null || objectid.length() == 0) {
            MCRDerivate derObj = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivate));
            MCRObjectID ownerID = derObj.getOwnerID();
            objectid = ownerID.toString();
        }
        job.getRequest().setAttribute("XSL.objectID", objectid);
    } catch (Exception x) {
        LOGGER.warn("Unable to set \"XSL.objectID\" attribute to current request", x);
    }
    boolean metsExists = Files.exists(metsPath);
    if (metsExists && useExistingMets) {
        MCRContent content = new MCRPathContent(metsPath);
        content.setDocType("mets");
        return content;
    } else {
        Document mets = MCRMETSGeneratorFactory.create(MCRPath.getPath(derivate, "/")).generate().asDocument();
        if (!metsExists && STORE_METS_ON_GENERATE) {
            MCRMetsSave.saveMets(mets, MCRObjectID.getInstance(derivate));
        }
        return new MCRJDOMContent(mets);
    }
}
Also used : MCRPathContent(org.mycore.common.content.MCRPathContent) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

MCRPath (org.mycore.datamodel.niofs.MCRPath)96 IOException (java.io.IOException)49 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)26 Path (java.nio.file.Path)25 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)22 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)22 Document (org.jdom2.Document)15 JDOMException (org.jdom2.JDOMException)15 MCRPersistenceException (org.mycore.common.MCRPersistenceException)14 MCRException (org.mycore.common.MCRException)13 MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)13 MCRAccessException (org.mycore.access.MCRAccessException)12 Files (java.nio.file.Files)11 Collectors (java.util.stream.Collectors)11 LogManager (org.apache.logging.log4j.LogManager)11 Logger (org.apache.logging.log4j.Logger)11 FileVisitResult (java.nio.file.FileVisitResult)10 NoSuchFileException (java.nio.file.NoSuchFileException)10 Date (java.util.Date)10 List (java.util.List)10