001/*
002 * The Kuali Financial System, a comprehensive financial management system for higher education.
003 * 
004 * Copyright 2005-2014 The Kuali Foundation
005 * 
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Affero General Public License as
008 * published by the Free Software Foundation, either version 3 of the
009 * License, or (at your option) any later version.
010 * 
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Affero General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Affero General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.kuali.rice.kim.impl.jaxb;
020
021import java.io.Serializable;
022import java.util.List;
023import java.util.Set;
024
025import javax.xml.bind.Marshaller;
026import javax.xml.bind.UnmarshalException;
027import javax.xml.bind.Unmarshaller;
028import javax.xml.bind.annotation.XmlAccessType;
029import javax.xml.bind.annotation.XmlAccessorType;
030import javax.xml.bind.annotation.XmlElement;
031import javax.xml.bind.annotation.XmlType;
032
033import org.kuali.rice.core.util.jaxb.RiceXmlExportList;
034import org.kuali.rice.core.util.jaxb.RiceXmlImportList;
035import org.kuali.rice.core.util.jaxb.RiceXmlListAdditionListener;
036import org.kuali.rice.core.util.jaxb.RiceXmlListGetterListener;
037import org.kuali.rice.kim.api.role.RoleContract;
038
039/**
040 * This class represents a &lt;roles&gt; element. 
041 * 
042 * @author Kuali Rice Team (rice.collab@kuali.org)
043 */
044@XmlAccessorType(XmlAccessType.FIELD)
045@XmlType(name="RolesType", propOrder={"roles"})
046public class RolesXmlDTO implements RiceXmlListAdditionListener<RoleXmlDTO>, RiceXmlListGetterListener<RoleXmlDTO,Object>, Serializable {
047    
048    private static final long serialVersionUID = 1L;
049    
050    @XmlElement(name="role")
051    private List<RoleXmlDTO> roles;
052    
053    public RolesXmlDTO() {}
054    
055    public RolesXmlDTO(List<? extends Object> rolesToExport) {
056        this.roles = new RiceXmlExportList<RoleXmlDTO,Object>(rolesToExport, this);
057    }
058    
059    public List<RoleXmlDTO> getRoles() {
060        return roles;
061    }
062
063    public void setRoles(List<RoleXmlDTO> roles) {
064        this.roles = roles;
065    }
066
067    void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) {
068        roles = new RiceXmlImportList<RoleXmlDTO>(this);
069    }
070    
071    void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
072        roles = null;
073    }
074    
075    /**
076     * @see org.kuali.rice.core.util.jaxb.RiceXmlListAdditionListener#newItemAdded(java.lang.Object)
077     */
078    public void newItemAdded(RoleXmlDTO item) {
079        // Persist the role if it has not already been persisted yet.
080        if (!item.isAlreadyPersisted()) {
081            try {
082                RoleXmlUtil.validateAndPersistNewRole(item);
083            } catch (UnmarshalException e) {
084                throw new RuntimeException(e);
085            }
086        }
087        
088        // If a "roleMembers" element was present, remove any existing roles that do not match the new ones.
089        Set<String> existingRoleMemberIds = item.getExistingRoleMemberIds();
090        if (existingRoleMemberIds != null) {
091            RoleXmlUtil.removeRoleMembers(item.getRoleId(), existingRoleMemberIds);
092        }
093        item.setExistingRoleMemberIds(null);
094    }
095
096    void afterMarshal(Marshaller marshaller) {
097        roles = null;
098    }
099    
100    /**
101     * @see org.kuali.rice.core.util.jaxb.RiceXmlListGetterListener#gettingNextItem(java.lang.Object, int)
102     */
103    public RoleXmlDTO gettingNextItem(Object nextItem, int index) {
104        if (!(nextItem instanceof RoleContract)) {
105            throw new IllegalStateException("Object for exportation should have been a role");
106        }
107        RoleContract role = ((RoleContract)nextItem);
108        return new RoleXmlDTO(role, new RoleMembersXmlDTO.WithinRole(role.getId()), new RolePermissionsXmlDTO.WithinRole(role.getId()));
109    }
110}