1 package mobisnap.common.reservation;
2
3 import java.io.*;
4 import java.util.*;
5 import mobisnap.mobile_trx.*;
6
7 /***
8 * Base class for reservations
9 *
10 * @version 0.045 12-Dec-2000
11 * @author Nuno Pregui�a
12 */
13 public abstract class ReservationBase
14 implements Serializable
15 {
16 java.rmi.server.UID uid;
17 String table;
18 String column;
19 Vector conds;
20 int lease;
21 long time;
22
23 public ReservationBase( String table, String column) {
24 uid = new java.rmi.server.UID();
25 this.table = table;
26 this.column = column;
27 conds = new Vector();
28 this.lease = 0;
29 this.time = 0;
30 }
31
32 public void addCondition( mobisnap.mobile_trx.ColumnCondition cond) {
33 conds.addElement( cond);
34 }
35
36 public mobisnap.mobile_trx.ColumnCondition getCondition( int pos){
37 try{
38 return (mobisnap.mobile_trx.ColumnCondition) conds.elementAt( pos);
39 } catch (Exception e){
40 // do nothing
41 }
42 return null;
43 }
44
45 public Vector getConditions(){
46 return conds;
47 }
48
49 public String getTableName(){
50 return table;
51 }
52
53 public String getColumnName(){
54 return column;
55 }
56
57 public java.rmi.server.UID getUID() {
58 return uid;
59 }
60
61 public int getLease(){
62 return lease;
63 }
64
65 public long getTime(){
66 return time;
67 }
68
69 public void setLeaseTime(long time, int lease){
70 this.time = time;
71 this.lease = lease;
72
73 }
74
75 /***
76 * Checks if there exists a reservation that satisfies the given conditions.
77 * NOTE: Currently only select in the form of
78 * "select col(,col)* into var(,var)* from table where cond1 (AND cond2)*"
79 * can be checked
80 *
81 * @param table Table name of possible reservation
82 * @param column Column name of possible reservation
83 * @param where Table row specification
84 * @return Return true if this resrvation backs up the given conditions
85 */
86 public boolean checkReservation( String table, String column, SimpleNode where) {
87 if( ! table.equals( this.table))
88 return false;
89 if( (! column.equals( this.column))||( !column.equals("*")))
90 return false;
91
92 return checkReservationConditions( 0, new BitSet( conds.size()), where) == conds.size();
93 }
94
95 /***
96 * Checks if there exists a reservation that satisfies the given conditions.
97 * NOTE: Currently only select in the form of
98 * "select col(,col)* into var(,var)* from table where cond1 (AND cond2)*"
99 * can be checked
100 *
101 * @param table Table name of possible reservation
102 * @param column Column name of possible reservation
103 * @param where Table row specification
104 * @return Return true if this resrvation backs up the given conditions
105 */
106 public boolean checkReservation( String table, String column, Vector specs) {
107 if( ! table.equals( this.table))
108 return false;
109 if( (! column.equals( this.column))||( column.equals("*")))
110 return false;
111
112 return checkReservationConditions( specs) == conds.size();
113 }
114
115 /***
116 * Checks which conditions the given node satisfies
117 *
118 * @param checked Number of conditions already satisfied
119 * @param bs Bitset with the conditions already satisfied
120 * @param node Node with the current condition
121 */
122 private int checkReservationConditions( int nchecked, BitSet bs, SimpleNode node) {
123 if( node instanceof ASTSQLAndExpression) {
124 Vector ands = ((ASTSQLAndExpression)node).ands;
125 for( int i = 0; i < ands.size(); i++) {
126 SimpleNode node0 = (SimpleNode)ands.elementAt(i);
127 nchecked = checkReservationConditions( nchecked, bs, node0);
128 if( nchecked == conds.size())
129 return nchecked;
130 }
131 } else {
132 for( int i = 0; i < conds.size(); i++) {
133 if( bs.get( i))
134 continue;
135 if( ((ColumnCondition)conds.elementAt( i)).sameCondition( node)){
136 bs.set(i);
137 nchecked++;
138 }
139 }
140 }
141 return nchecked;
142 }
143
144
145 /***
146 * Checks which conditions the given node satisfies
147 *
148 * @param specs conditions vector
149 */
150 private int checkReservationConditions( Vector specs) {
151 for( int i = 0; i < conds.size(); i++) {
152 boolean checked = false;
153 int j = 0;
154 while( j < specs.size() ){
155 if ( ((ColumnCondition)conds.elementAt( i)).belongsTo( (RelopCondition)specs.elementAt( j)) )
156 break;
157 j++;
158 }
159 if ( j == specs.size())
160 return conds.size() - 1;
161 }
162 return conds.size();
163 }
164
165 /***
166 * Checks the given parameters are backed up by this reservation.
167 * If so, sets the reserved value to the variable and
168 * associate the reservation with the given variable - through a REservationUseBase
169 * class which is returned
170 * NOTE: Currently only select in the form of
171 * "select col(,col)* into var(,var)* from table where cond1 (AND cond2)*"
172 * can be checked
173 *
174 * @param var Variable involved
175 * @param table Table name of possible reservation
176 * @param column Column name of possible reservation
177 * @param where Table row specification
178 * @return Return null if there is no associateion between the given parameters
179 * ans this reservation. Otherwise returns a class to represent the reservation use
180 */
181 public abstract ReservationUseBase checkReservation( MSQLTVariable var, String table, String column, SimpleNode where);
182
183 /***
184 * Returns a readable representation of this reervation - format:<BR>
185 * rsrv_id reservation column from table where row specification
186 */
187 public String toString() {
188 StringBuffer buffer = new StringBuffer();
189 buffer.append( uid + " RESERVATION " + column + " FROM " + table + " WHERE ");
190 for( int i = 0; i < conds.size(); i++) {
191 if( i > 0)
192 buffer.append( " AND ");
193 buffer.append( conds.elementAt( i));
194 }
195 buffer.append(" WITH LEASE "+lease);
196 return buffer.toString();
197 }
198 }
199
This page was automatically generated by Maven