Java tutorial
/* * 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.identity; import com.google.common.collect.Lists; import de.kaiserpfalzEdv.commons.NameableReadable; import de.kaiserpfalzEdv.iam.core.base.IAMBaseObject; import de.kaiserpfalzEdv.iam.core.role.RoleBuilder; import de.kaiserpfalzEdv.iam.core.role.RoleDO; import de.kaiserpfalzEdv.iam.identity.Identity; import de.kaiserpfalzEdv.iam.identity.Subject; import de.kaiserpfalzEdv.iam.identity.SubjectWritable; import de.kaiserpfalzEdv.iam.role.Role; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.UUID; import static com.google.common.base.Preconditions.checkArgument; /** * @author klenkes <rlichti@kaiserpfalz-edv.de> * @since 2014Q3 */ public class SubjectDO extends IAMBaseObject implements Subject, SubjectWritable { private static final long serialVersionUID = -6584467331451446363L; private static final Logger LOG = LoggerFactory.getLogger(SubjectDO.class); private IdentityDO identity; private ArrayList<RoleDO> roles = new ArrayList<>(); @SuppressWarnings({ "UnusedDeclaration", "deprecation" }) @Deprecated // Only for JPA, JAXB, builders and so on ... protected SubjectDO() { } protected SubjectDO(final UUID tenant, final UUID id, final NameableReadable name) { super(tenant, id, name); } @Override public Identity getIdentity() { return identity; } @Override public void setIdentity(Identity identity) { checkArgument(identity != null, "Can't set identity to NULL. You have to use unsetIdentity() to do this"); unsetIdentity(); this.identity = convertToIdentityDO(identity); this.identity.addSubject(this); LOG.info("Set identity for subject '{}'.", this); } public void unsetIdentity() { if (identity != null) { IdentityDO i = identity; identity = null; i.removeSubject(this); LOG.info("Removed identity from subject '{}'.", this); } } private IdentityDO convertToIdentityDO(Identity identity) { try { return (IdentityDO) identity; } catch (ClassCastException e) { return new IdentityBuilder().withIdentity(identity).build(); } } @Override public List<RoleDO> getRoles() { ArrayList<RoleDO> result = new ArrayList<>(); for (RoleDO r : roles) { result.addAll(convertToRoleDO(r.getRoles())); } return result; } @Override public List<RoleDO> getDirectAssignedRoles() { return Collections.unmodifiableList(roles); } @Override public void addRole(Role role) { if (role == null) return; RoleDO r = convertToRoleDO(role); if (!roles.contains(r)) { roles.add(r); LOG.info("Role '{}' added to subject '{}'.", role, this); } } @Override public void removeRole(Role role) { if (role == null) return; RoleDO r = convertToRoleDO(role); if (roles.contains(r)) { roles.remove(r); LOG.info("Role '{}' removed from subject '{}'.", role, this); } } private <R extends Role> RoleDO convertToRoleDO(R role) { try { return (RoleDO) role; } catch (ClassCastException e) { return new RoleBuilder().withRole(role).build(); } } private List<RoleDO> convertToRoleDO(List<? extends Role> elements) { ArrayList<RoleDO> result = new ArrayList<>(elements.size()); for (Role r : Lists.newArrayList(elements)) { result.add(convertToRoleDO(r)); } return result; } @Override public String toString() { ToStringBuilder result = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("id", id); if (identity != null) result.append("identity", identity); return result.append("name", getNameable()).build(); } }