Search in sources :

Example 26 with MCRDerivate

use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.

the class MCRDerivateCommands method processFromFile.

/**
 * Loads or updates an MCRDerivates from an XML file.
 *
 * @param file
 *            the location of the xml file
 * @param update
 *            if true, object will be updated, else object is created
 * @param importMode
 *            if true, servdates are taken from xml file
 * @throws SAXParseException
 * @throws MCRAccessException see {@link MCRMetadataManager#update(MCRDerivate)}
 * @throws MCRPersistenceException
 */
private static boolean processFromFile(File file, boolean update, boolean importMode) throws SAXParseException, IOException, MCRPersistenceException, MCRAccessException {
    if (!file.getName().endsWith(".xml")) {
        LOGGER.warn("{} ignored, does not end with *.xml", file);
        return false;
    }
    if (!file.isFile()) {
        LOGGER.warn("{} ignored, is not a file.", file);
        return false;
    }
    LOGGER.info("Reading file {} ...", file);
    MCRDerivate derivate = new MCRDerivate(file.toURI());
    derivate.setImportMode(importMode);
    // Replace relative path with absolute path of files
    if (derivate.getDerivate().getInternals() != null) {
        String path = derivate.getDerivate().getInternals().getSourcePath();
        path = path.replace('/', File.separatorChar).replace('\\', File.separatorChar);
        if (path.trim().length() <= 1) {
            // the path is the path name plus the name of the derivate -
            path = derivate.getId().toString();
        }
        File sPath = new File(path);
        if (!sPath.isAbsolute()) {
            // only change path to absolute path when relative
            String prefix = file.getParent();
            if (prefix != null) {
                path = prefix + File.separator + path;
            }
        }
        derivate.getDerivate().getInternals().setSourcePath(path);
        LOGGER.info("Source path --> {}", path);
    }
    LOGGER.info("Label --> {}", derivate.getLabel());
    if (update) {
        MCRMetadataManager.update(derivate);
        LOGGER.info("{} updated.", derivate.getId());
        LOGGER.info("");
    } else {
        MCRMetadataManager.create(derivate);
        LOGGER.info("{} loaded.", derivate.getId());
        LOGGER.info("");
    }
    return true;
}
Also used : MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) File(java.io.File)

Example 27 with MCRDerivate

use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.

the class MCRBasketPersistence method updateBasket.

/**
 * Updates the basket's data in the persistent store by saving its XML representation
 * to a file in a derivate. The ID of the derivate is given in the basket's properties.
 */
public static void updateBasket(MCRBasket basket) throws Exception {
    String derivateID = basket.getDerivateID();
    MCRObjectID derivateOID = MCRObjectID.getInstance(derivateID);
    MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(derivateOID);
    MCRPath file = getBasketFile(derivateID);
    writeBasketToFile(basket, derivate, file);
}
Also used : MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 28 with MCRDerivate

use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.

the class MCRBasketPersistence method createDerivateWithBasket.

/**
 * Creates a new derivate including a file basket.xml which stores the persistent
 * data of the given basket.
 *
 * @param basket the basket to store in a new file in a new derivate
 * @param ownerID the ID of the MCRObject owning the new derivate
 * @throws MCRAccessException see {@link MCRMetadataManager#create(MCRDerivate)}
 * @throws MCRPersistenceException
 */
public static void createDerivateWithBasket(MCRBasket basket, MCRObjectID ownerID) throws IOException, MCRPersistenceException, MCRAccessException {
    String base = ownerID.getProjectId() + "_derivate";
    MCRObjectID derivateOID = MCRObjectID.getNextFreeId(base);
    String derivateID = derivateOID.toString();
    MCRDerivate derivate = createNewDerivate(ownerID, derivateOID);
    basket.setDerivateID(derivateID);
    writeBasketToFile(basket, derivate, getBasketFile(derivateID));
}
Also used : MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Example 29 with MCRDerivate

use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.

the class MCRViewerConfiguration method getFilePath.

/**
 * Helper method to get the path to the start file. The path is
 * URI decoded and starts with a slash.
 *
 * @param request http request
 * @return path to the file or null if the path couldn't be retrieved
 */
public static String getFilePath(HttpServletRequest request) {
    try {
        String fromPath = getFromPath(request.getPathInfo(), 2);
        if (fromPath == null || fromPath.isEmpty() || fromPath.equals("/")) {
            String derivate = getDerivate(request);
            MCRDerivate deriv = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivate));
            String nameOfMainFile = deriv.getDerivate().getInternals().getMainDoc();
            return "/" + nameOfMainFile;
        }
        return fromPath;
    } catch (Exception exc) {
        LOGGER.warn("Unable to get the file path of request {}", request.getRequestURI());
        return null;
    }
}
Also used : MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) JAXBException(javax.xml.bind.JAXBException)

Example 30 with MCRDerivate

use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.

the class MCRDerivateCommands method setMainFile.

@MCRCommand(syntax = "set main file of {0} to {1}", help = "Sets the main file of the derivate with the id {0} to " + "the file with the path {1}")
public static void setMainFile(final String derivateIDString, final String filePath) {
    if (!MCRObjectID.isValid(derivateIDString)) {
        LOGGER.error("{} is not valid. ", derivateIDString);
        return;
    }
    // check for derivate exist
    final MCRObjectID derivateID = MCRObjectID.getInstance(derivateIDString);
    if (!MCRMetadataManager.exists(derivateID)) {
        LOGGER.error("{} does not exist!", derivateIDString);
        return;
    }
    // remove leading slash
    String cleanPath = filePath;
    if (filePath.startsWith(String.valueOf(MCRAbstractFileSystem.SEPARATOR))) {
        cleanPath = filePath.substring(1, filePath.length());
    }
    // check for file exist
    final MCRPath path = MCRPath.getPath(derivateID.toString(), cleanPath);
    if (!Files.exists(path)) {
        LOGGER.error("File {} does not exist!", cleanPath);
        return;
    }
    final MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(derivateID);
    derivate.getDerivate().getInternals().setMainDoc(cleanPath);
    MCRMetadataManager.updateMCRDerivateXML(derivate);
    LOGGER.info("The main file of {} is now '{}'!", derivateIDString, cleanPath);
}
Also used : MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)53 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)27 IOException (java.io.IOException)22 MCRPath (org.mycore.datamodel.niofs.MCRPath)19 MCRObject (org.mycore.datamodel.metadata.MCRObject)13 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)10 MCRAccessException (org.mycore.access.MCRAccessException)9 Path (java.nio.file.Path)8 MCRException (org.mycore.common.MCRException)8 MCRRestAPIException (org.mycore.restapi.v1.errors.MCRRestAPIException)8 Document (org.jdom2.Document)7 MCRPersistenceException (org.mycore.common.MCRPersistenceException)7 Date (java.util.Date)6 JDOMException (org.jdom2.JDOMException)6 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)6 ArrayList (java.util.ArrayList)5 MCRMetaIFS (org.mycore.datamodel.metadata.MCRMetaIFS)5 MCRObjectDerivate (org.mycore.datamodel.metadata.MCRObjectDerivate)5 SAXException (org.xml.sax.SAXException)5 StringWriter (java.io.StringWriter)4