001    /**
002     * Copyright 2005-2012 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     */
016    package org.kuali.rice.kim.bo.ui;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.hibernate.annotations.GenericGenerator;
020    import org.hibernate.annotations.Parameter;
021    import org.kuali.rice.core.api.membership.MemberType;
022    import org.kuali.rice.kim.api.KimConstants;
023    import org.kuali.rice.kim.api.group.Group;
024    import org.kuali.rice.kim.api.identity.principal.Principal;
025    import org.kuali.rice.kim.api.role.Role;
026    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
027    import org.springframework.util.AutoPopulatingList;
028    
029    import javax.persistence.CascadeType;
030    import javax.persistence.Column;
031    import javax.persistence.Entity;
032    import javax.persistence.FetchType;
033    import javax.persistence.GeneratedValue;
034    import javax.persistence.Id;
035    import javax.persistence.IdClass;
036    import javax.persistence.JoinColumn;
037    import javax.persistence.JoinColumns;
038    import javax.persistence.OneToMany;
039    import javax.persistence.Table;
040    import javax.persistence.Transient;
041    import java.util.ArrayList;
042    import java.util.HashMap;
043    import java.util.List;
044    import java.util.Map;
045    
046    /**
047     * This is a description of what this class does - shyu don't forget to fill this in. 
048     * 
049     * @author Kuali Rice Team (rice.collab@kuali.org)
050     *
051     */
052    @IdClass(KimDocumentRoleMemberId.class)
053    @Entity
054    @Table(name="KRIM_PND_ROLE_MBR_MT")
055    public class KimDocumentRoleMember  extends KimDocumentBoActivatableToFromEditableBase {
056            private static final long serialVersionUID = -2463865643038170979L;
057    
058            @Id
059            @GeneratedValue(generator="KRIM_ROLE_MBR_ID_S")
060            @GenericGenerator(name="KRIM_ROLE_MBR_ID_S",strategy="org.kuali.rice.core.jpa.spring.RiceNumericStringSequenceStyleGenerator",parameters={
061                            @Parameter(name="sequence_name",value="KRIM_ROLE_MBR_ID_S"),
062                            @Parameter(name="value_column",value="id")
063                    })
064            @Column(name="ROLE_MBR_ID")
065            protected String roleMemberId;
066            
067            @Column(name="ROLE_ID")
068            protected String roleId;
069            @Column(name="MBR_ID")
070            protected String memberId;
071            
072            //TODO: remove the default
073            @Column(name="MBR_TYP_CD")
074            protected String memberTypeCode = KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE.getCode();
075            @Transient
076            protected String memberName;
077            @Transient
078            protected String memberNamespaceCode;
079            
080            protected String memberFullName;
081    
082            @OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
083            @JoinColumns({
084                @JoinColumn(name="ROLE_MBR_ID", insertable = false, updatable = false), 
085                @JoinColumn(name="FDOC_NBR", insertable = false, updatable = false)
086            })
087            protected List <KimDocumentRoleQualifier> qualifiers = new AutoPopulatingList(KimDocumentRoleQualifier.class);
088            @Transient
089            protected String qualifiersToDisplay;
090            
091            @OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
092            @JoinColumns({
093                @JoinColumn(name="ROLE_MBR_ID", insertable = false, updatable = false), 
094                @JoinColumn(name="FDOC_NBR", insertable = false, updatable = false)
095            })
096            private List<KimDocumentRoleResponsibilityAction> roleRspActions;
097    
098            public KimDocumentRoleMember() {
099                    qualifiers = new ArrayList <KimDocumentRoleQualifier>();
100                    roleRspActions = new ArrayList <KimDocumentRoleResponsibilityAction>();
101            }
102    
103            public int getNumberOfQualifiers(){
104                    return qualifiers==null?0:qualifiers.size();
105            }
106            
107            /**
108             * @return the memberId
109             */
110            public String getMemberId() {
111                    return this.memberId;
112            }
113    
114            /**
115             * @param memberId the memberId to set
116             */
117            public void setMemberId(String memberId) {
118                    this.memberId = memberId;
119            }
120    
121            public String getRoleMemberId() {
122                    return this.roleMemberId;
123            }
124    
125            public void setRoleMemberId(String roleMemberId) {
126                    this.roleMemberId = roleMemberId;
127            }
128    
129            public String getRoleId() {
130                    return this.roleId;
131            }
132    
133            public void setRoleId(String roleId) {
134                    this.roleId = roleId;
135            }
136    
137            public KimDocumentRoleQualifier getQualifier(String kimAttributeDefnId) {
138                    for(KimDocumentRoleQualifier qualifier:qualifiers){
139                            if(qualifier.getKimAttrDefnId().equals(kimAttributeDefnId)) {
140                                    return qualifier;
141                }
142                    }
143                    return null;
144            }
145    
146            public List<KimDocumentRoleQualifier> getQualifiers() {
147                    return this.qualifiers;
148            }
149    
150            public void setQualifiers(List<KimDocumentRoleQualifier> qualifiers) {
151                    this.qualifiers = qualifiers;
152            }
153    
154            /**
155             * @return the memberTypeCode
156             */
157            public String getMemberTypeCode() {
158                    return this.memberTypeCode;
159            }
160    
161            /**
162             * @param memberTypeCode the memberTypeCode to set
163             */
164            public void setMemberTypeCode(String memberTypeCode) {
165                    this.memberTypeCode = memberTypeCode;
166            }
167    
168            /**
169             * @return the memberName
170             */
171            public String getMemberName() {
172                    if ( memberName == null ) {
173                            populateDerivedValues();
174                    }
175                    return memberName;
176            }
177    
178            /**
179             * @param memberName the memberName to set
180             */
181            public void setMemberName(String memberName) {
182                    this.memberName = memberName;
183            }
184    
185            public List<KimDocumentRoleResponsibilityAction> getRoleRspActions() {
186                    return this.roleRspActions;
187            }
188    
189            public void setRoleRspActions(
190                            List<KimDocumentRoleResponsibilityAction> roleRspActions) {
191                    this.roleRspActions = roleRspActions;
192            }
193    
194            /**
195             * @return the memberNamespaceCode
196             */
197            public String getMemberNamespaceCode() {
198                    if ( memberNamespaceCode == null ) {
199                            populateDerivedValues();
200                    }
201                    return memberNamespaceCode;
202            }
203    
204            /**
205             * @param memberNamespaceCode the memberNamespaceCode to set
206             */
207            public void setMemberNamespaceCode(String memberNamespaceCode) {
208                    this.memberNamespaceCode = memberNamespaceCode;
209            }
210    
211            protected void populateDerivedValues() {
212            if(MemberType.PRINCIPAL.getCode().equals(getMemberTypeCode())){
213                if(!StringUtils.isEmpty(getMemberId())){
214                    Principal principalInfo = KimApiServiceLocator.getIdentityService().getPrincipal(getMemberId());
215                    if (principalInfo != null) {
216                        setMemberName(principalInfo.getPrincipalName());
217                        }
218                }
219            } else if(MemberType.GROUP.getCode().equals(getMemberTypeCode())){
220                    Group groupInfo = KimApiServiceLocator.getGroupService().getGroup(getMemberId());
221                    if (groupInfo != null) {
222                            setMemberName(groupInfo.getName());
223                            setMemberNamespaceCode(groupInfo.getNamespaceCode());
224                    }
225                    
226            } else if(MemberType.ROLE.getCode().equals(getMemberTypeCode())){
227                    Role role = KimApiServiceLocator.getRoleService().getRole(getMemberId());
228                    setMemberName(role.getName());
229                    setMemberNamespaceCode(role.getNamespaceCode());
230            }
231    
232            }
233            
234            public boolean isRole(){
235                    return getMemberTypeCode()!=null && getMemberTypeCode().equals(MemberType.ROLE.getCode());
236            }
237            
238            public boolean isGroup(){
239                    return getMemberTypeCode()!=null && getMemberTypeCode().equals(MemberType.GROUP.getCode());
240            }
241    
242            public boolean isPrincipal(){
243                    return getMemberTypeCode()!=null && getMemberTypeCode().equals(MemberType.PRINCIPAL.getCode());
244            }
245    
246            public Map<String, String> getQualifierAsMap() {
247                    Map<String, String> m = new HashMap<String, String>();
248                    for ( KimDocumentRoleQualifier data : getQualifiers() ) {
249                            if (data.getKimAttribute() == null){
250                                    data.refreshReferenceObject("kimAttribute");
251                            }
252                            if (data.getKimAttribute() != null){
253                                    m.put( data.getKimAttribute().getAttributeName(), data.getAttrVal() );
254                            }
255                    }
256                    return m;
257            }
258    
259            /**
260             * @return the qualifiersToDisplay
261             */
262            public String getQualifiersToDisplay() {
263                    return this.qualifiersToDisplay;
264            }
265    
266            /**
267             * @param qualifiersToDisplay the qualifiersToDisplay to set
268             */
269            public void setQualifiersToDisplay(String qualifiersToDisplay) {
270                    this.qualifiersToDisplay = qualifiersToDisplay;
271            }
272            
273            /**
274             * @return the memberFullName
275             */
276            public String getMemberFullName() {
277                    return this.memberFullName;
278            }
279    
280            /**
281             * @param memberFullName the memberFullName to set
282             */
283            public void setMemberFullName(String memberFullName) {
284                    this.memberFullName = memberFullName;
285            }
286    
287    }