001/** 002 * Copyright 2005-2017 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.kim.document; 017 018import org.apache.log4j.Logger; 019import org.kuali.rice.core.api.delegation.DelegationType; 020import org.kuali.rice.kim.api.KimConstants; 021import org.kuali.rice.kim.api.type.KimAttributeField; 022import org.kuali.rice.kim.bo.ui.RoleDocumentDelegation; 023import org.kuali.rice.kim.bo.ui.RoleDocumentDelegationMember; 024import org.kuali.rice.kim.impl.role.RoleBo; 025import org.kuali.rice.kim.impl.services.KimImplServiceLocator; 026import org.kuali.rice.krad.data.platform.MaxValueIncrementerFactory; 027import org.kuali.rice.krad.document.TransactionalDocumentBase; 028import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer; 029import org.springframework.util.AutoPopulatingList; 030 031import javax.persistence.AttributeOverride; 032import javax.persistence.AttributeOverrides; 033import javax.persistence.CascadeType; 034import javax.persistence.Column; 035import javax.persistence.FetchType; 036import javax.persistence.JoinColumn; 037import javax.persistence.MappedSuperclass; 038import javax.persistence.OneToMany; 039import javax.persistence.Transient; 040import java.util.List; 041 042/** 043 * This is a description of what this class does - bhargavp don't forget to fill this in. 044 * 045 * @author Kuali Rice Team (rice.collab@kuali.org) 046 * 047 */ 048@MappedSuperclass 049@AttributeOverrides({ 050 @AttributeOverride(name="documentNumber",column=@Column(name="FDOC_NBR")) 051}) 052public class IdentityManagementKimDocument extends TransactionalDocumentBase { 053 private static final long serialVersionUID = 1L; 054 055 protected static final Logger LOG = Logger.getLogger(IdentityManagementKimDocument.class); 056 057 @OneToMany(targetEntity=RoleDocumentDelegation.class, fetch=FetchType.EAGER, cascade={CascadeType.ALL}) 058 @JoinColumn(name="FDOC_NBR",insertable=false,updatable=false) 059 protected List<RoleDocumentDelegation> delegations = new AutoPopulatingList<RoleDocumentDelegation>(RoleDocumentDelegation.class); 060 @Transient 061 protected List<RoleDocumentDelegationMember> delegationMembers = new AutoPopulatingList<RoleDocumentDelegationMember>(RoleDocumentDelegationMember.class); 062 063 protected void addDelegationMemberToDelegation(RoleDocumentDelegationMember delegationMember){ 064 // the statement below will lazily load the RoleBo onto our delegation member so that we have access to i 065 delegationMember.loadTransientRoleFields(); 066 // now that we've done that we can fetch the RoleBo 067 RoleBo role = delegationMember.getRoleBo(); 068 RoleDocumentDelegation delegation; 069 if (DelegationType.PRIMARY.getCode().equals(delegationMember.getDelegationTypeCode())) { 070 delegation = getPrimaryDelegation(role); 071 // if the delegation type was changed on the document, we need to make sure we remove the member from the secondary delegation 072 getSecondaryDelegation(role).getMembers().remove(delegationMember); 073 } else { 074 delegation = getSecondaryDelegation(role); 075 // if the delegation type was changed on the document, we need to make sure we remove the member from the primary delegation 076 getPrimaryDelegation(role).getMembers().remove(delegationMember); 077 } 078 delegationMember.setDelegationId(delegation.getDelegationId()); 079 if (!delegation.getMembers().contains(delegationMember)) { 080 delegation.getMembers().add(delegationMember); 081 } 082 } 083 084 protected RoleDocumentDelegation getPrimaryDelegation(RoleBo role) { 085 RoleDocumentDelegation primaryDelegation = null; 086 for(RoleDocumentDelegation delegation: getDelegations()){ 087 if(role.getId().equals(delegation.getRoleId()) && delegation.isDelegationPrimary()) { 088 primaryDelegation = delegation; 089 break; 090 } 091 } 092 if(primaryDelegation == null) { 093 primaryDelegation = new RoleDocumentDelegation(); 094 primaryDelegation.setRoleId(role.getId()); 095 primaryDelegation.setKimTypeId(role.getKimTypeId()); 096 primaryDelegation.setDelegationId(getDelegationId()); 097 primaryDelegation.setDelegationTypeCode(DelegationType.PRIMARY.getCode()); 098 primaryDelegation.setDocumentNumber(getDocumentNumber()); 099 getDelegations().add(primaryDelegation); 100 } 101 return primaryDelegation; 102 } 103 104 protected String getDelegationId(){ 105 DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_DLGN_ID_S); 106 return incrementer.nextStringValue(); 107 } 108 109 protected RoleDocumentDelegation getSecondaryDelegation(RoleBo role) { 110 RoleDocumentDelegation secondaryDelegation = null; 111 for (RoleDocumentDelegation delegation: getDelegations()) { 112 if (role.getId().equals(delegation.getRoleId()) && delegation.isDelegationSecondary()) { 113 secondaryDelegation = delegation; 114 break; 115 } 116 } 117 if(secondaryDelegation == null) { 118 secondaryDelegation = new RoleDocumentDelegation(); 119 secondaryDelegation.setRoleId(role.getId()); 120 secondaryDelegation.setKimTypeId(role.getKimTypeId()); 121 secondaryDelegation.setDelegationId(getDelegationId()); 122 secondaryDelegation.setDelegationTypeCode(DelegationType.SECONDARY.getCode()); 123 secondaryDelegation.setDocumentNumber(getDocumentNumber()); 124 getDelegations().add(secondaryDelegation); 125 } 126 return secondaryDelegation; 127 } 128 129 public List<RoleDocumentDelegation> getDelegations() { 130 return this.delegations; 131 } 132 133 public void setDelegations(List<RoleDocumentDelegation> delegations) { 134 this.delegations = delegations; 135 } 136 137 public List<RoleDocumentDelegationMember> getDelegationMembers() { 138 return this.delegationMembers; 139 } 140 141 public void setDelegationMembers( 142 List<RoleDocumentDelegationMember> delegationMembers) { 143 this.delegationMembers = delegationMembers; 144 } 145 146 public String getKimAttributeDefnId(KimAttributeField definition){ 147 return definition.getId(); 148 } 149 150}