Coverage Report - org.kuali.rice.kim.impl.jaxb.RoleMemberXmlDTO
 
Classes in this File Line Coverage Branch Coverage Complexity
RoleMemberXmlDTO
0%
0/98
0%
0/66
2.459
RoleMemberXmlDTO$OutsideOfRole
0%
0/17
0%
0/6
2.459
RoleMemberXmlDTO$WithinRole
0%
0/9
0%
0/2
2.459
 
 1  
 /*
 2  
  * Copyright 2011 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/ecl1.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.jaxb;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.core.util.jaxb.NameAndNamespacePair;
 20  
 import org.kuali.rice.core.util.jaxb.NameAndNamespacePairValidatingAdapter;
 21  
 import org.kuali.rice.core.util.jaxb.StringToDateTimeAdapter;
 22  
 import org.kuali.rice.kim.api.group.GroupContract;
 23  
 import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
 24  
 import org.kuali.rice.kim.api.jaxb.QualificationListAdapter;
 25  
 import org.kuali.rice.kim.api.role.RoleContract;
 26  
 import org.kuali.rice.kim.api.role.RoleMemberContract;
 27  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 28  
 import org.kuali.rice.kim.util.KimConstants.KimUIConstants;
 29  
 
 30  
 import javax.xml.bind.Unmarshaller;
 31  
 import javax.xml.bind.annotation.XmlAccessType;
 32  
 import javax.xml.bind.annotation.XmlAccessorType;
 33  
 import javax.xml.bind.annotation.XmlElement;
 34  
 import javax.xml.bind.annotation.XmlTransient;
 35  
 import javax.xml.bind.annotation.XmlType;
 36  
 import javax.xml.bind.annotation.adapters.NormalizedStringAdapter;
 37  
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 38  
 import java.io.Serializable;
 39  
 import java.util.HashMap;
 40  
 import java.util.Map;
 41  
 
 42  
 /**
 43  
  * Base class representing an unmarshalled <roleMember> element.
 44  
  * Refer to the static inner classes for more information about the specific contexts.
 45  
  * 
 46  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 47  
  */
 48  
 @XmlTransient
 49  
 public abstract class RoleMemberXmlDTO implements Serializable {
 50  
 
 51  
     private static final long serialVersionUID = 1L;
 52  
 
 53  
     @XmlElement(name="principalId")
 54  
     @XmlJavaTypeAdapter(NormalizedStringAdapter.class)
 55  
     private String principalId;
 56  
     
 57  
     @XmlElement(name="principalName")
 58  
     @XmlJavaTypeAdapter(NormalizedStringAdapter.class)
 59  
     private String principalName;
 60  
     
 61  
     @XmlElement(name="groupId")
 62  
     @XmlJavaTypeAdapter(NormalizedStringAdapter.class)
 63  
     private String groupId;
 64  
     
 65  
     @XmlElement(name="groupName")
 66  
     @XmlJavaTypeAdapter(NameAndNamespacePairValidatingAdapter.class)
 67  
     private NameAndNamespacePair groupName;
 68  
     
 69  
     @XmlElement(name="roleIdAsMember")
 70  
     @XmlJavaTypeAdapter(NormalizedStringAdapter.class)
 71  
     private String roleIdAsMember;
 72  
     
 73  
     @XmlElement(name="roleNameAsMember")
 74  
     @XmlJavaTypeAdapter(NameAndNamespacePairValidatingAdapter.class)
 75  
     private NameAndNamespacePair roleNameAsMember;
 76  
     
 77  
     @XmlElement(name="activeFromDate")
 78  
     @XmlJavaTypeAdapter(StringToDateTimeAdapter.class)
 79  
     private java.util.Date activeFromDate;
 80  
     
 81  
     @XmlElement(name="activeToDate")
 82  
     @XmlJavaTypeAdapter(StringToDateTimeAdapter.class)
 83  
     private java.util.Date activeToDate;
 84  
     
 85  
     @XmlElement(name="qualifications")
 86  
     @XmlJavaTypeAdapter(QualificationListAdapter.class)
 87  
     private Map<String, String> qualifications;
 88  
     
 89  
     @XmlTransient
 90  
     private String memberTypeCode;
 91  
     
 92  
     /**
 93  
      * Constructs an empty RoleMemberXmlDTO instance.
 94  
      */
 95  0
     public RoleMemberXmlDTO() {}
 96  
     
 97  
     /**
 98  
      * Constructs a RoleMemberXmlDTO instance that is populated with the info from the given role member.
 99  
      * 
 100  
      * @param roleMember The role member that this DTO should populate its data from.
 101  
      * @param populateMemberId If true, the member principal/group/role ID will get populated; otherwise, only
 102  
      * the member principal/group/role name and (if applicable) namespace will get populated.
 103  
      * @throws IllegalArgumentException if roleMember is null, has an invalid member type code, or refers to a nonexistent principal/group/role.
 104  
      */
 105  0
     public RoleMemberXmlDTO(RoleMemberContract roleMember, boolean populateMemberId) {
 106  0
         if (roleMember == null) {
 107  0
             throw new IllegalArgumentException("roleMember cannot be null");
 108  
         }
 109  0
         this.memberTypeCode = roleMember.getMemberTypeCode();
 110  0
         this.activeFromDate = roleMember.getActiveFromDate();
 111  0
         this.activeToDate = roleMember.getActiveToDate();
 112  0
         this.qualifications = (roleMember.getAttributes() != null) ? roleMember.getAttributes() : new HashMap<String, String>();
 113  
         
 114  0
         if (KimUIConstants.MEMBER_TYPE_PRINCIPAL_CODE.equals(memberTypeCode)) {
 115  0
             if (populateMemberId) {
 116  0
                 this.principalId = roleMember.getMemberId();
 117  
             }
 118  0
             PrincipalContract principal = KimApiServiceLocator.getIdentityService().getPrincipal(roleMember.getMemberId());
 119  0
             if (principal == null) {
 120  0
                 throw new IllegalArgumentException("Cannot find principal with ID \"" +  roleMember.getMemberId() + "\"");
 121  
             }
 122  0
             this.principalName = principal.getPrincipalName();
 123  0
         } else if (KimUIConstants.MEMBER_TYPE_GROUP_CODE.equals(memberTypeCode)) {
 124  0
             if (populateMemberId) {
 125  0
                 this.groupId = roleMember.getMemberId();
 126  
             }
 127  0
             GroupContract group = KimApiServiceLocator.getGroupService().getGroup(roleMember.getMemberId());
 128  0
             if (group == null) {
 129  0
                 throw new IllegalArgumentException("Cannot find group with ID \"" + roleMember.getMemberId() + "\"");
 130  
             }
 131  0
             this.groupName = new NameAndNamespacePair(group.getNamespaceCode(), group.getName());
 132  0
         } else if (KimUIConstants.MEMBER_TYPE_ROLE_CODE.equals(memberTypeCode)) {
 133  0
             if (populateMemberId) {
 134  0
                 this.roleIdAsMember = roleMember.getMemberId();
 135  
             }
 136  0
             RoleContract role = KimApiServiceLocator.getRoleService().getRole(roleMember.getMemberId());
 137  0
             if (role == null) {
 138  0
                 throw new IllegalArgumentException("Cannot find role with ID \"" + roleMember.getMemberId() + "\"");
 139  
             }
 140  0
             this.roleNameAsMember = new NameAndNamespacePair(role.getNamespaceCode(), role.getName());
 141  0
         } else {
 142  0
             throw new IllegalArgumentException("Cannot construct a RoleMemberXmlDTO from a role member with an unrecognized member type code of \"" +
 143  
                     memberTypeCode + "\"");
 144  
         }    
 145  0
     }
 146  
 
 147  
     /**
 148  
      * @return the principalId
 149  
      */
 150  
     public String getPrincipalId() {
 151  0
         return this.principalId;
 152  
     }
 153  
 
 154  
     /**
 155  
      * @param principalId the principalId to set
 156  
      */
 157  
     public void setPrincipalId(String principalId) {
 158  0
         this.principalId = principalId;
 159  0
     }
 160  
 
 161  
     /**
 162  
      * @return the principalName
 163  
      */
 164  
     public String getPrincipalName() {
 165  0
         return this.principalName;
 166  
     }
 167  
 
 168  
     /**
 169  
      * @param principalName the principalName to set
 170  
      */
 171  
     public void setPrincipalName(String principalName) {
 172  0
         this.principalName = principalName;
 173  0
     }
 174  
 
 175  
     /**
 176  
      * @return the groupId
 177  
      */
 178  
     public String getGroupId() {
 179  0
         return this.groupId;
 180  
     }
 181  
 
 182  
     /**
 183  
      * @param groupId the groupId to set
 184  
      */
 185  
     public void setGroupId(String groupId) {
 186  0
         this.groupId = groupId;
 187  0
     }
 188  
 
 189  
     /**
 190  
      * @return the groupName
 191  
      */
 192  
     public NameAndNamespacePair getGroupName() {
 193  0
         return this.groupName;
 194  
     }
 195  
 
 196  
     /**
 197  
      * @param groupName the groupName to set
 198  
      */
 199  
     public void setGroupName(NameAndNamespacePair groupName) {
 200  0
         this.groupName = groupName;
 201  0
     }
 202  
 
 203  
     /**
 204  
      * @return the roleIdAsMember
 205  
      */
 206  
     public String getRoleIdAsMember() {
 207  0
         return this.roleIdAsMember;
 208  
     }
 209  
 
 210  
     /**
 211  
      * @param roleIdAsMember the roleIdAsMember to set
 212  
      */
 213  
     public void setRoleIdAsMember(String roleIdAsMember) {
 214  0
         this.roleIdAsMember = roleIdAsMember;
 215  0
     }
 216  
 
 217  
     /**
 218  
      * @return the roleNameAsMember
 219  
      */
 220  
     public NameAndNamespacePair getRoleNameAsMember() {
 221  0
         return this.roleNameAsMember;
 222  
     }
 223  
 
 224  
     /**
 225  
      * @param roleNameAsMember the roleNameAsMember to set
 226  
      */
 227  
     public void setRoleNameAsMember(NameAndNamespacePair roleNameAsMember) {
 228  0
         this.roleNameAsMember = roleNameAsMember;
 229  0
     }
 230  
 
 231  
     /**
 232  
      * @return the activeFromDate
 233  
      */
 234  
     public java.util.Date getActiveFromDate() {
 235  0
         return this.activeFromDate;
 236  
     }
 237  
 
 238  
     /**
 239  
      * @param activeFromDate the activeFromDate to set
 240  
      */
 241  
     public void setActiveFromDate(java.util.Date activeFromDate) {
 242  0
         this.activeFromDate = activeFromDate;
 243  0
     }
 244  
 
 245  
     /**
 246  
      * @return the activeToDate
 247  
      */
 248  
     public java.util.Date getActiveToDate() {
 249  0
         return this.activeToDate;
 250  
     }
 251  
 
 252  
     /**
 253  
      * @param activeToDate the activeToDate to set
 254  
      */
 255  
     public void setActiveToDate(java.util.Date activeToDate) {
 256  0
         this.activeToDate = activeToDate;
 257  0
     }
 258  
 
 259  
     /**
 260  
      * @return the qualifications
 261  
      */
 262  
     public Map<String, String> getQualifications() {
 263  0
         return this.qualifications;
 264  
     }
 265  
 
 266  
     /**
 267  
      * @param qualifications the qualifications to set
 268  
      */
 269  
     public void setQualifications(Map<String, String> qualifications) {
 270  0
         this.qualifications = qualifications;
 271  0
     }
 272  
 
 273  
     /**
 274  
      * Retrieves the member type code.
 275  
      * 
 276  
      * <p>If the member type code is null at the time that this method is invoked, an attempt will be made to set its
 277  
      * value based on any populated member principal/group/role ID/name information.
 278  
      * 
 279  
      * @return the member type code, or null if no membership identification information has been set on this member.
 280  
      * @throws IllegalStateException if the role member is populated simultaneously with multiple member ID/name information
 281  
      */
 282  
     public String getMemberTypeCode() {
 283  0
         if (memberTypeCode == null) {
 284  0
             boolean foundMemberInfo = false;
 285  
             
 286  0
             if (StringUtils.isNotBlank(principalId) || StringUtils.isNotBlank(principalName)) {
 287  0
                 memberTypeCode = KimUIConstants.MEMBER_TYPE_PRINCIPAL_CODE;
 288  0
                 foundMemberInfo = true;
 289  
             }
 290  
             
 291  0
             if (StringUtils.isNotBlank(groupId) || groupName != null) {
 292  0
                 if (foundMemberInfo) {
 293  0
                     memberTypeCode = null;
 294  0
                     throw new IllegalStateException("Cannot have a role member that is simultaneously populated with member principal, member group, and/or member role information");
 295  
                 }
 296  0
                 memberTypeCode = KimUIConstants.MEMBER_TYPE_GROUP_CODE;
 297  0
                 foundMemberInfo = true;
 298  
             }
 299  
             
 300  0
             if (StringUtils.isNotBlank(roleIdAsMember) || roleNameAsMember != null) {
 301  0
                 if (foundMemberInfo) {
 302  0
                     memberTypeCode = null;
 303  0
                     throw new IllegalStateException("Cannot have a role member that is simultaneously populated with member principal, member group, and/or member role information");
 304  
                 }
 305  0
                 memberTypeCode = KimUIConstants.MEMBER_TYPE_ROLE_CODE;
 306  0
                 foundMemberInfo = true;
 307  
             }
 308  
         }
 309  0
         return this.memberTypeCode;
 310  
     }
 311  
 
 312  
     /**
 313  
      * Retrieves the role member's ID, based on the member type code and any populated member principal/group/role IDs.
 314  
      * 
 315  
      * <p>If the member type code is null at the time that this method is invoked, an attempt will be made to set its
 316  
      * value based on any populated member principal/group/role ID/name information.
 317  
      * 
 318  
      * @return The member's ID, or null if the member type code is null or the associated member ID information is null.
 319  
      */
 320  
     public String getMemberId() {
 321  0
         if (KimUIConstants.MEMBER_TYPE_PRINCIPAL_CODE.equals(getMemberTypeCode())) {
 322  0
             return principalId;
 323  0
         } else if (KimUIConstants.MEMBER_TYPE_GROUP_CODE.equals(getMemberTypeCode())) {
 324  0
             return groupId;
 325  0
         } else if (KimUIConstants.MEMBER_TYPE_ROLE_CODE.equals(getMemberTypeCode())) {
 326  0
             return roleIdAsMember;
 327  
         }
 328  0
         return null;
 329  
     }
 330  
     
 331  
     /**
 332  
      * Retrieves the role member's name, based on the member type code and any populated member principal/group/role names.
 333  
      * 
 334  
      * <p>If the member type code is null at the time that this method is invoked, an attempt will be made to set its
 335  
      * value based on any populated member principal/group/role ID/name information.
 336  
      * 
 337  
      * @return The member's name, or null if the member type code is null or the associated member name information is null.
 338  
      */
 339  
     public String getMemberName() {
 340  0
         if (KimUIConstants.MEMBER_TYPE_PRINCIPAL_CODE.equals(getMemberTypeCode())) {
 341  0
             return principalName;
 342  0
         } else if (KimUIConstants.MEMBER_TYPE_GROUP_CODE.equals(getMemberTypeCode())) {
 343  0
             return (groupName != null) ? groupName.getName() : null;
 344  0
         } else if (KimUIConstants.MEMBER_TYPE_ROLE_CODE.equals(getMemberTypeCode())) {
 345  0
             return (roleNameAsMember != null) ? roleNameAsMember.getName() : null;
 346  
         }
 347  0
         return null;
 348  
     }
 349  
     
 350  
     /**
 351  
      * Retrieves the role member's namespace code, based on the member type code and any populated member principal/group/role names.
 352  
      * 
 353  
      * <p>If the member type code is null at the time that this method is invoked, an attempt will be made to set its
 354  
      * value based on any populated member principal/group/role ID/name information.
 355  
      * 
 356  
      * @return The member's namespace code, or null if the member type code is null, the associated member name information is null,
 357  
      * or the role member is a principal.
 358  
      */
 359  
     public String getMemberNamespaceCode() {
 360  0
         if (KimUIConstants.MEMBER_TYPE_PRINCIPAL_CODE.equals(getMemberTypeCode())) {
 361  0
             return null;
 362  0
         } else if (KimUIConstants.MEMBER_TYPE_GROUP_CODE.equals(getMemberTypeCode())) {
 363  0
             return (groupName != null) ? groupName.getName() : null;
 364  0
         } else if (KimUIConstants.MEMBER_TYPE_ROLE_CODE.equals(getMemberTypeCode())) {
 365  0
             return (roleNameAsMember != null) ? roleNameAsMember.getName() : null;
 366  
         }
 367  0
         return null;
 368  
     }
 369  
     
 370  
     /**
 371  
      * Retrieves the ID of the role that this member belongs to.
 372  
      * Subclasses are responsible for implementing this method so that it does so.
 373  
      * 
 374  
      * @return The role ID of the role that this member belongs to.
 375  
      */
 376  
     public abstract String getRoleId();
 377  
     
 378  
     // =======================================================================================================
 379  
     
 380  
     /**
 381  
      * This class represents a &lt;roleMember&gt; element that is not a descendant of a &lt;role&gt; element.
 382  
      * 
 383  
      * @author Kuali Rice Team (rice.collab@kuali.org)
 384  
      */
 385  
     @XmlAccessorType(XmlAccessType.FIELD)
 386  
     @XmlType(name="StandaloneRoleMemberType", propOrder={
 387  
             "roleId", "roleNameAndNamespace", "principalId", "principalName", "groupId", "groupName", "roleIdAsMember",
 388  
                     "roleNameAsMember", "activeFromDate", "activeToDate", "qualifications"
 389  
     })
 390  
     public static class OutsideOfRole extends RoleMemberXmlDTO {
 391  
 
 392  
         private static final long serialVersionUID = 1L;
 393  
 
 394  
         @XmlElement(name="roleId")
 395  
         @XmlJavaTypeAdapter(NormalizedStringAdapter.class)
 396  
         private String roleId;
 397  
 
 398  
         @XmlElement(name="roleName")
 399  
         @XmlJavaTypeAdapter(NameAndNamespacePairValidatingAdapter.class)
 400  
         private NameAndNamespacePair roleNameAndNamespace;
 401  
         
 402  
         public OutsideOfRole() {
 403  0
             super();
 404  0
         }
 405  
         
 406  
         public OutsideOfRole(RoleMemberContract roleMember, boolean populateMemberId) {
 407  0
             super(roleMember, populateMemberId);
 408  0
             this.roleId = roleMember.getRoleId();
 409  0
             RoleContract tempRole = KimApiServiceLocator.getRoleService().getRole(roleId);
 410  0
             if (tempRole == null) {
 411  0
                 throw new IllegalArgumentException("Cannot find role with ID \"" + roleId + "\"");
 412  
             }
 413  0
             this.roleNameAndNamespace = new NameAndNamespacePair(tempRole.getNamespaceCode(), tempRole.getName());
 414  0
         }
 415  
         
 416  
         /**
 417  
          * @see org.kuali.rice.kim.impl.jaxb.RoleMemberXmlDTO#getRoleId()
 418  
          */
 419  
         @Override
 420  
         public String getRoleId() {
 421  0
             return roleId;
 422  
         }
 423  
 
 424  
         /**
 425  
          * @param roleId the roleId to set
 426  
          */
 427  
         public void setRoleId(String roleId) {
 428  0
             this.roleId = roleId;
 429  0
         }
 430  
         
 431  
         /**
 432  
          * @return the roleNameAndNamespace
 433  
          */
 434  
         public NameAndNamespacePair getRoleNameAndNamespace() {
 435  0
             return this.roleNameAndNamespace;
 436  
         }
 437  
 
 438  
         /**
 439  
          * @param roleNameAndNamespace the roleNameAndNamespace to set
 440  
          */
 441  
         public void setRoleNameAndNamespace(NameAndNamespacePair roleNameAndNamespace) {
 442  0
             this.roleNameAndNamespace = roleNameAndNamespace;
 443  0
         }
 444  
 
 445  
         /**
 446  
          * Retrieves the role name from the role-name-and-namespace combo.
 447  
          * 
 448  
          * @return The name of the role that this member belongs to, or null if the role-name-and-namespace combo is null.
 449  
          */
 450  
         public String getRoleName() {
 451  0
             return (roleNameAndNamespace != null) ? roleNameAndNamespace.getName() : null;
 452  
         }
 453  
 
 454  
         /**
 455  
          * Retrieves the role namespace code from the role-name-and-namespace combo.
 456  
          * 
 457  
          * @return The namespace code of the role that this member belongs to, or null if the role-name-and-namespace combo is null.
 458  
          */
 459  
         public String getRoleNamespaceCode() {
 460  0
             return (roleNameAndNamespace != null) ? roleNameAndNamespace.getNamespaceCode() : null;
 461  
         }
 462  
     }
 463  
     
 464  
     // =======================================================================================================
 465  
     
 466  
     /**
 467  
      * This class represents a &lt;roleMember&gt; element that is a descendant of a &lt;role&gt; element.
 468  
      * 
 469  
      * @author Kuali Rice Team (rice.collab@kuali.org)
 470  
      */
 471  
     @XmlAccessorType(XmlAccessType.FIELD)
 472  
     @XmlType(name="RoleMemberType", propOrder={
 473  
             "principalId", "principalName", "groupId", "groupName", "roleIdAsMember",
 474  
                     "roleNameAsMember", "activeFromDate", "activeToDate", "qualifications"
 475  
     })
 476  
     public static class WithinRole extends RoleMemberXmlDTO {
 477  
 
 478  
         private static final long serialVersionUID = 1L;
 479  
         
 480  
         @XmlTransient
 481  
         private String roleId;
 482  
 
 483  
         public WithinRole() {
 484  0
             super();
 485  0
         }
 486  
         
 487  
         public WithinRole(RoleMemberContract roleMember, boolean populateMemberId) {
 488  0
             super(roleMember, populateMemberId);
 489  0
             this.roleId = roleMember.getRoleId();
 490  0
         }
 491  
         
 492  
         void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) {
 493  0
             if (parent instanceof RoleMembersXmlDTO.WithinRole) {
 494  0
                 this.roleId = ((RoleMembersXmlDTO.WithinRole)parent).getRoleId();
 495  
             }
 496  0
         }
 497  
         
 498  
         /**
 499  
          * @see org.kuali.rice.kim.impl.jaxb.RoleMemberXmlDTO#getRoleId()
 500  
          */
 501  
         @Override
 502  
         public String getRoleId() {
 503  0
             return roleId;
 504  
         }
 505  
         
 506  
     }
 507  
 }