Coverage Report - org.kuali.rice.kim.impl.jaxb.RoleMembersXmlDTO
 
Classes in this File Line Coverage Branch Coverage Complexity
RoleMembersXmlDTO
0%
0/6
N/A
1.667
RoleMembersXmlDTO$OutsideOfRole
0%
0/10
N/A
1.667
RoleMembersXmlDTO$WithinRole
0%
0/39
0%
0/16
1.667
 
 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 java.io.Serializable;
 19  
 import java.util.ArrayList;
 20  
 import java.util.Collections;
 21  
 import java.util.HashSet;
 22  
 import java.util.List;
 23  
 import java.util.Set;
 24  
 
 25  
 import javax.xml.bind.Marshaller;
 26  
 import javax.xml.bind.UnmarshalException;
 27  
 import javax.xml.bind.Unmarshaller;
 28  
 import javax.xml.bind.annotation.XmlAccessType;
 29  
 import javax.xml.bind.annotation.XmlAccessorType;
 30  
 import javax.xml.bind.annotation.XmlElement;
 31  
 import javax.xml.bind.annotation.XmlTransient;
 32  
 import javax.xml.bind.annotation.XmlType;
 33  
 
 34  
 import org.kuali.rice.core.util.jaxb.RiceXmlExportList;
 35  
 import org.kuali.rice.core.util.jaxb.RiceXmlImportList;
 36  
 import org.kuali.rice.core.util.jaxb.RiceXmlListAdditionListener;
 37  
 import org.kuali.rice.core.util.jaxb.RiceXmlListGetterListener;
 38  
 import org.kuali.rice.kim.api.role.RoleMemberContract;
 39  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 40  
 
 41  
 /**
 42  
  * Base class representing an unmarshalled <roleMembers> element.
 43  
  * Refer to the static inner classes for more information about the specific contexts.
 44  
  * 
 45  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 46  
  */
 47  
 @XmlTransient
 48  0
 public abstract class RoleMembersXmlDTO<T extends RoleMemberXmlDTO> implements RiceXmlListAdditionListener<T>, Serializable {
 49  
 
 50  
     private static final long serialVersionUID = 1L;
 51  
 
 52  
     public abstract List<T> getRoleMembers();
 53  
     
 54  
     public abstract void setRoleMembers(List<T> roleMembers);
 55  
     
 56  
     void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) throws UnmarshalException {
 57  0
         setRoleMembers(new RiceXmlImportList<T>(this));
 58  0
     }
 59  
     
 60  
     void afterUnmarshal(Unmarshaller unmarshaller, Object parent) throws UnmarshalException {
 61  0
         setRoleMembers(null);
 62  0
     }
 63  
     
 64  
     // =======================================================================================================
 65  
     
 66  
     /**
 67  
      * This class represents a &lt;roleMembers&gt; element that is not a child of a &lt;role&gt; element.
 68  
      * 
 69  
      * @author Kuali Rice Team (rice.collab@kuali.org)
 70  
      */
 71  0
     @XmlAccessorType(XmlAccessType.FIELD)
 72  
     @XmlType(name="StandaloneRoleMembersType", propOrder={"roleMembers"})
 73  0
     public static class OutsideOfRole extends RoleMembersXmlDTO<RoleMemberXmlDTO.OutsideOfRole> {
 74  
 
 75  
         private static final long serialVersionUID = 1L;
 76  
         
 77  
         @XmlElement(name="roleMember")
 78  
         private List<RoleMemberXmlDTO.OutsideOfRole> roleMembers;
 79  
 
 80  
         /**
 81  
          * @see org.kuali.rice.kim.impl.jaxb.RoleMembersXmlDTO#getRoleMembers()
 82  
          */
 83  
         @Override
 84  
         public List<RoleMemberXmlDTO.OutsideOfRole> getRoleMembers() {
 85  0
             return this.roleMembers;
 86  
         }
 87  
 
 88  
         /**
 89  
          * @see org.kuali.rice.kim.impl.jaxb.RoleMembersXmlDTO#setRoleMembers(java.util.List)
 90  
          */
 91  
         @Override
 92  
         public void setRoleMembers(List<RoleMemberXmlDTO.OutsideOfRole> roleMembers) {
 93  0
             this.roleMembers = roleMembers;
 94  0
         }
 95  
 
 96  
         /**
 97  
          * @see org.kuali.rice.core.util.jaxb.RiceXmlListAdditionListener#newItemAdded(java.lang.Object)
 98  
          */
 99  
         @Override
 100  
         public void newItemAdded(RoleMemberXmlDTO.OutsideOfRole item) {
 101  
             try {
 102  0
                 RoleXmlUtil.validateAndPersistNewRoleMember(item);
 103  0
             } catch (UnmarshalException e) {
 104  0
                 throw new RuntimeException(e);
 105  0
             }
 106  0
         }
 107  
     }
 108  
     
 109  
     // =======================================================================================================
 110  
     
 111  
     /**
 112  
      * This class represents a &lt;roleMembers&gt; element that is a child of a &lt;role&gt; element.
 113  
      * 
 114  
      * @author Kuali Rice Team (rice.collab@kuali.org)
 115  
      */
 116  0
     @XmlAccessorType(XmlAccessType.FIELD)
 117  
     @XmlType(name="RoleMembersType", propOrder={"roleMembers"})
 118  
     public static class WithinRole extends RoleMembersXmlDTO<RoleMemberXmlDTO.WithinRole>
 119  
             implements RiceXmlListGetterListener<RoleMemberXmlDTO.WithinRole,String> {
 120  
 
 121  
         private static final long serialVersionUID = 1L;
 122  
         
 123  
         @XmlElement(name="roleMember")
 124  
         private List<RoleMemberXmlDTO.WithinRole> roleMembers;
 125  
 
 126  
         @XmlTransient
 127  
         private String roleId;
 128  
         
 129  
         @XmlTransient
 130  
         private Set<String> existingRoleMemberIds;
 131  
         
 132  0
         public WithinRole() {}
 133  
         
 134  0
         public WithinRole(String roleId) {
 135  0
             this.roleId = roleId;
 136  0
         }
 137  
         
 138  
         /**
 139  
          * @see org.kuali.rice.kim.impl.jaxb.RoleMembersXmlDTO#getRoleMembers()
 140  
          */
 141  
         @Override
 142  
         public List<org.kuali.rice.kim.impl.jaxb.RoleMemberXmlDTO.WithinRole> getRoleMembers() {
 143  0
             return this.roleMembers;
 144  
         }
 145  
 
 146  
         /**
 147  
          * @see org.kuali.rice.kim.impl.jaxb.RoleMembersXmlDTO#setRoleMembers(java.util.List)
 148  
          */
 149  
         @Override
 150  
         public void setRoleMembers(List<org.kuali.rice.kim.impl.jaxb.RoleMemberXmlDTO.WithinRole> roleMembers) {
 151  0
             this.roleMembers = roleMembers;
 152  0
         }
 153  
         
 154  
         /**
 155  
          * @return the roleId
 156  
          */
 157  
         public String getRoleId() {
 158  0
             return this.roleId;
 159  
         }
 160  
 
 161  
         /**
 162  
          * @see org.kuali.rice.kim.impl.jaxb.RoleMembersXmlDTO#beforeUnmarshal(javax.xml.bind.Unmarshaller, java.lang.Object)
 163  
          */
 164  
         @Override
 165  
         void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) throws UnmarshalException {
 166  0
             if (parent instanceof RoleXmlDTO) {
 167  
                 // Obtain the role ID from the enclosing role, and persist the role if it has not been persisted yet.
 168  0
                 RoleXmlDTO parentRole = (RoleXmlDTO) parent;
 169  0
                 if (!parentRole.isAlreadyPersisted()) {
 170  0
                     RoleXmlUtil.validateAndPersistNewRole(parentRole);
 171  
                 }
 172  0
                 roleId = parentRole.getRoleId();
 173  
             }
 174  0
             existingRoleMemberIds = new HashSet<String>();
 175  0
             super.beforeUnmarshal(unmarshaller, parent);
 176  0
         }
 177  
 
 178  
         /**
 179  
          * This overridden method ...
 180  
          * 
 181  
          * @see org.kuali.rice.kim.impl.jaxb.RoleMembersXmlDTO#afterUnmarshal(javax.xml.bind.Unmarshaller, java.lang.Object)
 182  
          */
 183  
         @Override
 184  
         void afterUnmarshal(Unmarshaller unmarshaller, Object parent) throws UnmarshalException {
 185  0
             super.afterUnmarshal(unmarshaller, parent);
 186  0
             if (parent instanceof RoleXmlDTO) {
 187  0
                 ((RoleXmlDTO)parent).setExistingRoleMemberIds(existingRoleMemberIds);
 188  
             }
 189  0
             existingRoleMemberIds = null;
 190  0
         }
 191  
 
 192  
         /**
 193  
          * @see org.kuali.rice.core.util.jaxb.RiceXmlListAdditionListener#newItemAdded(java.lang.Object)
 194  
          */
 195  
         @Override
 196  
         public void newItemAdded(org.kuali.rice.kim.impl.jaxb.RoleMemberXmlDTO.WithinRole item) {
 197  
             // Persist the role member and add it to the set of role members that should not be removed from the role.
 198  
             try {
 199  0
                 existingRoleMemberIds.add(RoleXmlUtil.validateAndPersistNewRoleMember(item));
 200  0
             } catch (UnmarshalException e) {
 201  0
                 throw new RuntimeException(e);
 202  0
             }
 203  0
         }
 204  
         
 205  
         void beforeMarshal(Marshaller marshaller) {
 206  0
             List<? extends RoleMemberContract> tempMembers = KimApiServiceLocator.getRoleService().findRoleMembers(Collections.singletonMap("roleId", roleId));
 207  0
             if (tempMembers != null && !tempMembers.isEmpty()) {
 208  0
                 List<String> roleMemberIds = new ArrayList<String>();
 209  
                 
 210  0
                 for (RoleMemberContract tempMember : tempMembers) {
 211  0
                     if (tempMember.isActive(null)) {
 212  0
                         roleMemberIds.add(tempMember.getRoleMemberId());
 213  
                     }
 214  
                 }
 215  
                 
 216  0
                 if (!roleMemberIds.isEmpty()) {
 217  0
                     setRoleMembers(new RiceXmlExportList<RoleMemberXmlDTO.WithinRole,String>(roleMemberIds, this));
 218  
                 }
 219  
             }
 220  0
         }
 221  
         
 222  
         void afterMarshal(Marshaller marshaller) {
 223  0
             setRoleMembers(null);
 224  0
         }
 225  
 
 226  
         /**
 227  
          * @see org.kuali.rice.core.util.jaxb.RiceXmlListGetterListener#gettingNextItem(java.lang.Object, int)
 228  
          */
 229  
         @Override
 230  
         public RoleMemberXmlDTO.WithinRole gettingNextItem(String nextItem, int index) {
 231  0
             return new RoleMemberXmlDTO.WithinRole(KimApiServiceLocator.getRoleService().findRoleMembers(
 232  
                     Collections.singletonMap("roleMemberId", nextItem)).get(0), false);
 233  
         }
 234  
     }
 235  
 }