KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > security > impl > db > entity > TurbinePermissionPeer


1 package org.apache.fulcrum.security.impl.db.entity;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001-2002 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.util.Enumeration JavaDoc;
58 import java.util.List JavaDoc;
59 import java.util.Vector JavaDoc;
60
61 import org.apache.fulcrum.security.entity.Permission;
62 import org.apache.fulcrum.security.entity.Role;
63 import org.apache.fulcrum.security.entity.SecurityEntity;
64 import org.apache.fulcrum.security.util.DataBackendException;
65 import org.apache.fulcrum.security.util.PermissionSet;
66 import org.apache.torque.om.BaseObject;
67 import org.apache.torque.util.BasePeer;
68 import org.apache.torque.util.Criteria;
69
70 /**
71  * This class handles all the database access for the PERMISSION
72  * table. This table contains all the permissions that are used in
73  * the system.
74  *
75  * @author <a HREF="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
76  * @author <a HREF="mailto:jmcnally@collab.net">John D. McNally</a>
77  * @author <a HREF="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
78  * @author <a HREF="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
79  * @version $Id: TurbinePermissionPeer.java,v 1.1 2004/11/12 10:25:43 epugh Exp $
80  */

81 public class TurbinePermissionPeer
82     extends org.apache.fulcrum.security.impl.db.entity.BaseTurbinePermissionPeer
83 {
84     /** The column name for the name field. */
85     public static final String JavaDoc NAME = PERMISSION_NAME;
86
87     /**
88      * Checks if a Permission is defined in the system. The name
89      * is used as query criteria.
90      *
91      * @param permission The Permission to be checked.
92      * @return <code>true</code> if given Permission exists in the system.
93      * @throws DataBackendException when more than one Permission with
94      * the same name exists.
95      * @throws Exception, a generic exception.
96      */

97     public static boolean checkExists( Permission permission )
98         throws DataBackendException, Exception JavaDoc
99     {
100         Criteria criteria = new Criteria();
101         criteria.addSelectColumn(PERMISSION_ID);
102         criteria.add(NAME, ((SecurityEntity)permission).getName());
103         List JavaDoc results = BasePeer.doSelect(criteria);
104         if (results.size() > 1)
105         {
106             throw new DataBackendException("Multiple permissions named '" +
107                 ((SecurityEntity)permission).getName() + "' exist!");
108         }
109         return (results.size()==1);
110     }
111
112     /**
113      * Returns the full name of a column.
114      *
115      * @return A String with the full name of the column.
116      */

117     public static String JavaDoc getColumnName (String JavaDoc name)
118     {
119         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
120         sb.append(TurbinePermissionPeer.TABLE_NAME);
121         sb.append(".");
122         sb.append(name);
123         return sb.toString();
124     }
125
126     /**
127      * Get the name of this table.
128      *
129      * @return A String with the name of the table.
130      */

131     public static String JavaDoc getTableName()
132     {
133         return TABLE_NAME;
134     }
135
136     /**
137      * Retrieves/assembles a PermissionSet
138      *
139      * @param criteria The criteria to use.
140      * @return A PermissionSet.
141      * @exception Exception, a generic exception.
142      */

143     public static PermissionSet retrieveSet(Criteria criteria)
144         throws Exception JavaDoc
145     {
146         List JavaDoc results = doSelect(criteria);
147         PermissionSet ps = new PermissionSet();
148         for (int i=0; i<results.size(); i++)
149         {
150             ps.add( (Permission)results.get(i) );
151         }
152         return ps;
153     }
154
155     /**
156      * Retrieves a set of Permissions associated with a particular Role.
157      *
158      * @param role The role to query permissions of.
159      * @return A set of permissions associated with the Role.
160      * @exception Exception, a generic exception.
161      */

162     public static PermissionSet retrieveSet( Role role )
163         throws Exception JavaDoc
164     {
165         Criteria criteria = new Criteria();
166         criteria.add(TurbineRolePermissionPeer.ROLE_ID,
167                      ((TurbineRole)role).getPrimaryKey());
168         criteria.addJoin(TurbineRolePermissionPeer.PERMISSION_ID,
169                          TurbinePermissionPeer.PERMISSION_ID);
170         return retrieveSet(criteria);
171     }
172
173     /**
174      * Pass in two Vector's of Permission Objects. It will return a
175      * new Vector with the difference of the two Vectors: C = (A - B).
176      *
177      * @param some Vector B in C = (A - B).
178      * @param all Vector A in C = (A - B).
179      * @return Vector C in C = (A - B).
180      */

181     public static final Vector JavaDoc getDifference(Vector JavaDoc some, Vector JavaDoc all)
182     {
183         Vector JavaDoc clone = (Vector JavaDoc)all.clone();
184         for (Enumeration JavaDoc e = some.elements() ; e.hasMoreElements() ;)
185         {
186             Permission tmp = (Permission) e.nextElement();
187             for (Enumeration JavaDoc f = clone.elements() ; f.hasMoreElements() ;)
188             {
189                 Permission tmp2 = (Permission) f.nextElement();
190                 if (((BaseObject)tmp).getPrimaryKey() ==
191                     ((BaseObject)tmp2).getPrimaryKey())
192                 {
193                     clone.removeElement(tmp2);
194                     break;
195                 }
196             }
197         }
198         return clone;
199     }
200
201 }
202
Popular Tags