001package org.apache.fulcrum.security.torque.dynamic;
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.sql.Connection;
021import java.util.List;
022import java.util.Set;
023
024import org.apache.fulcrum.security.entity.Role;
025import org.apache.fulcrum.security.model.dynamic.entity.DynamicPermission;
026import org.apache.fulcrum.security.torque.om.TorqueDynamicPermissionPeer;
027import org.apache.fulcrum.security.torque.om.TorqueDynamicRolePermission;
028import org.apache.fulcrum.security.torque.om.TorqueDynamicRolePermissionPeer;
029import org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity;
030import org.apache.fulcrum.security.util.DataBackendException;
031import org.apache.fulcrum.security.util.RoleSet;
032import org.apache.torque.TorqueException;
033import org.apache.torque.criteria.Criteria;
034import org.apache.torque.om.SimpleKey;
035/**
036 * This abstract class provides the SecurityInterface to the managers.
037 *
038 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
039 * @version $Id:$
040 */
041public abstract class TorqueAbstractDynamicPermission extends TorqueAbstractSecurityEntity
042    implements DynamicPermission
043{
044    /** Serial version */
045        private static final long serialVersionUID = -6857144824327889029L;
046        /** a cache of role objects */
047    private Set<Role> roleSet = null;
048
049    /**
050     * Forward reference to generated code
051     *
052     * Get a list of association objects, pre-populated with their TorqueDynamicRole
053     * objects.
054     *
055     * @param criteria Criteria to define the selection of records
056     * @param con a database connection
057     * @throws TorqueException  if any database error occurs
058     *
059     * @return a list of Role/Permission relations
060     */
061    protected List<TorqueDynamicRolePermission> getTorqueDynamicRolePermissionsJoinTorqueDynamicRole(Criteria criteria, Connection con)
062        throws TorqueException
063    {
064        criteria.and(TorqueDynamicRolePermissionPeer.PERMISSION_ID, getEntityId() );
065        return TorqueDynamicRolePermissionPeer.doSelectJoinTorqueDynamicRole(criteria, con);
066    }
067
068    /**
069     * @see org.apache.fulcrum.security.model.dynamic.entity.DynamicPermission#addRole(org.apache.fulcrum.security.entity.Role)
070     */
071    public void addRole(Role role)
072    {
073        getRoles().add(role);
074    }
075
076    /**
077     * @see org.apache.fulcrum.security.model.dynamic.entity.DynamicPermission#getRoles()
078     */
079    public RoleSet getRoles()
080    {
081        if (roleSet == null)
082        {
083            roleSet = new RoleSet();
084        }
085        else if(!(roleSet instanceof RoleSet))
086        {
087            roleSet = new RoleSet(roleSet);
088        }
089
090        return (RoleSet)roleSet;
091    }
092
093    /**
094     * @see org.apache.fulcrum.security.model.dynamic.entity.DynamicPermission#getRolesAsSet()
095     */
096    @SuppressWarnings("unchecked")
097        public <T extends Role> Set<T> getRolesAsSet()
098    {
099        return (Set<T>)roleSet;
100    }
101
102    /**
103     * @see org.apache.fulcrum.security.model.dynamic.entity.DynamicPermission#removeRole(org.apache.fulcrum.security.entity.Role)
104     */
105    public void removeRole(Role role)
106    {
107        getRoles().remove(role);
108    }
109
110    /**
111     * @see org.apache.fulcrum.security.model.dynamic.entity.DynamicPermission#setRoles(org.apache.fulcrum.security.util.RoleSet)
112     */
113    public void setRoles(RoleSet roleSet)
114    {
115        if (roleSet != null)
116        {
117            this.roleSet = roleSet;
118        }
119        else
120        {
121            this.roleSet = new RoleSet();
122        }
123    }
124
125    /**
126     * @see org.apache.fulcrum.security.model.dynamic.entity.DynamicPermission#setRolesAsSet(java.util.Set)
127     */
128    public <T extends Role> void setRolesAsSet(Set<T> roles)
129    {
130        setRoles(new RoleSet(roles));
131    }
132
133    /**
134     * @return the database name
135     */
136    public String getDatabaseName()
137    {
138        return TorqueDynamicPermissionPeer.DATABASE_NAME;
139    }
140    
141    @Override
142    public void retrieveAttachedObjects( Connection con )
143        throws DataBackendException
144    {
145        retrieveAttachedObjects( con, false );
146    }
147
148    /**
149     * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#retrieveAttachedObjects(Connection, Boolean)
150     */
151    @Override
152    public void retrieveAttachedObjects( Connection con, Boolean lazy )
153        throws DataBackendException
154    {
155        this.roleSet = new RoleSet();
156
157        try {
158            List<TorqueDynamicRolePermission> rolepermissions = getTorqueDynamicRolePermissionsJoinTorqueDynamicRole(new Criteria(), con);
159    
160            for (TorqueDynamicRolePermission tdrp : rolepermissions)
161            {
162                roleSet.add(tdrp.getTorqueDynamicRole());
163            }
164        } catch (TorqueException e ) {
165            throw new DataBackendException( e.getMessage(),e );
166        }
167    }
168
169    /**
170     * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#update(java.sql.Connection)
171     */
172    public void update(Connection con) throws TorqueException
173    {
174        if (roleSet != null)
175        {
176            Criteria criteria = new Criteria();
177
178            /* remove old entries */
179            criteria.where(TorqueDynamicRolePermissionPeer.PERMISSION_ID, getEntityId());
180            TorqueDynamicRolePermissionPeer.doDelete(criteria, con);
181
182            for (Role r : roleSet)
183            {
184                TorqueDynamicRolePermission rp = new TorqueDynamicRolePermission();
185                rp.setRoleId((Integer)r.getId());
186                rp.setPermissionId(getEntityId());
187                rp.save(con);
188            }
189        }
190
191        try
192        {
193            save(con);
194        }
195        catch (Exception e)
196        {
197            throw new TorqueException(e);
198        }
199    }
200
201    /**
202     * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#delete()
203     */
204    public void delete() throws TorqueException
205    {
206        TorqueDynamicPermissionPeer.doDelete(SimpleKey.keyFor(getEntityId()));
207    }
208}