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.impl.group;
017
018import org.joda.time.DateTime;
019import org.kuali.rice.kim.api.KimConstants;
020import org.kuali.rice.kim.api.group.Group;
021import org.kuali.rice.kim.api.identity.Person;
022import org.kuali.rice.kim.api.services.KimApiServiceLocator;
023import org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo;
024import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
025
026import javax.persistence.Cacheable;
027import javax.persistence.CascadeType;
028import javax.persistence.Column;
029import javax.persistence.Entity;
030import javax.persistence.GeneratedValue;
031import javax.persistence.Id;
032import javax.persistence.JoinColumn;
033import javax.persistence.OneToMany;
034import javax.persistence.Table;
035import javax.persistence.Transient;
036import java.util.ArrayList;
037import java.util.List;
038import java.util.Map;
039
040@Cacheable(false)
041@Entity
042@Table(name = "KRIM_GRP_T")
043public class GroupBo extends GroupBase {
044
045    private static final long serialVersionUID = 1L;
046
047    @PortableSequenceGenerator(name = "KRIM_GRP_ID_S")
048    @GeneratedValue(generator = "KRIM_GRP_ID_S")
049    @Id
050    @Column(name = "GRP_ID")
051    private String id;
052
053    @OneToMany(targetEntity = GroupMemberBo.class, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
054    @JoinColumn(name = "GRP_ID", referencedColumnName = "GRP_ID", insertable = false, updatable = false)
055    private List<GroupMemberBo> members;
056
057    @OneToMany(targetEntity = GroupAttributeBo.class, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
058    @JoinColumn(name = "GRP_ID", referencedColumnName = "GRP_ID" )
059    private List<GroupAttributeBo> attributeDetails;
060
061    @Transient
062    private List<Person> memberPersons;
063
064    @Transient
065    private List<Group> memberGroups;
066
067    @Override
068    public Map<String, String> getAttributes() {
069        return attributeDetails != null ? KimAttributeDataBo.toAttributes(attributeDetails) : attributes;
070    }
071
072    @Override
073    public String getId() {
074        return id;
075    }
076
077    public void setId(String id) {
078        this.id = id;
079    }
080
081    public List<GroupMemberBo> getMembers() {
082        return members;
083    }
084
085    public void setMembers(List<GroupMemberBo> members) {
086        this.members = members;
087    }
088
089    public List<GroupAttributeBo> getAttributeDetails() {
090        return attributeDetails;
091    }
092
093    public void setAttributeDetails(List<GroupAttributeBo> attributeDetails) {
094        this.attributeDetails = attributeDetails;
095    }
096
097    /**
098     * Converts a mutable bo to its immutable counterpart
099     * @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}