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