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