Search in sources :

Example 26 with MCRCommand

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

the class MCRAccessCommands method exportAllPermissionsToFile.

/**
 * This method just export the permissions to a file
 *
 * @param filename
 *            the file written to
 */
@MCRCommand(syntax = "export all permissions to file {0}", help = "Export all permissions from the Access Control System to the file {0}.", order = 50)
public static void exportAllPermissionsToFile(String filename) throws Exception {
    MCRAccessInterface AI = MCRAccessManager.getAccessImpl();
    Element mcrpermissions = new Element("mcrpermissions");
    mcrpermissions.addNamespaceDeclaration(XSI_NAMESPACE);
    mcrpermissions.addNamespaceDeclaration(XLINK_NAMESPACE);
    mcrpermissions.setAttribute("noNamespaceSchemaLocation", "MCRPermissions.xsd", XSI_NAMESPACE);
    Document doc = new Document(mcrpermissions);
    Collection<String> permissions = AI.getPermissions();
    for (String permission : permissions) {
        Element mcrpermission = new Element("mcrpermission");
        mcrpermission.setAttribute("name", permission);
        String ruleDescription = AI.getRuleDescription(permission);
        if (!ruleDescription.equals("")) {
            mcrpermission.setAttribute("ruledescription", ruleDescription);
        }
        Element rule = AI.getRule(permission);
        mcrpermission.addContent(rule);
        mcrpermissions.addContent(mcrpermission);
    }
    File file = new File(filename);
    if (file.exists()) {
        LOGGER.warn("File {} yet exists, overwrite.", filename);
    }
    FileOutputStream fos = new FileOutputStream(file);
    LOGGER.info("Writing to file {} ...", filename);
    String mcr_encoding = CONFIG.getString("MCR.Metadata.DefaultEncoding", DEFAULT_ENCODING);
    XMLOutputter out = new XMLOutputter(Format.getPrettyFormat().setEncoding(mcr_encoding));
    out.output(doc, fos);
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) MCRAccessInterface(org.mycore.access.MCRAccessInterface) Element(org.jdom2.Element) FileOutputStream(java.io.FileOutputStream) Document(org.jdom2.Document) File(java.io.File) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 27 with MCRCommand

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

the class MCRAccessCommands method permissionUpdateForSelected.

/**
 * updates the permissions for all ids of a given MCRObjectID-Type and for a
 * given permission type with a given rule
 *
 * @param permission
 *            String type of permission like read, writedb, etc.
 * @param strFileRule
 *            String the path to the xml file, that contains the rule
 * @param description
 *            String give a special description, if the semantics of your
 *            rule is multiple used
 */
@MCRCommand(syntax = "update permission {0} for selected with rulefile {1} described by {2}", help = "The command updates access rule for a given permission and all ids of a given MCRObject-Type with a given special rule", order = 80)
public static void permissionUpdateForSelected(String permission, String strFileRule, String description) throws Exception {
    MCRAccessInterface AI = MCRAccessManager.getAccessImpl();
    Element rule = getRuleFromFile(strFileRule);
    if (rule == null) {
        return;
    }
    for (String id : MCRObjectCommands.getSelectedObjectIDs()) {
        AI.addRule(id, permission, rule, description);
    }
}
Also used : MCRAccessInterface(org.mycore.access.MCRAccessInterface) Element(org.jdom2.Element) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 28 with MCRCommand

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

the class MCRAccessCommands method permissionDeleteForID.

/**
 * delete a given permission for a given id
 *
 * @param permission
 *            String type of permission like read, writedb, etc.
 * @param id
 *            String the id of the object the rule is assigned to
 */
@MCRCommand(syntax = "delete permission {0} for id {1}", help = "The command delete access rule for a given id of a given permission", order = 110)
public static void permissionDeleteForID(String permission, String id) {
    MCRAccessInterface AI = MCRAccessManager.getAccessImpl();
    AI.removeRule(id, permission);
}
Also used : MCRAccessInterface(org.mycore.access.MCRAccessInterface) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 29 with MCRCommand

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

the class MCRIFS2Commands method repairUnicodeInContentStores.

@MCRCommand(syntax = "repair unicode in content stores {0}", help = "this fixes consequences of MCR-1423 in content" + " stores . If {0} is false then nothing will be done (dry run).")
public static void repairUnicodeInContentStores(String execute) {
    boolean dry = execute.toLowerCase(Locale.ROOT).equals(Boolean.FALSE.toString().toLowerCase(Locale.ROOT));
    MCRContentStoreFactory.getAvailableStores().forEach((name, cs) -> {
        LOGGER.info("{} store: {} ", dry ? "would fix" : "fixing", name);
        try {
            Path path = cs.getBaseDir().toPath();
            LOGGER.info("Starting with path : {}", path);
            java.nio.file.Files.walkFileTree(path, new MCRUnicodeFilenameNormalizer(dry));
        } catch (IOException e) {
            throw new MCRException("Error while get basedir of content store " + name, e);
        }
    });
}
Also used : Path(java.nio.file.Path) MCRException(org.mycore.common.MCRException) IOException(java.io.IOException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 30 with MCRCommand

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

the class MCRIFS2Commands method repairUnicodeInDatabase.

@MCRCommand(syntax = "repair unicode in database {0}", help = "this fixes consequences of MCR-1423 in Database. If " + "{0} is false then nothing will be done (dry run).")
public static void repairUnicodeInDatabase(String execute) {
    boolean dry = execute.toLowerCase(Locale.ROOT).equals(Boolean.FALSE.toString().toLowerCase(Locale.ROOT));
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<MCRFSNODES> getQuery = cb.createQuery(MCRFSNODES.class);
    List<MCRFSNODES> resultList = em.createQuery(getQuery.select(getQuery.from(MCRFSNODES.class))).getResultList();
    resultList.stream().forEach(node -> {
        String unnormalName = node.getName();
        String normalName = MCRXMLFunctions.normalizeUnicode(unnormalName);
        if (!unnormalName.equals(normalName)) {
            LOGGER.info("{} node {} with name {}", (dry) ? "Would Fix" : "Fixing", node.getId(), unnormalName);
            if (!dry) {
                node.setName(normalName);
            }
        }
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES) 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