001/**
002 * Copyright 2005-2016 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.impl.common.delegate;
017
018import org.apache.commons.collections.CollectionUtils;
019import org.eclipse.persistence.annotations.JoinFetch;
020import org.eclipse.persistence.annotations.JoinFetchType;
021import org.kuali.rice.kim.api.common.delegate.DelegateMember;
022import org.kuali.rice.kim.api.common.delegate.DelegateMemberContract;
023import org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo;
024import org.kuali.rice.kim.impl.membership.AbstractMemberBo;
025import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
026import org.springframework.util.AutoPopulatingList;
027
028import javax.persistence.CascadeType;
029import javax.persistence.Column;
030import javax.persistence.Entity;
031import javax.persistence.GeneratedValue;
032import javax.persistence.Id;
033import javax.persistence.JoinColumn;
034import javax.persistence.OneToMany;
035import javax.persistence.Table;
036import javax.persistence.Transient;
037import java.sql.Timestamp;
038import java.util.HashMap;
039import java.util.List;
040import java.util.Map;
041
042@Entity
043@Table(name = "KRIM_DLGN_MBR_T")
044public class DelegateMemberBo extends AbstractMemberBo implements DelegateMemberContract {
045    private static final long serialVersionUID = 1L;
046
047    @PortableSequenceGenerator(name = "KRIM_DLGN_MBR_ID_S")
048    @GeneratedValue(generator = "KRIM_DLGN_MBR_ID_S")
049    @Id
050    @Column(name = "DLGN_MBR_ID")
051    private String delegationMemberId;
052
053    @Column(name = "DLGN_ID")
054    private String delegationId;
055
056    @Column(name = "ROLE_MBR_ID")
057    private String roleMemberId;
058
059    @JoinFetch(value= JoinFetchType.OUTER)
060    @OneToMany(targetEntity = DelegateMemberAttributeDataBo.class, orphanRemoval = true, cascade = CascadeType.ALL)
061    @JoinColumn(name = "DLGN_MBR_ID", referencedColumnName = "DLGN_MBR_ID")
062    private List<DelegateMemberAttributeDataBo> attributeDetails = new AutoPopulatingList<DelegateMemberAttributeDataBo>(DelegateMemberAttributeDataBo.class);
063
064    @Transient
065    private Map<String, String> attributes;
066
067    /**
068     * Returns Attributes derived from the internal List of DelegateMemberAttributeDataBos.  This field is
069     * not exposed in the DelegateMemberContract as it is not a required field in the DelegateMember DTO
070     *
071     * @return
072     */
073    public Map<String, String> getQualifier() {
074        Map<String, String> attribs = new HashMap<String, String>();
075        if (attributeDetails == null) {
076            return attribs;
077        }
078        for (DelegateMemberAttributeDataBo attr : attributeDetails) {
079            attribs.put(attr.getKimAttribute().getAttributeName(), attr.getAttributeValue());
080        }
081        return attribs;
082    }
083
084    public List<DelegateMemberAttributeDataBo> getAttributeDetails() {
085        if (this.attributeDetails == null) {
086            return new AutoPopulatingList<DelegateMemberAttributeDataBo>(DelegateMemberAttributeDataBo.class);
087        }
088        return this.attributeDetails;
089    }
090
091    public void setAttributeDetails(List<DelegateMemberAttributeDataBo> attributeDetails) {
092        this.attributeDetails = attributeDetails;
093    }
094
095    @Override
096    public Map<String, String> getAttributes() {
097        return CollectionUtils.isNotEmpty(attributeDetails) ? KimAttributeDataBo.toAttributes(attributeDetails) : attributes;
098    }
099
100    public static DelegateMember to(DelegateMemberBo bo) {
101        if (bo == null) {
102            return null;
103        }
104        return DelegateMember.Builder.create(bo).build();
105    }
106
107    public static DelegateMemberBo from(DelegateMember immutable) {
108        if (immutable == null) {
109            return null;
110        }
111        DelegateMemberBo bo = new DelegateMemberBo();
112        bo.setDelegationMemberId(immutable.getDelegationMemberId());
113        bo.setActiveFromDateValue(immutable.getActiveFromDate() == null ? null : new Timestamp(immutable.getActiveFromDate().getMillis()));
114        bo.setActiveToDateValue(immutable.getActiveToDate() == null ? null : new Timestamp(immutable.getActiveToDate().getMillis()));
115        bo.setDelegationId(immutable.getDelegationId());
116        bo.setMemberId(immutable.getMemberId());
117        bo.setRoleMemberId(immutable.getRoleMemberId());
118        bo.setTypeCode(immutable.getType().getCode());
119        bo.setVersionNumber(immutable.getVersionNumber());
120        bo.setAttributes(immutable.getAttributes());
121        return bo;
122    }
123
124    @Override
125    public String getDelegationMemberId() {
126        return delegationMemberId;
127    }
128
129    public void setDelegationMemberId(String delegationMemberId) {
130        this.delegationMemberId = delegationMemberId;
131    }
132
133    @Override
134    public String getDelegationId() {
135        return delegationId;
136    }
137
138    public void setDelegationId(String delegationId) {
139        this.delegationId = delegationId;
140    }
141
142    @Override
143    public String getRoleMemberId() {
144        return roleMemberId;
145    }
146
147    public void setRoleMemberId(String roleMemberId) {
148        this.roleMemberId = roleMemberId;
149    }
150
151    public void setAttributes(Map<String, String> attributes) {
152        this.attributes = attributes;
153    }
154}