001/**
002 * Copyright 2005-2015 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.krad.document.TransactionalDocumentBase;
025import org.kuali.rice.krad.service.KRADServiceLocator;
026import org.kuali.rice.krad.service.SequenceAccessorService;
027import org.springframework.util.AutoPopulatingList;
028
029import javax.persistence.AssociationOverride;
030import javax.persistence.AssociationOverrides;
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})
052@AssociationOverrides({
053        @AssociationOverride(name="documentHeader",joinColumns=@JoinColumn(name="FDOC_NBR",referencedColumnName="DOC_HDR_ID",insertable=false,updatable=false))
054})
055public class IdentityManagementKimDocument extends TransactionalDocumentBase {
056
057        protected static final Logger LOG = Logger.getLogger(IdentityManagementKimDocument.class);
058        
059        @OneToMany(targetEntity=RoleDocumentDelegation.class, fetch=FetchType.EAGER, cascade={CascadeType.ALL})
060    @JoinColumn(name="FDOC_NBR",insertable=false,updatable=false)
061        protected List<RoleDocumentDelegation> delegations = new AutoPopulatingList(RoleDocumentDelegation.class);
062        @Transient
063        protected List<RoleDocumentDelegationMember> delegationMembers = new AutoPopulatingList(RoleDocumentDelegationMember.class);
064        @Transient
065        protected transient SequenceAccessorService sequenceAccessorService;
066        
067        protected void addDelegationMemberToDelegation(RoleDocumentDelegationMember delegationMember){
068                RoleDocumentDelegation delegation;
069                if(DelegationType.PRIMARY.getCode().equals(delegationMember.getDelegationTypeCode())){
070                        delegation = getPrimaryDelegation();
071                } else{
072                        delegation = getSecondaryDelegation();
073                }
074                delegationMember.setDelegationId(delegation.getDelegationId());
075                delegation.getMembers().add(delegationMember);
076                delegation.setRoleId(delegationMember.getRoleBo().getId());
077                delegation.setKimTypeId(delegationMember.getRoleBo().getKimTypeId());
078        }
079
080        protected RoleDocumentDelegation getPrimaryDelegation(){
081                RoleDocumentDelegation primaryDelegation = null;
082                for(RoleDocumentDelegation delegation: getDelegations()){
083                        if(delegation.isDelegationPrimary()) {
084                                primaryDelegation = delegation;
085            }
086                }
087                if(primaryDelegation==null){
088                        primaryDelegation = new RoleDocumentDelegation();
089                        primaryDelegation.setDelegationId(getDelegationId());
090                        primaryDelegation.setDelegationTypeCode(DelegationType.PRIMARY.getCode());
091                        getDelegations().add(primaryDelegation);
092                }
093                return primaryDelegation;
094        }
095
096        protected String getDelegationId(){
097                return getSequenceAccessorService().getNextAvailableSequenceNumber(KimConstants.SequenceNames.KRIM_DLGN_ID_S, this.getClass()).toString();
098        }
099        
100        protected RoleDocumentDelegation getSecondaryDelegation(){
101                RoleDocumentDelegation secondaryDelegation = null;
102                for(RoleDocumentDelegation delegation: getDelegations()){
103                        if(delegation.isDelegationSecondary()) {
104                                secondaryDelegation = delegation;
105            }
106                }
107                if(secondaryDelegation==null){
108                        secondaryDelegation = new RoleDocumentDelegation();
109                        secondaryDelegation.setDelegationId(getDelegationId());
110                        secondaryDelegation.setDelegationTypeCode(DelegationType.SECONDARY.getCode());
111                        getDelegations().add(secondaryDelegation);
112                }
113                return secondaryDelegation;
114        }
115
116        /**
117         * @return the delegations
118         */
119        public List<RoleDocumentDelegation> getDelegations() {
120                return this.delegations;
121        }
122
123        /**
124         * @param delegations the delegations to set
125         */
126        public void setDelegations(List<RoleDocumentDelegation> delegations) {
127                this.delegations = delegations;
128        }
129
130        /**
131         * @return the delegationMembers
132         */
133        public List<RoleDocumentDelegationMember> getDelegationMembers() {
134                return this.delegationMembers;
135        }
136
137        /**
138         * @param delegationMembers the delegationMembers to set
139         */
140        public void setDelegationMembers(
141                        List<RoleDocumentDelegationMember> delegationMembers) {
142                this.delegationMembers = delegationMembers;
143        }
144        
145        protected SequenceAccessorService getSequenceAccessorService(){
146                if(this.sequenceAccessorService==null){
147                this.sequenceAccessorService = KRADServiceLocator.getSequenceAccessorService();
148                }
149                return this.sequenceAccessorService;
150        }
151
152    public String getKimAttributeDefnId(KimAttributeField definition){
153                return definition.getId();
154    }
155
156}