View Javadoc
1   /**
2    * Copyright 2005-2016 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.group;
17  
18  import org.joda.time.DateTime;
19  import org.kuali.rice.kim.api.KimConstants;
20  import org.kuali.rice.kim.api.group.Group;
21  import org.kuali.rice.kim.api.identity.Person;
22  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
23  import org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo;
24  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
25  
26  import javax.persistence.Cacheable;
27  import javax.persistence.CascadeType;
28  import javax.persistence.Column;
29  import javax.persistence.Entity;
30  import javax.persistence.GeneratedValue;
31  import javax.persistence.Id;
32  import javax.persistence.JoinColumn;
33  import javax.persistence.OneToMany;
34  import javax.persistence.Table;
35  import javax.persistence.Transient;
36  import java.util.ArrayList;
37  import java.util.List;
38  import java.util.Map;
39  
40  @Cacheable(false)
41  @Entity
42  @Table(name = "KRIM_GRP_T")
43  public class GroupBo extends GroupBase {
44  
45      private static final long serialVersionUID = 1L;
46  
47      @PortableSequenceGenerator(name = "KRIM_GRP_ID_S")
48      @GeneratedValue(generator = "KRIM_GRP_ID_S")
49      @Id
50      @Column(name = "GRP_ID")
51      private String id;
52  
53      @OneToMany(targetEntity = GroupMemberBo.class, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
54      @JoinColumn(name = "GRP_ID", referencedColumnName = "GRP_ID", insertable = false, updatable = false)
55      private List<GroupMemberBo> members;
56  
57      @OneToMany(targetEntity = GroupAttributeBo.class, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
58      @JoinColumn(name = "GRP_ID", referencedColumnName = "GRP_ID" )
59      private List<GroupAttributeBo> attributeDetails;
60  
61      @Transient
62      private List<Person> memberPersons;
63  
64      @Transient
65      private List<Group> memberGroups;
66  
67      @Override
68      public Map<String, String> getAttributes() {
69          return attributeDetails != null ? KimAttributeDataBo.toAttributes(attributeDetails) : attributes;
70      }
71  
72      @Override
73      public String getId() {
74          return id;
75      }
76  
77      public void setId(String id) {
78          this.id = id;
79      }
80  
81      public List<GroupMemberBo> getMembers() {
82          return members;
83      }
84  
85      public void setMembers(List<GroupMemberBo> members) {
86          this.members = members;
87      }
88  
89      public List<GroupAttributeBo> getAttributeDetails() {
90          return attributeDetails;
91      }
92  
93      public void setAttributeDetails(List<GroupAttributeBo> attributeDetails) {
94          this.attributeDetails = attributeDetails;
95      }
96  
97      /**
98       * Converts a mutable bo to its immutable counterpart
99       * @param bo the mutable business object
100      * @return the immutable object
101      */
102     public static Group to(GroupBo bo) {
103         if (bo == null) {
104             return null;
105         }
106         return Group.Builder.create(bo).build();
107     }
108 
109     /**
110      * Converts a immutable object to its mutable counterpart
111      * @param im immutable object
112      * @return the mutable bo
113      */
114     public static GroupBo from(Group im) {
115         if (im == null) {
116             return null;
117         }
118         GroupBo bo = new GroupBo();
119         bo.setId(im.getId());
120         bo.setNamespaceCode(im.getNamespaceCode());
121         bo.setName(im.getName());
122         bo.setDescription(im.getDescription());
123         bo.setActive(im.isActive());
124         bo.setKimTypeId(im.getKimTypeId());
125         bo.setAttributes(im.getAttributes());
126         bo.setVersionNumber(im.getVersionNumber());
127         bo.setObjectId(im.getObjectId());
128         return bo;
129     }
130 
131     //helper function to get Attribute Value with specific id                      
132     public String getGroupAttributeValueById(String attributeId) {
133         for (GroupAttributeBo gad : getAttributeDetails()) {
134             if (gad.getKimAttributeId().equals(attributeId.trim())) {
135                 return gad.getAttributeValue();
136             }
137         }
138         return null;
139     }
140 
141     private void splitMembersToTypes() {
142         memberPersons = new ArrayList<Person>();
143         memberGroups = new ArrayList<Group>();
144         if (getMembers() != null) {
145             for (GroupMemberBo groupMember : getMembers()) {
146                 if (groupMember.isActive(new DateTime())) {
147                     if (KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE.equals(groupMember.getType())) {
148                         Person tempPerson = KimApiServiceLocator.getPersonService().getPerson(groupMember.getMemberId());
149                         if (tempPerson != null && tempPerson.isActive()) {
150                             memberPersons.add(tempPerson);
151                         }
152                     } else if (KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE.equals(groupMember.getType())) {
153                         Group tempGroup = KimApiServiceLocator.getGroupService().getGroup(groupMember.getMemberId());
154                         if (tempGroup != null && tempGroup.isActive()) {
155                             memberGroups.add(tempGroup);
156                         }
157                     }
158                 }
159             }
160         }
161     }
162 
163     public List<Person> getMemberPersons() {
164         if (this.memberPersons == null) {
165             splitMembersToTypes();
166         }
167         return this.memberPersons;
168     }
169 
170     public void setMemberPersons(List<Person> memberPersons) {
171         this.memberPersons = memberPersons;
172     }
173 
174     public List<String> getMemberPrincipalIds() {
175         List<String> principalIds = new ArrayList<String>();
176         if (getMembers() != null) {
177             for (GroupMemberBo groupMember : getMembers()) {
178                 if (groupMember.isActive(new DateTime())) {
179                     if (KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE.equals(groupMember.getType())) {
180                         principalIds.add(groupMember.getMemberId());
181                     }
182                 }
183             }
184         }
185         return principalIds;
186     }
187 
188     public List<String> getMemberGroupIds() {
189         List<String> principalIds = new ArrayList<String>();
190         if (getMembers() != null) {
191             for (GroupMemberBo groupMember : getMembers()) {
192                 if (groupMember.isActive(new DateTime())) {
193                     if (KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE.equals(groupMember.getType())) {
194                         principalIds.add(groupMember.getMemberId());
195                     }
196                 }
197             }
198         }
199         return principalIds;
200     }
201 
202     public List<Group> getMemberGroups() {
203         if (this.memberGroups == null) {
204             splitMembersToTypes();
205         }
206         return this.memberGroups;
207     }
208 
209     public void setMemberGroups(List<Group> memberGroups) {
210         this.memberGroups = memberGroups;
211     }
212 }