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