KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > dbobj > RowGroupPerms


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.services.dbobj;
66
67 import com.jcorporate.expresso.core.db.DBException;
68 import com.jcorporate.expresso.core.dbobj.DBField;
69 import com.jcorporate.expresso.core.dbobj.DBObject;
70
71 /**
72  * storage for read/write permissions for a group, concerning a given row in the database
73  * this object and its table are a complement to the RowPermissions object and table.
74  * RowGroupPerms holds group permissions.
75  * <p/>
76  * this object and its table should be manipulated only through RowSecuredDBObject
77  *
78  * @author larry hamel
79  * @see com.jcorporate.expresso.core.dbobj.RowSecuredDBObject
80  * @see com.jcorporate.expresso.services.dbobj.RowPermissions
81  */

82 public class RowGroupPerms extends DBObject /* ironically, we do not subclass SecuredDBObject because anybody must be able to write their own permissions */ {
83
84     public static final String JavaDoc GROUP_PERMISSIONS_TABLE_NAME = "ROW_GRP_PERMS";
85     /**
86      * field name for name of table
87      */

88     public static final String JavaDoc TABLE_NAME = "TARGET_TABLE";
89
90     /**
91      * field name for primary key of row
92      */

93     public static final String JavaDoc ROW_KEY = "ROW_KEY";
94
95
96     /**
97      * field name for the group about which "group privilege settings" (read/write) apply
98      */

99     public static final String JavaDoc GROUP = "PERM_GROUP";
100
101
102     /**
103      * field name for bits that make up permissions; perms stored in this table, ROW_GRP_PERMS, have bits for owner and other
104      * which are unused. Conversely, perms stored in table ROW_PERMISSIONS have bits for group permissions that are unused.
105      */

106     public static final String JavaDoc PERMISSIONS = "PERMISSIONS";
107
108     /**
109      * default constructor
110      */

111     public RowGroupPerms() throws DBException {
112     }
113
114     /**
115      * convenience constructor which sets table, key
116      * @param table tablename
117      * @param rowKey pipe-delimited concatenation of all necessary values for keys
118      */

119     public RowGroupPerms(String JavaDoc table, String JavaDoc rowKey) throws DBException {
120         // test for key length being too large
121
if (rowKey == null) {
122             throw new DBException("null row key");
123         }
124         if (table == null) {
125             throw new DBException("null table name");
126         }
127
128         if ((rowKey.length() + table.length()) > RowPermissions.sMaxKeyLen) {
129             throw new DBException("Cannot create row permissions for table: "
130                     + this.getJDBCMetaData().getTargetTable() + " row: " + rowKey
131                     + " because table name + row's ID (PK) exceeds maximum of " + RowPermissions.sMaxKeyLen);
132         }
133
134         setField(TABLE_NAME, table);
135         setField(ROW_KEY, rowKey);
136     }
137
138     /**
139      * convenience constructor which sets table, key, group
140      * @param table tablename
141      * @param rowKey pipe-delimited concatenation of all necessary values for keys
142      * @param grp name of group
143      */

144     public RowGroupPerms(String JavaDoc table, String JavaDoc rowKey, String JavaDoc grp) throws DBException {
145         // test for key length being too large
146
if (rowKey == null) {
147             throw new DBException("null row key");
148         }
149         if (table == null) {
150             throw new DBException("null table name");
151         }
152
153         if (grp == null) {
154             throw new DBException("null grp name");
155         }
156
157         if ((rowKey.length() + table.length() + grp.length()) > RowPermissions.sMaxKeyLen) {
158             throw new DBException("Cannot create row permissions for table: "
159                     + this.getJDBCMetaData().getTargetTable() + " row: " + rowKey
160                     + " because table name + row's ID (PK) exceeds maximum of " + RowPermissions.sMaxKeyLen);
161         }
162
163         setField(TABLE_NAME, table);
164         setField(ROW_KEY, rowKey);
165         setField(GROUP, grp);
166     }
167
168     /**
169      * convenience copy constructor
170      * @param model an object from which all attributes will be copies (table, group, perms)
171      */

172     public RowGroupPerms(RowGroupPerms model) throws DBException {
173         this(model.table(), model.getKey());
174         group(model.group());
175         permissions(model.permissions());
176     }
177
178     /**
179      * @return true if this group can administrate
180      */

181     public boolean canGroupAdministrate() throws DBException {
182         return (RowPermissions.GROUP_PERMISSION_MASK & permissions()) == RowPermissions.GROUP_PERMISSION_MASK;
183     }
184
185     /**
186      * @return true if this group can read
187      */

188     public boolean canGroupRead() throws DBException {
189         return (RowPermissions.GROUP_READ_MASK & permissions()) == RowPermissions.GROUP_READ_MASK;
190     }
191
192     /**
193      * @return true if this group can write
194      */

195     public boolean canGroupWrite() throws DBException {
196         return (RowPermissions.GROUP_WRITE_MASK & permissions()) == RowPermissions.GROUP_WRITE_MASK;
197     }
198
199
200     /**
201      * override in subclesses, and be sure to call this as first line of override
202      *
203      * @throws DBException upon error
204      */

205     protected synchronized void setupFields() throws DBException {
206         setTargetTable(GROUP_PERMISSIONS_TABLE_NAME);
207         setDescription("RowGroupPermissons");
208         addField(TABLE_NAME, DBField.VARCHAR_TYPE,
209                 RowPermissions.MAX_TABLE_NAME_LENGTH, false, "Targettablename");
210         /**
211          * @todo should ROW_KEY be longvarchar? is that indexable on all databases?
212          */

213         addField(ROW_KEY, RowPermissions.sKeyType, RowPermissions.sMaxKeyLen, false, "Rowkey");
214         addField(GROUP, DBField.CHAR_TYPE, UserGroup.GROUP_NAME_MAX_LEN, false, "Group");
215         addField(PERMISSIONS, DBField.INT_TYPE, 0, true, "Permissionbits");
216
217         addKey(TABLE_NAME);
218         addKey(ROW_KEY);
219         addKey(GROUP);
220
221         addIndex("tablerow", TABLE_NAME + "," + ROW_KEY, false);
222     }
223
224
225     /**
226      * get group name
227      */

228     public String JavaDoc group() throws DBException {
229         return this.getField(GROUP);
230     }
231
232     /**
233      * Set group;
234      * will throw run-time exception if the entire key for this row,
235      * which includes tablename, row key, and group name,
236      * is longer than permitted maximum for the host database.
237      *
238      * @param group the group string?
239      * @throws DBException upon error
240      */

241     public void group(String JavaDoc group) throws DBException {
242         if (group == null) {
243             throw new DBException("null group name");
244         }
245
246         String JavaDoc tablename = getField(TABLE_NAME);
247         String JavaDoc rowKey = getField(ROW_KEY);
248         if (tablename.length() + rowKey.length() + group.length() > RowPermissions.sMaxKeyLen) {
249             throw new DBException("Cannot create group row permissions for table: "
250                     + tablename + ", row: " + rowKey
251                     + ", group: " + group
252                     + " because table name + rowKey + group exceeds maximum of " + RowPermissions.sMaxKeyLen);
253         }
254         setField(GROUP, group);
255     }
256
257     /**
258      * set group group is "Everybody"
259      * just sets fields--does not save; caller must call update()
260      *
261      * @throws DBException upon error
262      */

263     public void setDefaultPermissions() throws DBException {
264         permissions(RowPermissions.DEFAULT_PERMISSIONS);
265         group(RowPermissions.DEFAULT_PERMISSION_GROUP);
266     }
267
268
269     /**
270      * set permissions
271      *
272      * @param perm Permissions code
273      * @throws DBException upon error
274      */

275     public void permissions(int perm) throws DBException {
276         setField(PERMISSIONS, perm);
277     }
278
279     /**
280      * set permissions; not protected by test
281      *
282      * @return integer permission code
283      * @throws DBException upon error
284      */

285     public int permissions() throws DBException {
286         // protect against empty perm.
287
if (getField(PERMISSIONS).length() == 0) {
288             return 0;
289         }
290         return getFieldInt(PERMISSIONS);
291     }
292
293     /**
294      * accessor for table name
295      *
296      * @return table name
297      */

298     public String JavaDoc table() throws DBException {
299         return getField(TABLE_NAME);
300     }
301
302     /**
303      * set the target table
304      */

305     public void table(String JavaDoc targetTable) throws DBException {
306         setField(TABLE_NAME, targetTable);
307     }
308
309     /**
310      * @return row key
311      */

312     public String JavaDoc key() throws DBException {
313         return getField(ROW_KEY);
314     }
315
316     /**
317      * set the row key
318      */

319     public void key(String JavaDoc key) throws DBException {
320         setField(ROW_KEY, key);
321     }
322 }
323
Popular Tags