001: /**
002: * com.mckoi.database.GTPrivMapDataSource 26 Aug 2002
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.database;
024:
025: import java.util.ArrayList;
026: import com.mckoi.util.BigNumber;
027:
028: /**
029: * A GTDataSource that maps a Privs 11-bit set to strings that represent the
030: * priv in human understandable string. Each 11-bit priv set contains 12
031: * entries for each bit that was set.
032: * <p>
033: * This table provides a convenient way to join the system grant table and
034: * 'expand' the privs that are allowed though it.
035: *
036: * @author Tobias Downer
037: */
038:
039: public class GTPrivMapDataSource extends GTDataSource {
040:
041: /**
042: * Number of bits.
043: */
044: private static int BIT_COUNT = Privileges.BIT_COUNT;
045:
046: /**
047: * Constructor.
048: */
049: public GTPrivMapDataSource(DatabaseConnection connection) {
050: super (connection.getSystem());
051: }
052:
053: // ---------- Implemented from GTDataSource ----------
054:
055: public DataTableDef getDataTableDef() {
056: return DEF_DATA_TABLE_DEF;
057: }
058:
059: public int getRowCount() {
060: return (1 << BIT_COUNT) * BIT_COUNT;
061: }
062:
063: public TObject getCellContents(final int column, final int row) {
064: int c1 = row / BIT_COUNT;
065: if (column == 0) {
066: return columnValue(column, BigNumber.fromInt(c1));
067: } else {
068: int priv_bit = (1 << (row % BIT_COUNT));
069: String priv_string = null;
070: if ((c1 & priv_bit) != 0) {
071: priv_string = Privileges.formatPriv(priv_bit);
072: }
073: return columnValue(column, priv_string);
074: }
075: }
076:
077: // ---------- Overwritten from GTDataSource ----------
078:
079: public SelectableScheme getColumnScheme(int column) {
080: if (column == 0) {
081: return new PrivMapSearch(this , column);
082: } else {
083: return new BlindSearch(this , column);
084: }
085: }
086:
087: // ---------- Static ----------
088:
089: /**
090: * The data table def that describes this table of data source.
091: */
092: static final DataTableDef DEF_DATA_TABLE_DEF;
093:
094: static {
095:
096: DataTableDef def = new DataTableDef();
097: def.setTableName(new TableName(Database.SYSTEM_SCHEMA,
098: "sUSRPrivMap"));
099:
100: // Add column definitions
101: def.addColumn(numericColumn("priv_bit"));
102: def.addColumn(stringColumn("description"));
103:
104: // Set to immutable
105: def.setImmutable();
106:
107: DEF_DATA_TABLE_DEF = def;
108:
109: }
110:
111: // ---------- Inner classes ----------
112:
113: /**
114: * A SelectableScheme that makes searching on the 'priv_bit' column a lot
115: * less painless!
116: */
117: private static final class PrivMapSearch extends CollatedBaseSearch {
118:
119: PrivMapSearch(TableDataSource table, int column) {
120: super (table, column);
121: }
122:
123: public SelectableScheme copy(TableDataSource table,
124: boolean immutable) {
125: // Return a fresh object. This implementation has no state so we can
126: // ignore the 'immutable' flag.
127: return new BlindSearch(table, getColumn());
128: }
129:
130: protected int searchFirst(TObject val) {
131: if (val.isNull()) {
132: return -1;
133: }
134:
135: int num = ((BigNumber) val.getObject()).intValue();
136:
137: if (num < 0) {
138: return -1;
139: } else if (num > (1 << BIT_COUNT)) {
140: return -(((1 << BIT_COUNT) * BIT_COUNT) + 1);
141: }
142:
143: return (num * BIT_COUNT);
144: }
145:
146: protected int searchLast(TObject val) {
147: int p = searchFirst(val);
148: if (p >= 0) {
149: return p + (BIT_COUNT - 1);
150: } else {
151: return p;
152: }
153: }
154:
155: }
156:
157: }
|