Search in sources :

Example 51 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.

the class MCRCategoryImpl method calculateLeftRightAndLevel.

/**
 * calculates left and right value throug the subtree rooted at
 * <code>co</code>.
 *
 * @param leftStart
 *            this.left will be set to this value
 * @param levelStart
 *            this.getLevel() will return this value
 * @return this.right
 */
public int calculateLeftRightAndLevel(int leftStart, int levelStart) {
    int curValue = leftStart;
    final int nextLevel = levelStart + 1;
    setLeft(leftStart);
    setLevel(levelStart);
    for (MCRCategory child : getChildren()) {
        LOGGER.debug(child.getId());
        curValue = ((MCRCategoryImpl) child).calculateLeftRightAndLevel(++curValue, nextLevel);
    }
    setRight(++curValue);
    return curValue;
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) UniqueConstraint(javax.persistence.UniqueConstraint)

Example 52 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.

the class MCRCategoryImpl method getPositionInParentByID.

@Transient
private int getPositionInParentByID() {
    int position = 0;
    for (MCRCategory sibling : parent.getChildren()) {
        if (getId().equals(sibling.getId())) {
            return position;
        }
        position++;
    }
    if (LOGGER.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder("List of children of parent: ");
        sb.append(parent.getId()).append('\n');
        for (MCRCategory sibling : parent.getChildren()) {
            sb.append(sibling.getId()).append('\n');
        }
        LOGGER.debug(sb.toString());
    }
    throw new IndexOutOfBoundsException("Position -1 is not valid: " + getId() + " parent:" + parent.getId() + " children: " + parent.getChildren().stream().map(MCRCategory::getId).map(MCRCategoryID::getID).collect(Collectors.joining(", ")));
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) UniqueConstraint(javax.persistence.UniqueConstraint) Transient(javax.persistence.Transient)

Example 53 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.

the class MCRClassificationUtils method fromStream.

/**
 * Imports a classification from the given input stream. If the classification
 * already exists, it will be replaced.
 *
 * @param inputStream the classification stream
 * @throws MCRException xml parsing went wrong
 * @throws SAXParseException xml parsing went wrong
 * @throws URISyntaxException unable to transform the xml to a {@link MCRCategory}
 * @throws MCRAccessException you are not allowed to import the classification
 */
public static void fromStream(InputStream inputStream) throws MCRException, SAXParseException, URISyntaxException, MCRAccessException {
    MCRCategoryDAO DAO = MCRCategoryDAOFactory.getInstance();
    Document jdom = MCRXMLParserFactory.getParser().parseXML(new MCRStreamContent(inputStream));
    MCRCategory classification = MCRXMLTransformer.getCategory(jdom);
    if (DAO.exist(classification.getId())) {
        if (!MCRAccessManager.checkPermission(classification.getId().getRootID(), PERMISSION_WRITE)) {
            throw MCRAccessException.missingPermission("update classification " + classification.getId().getRootID(), classification.getId().getRootID(), PERMISSION_WRITE);
        }
        DAO.replaceCategory(classification);
    } else {
        if (!MCRAccessManager.checkPermission(CREATE_CLASS_PERMISSION)) {
            throw MCRAccessException.missingPermission("create classification " + classification.getId().getRootID(), classification.getId().getRootID(), CREATE_CLASS_PERMISSION);
        }
        DAO.addCategory(null, classification);
    }
}
Also used : MCRCategoryDAO(org.mycore.datamodel.classifications2.MCRCategoryDAO) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) Document(org.jdom2.Document) MCRStreamContent(org.mycore.common.content.MCRStreamContent)

Example 54 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.

the class MCRRoleManager method loadSystemRoles.

private static void loadSystemRoles() {
    if (lastLoaded == DAO.getLastModified() || !DAO.exist(MCRUser2Constants.ROLE_CLASSID)) {
        return;
    }
    lastLoaded = DAO.getLastModified();
    int onlyNonHierarchicalRoles = 1;
    MCRCategory roleCategory = DAO.getCategory(MCRUser2Constants.ROLE_CLASSID, onlyNonHierarchicalRoles);
    rolesByName.clear();
    rolesList.clear();
    if (roleCategory != null) {
        for (MCRCategory child : roleCategory.getChildren()) {
            String name = child.getId().getID();
            MCRRole role = new MCRRole(name, child.getLabels());
            rolesByName.put(name, role);
            rolesList.add(role);
        }
    }
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory)

Example 55 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.

the class MCRRoleManager method addRole.

/**
 * Adds <code>role</code> to the classification system.
 * If the representing {@link MCRCategory} already exists this method does nothing.
 * It will create any category if necessary.
 */
public static void addRole(MCRRole role) {
    MCRCategoryID categoryID = null;
    if (role.isSystemRole()) {
        categoryID = new MCRCategoryID(MCRUser2Constants.ROLE_CLASSID.getRootID(), role.getName());
    } else {
        categoryID = MCRCategoryID.fromString(role.getName());
    }
    if (DAO.exist(categoryID)) {
        return;
    }
    MCRCategoryID rootID = MCRCategoryID.rootID(categoryID.getRootID());
    if (!DAO.exist(rootID)) {
        MCRCategoryImpl category = new MCRCategoryImpl();
        category.setId(rootID);
        HashSet<MCRLabel> labels = new HashSet<>();
        labels.add(new MCRLabel("de", "Systemrollen", null));
        labels.add(new MCRLabel("en", "system roles", null));
        category.setLabels(labels);
        DAO.addCategory(null, category);
    }
    MCRCategoryImpl category = new MCRCategoryImpl();
    category.setId(categoryID);
    category.getLabels().addAll(role.getLabels());
    DAO.addCategory(rootID, category);
}
Also used : MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRCategoryImpl(org.mycore.datamodel.classifications2.impl.MCRCategoryImpl) MCRLabel(org.mycore.datamodel.classifications2.MCRLabel) HashSet(java.util.HashSet)

Aggregations

MCRCategory (org.mycore.datamodel.classifications2.MCRCategory)87 MCRCategoryID (org.mycore.datamodel.classifications2.MCRCategoryID)36 Test (org.junit.Test)24 MCRLabel (org.mycore.datamodel.classifications2.MCRLabel)17 MCRCategoryDAO (org.mycore.datamodel.classifications2.MCRCategoryDAO)10 ArrayList (java.util.ArrayList)9 Document (org.jdom2.Document)9 Element (org.jdom2.Element)8 MCRException (org.mycore.common.MCRException)8 IOException (java.io.IOException)6 EntityManager (javax.persistence.EntityManager)6 URI (java.net.URI)5 Collection (java.util.Collection)5 HashMap (java.util.HashMap)5 LogManager (org.apache.logging.log4j.LogManager)5 Logger (org.apache.logging.log4j.Logger)5 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)5 HashSet (java.util.HashSet)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4