Java tutorial
/* * #%L * BroadleafCommerce Open Admin Platform * %% * Copyright (C) 2009 - 2013 Broadleaf Commerce * %% * 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. * #L% */ package org.broadleafcommerce.openadmin.server.security.domain; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.broadleafcommerce.common.admin.domain.AdminMainEntity; import org.broadleafcommerce.common.extensibility.jpa.copy.DirectCopyTransform; import org.broadleafcommerce.common.extensibility.jpa.copy.DirectCopyTransformMember; import org.broadleafcommerce.common.extensibility.jpa.copy.DirectCopyTransformTypes; import org.broadleafcommerce.common.presentation.AdminPresentation; import org.broadleafcommerce.common.presentation.AdminPresentationClass; import org.broadleafcommerce.common.presentation.AdminPresentationCollection; import org.broadleafcommerce.common.presentation.AdminPresentationOperationTypes; import org.broadleafcommerce.common.presentation.client.AddMethodType; import org.broadleafcommerce.common.presentation.client.OperationType; import org.broadleafcommerce.common.presentation.client.VisibilityEnum; import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.GenericGenerator; import org.hibernate.annotations.Parameter; import java.lang.reflect.Method; import java.util.HashSet; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; /** * * @author jfischer * */ @Entity @Inheritance(strategy = InheritanceType.JOINED) @Table(name = "BLC_ADMIN_ROLE") @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "blStandardElements") @AdminPresentationClass(friendlyName = "AdminRoleImpl_baseAdminRole") @DirectCopyTransform({ @DirectCopyTransformMember(templateTokens = DirectCopyTransformTypes.MULTITENANT_ADMINROLE) }) public class AdminRoleImpl implements AdminRole, AdminMainEntity { private static final Log LOG = LogFactory.getLog(AdminRoleImpl.class); private static final long serialVersionUID = 1L; @Id @GeneratedValue(generator = "AdminRoleId") @GenericGenerator(name = "AdminRoleId", strategy = "org.broadleafcommerce.common.persistence.IdOverrideTableGenerator", parameters = { @Parameter(name = "segment_value", value = "AdminRoleImpl"), @Parameter(name = "entity_name", value = "org.broadleafcommerce.openadmin.server.security.domain.AdminRoleImpl") }) @Column(name = "ADMIN_ROLE_ID") @AdminPresentation(friendlyName = "AdminRoleImpl_Admin_Role_ID", group = "AdminRoleImpl_Primary_Key", visibility = VisibilityEnum.HIDDEN_ALL) protected Long id; @Column(name = "NAME", nullable = false) @AdminPresentation(friendlyName = "AdminRoleImpl_Name", order = 1, group = "AdminRoleImpl_Role") protected String name; @Column(name = "DESCRIPTION", nullable = false) @AdminPresentation(friendlyName = "AdminRoleImpl_Description", order = 2, group = "AdminRoleImpl_Role", prominent = true) protected String description; /** All users that have this role */ @ManyToMany(fetch = FetchType.LAZY, targetEntity = AdminUserImpl.class) @JoinTable(name = "BLC_ADMIN_USER_ROLE_XREF", joinColumns = @JoinColumn(name = "ADMIN_ROLE_ID", referencedColumnName = "ADMIN_ROLE_ID"), inverseJoinColumns = @JoinColumn(name = "ADMIN_USER_ID", referencedColumnName = "ADMIN_USER_ID")) @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "blStandardElements") @BatchSize(size = 50) protected Set<AdminUser> allUsers = new HashSet<AdminUser>(); @ManyToMany(fetch = FetchType.LAZY, targetEntity = AdminPermissionImpl.class) @JoinTable(name = "BLC_ADMIN_ROLE_PERMISSION_XREF", joinColumns = @JoinColumn(name = "ADMIN_ROLE_ID", referencedColumnName = "ADMIN_ROLE_ID"), inverseJoinColumns = @JoinColumn(name = "ADMIN_PERMISSION_ID", referencedColumnName = "ADMIN_PERMISSION_ID")) @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "blStandardElements") @BatchSize(size = 50) @AdminPresentationCollection(addType = AddMethodType.LOOKUP, friendlyName = "permissionListTitle", manyToField = "allRoles", customCriteria = "includeFriendlyOnly", operationTypes = @AdminPresentationOperationTypes(removeType = OperationType.NONDESTRUCTIVEREMOVE)) protected Set<AdminPermission> allPermissions = new HashSet<AdminPermission>(); @Override public Set<AdminPermission> getAllPermissions() { return allPermissions; } @Override public Long getId() { return id; } @Override public void setId(Long id) { this.id = id; } @Override public String getName() { return name; } public Set<AdminUser> getAllUsers() { return allUsers; } @Override public void setName(String name) { this.name = name; } @Override public String getDescription() { return description; } @Override public void setDescription(String description) { this.description = description; } public void setAllPermissions(Set<AdminPermission> allPermissions) { this.allPermissions = allPermissions; } public void checkCloneable(AdminRole adminRole) throws CloneNotSupportedException, SecurityException, NoSuchMethodException { Method cloneMethod = adminRole.getClass().getMethod("clone", new Class[] {}); if (cloneMethod.getDeclaringClass().getName().startsWith("org.broadleafcommerce") && !adminRole.getClass().getName().startsWith("org.broadleafcommerce")) { //subclass is not implementing the clone method throw new CloneNotSupportedException("Custom extensions and implementations should implement clone."); } } @Override public AdminRole clone() { AdminRole clone; try { clone = (AdminRole) Class.forName(this.getClass().getName()).newInstance(); try { checkCloneable(clone); } catch (CloneNotSupportedException e) { LOG.warn("Clone implementation missing in inheritance hierarchy outside of Broadleaf: " + clone.getClass().getName(), e); } clone.setId(id); clone.setName(name); clone.setDescription(description); //don't clone the allUsers collection, as it would cause a recursion if (allPermissions != null) { for (AdminPermission permission : allPermissions) { AdminPermission permissionClone = permission.clone(); clone.getAllPermissions().add(permissionClone); } } } catch (Exception e) { throw new RuntimeException(e); } return clone; } @Override public String getMainEntityName() { return getName(); } }