Search in sources :

Example 51 with MCRCommand

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

the class MCRMetsCommands method fixInvalidMets.

@MCRCommand(syntax = "try fix invalid mets", help = "This Command can be used to fix invalid mets files that was found in any validate selected mets runs.", order = 15)
public static void fixInvalidMets() {
    String selectedObjectID;
    while ((selectedObjectID = invalidMetsQueue.poll()) != null) {
        LOGGER.info("Try to fix METS of {}", selectedObjectID);
        MCRPath metsFile = MCRPath.getPath(selectedObjectID, "/mets.xml");
        SAXBuilder saxBuilder = new SAXBuilder();
        Document metsDocument;
        try (InputStream metsInputStream = Files.newInputStream(metsFile)) {
            metsDocument = saxBuilder.build(metsInputStream);
        } catch (IOException | JDOMException e) {
            LOGGER.error(MessageFormat.format("Cannot fix METS of {0}. Can not parse mets.xml!", selectedObjectID), e);
            return;
        }
        MCRMetsSimpleModel mcrMetsSimpleModel;
        try {
            mcrMetsSimpleModel = MCRXMLSimpleModelConverter.fromXML(metsDocument);
        } catch (Exception e) {
            LOGGER.error(MessageFormat.format("Cannot fix METS of {0}. Can not convert to SimpleModel!", selectedObjectID), e);
            return;
        }
        Document newMets = MCRSimpleModelXMLConverter.toXML(mcrMetsSimpleModel);
        XMLOutputter xmlOutputter = new XMLOutputter();
        try (OutputStream os = Files.newOutputStream(metsFile)) {
            xmlOutputter.output(newMets, os);
        } catch (IOException e) {
            LOGGER.error(MessageFormat.format("Cannot fix METS of {0}. Can not write mets to derivate.", selectedObjectID));
        }
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) SAXBuilder(org.jdom2.input.SAXBuilder) InputStream(java.io.InputStream) MCRMetsSimpleModel(org.mycore.mets.model.simple.MCRMetsSimpleModel) OutputStream(java.io.OutputStream) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) ValidationException(org.mycore.mets.validator.validators.ValidationException) JDOMException(org.jdom2.JDOMException) IOException(java.io.IOException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 52 with MCRCommand

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

the class MCRUserCommands method exportUserToFile.

/**
 * This command takes a userID and file name as a parameter, retrieves the user from MCRUserMgr as JDOM document and
 * export this to the given file.
 *
 * @param userID
 *            ID of the user to be saved
 * @param filename
 *            Name of the file to store the exported user
 */
@MCRCommand(syntax = "export user {0} to file {1}", help = "Exports the data of user {0} to the file {1}.", order = 180)
public static void exportUserToFile(String userID, String filename) throws IOException {
    MCRUser user = MCRUserManager.getUser(userID);
    if (user.getSystemRoleIDs().isEmpty()) {
        LOGGER.warn("User {} has not any system roles.", user.getUserID());
    }
    FileOutputStream outFile = new FileOutputStream(filename);
    LOGGER.info("Writing to file {} ...", filename);
    saveToXMLFile(user, outFile);
}
Also used : FileOutputStream(java.io.FileOutputStream) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 53 with MCRCommand

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

the class MCRUserCommands method initSuperuser.

/**
 * This method initializes the user and role system an creates a superuser with values set in
 * mycore.properties.private As 'super' default, if no properties were set, mcradmin with password mycore will be
 * used.
 */
@MCRCommand(syntax = "init superuser", help = "Initializes the user system. This command runs only if the user database does not exist.", order = 30)
public static List<String> initSuperuser() {
    final String suser = CONFIG.getString("MCR.Users.Superuser.UserName");
    final String spasswd = CONFIG.getString("MCR.Users.Superuser.UserPasswd");
    final String srole = CONFIG.getString("MCR.Users.Superuser.GroupName");
    if (MCRUserManager.exists(suser)) {
        LOGGER.error("The superuser already exists!");
        return null;
    }
    // the superuser role
    try {
        Set<MCRLabel> labels = new HashSet<>();
        labels.add(new MCRLabel("en", "The superuser role", null));
        MCRRole mcrRole = new MCRRole(srole, labels);
        MCRRoleManager.addRole(mcrRole);
    } catch (Exception e) {
        throw new MCRException("Can't create the superuser role.", e);
    }
    LOGGER.info("The role {} is installed.", srole);
    // the superuser
    try {
        MCRUser mcrUser = new MCRUser(suser);
        mcrUser.setRealName("Superuser");
        mcrUser.assignRole(srole);
        MCRUserManager.updatePasswordHashToSHA256(mcrUser, spasswd);
        MCRUserManager.createUser(mcrUser);
    } catch (Exception e) {
        throw new MCRException("Can't create the superuser.", e);
    }
    LOGGER.info(MessageFormat.format("The user {0} with password {1} is installed.", suser, spasswd));
    return Collections.singletonList("change to user " + suser + " with " + spasswd);
}
Also used : MCRException(org.mycore.common.MCRException) MCRLabel(org.mycore.datamodel.classifications2.MCRLabel) MCRException(org.mycore.common.MCRException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXParseException(org.xml.sax.SAXParseException) HashSet(java.util.HashSet) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 54 with MCRCommand

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

the class MCRUserCommands method changeToUser.

/**
 * This command changes the user of the session context to a new user.
 *
 * @param user
 *            the new user ID
 * @param password
 *            the password of the new user
 */
@MCRCommand(syntax = "change to user {0} with {1}", help = "Changes to the user {0} with the given password {1}.", order = 10)
public static void changeToUser(String user, String password) {
    MCRSession session = MCRSessionMgr.getCurrentSession();
    System.out.println(SYSTEM + " The old user ID is " + session.getUserInformation().getUserID());
    if (MCRUserManager.login(user, password) != null) {
        System.out.println(SYSTEM + " The new user ID is " + session.getUserInformation().getUserID());
    } else {
        LOGGER.warn("Wrong password, no changes of user ID in session context!");
    }
}
Also used : MCRSession(org.mycore.common.MCRSession) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 55 with MCRCommand

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

the class MCRUserCommands method exportAllUserToDirectory.

@MCRCommand(syntax = "export all users to directory {0}", help = "Exports the data of all users to the directory {0}.")
public static List<String> exportAllUserToDirectory(String directory) throws IOException {
    File dir = new File(directory);
    if (!dir.exists() || !dir.isDirectory()) {
        throw new MCRException("Directory does not exist: " + dir.getAbsolutePath());
    }
    List<MCRUser> users = MCRUserManager.listUsers(null, null, null);
    ArrayList<String> commands = new ArrayList<>(users.size());
    for (MCRUser user : users) {
        File userFile = new File(dir, user.getUserID() + ".xml");
        commands.add("export user " + user.getUserID() + " to file " + userFile.getAbsolutePath());
    }
    return commands;
}
Also used : MCRException(org.mycore.common.MCRException) ArrayList(java.util.ArrayList) File(java.io.File) 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