001package org.apache.fulcrum.security.torque.security.turbine;
002/*
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 */
020import java.util.HashSet;
021import java.util.Set;
022
023import org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRole;
024import org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRoleEntity;
025import org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity;
026import org.apache.fulcrum.security.util.DataBackendException;
027/**
028 * This abstract class provides the SecurityInterface to the managers.
029 * 
030 * Additional Torque contract
031 *
032 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
033 * @version $Id:$
034 */
035public abstract class TorqueAbstractTurbineTurbineSecurityEntity extends TorqueAbstractSecurityEntity
036    implements TurbineUserGroupRoleEntity // not in group and role but already in TurbineUser interface 
037{
038    /** Serial version */
039        private static final long serialVersionUID = -6230312046016785990L;
040
041        /** a cache of user_group_role objects */
042    private Set<? extends TurbineUserGroupRole> userGroupRoleSet = null;
043    
044    /* (non-Javadoc)
045     * @see org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRoleEntity#addUserGroupRole(org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRole)
046     */
047    public void addUserGroupRole(TurbineUserGroupRole userGroupRole) throws DataBackendException
048    {
049        getUserGroupRoleSet().add(userGroupRole);
050    }
051    
052    /**
053     * @param user_group_role u/g/r to add
054     * @param isLazilyLoaded <code>true</code> for lazy loading
055     * @throws DataBackendException if the database backend is not found
056     */
057    public void addUserGroupRole( TurbineUserGroupRole user_group_role, boolean isLazilyLoaded ) throws DataBackendException {
058        if (isLazilyLoaded) 
059        {
060            getDefaultUserGroupRoleSet().add( user_group_role ); 
061        } 
062        else  
063        { 
064            getUserGroupRoleSet().add( user_group_role );
065        }
066    }
067    
068    /**
069     * @return get default u/g/r set
070     * @throws DataBackendException if the database backend is not found
071     */
072    @SuppressWarnings("unchecked")
073    private <T extends TurbineUserGroupRole> Set<T> getDefaultUserGroupRoleSet() throws DataBackendException
074    {
075        if (userGroupRoleSet == null)
076        {
077            userGroupRoleSet = new HashSet<TurbineUserGroupRole>();
078        }
079
080        return (Set<T>) userGroupRoleSet;
081    }
082
083        /* (non-Javadoc)
084         * @see org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRoleEntity#getUserGroupRoleSet()
085         * @throws DataBackendException if loaded lazily
086         */
087        public <T extends TurbineUserGroupRole> Set<T> getUserGroupRoleSet() throws DataBackendException
088    {
089        return getDefaultUserGroupRoleSet();
090    }
091
092    /* (non-Javadoc)
093     * @see org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRoleEntity#removeUserGroupRole(org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRole)
094     */
095    public void removeUserGroupRole(TurbineUserGroupRole userGroupRole) throws DataBackendException
096    {
097        getUserGroupRoleSet().remove(userGroupRole);
098    }
099    
100    /**
101     * @param user_group_role u/g/r to remove
102     * @param isLazilyLoaded <code>true</code> for lazy loading
103     * @throws DataBackendException if the database backend is not found
104     */
105    public void removeUserGroupRole( TurbineUserGroupRole user_group_role, boolean isLazilyLoaded ) throws DataBackendException {
106        if (isLazilyLoaded) 
107        {
108            getDefaultUserGroupRoleSet().remove( user_group_role ); 
109        } 
110        else  
111        { 
112            getUserGroupRoleSet().remove( user_group_role );
113        }
114    }
115
116    /* (non-Javadoc)
117     * @see org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRoleEntity#setUserGroupRoleSet(java.util.Set)
118     */
119    public <T extends TurbineUserGroupRole> void setUserGroupRoleSet(Set<T> userGroupRoleSet)
120    {
121        if (userGroupRoleSet != null)
122        {
123            this.userGroupRoleSet = userGroupRoleSet;
124        }
125        else
126        {
127            this.userGroupRoleSet = new HashSet<TurbineUserGroupRole>();
128        }
129    }
130
131}