Search in sources :

Example 71 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRUserCommands method assignUserToRole.

/**
 * This method adds a user as a member to a role
 *
 * @param userID
 *            the ID of the user which will be a member of the role represented by roleID
 * @param roleID
 *            the ID of the role to which the user with ID mbrUserID will be added
 */
@MCRCommand(syntax = "assign user {0} to role {1}", help = "Adds a user {0} as secondary member in the role {1}.", order = 120)
public static void assignUserToRole(String userID, String roleID) throws MCRException {
    try {
        MCRUser user = MCRUserManager.getUser(userID);
        user.assignRole(roleID);
        MCRUserManager.updateUser(user);
    } catch (Exception e) {
        throw new MCRException("Error while assigning " + userID + " to role " + roleID + ".", e);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRException(org.mycore.common.MCRException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXParseException(org.xml.sax.SAXParseException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 72 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRDerivateCommands method export.

/**
 * Export any MCRDerivate's to files named <em>MCRObjectID</em> .xml in a
 * directory and the objects under them named <em>MCRObjectID</em>. The
 * saving starts with fromID and runs to toID. ID's they was not found will
 * skiped. The method use the converter stylesheet mcr_<em>style</em>
 * _object.xsl.
 *
 * @param fromID
 *            the ID of the MCRObject from be save.
 * @param toID
 *            the ID of the MCRObject to be save.
 * @param dirname
 *            the filename to store the object
 * @param style
 *            the type of the stylesheet
 */
@MCRCommand(syntax = "export derivate from {0} to {1} to directory {2} with {3}", help = "The command store all derivates with MCRObjectID's between {0} and {1} to the directory {2} with the stylesheet {3}-object.xsl. For {3} save is the default.", order = 80)
public static void export(String fromID, String toID, String dirname, String style) {
    // check fromID and toID
    MCRObjectID fid = null;
    MCRObjectID tid = null;
    try {
        fid = MCRObjectID.getInstance(fromID);
    } catch (Exception ex) {
        LOGGER.error("FromID : {}", ex.getMessage());
        return;
    }
    try {
        tid = MCRObjectID.getInstance(toID);
    } catch (Exception ex) {
        LOGGER.error("ToID : {}", ex.getMessage());
        return;
    }
    // check dirname
    File dir = new File(dirname);
    if (dir.isFile()) {
        LOGGER.error("{} is not a dirctory.", dirname);
        return;
    }
    Transformer trans = getTransformer(style);
    int k = 0;
    try {
        for (int i = fid.getNumberAsInteger(); i < tid.getNumberAsInteger() + 1; i++) {
            exportDerivate(dir, trans, MCRObjectID.formatID(fid.getProjectId(), fid.getTypeId(), i));
            k++;
        }
    } catch (Exception ex) {
        LOGGER.error(ex.getMessage());
        LOGGER.error("Exception while store file or objects to {}", dir.getAbsolutePath(), ex);
        return;
    }
    LOGGER.info("{} Object's stored under {}.", k, dir.getAbsolutePath());
}
Also used : Transformer(javax.xml.transform.Transformer) MCRXSLTransformer(org.mycore.common.content.transformer.MCRXSLTransformer) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) File(java.io.File) JDOMException(org.jdom2.JDOMException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) TransformerException(javax.xml.transform.TransformerException) MCRException(org.mycore.common.MCRException) MCRAccessException(org.mycore.access.MCRAccessException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 73 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRDerivateCommands method transformXMLMatchingPatternWithStylesheet.

@MCRCommand(syntax = "transform xml matching file name pattern {0} in derivate {1} with stylesheet {2}", help = "Finds all files in Derivate {1} which match the pattern {0} (the complete path with regex: or glob:*.xml syntax) and transforms them with stylesheet {2}")
public static void transformXMLMatchingPatternWithStylesheet(String pattern, String derivate, String stylesheet) throws IOException {
    MCRXSLTransformer transformer = new MCRXSLTransformer(stylesheet);
    MCRPath derivateRoot = MCRPath.getPath(derivate, "/");
    PathMatcher matcher = derivateRoot.getFileSystem().getPathMatcher(pattern);
    Files.walkFileTree(derivateRoot, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (matcher.matches(file)) {
                LOGGER.info("The file {} matches the pattern {}", file, pattern);
                MCRContent sourceContent = new MCRPathContent(file);
                MCRContent resultContent = transformer.transform(sourceContent);
                try {
                    Document source = sourceContent.asXML();
                    Document result = resultContent.asXML();
                    LOGGER.info("Transforming complete!");
                    if (!MCRXMLHelper.deepEqual(source, result)) {
                        LOGGER.info("Writing result..");
                        resultContent.sendTo(file, StandardCopyOption.REPLACE_EXISTING);
                    } else {
                        LOGGER.info("Result and Source is the same..");
                    }
                } catch (JDOMException | SAXException e) {
                    throw new IOException("Error while processing file : " + file, e);
                }
            }
            return FileVisitResult.CONTINUE;
        }
    });
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) PathMatcher(java.nio.file.PathMatcher) MCRPathContent(org.mycore.common.content.MCRPathContent) MCRXSLTransformer(org.mycore.common.content.transformer.MCRXSLTransformer) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 74 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand 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)

Example 75 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRDerivateCommands method delete.

/**
 * Delete an MCRDerivate from the datastore.
 *
 * @param ID
 *            the ID of the MCRDerivate that should be deleted
 * @throws MCRActiveLinkException
 * @throws MCRAccessException see {@link MCRMetadataManager#delete(MCRDerivate)}
 */
@MCRCommand(syntax = "delete derivate {0}", help = "The command remove a derivate with the MCRObjectID {0}", order = 30)
public static void delete(String ID) throws MCRPersistenceException, MCRActiveLinkException, MCRAccessException {
    MCRObjectID objectID = MCRObjectID.getInstance(ID);
    MCRMetadataManager.deleteMCRDerivate(objectID);
    LOGGER.info("{} deleted.", objectID);
}
Also used : MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)106 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)37 MCRException (org.mycore.common.MCRException)34 IOException (java.io.IOException)30 File (java.io.File)22 ArrayList (java.util.ArrayList)18 Document (org.jdom2.Document)17 JDOMException (org.jdom2.JDOMException)17 MCRObject (org.mycore.datamodel.metadata.MCRObject)17 Path (java.nio.file.Path)16 SAXException (org.xml.sax.SAXException)16 EntityManager (javax.persistence.EntityManager)15 MCRAccessException (org.mycore.access.MCRAccessException)15 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)15 MCRPath (org.mycore.datamodel.niofs.MCRPath)15 FileNotFoundException (java.io.FileNotFoundException)13 SAXParseException (org.xml.sax.SAXParseException)12 List (java.util.List)11 Element (org.jdom2.Element)11 MCRPersistenceException (org.mycore.common.MCRPersistenceException)11