de.kaiserpfalzEdv.iam.core.role.RoleBuilder.java Source code

Java tutorial

Introduction

Here is the source code for de.kaiserpfalzEdv.iam.core.role.RoleBuilder.java

Source

/*
 * Copyright (c) 2014 Kaiserpfalz EDV-Service, Roland Lichti
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.kaiserpfalzEdv.iam.core.role;

import de.kaiserpfalzEdv.commons.NameableReadable;
import de.kaiserpfalzEdv.iam.core.base.IAMBaseObjectBuilder;
import de.kaiserpfalzEdv.iam.role.Permission;
import de.kaiserpfalzEdv.iam.role.Role;
import de.kaiserpfalzEdv.iam.role.RoleWritable;
import org.apache.commons.lang3.builder.Builder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;

/**
 * @author klenkes
 * @since 2014Q
 */
public class RoleBuilder extends IAMBaseObjectBuilder<RoleBuilder>
        implements Builder<RoleWritable>, IAMBaseObjectBuilder.ConcreteObjectGenerator<RoleWritable> {
    private final HashSet<Permission> extendingPermissions = new HashSet<>();
    private final HashSet<Permission> restrictingPermissions = new HashSet<>();
    private final List<Role> children = new ArrayList<>();

    public RoleBuilder() {
        withGenerator(this);
    }

    @Override
    public RoleDO build() {
        RoleDO result;

        try {
            result = generate();
        } catch (IllegalArgumentException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }

        for (Permission p : extendingPermissions)
            result.extendPermission(p);
        for (Permission p : restrictingPermissions)
            result.restrictPermission(p);
        for (Role r : children)
            result.addRole(r);

        result.validate();

        return result;
    }

    @Override
    public RoleWritable generate(UUID tenant, UUID id, NameableReadable name) {
        return new RoleDO(tenant, id, name);
    }

    public RoleBuilder withRole(Role role) {
        withTenant(role.getTenantId());
        withId(role.getId());
        withName(role.getNameable());

        withChildren(role.getChildren());
        withExtendingPermissions(role.getExtendingPermissions());
        withRestrictingPermissions(role.getRestrictingPermissions());

        return this;
    }

    public RoleBuilder withChildren(List<? extends Role> roles) {
        children.clear();

        if (roles != null)
            children.addAll(roles);

        return this;
    }

    public RoleBuilder withExtendingPermissions(Collection<? extends Permission> permissions) {
        this.extendingPermissions.clear();

        if (permissions != null)
            this.extendingPermissions.addAll(permissions);

        return this;
    }

    public RoleBuilder withRestrictingPermissions(Collection<? extends Permission> permissions) {
        this.restrictingPermissions.clear();

        if (permissions != null)
            this.restrictingPermissions.addAll(permissions);

        return this;
    }
}