001package org.apache.fulcrum.security.torque.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.sql.Connection;
021import java.util.HashSet;
022import java.util.List;
023import java.util.Set;
024
025import org.apache.fulcrum.security.entity.User;
026import org.apache.fulcrum.security.model.turbine.entity.TurbineUser;
027import org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRole;
028import org.apache.fulcrum.security.torque.om.TurbineUserGroupRolePeer;
029import org.apache.fulcrum.security.torque.om.TurbineUserPeer;
030import org.apache.fulcrum.security.torque.peer.TurbineUserGroupRoleModelPeerMapper;
031import org.apache.fulcrum.security.torque.security.turbine.TorqueAbstractTurbineTurbineSecurityEntityDefault;
032import org.apache.fulcrum.security.util.DataBackendException;
033import org.apache.torque.TorqueException;
034import org.apache.torque.criteria.Criteria;
035import org.apache.torque.om.SimpleKey;
036
037/**
038 * This abstract class provides the SecurityInterface to the managers.
039 * 
040 * An implementing class is required to implement {@link User} at least. Most probably the OM classes allow to implement {@link TurbineUser}, which includes User.
041 *
042 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
043 * @version $Id:$
044 */
045public abstract class DefaultAbstractTurbineUser extends TorqueAbstractTurbineTurbineSecurityEntityDefault
046{
047    /** Serial version */
048        private static final long serialVersionUID = -7255623655281852566L;
049    
050    /**
051     * Forward reference to module generated code
052     *
053     * Get a list of association objects, pre-populated with their TurbineGroup
054     * objects.
055     * 
056     * Does intentionally not initialize the cache collTurbineUserGroupRoles for referenced objects. 
057     * 
058     * Be careful not to call any of the generated getTurbineUserGroupRoles methods in derived classes,
059     * the link {@link #save()} method otherwise might not update the right relationships.
060     *
061     * @param criteria Criteria to define the selection of records
062     * @param con a database connection
063     * @throws DataBackendException  
064     *
065     * @return a list of User/Group/Role relations
066     * @throws TorqueException if any database error occurs
067     */
068    protected <T extends TurbineUserGroupRoleModelPeerMapper> List<T> getTurbineUserGroupRolesJoinTurbineGroup(Criteria criteria, Connection con)
069        throws TorqueException, DataBackendException
070    {
071        criteria.and(TurbineUserGroupRolePeer.USER_ID, getEntityId() );
072        try {
073            return (List<T>) TurbineUserGroupRolePeer.doSelectJoinTurbineGroup(criteria, con);
074        } catch (  TorqueException e) {
075            throw new DataBackendException( e.getMessage(), e );
076        }
077    }
078    
079    /** 
080     * @see org.apache.fulcrum.security.torque.security.turbine.TorqueAbstractTurbineTurbineSecurityEntityDefault#retrieveAttachedObjects(java.sql.Connection, java.lang.Boolean, java.util.List)
081     */
082    @Override
083    public <T extends TurbineUserGroupRoleModelPeerMapper> void retrieveAttachedObjects( Connection con, Boolean lazy, List<T> ugrs ) throws DataBackendException, TorqueException
084    {
085        if (!lazy ) { 
086            Set<TurbineUserGroupRole> userGroupRoleSet = new HashSet<TurbineUserGroupRole>();
087    
088            if (ugrs == null) { // default 
089                // the groups are the keys in roleset, roles are not expected to be used as keys
090                ugrs = getTurbineUserGroupRolesJoinTurbineGroup(new Criteria(), con);
091            } 
092    
093            maptoModel( con, userGroupRoleSet, ugrs );
094            
095            setUserGroupRoleSet(userGroupRoleSet);
096        }
097    }
098    
099    /* (non-Javadoc)
100     * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#retrieveAttachedObjects(java.sql.Connection, java.lang.Boolean)
101     */
102    @Override
103    public void retrieveAttachedObjects( Connection con, Boolean lazy ) throws TorqueException, DataBackendException
104    {
105        if (!lazy) {
106            Set<TurbineUserGroupRole> userGroupRoleSet = new HashSet<TurbineUserGroupRole>();
107            
108            List<TurbineUserGroupRoleModelPeerMapper> ugrs = getTurbineUserGroupRolesJoinTurbineGroup(new Criteria(), con);
109    
110            // TODO: Need to call this too to fetch right role object? getTurbineUserGroupRolesJoinTurbineRole
111            // org.apache.fulcrum.security.torque.om.TurbineUserGroupRole
112            maptoModel( con, userGroupRoleSet, ugrs );
113    
114            setUserGroupRoleSet(userGroupRoleSet);
115        }
116    }
117
118    /* (non-Javadoc)
119     * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#retrieveAttachedObjects(java.sql.Connection)
120     */
121    @Override
122    public void retrieveAttachedObjects( Connection con )
123        throws DataBackendException, TorqueException
124    {
125        retrieveAttachedObjects( con, false );
126    }
127
128    /* (non-Javadoc)
129     * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#update(java.sql.Connection)
130     */
131    @Override
132        public void update(Connection con) throws TorqueException
133    {
134        try
135        {
136            Set<TurbineUserGroupRole> userGroupRoleSet = getUserGroupRoleSet();
137            if ( userGroupRoleSet != null ) //&& !userGroupRoleSet.isEmpty() commented allow delete/empty 
138            {
139                Criteria criteria = new Criteria();
140
141                /* remove old entries */
142                criteria.where(TurbineUserGroupRolePeer.USER_ID, getEntityId());
143                TurbineUserGroupRolePeer.doDelete(criteria, con);
144
145                for (TurbineUserGroupRole ugr : userGroupRoleSet)
146                {
147                    org.apache.fulcrum.security.torque.om.TurbineUserGroupRole ttugr = new org.apache.fulcrum.security.torque.om.TurbineUserGroupRole();
148                    ttugr.setGroupId((Integer)ugr.getGroup().getId());
149                    ttugr.setUserId((Integer)ugr.getUser().getId());
150                    ttugr.setRoleId((Integer)ugr.getRole().getId());
151                    ttugr.save(con);
152                }
153            }
154            save(con);
155        }
156        catch (Exception e)
157        {
158            throw new TorqueException(e);
159        }
160    }
161
162    /* (non-Javadoc)
163     * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#delete()
164     */
165    @Override
166        public void delete() throws TorqueException
167    {
168        TurbineUserPeer.doDelete(SimpleKey.keyFor(getEntityId()));
169    }
170    
171    /**
172     * @param con data connection
173     * @param userGroupRoleSet U/G/R to be set / target object
174     * @param ugrs list of all ugrs
175     * @throws TorqueException if data connection could not be found
176     */
177    private <T extends TurbineUserGroupRoleModelPeerMapper> void maptoModel( Connection con, 
178            Set<TurbineUserGroupRole> userGroupRoleSet,
179                             List<T> ugrs )
180        throws DataBackendException
181    {
182        try {
183            for (TurbineUserGroupRoleModelPeerMapper ttugr : ugrs)
184            {
185                TurbineUserGroupRole ugr = new TurbineUserGroupRole();
186                ugr.setUser((User) this);
187                ugr.setRole(ttugr.getTurbineRole(con));
188                
189                // org.apache.fulcrum.security.torque.om.TurbineGroup implements 
190                // org.apache.fulcrum.security.model.turbine.entity.TurbineGroup
191                // but may be hides it? 
192                ugr.setGroup(ttugr.getTurbineGroup(con));
193                userGroupRoleSet.add(ugr);
194            }
195        } catch (TorqueException e ) {
196            throw new DataBackendException( e.getMessage(),e );
197        }
198    }
199}