View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.impl.common.delegate;
17  
18  import java.sql.Timestamp;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.persistence.CascadeType;
24  import javax.persistence.Column;
25  import javax.persistence.Entity;
26  import javax.persistence.GeneratedValue;
27  import javax.persistence.Id;
28  import javax.persistence.JoinColumn;
29  import javax.persistence.OneToMany;
30  import javax.persistence.Table;
31  import javax.persistence.Transient;
32  
33  import org.apache.commons.collections.CollectionUtils;
34  import org.eclipse.persistence.annotations.JoinFetch;
35  import org.eclipse.persistence.annotations.JoinFetchType;
36  import org.kuali.rice.kim.api.common.delegate.DelegateMember;
37  import org.kuali.rice.kim.api.common.delegate.DelegateMemberContract;
38  import org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo;
39  import org.kuali.rice.kim.impl.membership.AbstractMemberBo;
40  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
41  import org.springframework.util.AutoPopulatingList;
42  
43  @Entity
44  @Table(name = "KRIM_DLGN_MBR_T")
45  public class DelegateMemberBo extends AbstractMemberBo implements DelegateMemberContract {
46      private static final long serialVersionUID = 1L;
47  
48      @PortableSequenceGenerator(name = "KRIM_DLGN_MBR_ID_S")
49      @GeneratedValue(generator = "KRIM_DLGN_MBR_ID_S")
50      @Id
51      @Column(name = "DLGN_MBR_ID")
52      private String delegationMemberId;
53  
54      @Column(name = "DLGN_ID")
55      private String delegationId;
56  
57      @Column(name = "ROLE_MBR_ID")
58      private String roleMemberId;
59  
60      @JoinFetch(value= JoinFetchType.OUTER)
61      @OneToMany(targetEntity = DelegateMemberAttributeDataBo.class, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
62      @JoinColumn(name = "DLGN_MBR_ID", referencedColumnName = "DLGN_MBR_ID", insertable = false, updatable = false)
63      private List<DelegateMemberAttributeDataBo> attributeDetails = new AutoPopulatingList<DelegateMemberAttributeDataBo>(DelegateMemberAttributeDataBo.class);
64  
65      @Transient
66      private Map<String, String> attributes;
67  
68      /**
69       * Returns Attributes derived from the internal List of DelegateMemberAttributeDataBos.  This field is
70       * not exposed in the DelegateMemberContract as it is not a required field in the DelegateMember DTO
71       *
72       * @return
73       */
74      public Map<String, String> getQualifier() {
75          Map<String, String> attribs = new HashMap<String, String>();
76          if (attributeDetails == null) {
77              return attribs;
78          }
79          for (DelegateMemberAttributeDataBo attr : attributeDetails) {
80              attribs.put(attr.getKimAttribute().getAttributeName(), attr.getAttributeValue());
81          }
82          return attribs;
83      }
84  
85      public List<DelegateMemberAttributeDataBo> getAttributeDetails() {
86          if (this.attributeDetails == null) {
87              return new AutoPopulatingList<DelegateMemberAttributeDataBo>(DelegateMemberAttributeDataBo.class);
88          }
89          return this.attributeDetails;
90      }
91  
92      public void setAttributeDetails(List<DelegateMemberAttributeDataBo> attributeDetails) {
93          this.attributeDetails = attributeDetails;
94      }
95  
96      @Override
97      public Map<String, String> getAttributes() {
98          return CollectionUtils.isNotEmpty(attributeDetails) ? KimAttributeDataBo.toAttributes(attributeDetails) : attributes;
99      }
100 
101     public static DelegateMember to(DelegateMemberBo bo) {
102         if (bo == null) {
103             return null;
104         }
105         return DelegateMember.Builder.create(bo).build();
106     }
107 
108     public static DelegateMemberBo from(DelegateMember immutable) {
109         if (immutable == null) {
110             return null;
111         }
112         DelegateMemberBo bo = new DelegateMemberBo();
113         bo.setDelegationMemberId(immutable.getDelegationMemberId());
114         bo.setActiveFromDateValue(immutable.getActiveFromDate() == null ? null : new Timestamp(immutable.getActiveFromDate().getMillis()));
115         bo.setActiveToDateValue(immutable.getActiveToDate() == null ? null : new Timestamp(immutable.getActiveToDate().getMillis()));
116         bo.setDelegationId(immutable.getDelegationId());
117         bo.setMemberId(immutable.getMemberId());
118         bo.setRoleMemberId(immutable.getRoleMemberId());
119         bo.setTypeCode(immutable.getType().getCode());
120         bo.setVersionNumber(immutable.getVersionNumber());
121         bo.setAttributes(immutable.getAttributes());
122         return bo;
123     }
124 
125     @Override
126     public String getDelegationMemberId() {
127         return delegationMemberId;
128     }
129 
130     public void setDelegationMemberId(String delegationMemberId) {
131         this.delegationMemberId = delegationMemberId;
132     }
133 
134     @Override
135     public String getDelegationId() {
136         return delegationId;
137     }
138 
139     public void setDelegationId(String delegationId) {
140         this.delegationId = delegationId;
141     }
142 
143     @Override
144     public String getRoleMemberId() {
145         return roleMemberId;
146     }
147 
148     public void setRoleMemberId(String roleMemberId) {
149         this.roleMemberId = roleMemberId;
150     }
151 
152     public void setAttributes(Map<String, String> attributes) {
153         this.attributes = attributes;
154     }
155 }