Search in sources :

Example 1 with GroupAlreadyExistsException

use of com.thinkbiganalytics.metadata.api.user.GroupAlreadyExistsException in project kylo by Teradata.

the class JcrUserProvider method createGroup.

/**
 * Creates a new group with the specified name.
 *
 * @param groupName the name of the group
 * @param ensure    {@code true} to return the group if it already exists, or {@code false} to throw an exception
 * @return the group
 * @throws GroupAlreadyExistsException if the group already exists and {@code ensure} is {@code false}
 * @throws MetadataRepositoryException if the group could not be created
 */
@Nonnull
private UserGroup createGroup(@Nonnull final String groupName, final boolean ensure) {
    final Session session = getSession();
    final String safeGroupName = encodeGroupName(groupName);
    final String groupPath = UsersPaths.groupPath(safeGroupName).toString();
    try {
        final Node groupsNode = session.getRootNode().getNode(UsersPaths.GROUPS.toString());
        if (session.getRootNode().hasNode(groupPath)) {
            if (ensure) {
                return JcrUtil.getJcrObject(groupsNode, safeGroupName, JcrUserGroup.class);
            } else {
                throw new GroupAlreadyExistsException(groupName);
            }
        } else {
            return JcrUtil.getOrCreateNode(groupsNode, safeGroupName, JcrUserGroup.NODE_TYPE, JcrUserGroup.class);
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException("Failed attempting to create a new group with name: " + groupName, e);
    }
}
Also used : MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) GroupAlreadyExistsException(com.thinkbiganalytics.metadata.api.user.GroupAlreadyExistsException) Node(javax.jcr.Node) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session) Nonnull(javax.annotation.Nonnull)

Aggregations

GroupAlreadyExistsException (com.thinkbiganalytics.metadata.api.user.GroupAlreadyExistsException)1 MetadataRepositoryException (com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException)1 Nonnull (javax.annotation.Nonnull)1 Node (javax.jcr.Node)1 RepositoryException (javax.jcr.RepositoryException)1 Session (javax.jcr.Session)1