001package org.apache.fulcrum.security.torque.basic;
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.Group;
025import org.apache.fulcrum.security.model.basic.entity.BasicUser;
026import org.apache.fulcrum.security.torque.om.TorqueBasicUserGroup;
027import org.apache.fulcrum.security.torque.om.TorqueBasicUserGroupPeer;
028import org.apache.fulcrum.security.torque.om.TorqueBasicUserPeer;
029import org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity;
030import org.apache.fulcrum.security.util.DataBackendException;
031import org.apache.fulcrum.security.util.GroupSet;
032import org.apache.torque.TorqueException;
033import org.apache.torque.criteria.Criteria;
034import org.apache.torque.om.SimpleKey;
035
036/**
037 * This abstract class provides the SecurityInterface to the managers.
038 *
039 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
040 * @version $Id:$
041 */
042public abstract class TorqueAbstractBasicUser extends TorqueAbstractSecurityEntity
043    implements BasicUser
044{
045    /** Serial version */
046        private static final long serialVersionUID = 7669398253522416329L;
047        /** a cache of group objects */
048    private Set<Group> groupSet = null;
049
050    /**
051     * Forward reference to generated code
052     *
053     * Get a list of association objects, pre-populated with their TorqueBasicGroup
054     * objects.
055     *
056     * @param criteria Criteria to define the selection of records
057     * @param con a database connection
058     * @throws TorqueException  if any database error occurs
059     *
060     * @return a list of User/Group relations
061     */
062    protected List<TorqueBasicUserGroup> getTorqueBasicUserGroupsJoinTorqueBasicGroup(Criteria criteria, Connection con)
063        throws TorqueException
064    {
065        criteria.and(TorqueBasicUserGroupPeer.USER_ID, getEntityId() );
066        return TorqueBasicUserGroupPeer.doSelectJoinTorqueBasicGroup(criteria, con);
067    }
068
069    /**
070     * @see org.apache.fulcrum.security.model.basic.entity.BasicUser#addGroup(org.apache.fulcrum.security.entity.Group)
071     */
072    public void addGroup(Group group)
073    {
074        getGroups().add(group);
075    }
076
077    /**
078     * @see org.apache.fulcrum.security.model.basic.entity.BasicUser#getGroups()
079     */
080    public GroupSet getGroups()
081    {
082        if (groupSet == null)
083        {
084            groupSet = new GroupSet();
085        }
086        else if(!(groupSet instanceof GroupSet))
087        {
088            groupSet = new GroupSet(groupSet);
089        }
090
091        return (GroupSet)groupSet;
092    }
093
094    /**
095     * @see org.apache.fulcrum.security.model.basic.entity.BasicUser#getGroupsAsSet()
096     */
097    @SuppressWarnings("unchecked")
098        public <T extends Group> Set<T> getGroupsAsSet()
099    {
100        return (Set<T>)groupSet;
101    }
102
103    /**
104     * @see org.apache.fulcrum.security.model.basic.entity.BasicUser#removeGroup(org.apache.fulcrum.security.entity.Group)
105     */
106    public void removeGroup(Group group)
107    {
108        getGroups().remove(group);
109    }
110
111    /**
112     * @see org.apache.fulcrum.security.model.basic.entity.BasicUser#setGroups(org.apache.fulcrum.security.util.GroupSet)
113     */
114    public void setGroups(GroupSet groups)
115    {
116        if(groups != null)
117        {
118            this.groupSet = groups;
119        }
120        else
121        {
122            this.groupSet = new GroupSet();
123        }
124    }
125
126    /**
127     * @see org.apache.fulcrum.security.model.basic.entity.BasicUser#setGroupsAsSet(java.util.Set)
128     */
129    public <T extends Group> void setGroupsAsSet(Set<T> groups)
130    {
131        setGroups(new GroupSet(groups));
132    }
133    /**
134     * Retrieve attached objects such as users, permissions,....
135     */
136    @Override
137    public void retrieveAttachedObjects( Connection con )
138        throws DataBackendException
139    {
140        retrieveAttachedObjects( con, false );
141    }
142
143    /**
144     * Retrieve attached objects such as users, permissions,....
145     */
146    @Override
147    public void retrieveAttachedObjects( Connection con, Boolean lazy )
148        throws DataBackendException
149    {
150        this.groupSet = new GroupSet();
151
152        try {
153            List<TorqueBasicUserGroup> usergroups = getTorqueBasicUserGroupsJoinTorqueBasicGroup(new Criteria(), con);
154    
155            for (TorqueBasicUserGroup tbug : usergroups)
156            {
157                groupSet.add(tbug.getTorqueBasicGroup());
158            }
159        } catch (TorqueException e ) {
160            throw new DataBackendException( e.getMessage(),e );
161        }
162    }
163
164    /**
165     * Update this instance to the database with all dependent objects
166     *
167     * @param con A database connection
168     */
169    public void update(Connection con) throws TorqueException
170    {
171        if (groupSet != null)
172        {
173            Criteria criteria = new Criteria();
174
175            /* remove old entries */
176            criteria.where(TorqueBasicUserGroupPeer.USER_ID, getEntityId());
177            TorqueBasicUserGroupPeer.doDelete(criteria, con);
178
179            for (Group g : groupSet)
180            {
181                TorqueBasicUserGroup ug = new TorqueBasicUserGroup();
182                ug.setUserId(getEntityId());
183                ug.setGroupId((Integer)g.getId());
184                ug.save(con);
185            }
186        }
187
188        try
189        {
190            save(con);
191        }
192        catch (Exception e)
193        {
194            throw new TorqueException(e);
195        }
196    }
197
198    /**
199     * Get the name of the connnection pool associated to this object
200     *
201     * @return the logical Torque database name
202     */
203    public String getDatabaseName()
204    {
205        return TorqueBasicUserPeer.DATABASE_NAME;
206    }
207
208    /**
209     * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#delete()
210     */
211    public void delete() throws TorqueException
212    {
213        TorqueBasicUserPeer.doDelete(SimpleKey.keyFor(getEntityId()));
214    }
215}