Search in sources :

Example 41 with MCRPath

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

the class MCRIView2Tools method getSupportedMainFile.

/**
 * @param derivateID
 *            ID of derivate
 * @return empty String or absolute path to main file of derivate if file is supported.
 */
public static String getSupportedMainFile(String derivateID) {
    try {
        MCRDerivate deriv = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivateID));
        String nameOfMainFile = deriv.getDerivate().getInternals().getMainDoc();
        // verify support
        if (nameOfMainFile != null && !nameOfMainFile.equals("")) {
            MCRPath mainFile = MCRPath.getPath(derivateID, '/' + nameOfMainFile);
            if (mainFile != null && isFileSupported(mainFile))
                return mainFile.getRoot().relativize(mainFile).toString();
        }
    } catch (Exception e) {
        LOGGER.warn("Could not get main file of derivate.", e);
    }
    return "";
}
Also used : MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRPath(org.mycore.datamodel.niofs.MCRPath) NoSuchFileException(java.nio.file.NoSuchFileException) JDOMException(org.jdom2.JDOMException) FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) IOException(java.io.IOException) FileSystemAlreadyExistsException(java.nio.file.FileSystemAlreadyExistsException)

Example 42 with MCRPath

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

the class MCRMetsResolver method resolve.

@Override
public Source resolve(String href, String base) throws TransformerException {
    String id = href.substring(href.indexOf(":") + 1);
    LOGGER.debug("Reading METS for ID {}", id);
    MCRObjectID objId = MCRObjectID.getInstance(id);
    if (!objId.getTypeId().equals("derivate")) {
        String derivateID = getDerivateFromObject(id);
        if (derivateID == null) {
            return new JDOMSource(new Element("mets", Namespace.getNamespace("mets", "http://www.loc.gov/METS/")));
        }
        id = derivateID;
    }
    MCRPath metsPath = MCRPath.getPath(id, "/mets.xml");
    try {
        if (Files.exists(metsPath)) {
            // ignoreNodes.add(metsFile);
            return new MCRPathContent(metsPath).getSource();
        }
        Document mets = MCRMETSGeneratorFactory.create(MCRPath.getPath(id, "/")).generate().asDocument();
        return new JDOMSource(mets);
    } catch (Exception e) {
        throw new TransformerException(e);
    }
}
Also used : Element(org.jdom2.Element) MCRPathContent(org.mycore.common.content.MCRPathContent) JDOMSource(org.jdom2.transform.JDOMSource) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document) TransformerException(javax.xml.transform.TransformerException) TransformerException(javax.xml.transform.TransformerException)

Example 43 with MCRPath

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

the class MCRMetsSave method isComplete.

/**
 * @return true if all files owned by the derivate appearing in the master file group or false otherwise
 */
public static boolean isComplete(Mets mets, MCRObjectID derivateId) {
    try {
        FileGrp fileGroup = mets.getFileSec().getFileGroup(FileGrp.USE_MASTER);
        MCRPath rootPath = MCRPath.getPath(derivateId.toString(), "/");
        return isComplete(fileGroup, rootPath);
    } catch (Exception ex) {
        LOGGER.error("Error while validating mets", ex);
        return false;
    }
}
Also used : FileGrp(org.mycore.mets.model.files.FileGrp) MCRPath(org.mycore.datamodel.niofs.MCRPath) 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)

Example 44 with MCRPath

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

the class MCRMetsSave method saveMets.

/**
 * Saves the content of the given document to file, if no mets present and then adds the file to
 * the derivate with the given id. The name of the file depends on property
 * 'MCR.Mets.Filename'. If this property has not been set 'mets.xml' is used
 * as a default filename.
 *
 * @param overwrite
 *          if true existing mets-file will be overwritten
 * @param validate
 *          if true the document will be validated before its stored
 * @return
 *          true if the given document was successfully saved, otherwise false
 */
public static synchronized boolean saveMets(Document document, MCRObjectID derivateId, boolean overwrite, boolean validate) {
    // add the file to the existing derivate in ifs
    MCRPath metsFile = getMetsFile(derivateId.toString());
    if (metsFile == null) {
        metsFile = createMetsFile(derivateId.toString());
    } else if (!overwrite) {
        return false;
    }
    if (validate && !Mets.isValid(document)) {
        LOGGER.warn("Storing mets.xml for {} failed cause the given document was invalid.", derivateId);
        return false;
    }
    try (OutputStream metsOut = Files.newOutputStream(metsFile)) {
        XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
        xout.output(document, metsOut);
        LOGGER.info("Storing file content from \"{}\" to derivate \"{}\"", getMetsFileName(), derivateId);
    } catch (Exception e) {
        LOGGER.error(e);
        return false;
    }
    return true;
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) OutputStream(java.io.OutputStream) MCRPath(org.mycore.datamodel.niofs.MCRPath) 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)

Example 45 with MCRPath

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

the class MetsResource method get.

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/crud/{derivateId}")
public String get(@PathParam("derivateId") String derivateId) {
    MCRObjectID derivateIdObject = MCRObjectID.getInstance(derivateId);
    checkDerivateExists(derivateIdObject);
    checkDerivateAccess(derivateIdObject, MCRAccessManager.PERMISSION_READ);
    MCRPath rootPath = MCRPath.getPath(derivateId, "/");
    if (!Files.isDirectory(rootPath)) {
        throw new WebApplicationException(Response.Status.NOT_FOUND);
    }
    MCRPath metsPath = MCRPath.getPath(derivateId, METS_XML_PATH);
    try {
        return MCRSimpleModelJSONConverter.toJSON(MCRXMLSimpleModelConverter.fromXML(getMetsDocument(metsPath)));
    } catch (Exception e) {
        throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRException(org.mycore.common.MCRException) JDOMException(org.jdom2.JDOMException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

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