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