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));
}
}
}
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);
}
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);
}
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!");
}
}
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;
}
Aggregations