001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kim.impl.jaxb;
017    
018    import java.io.Serializable;
019    import java.util.List;
020    import java.util.Set;
021    
022    import javax.xml.bind.Marshaller;
023    import javax.xml.bind.UnmarshalException;
024    import javax.xml.bind.Unmarshaller;
025    import javax.xml.bind.annotation.XmlAccessType;
026    import javax.xml.bind.annotation.XmlAccessorType;
027    import javax.xml.bind.annotation.XmlElement;
028    import javax.xml.bind.annotation.XmlType;
029    
030    import org.kuali.rice.core.util.jaxb.RiceXmlExportList;
031    import org.kuali.rice.core.util.jaxb.RiceXmlImportList;
032    import org.kuali.rice.core.util.jaxb.RiceXmlListAdditionListener;
033    import org.kuali.rice.core.util.jaxb.RiceXmlListGetterListener;
034    import org.kuali.rice.kim.api.role.RoleContract;
035    
036    /**
037     * This class represents a <roles> element. 
038     * 
039     * @author Kuali Rice Team (rice.collab@kuali.org)
040     */
041    @XmlAccessorType(XmlAccessType.FIELD)
042    @XmlType(name="RolesType", propOrder={"roles"})
043    public class RolesXmlDTO implements RiceXmlListAdditionListener<RoleXmlDTO>, RiceXmlListGetterListener<RoleXmlDTO,Object>, Serializable {
044        
045        private static final long serialVersionUID = 1L;
046        
047        @XmlElement(name="role")
048        private List<RoleXmlDTO> roles;
049        
050        public RolesXmlDTO() {}
051        
052        public RolesXmlDTO(List<? extends Object> rolesToExport) {
053            this.roles = new RiceXmlExportList<RoleXmlDTO,Object>(rolesToExport, this);
054        }
055        
056        public List<RoleXmlDTO> getRoles() {
057            return roles;
058        }
059    
060        public void setRoles(List<RoleXmlDTO> roles) {
061            this.roles = roles;
062        }
063    
064        void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) {
065            roles = new RiceXmlImportList<RoleXmlDTO>(this);
066        }
067        
068        void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
069            roles = null;
070        }
071        
072        /**
073         * @see org.kuali.rice.core.util.jaxb.RiceXmlListAdditionListener#newItemAdded(java.lang.Object)
074         */
075        public void newItemAdded(RoleXmlDTO item) {
076            // Persist the role if it has not already been persisted yet.
077            if (!item.isAlreadyPersisted()) {
078                try {
079                    RoleXmlUtil.validateAndPersistNewRole(item);
080                } catch (UnmarshalException e) {
081                    throw new RuntimeException(e);
082                }
083            }
084            
085            // If a "roleMembers" element was present, remove any existing roles that do not match the new ones.
086            Set<String> existingRoleMemberIds = item.getExistingRoleMemberIds();
087            if (existingRoleMemberIds != null) {
088                RoleXmlUtil.removeRoleMembers(item.getRoleId(), existingRoleMemberIds);
089            }
090            item.setExistingRoleMemberIds(null);
091        }
092    
093        void afterMarshal(Marshaller marshaller) {
094            roles = null;
095        }
096        
097        /**
098         * @see org.kuali.rice.core.util.jaxb.RiceXmlListGetterListener#gettingNextItem(java.lang.Object, int)
099         */
100        public RoleXmlDTO gettingNextItem(Object nextItem, int index) {
101            if (!(nextItem instanceof RoleContract)) {
102                throw new IllegalStateException("Object for exportation should have been a role");
103            }
104            RoleContract role = ((RoleContract)nextItem);
105            return new RoleXmlDTO(role, new RoleMembersXmlDTO.WithinRole(role.getId()), new RolePermissionsXmlDTO.WithinRole(role.getId()));
106        }
107    }