View Javadoc
1   package org.apache.fulcrum.security.torque.turbine;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  import java.sql.Connection;
21  import java.util.List;
22  import java.util.Set;
23  
24  import org.apache.fulcrum.security.entity.Group;
25  import org.apache.fulcrum.security.model.turbine.entity.TurbineGroup;
26  import org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRole;
27  import org.apache.fulcrum.security.torque.om.TorqueTurbineGroupPeer;
28  import org.apache.fulcrum.security.torque.peer.TorqueTurbinePeer;
29  import org.apache.fulcrum.security.torque.peer.managers.PeerGroupManager;
30  import org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity;
31  import org.apache.fulcrum.security.util.DataBackendException;
32  import org.apache.torque.NoRowsException;
33  import org.apache.torque.TooManyRowsException;
34  import org.apache.torque.TorqueException;
35  import org.apache.torque.criteria.Criteria;
36  import org.apache.torque.util.Transaction;
37  /**
38   * This implementation persists to a database via Torque.
39   *
40   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
41   * @version $Id:$
42   */
43  public class TorqueTurbineGroupManagerImpl extends PeerGroupManager
44  {
45      
46  
47  	/** Serial version */
48  	private static final long serialVersionUID = -5583297428186549693L;
49  
50  	/**
51       * @see org.apache.fulcrum.security.torque.TorqueAbstractGroupManager#doSelectAllGroups(java.sql.Connection)
52       */
53      @Override
54  	@SuppressWarnings("unchecked")
55  	protected <T extends Group> List<T> doSelectAllGroups(Connection con) throws TorqueException
56      {
57          Criteria criteria = new Criteria();
58          
59          if ( (getCustomPeer())) {
60              try
61              {
62              	TorqueTurbinePeer<T> peerInstance = (TorqueTurbinePeer<T>)getPeerInstance();
63                  return peerInstance.doSelect( criteria, con );
64              }
65              catch ( DataBackendException e )
66              {
67                  throw new TorqueException( e );
68              }
69          } else {
70              return (List<T>) TorqueTurbineGroupPeer.doSelect(criteria, con);
71          }
72  
73  
74      }
75  
76      /**
77       * @see org.apache.fulcrum.security.torque.TorqueAbstractGroupManager#doSelectById(java.lang.Integer, java.sql.Connection)
78       */
79      @Override
80  	@SuppressWarnings("unchecked")
81  	protected <T extends Group> T doSelectById(Integer id, Connection con) throws NoRowsException, TooManyRowsException, TorqueException
82      {
83          if ( (getCustomPeer())) {
84              try
85              {
86              	TorqueTurbinePeer<T> peerInstance = (TorqueTurbinePeer<T>)getPeerInstance();
87                  return peerInstance.retrieveByPK( id, con );
88              }
89              catch ( DataBackendException e )
90              {
91                  throw new TorqueException( e );
92              }
93          } else {
94              return  (T)  TorqueTurbineGroupPeer.retrieveByPK(id, con);
95          }
96  
97      }
98  
99      /**
100      * @see org.apache.fulcrum.security.torque.TorqueAbstractGroupManager#doSelectByName(java.lang.String, java.sql.Connection)
101      */
102     @Override
103 	@SuppressWarnings("unchecked")
104 	protected <T extends Group> T doSelectByName(String name, Connection con) throws NoRowsException, TooManyRowsException, TorqueException
105     {
106         Criteria criteria = new Criteria();
107         criteria.setIgnoreCase(true);
108         criteria.setSingleRecord(true);
109         List<T> groups = null;
110         
111         if ( (getCustomPeer())) {
112             try
113             {
114             	TorqueTurbinePeer<T> peerInstance = (TorqueTurbinePeer<T>)getPeerInstance();
115             	
116                 criteria.where(peerInstance.getTableMap().getColumn(getColumnName()), name);
117                 groups = peerInstance.doSelect( criteria, con );
118             }
119             catch ( DataBackendException e )
120             {
121                 throw new TorqueException( e );
122             }
123         } else {
124             criteria.where(TorqueTurbineGroupPeer.GROUP_NAME, name);
125             groups = (List<T>) TorqueTurbineGroupPeer.doSelect(criteria, con);
126         }
127 
128         if (groups.isEmpty())
129         {
130             throw new NoRowsException(name);
131         }
132 
133         return groups.get(0);
134     }
135 
136     
137     public Set<TurbineUserGroupRole> getUserGroupRoleSet(Group group) throws DataBackendException {
138         Connection con = null;
139 
140         if (getLazyLoading()) {
141             try
142             {
143                 con = Transaction.begin();
144                 // Add all dependent objects if they exist
145                 ((TorqueAbstractSecurityEntity)group).retrieveAttachedObjects(con, false);
146     
147                 Transaction.commit(con);
148                 con = null;
149             }
150             catch (TorqueException e)
151             {
152                 throw new DataBackendException("Error retrieving group information", e);
153             }
154             finally
155             {
156                 if (con != null)
157                 {
158                     Transaction.safeRollback(con);
159                 }
160             }
161         } 
162         return ((TurbineGroup)group).getUserGroupRoleSet();
163 
164         
165     }
166   
167 }