KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > engine > Right


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.engine;
6
7 import java.sql.SQLException JavaDoc;
8
9 import org.h2.message.Message;
10 import org.h2.message.Trace;
11 import org.h2.table.Table;
12
13 public class Right extends DbObject {
14     
15     public static final int SELECT = 1, DELETE = 2, INSERT = 4, UPDATE = 8, ALL = 15;
16     
17     private Role grantedRole;
18     private int grantedRight;
19     private Table grantedTable;
20     private RightOwner grantee;
21
22     public Right(Database db, int id, RightOwner grantee, Role grantedRole) {
23         super(db, id, "RIGHT_"+id, Trace.USER);
24         this.grantee = grantee;
25         this.grantedRole = grantedRole;
26     }
27     
28     public Right(Database db, int id, RightOwner grantee, int grantedRight, Table grantedRightOnTable) {
29         super(db, id, ""+id, Trace.USER);
30         this.grantee = grantee;
31         this.grantedRight = grantedRight;
32         this.grantedTable = grantedRightOnTable;
33     }
34     
35     private boolean appendRight(StringBuffer JavaDoc buff, int right, int mask, String JavaDoc name, boolean comma) {
36         if((right & mask) != 0) {
37             if(comma) {
38                 buff.append(", ");
39             }
40             buff.append(name);
41             return true;
42         }
43         return comma;
44     }
45     
46     public String JavaDoc getRights() {
47         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
48         if(grantedRight == ALL) {
49             buff.append("ALL");
50         } else {
51             boolean comma = false;
52             comma = appendRight(buff, grantedRight, SELECT, "SELECT", comma);
53             comma = appendRight(buff, grantedRight, DELETE, "DELETE", comma);
54             comma = appendRight(buff, grantedRight, INSERT, "INSERT", comma);
55             appendRight(buff, grantedRight, UPDATE, "UPDATE", comma);
56         }
57         return buff.toString();
58     }
59     
60     public Role getGrantedRole() {
61         return grantedRole;
62     }
63     
64     public Table getGrantedTable() {
65         return grantedTable;
66     }
67     
68     public DbObject getGrantee() {
69         return grantee;
70     }
71     
72     public String JavaDoc getCreateSQLForCopy(Table table, String JavaDoc quotedName) {
73         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
74         buff.append("GRANT ");
75         if(grantedRole != null) {
76             buff.append(grantedRole.getSQL());
77         } else {
78             buff.append(getRights());
79             buff.append(" ON ");
80             buff.append(table.getSQL());
81         }
82         buff.append(" TO ");
83         // TODO rights: need role 'PUBLIC'
84
buff.append(grantee.getSQL());
85         return buff.toString();
86     }
87     
88     public String JavaDoc getCreateSQL() {
89         return getCreateSQLForCopy(grantedTable, null);
90     }
91
92     public int getType() {
93         return DbObject.RIGHT;
94     }
95
96     public void removeChildrenAndResources(Session session) throws SQLException JavaDoc {
97         if(grantedTable != null) {
98             grantee.revokeRight(grantedTable);
99         } else {
100             grantee.revokeRole(session, grantedRole);
101         }
102         grantedRole = null;
103         grantedTable = null;
104         grantee = null;
105         invalidate();
106     }
107
108     public void checkRename() throws SQLException JavaDoc {
109         throw Message.getInternalError();
110     }
111     
112     public void setRightMask(int rightMask) {
113         grantedRight = rightMask;
114     }
115
116     public int getRightMask() {
117         return grantedRight;
118     }
119     
120 }
121
Popular Tags