KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > query > lib > QueriesUnion


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.query.lib;
19
20 import org.objectweb.medor.tuple.api.TupleCollection;
21 import org.objectweb.medor.tuple.api.Tuple;
22 import org.objectweb.medor.api.TupleStructure;
23 import org.objectweb.medor.expression.api.ParameterOperand;
24 import org.objectweb.medor.api.MedorException;
25 import org.objectweb.medor.api.EvaluationException;
26 import org.objectweb.speedo.pm.api.ProxyManager;
27 import org.objectweb.speedo.query.api.QueryDefinition;
28
29
30 /**
31  * Is in charge of making the union of Medor query. This union is hidden behind
32  * the TupleCollection interface, but the TupleStructure is variable.
33  *
34  * @author S.Chassande-Barrioz
35  */

36 public class QueriesUnion implements TupleCollection {
37
38     /**
39      * The common parameter operand of the queries
40      */

41     ParameterOperand[] pos;
42
43     /**
44      * The PersistenceManager holding the query
45      */

46     ProxyManager pm;
47
48     /**
49      * is the current TupleCollection over which the iteration is done
50      */

51     TupleCollection currentTC;
52
53     /**
54      * Is the index of the current query which is evaluated or read
55      */

56     int queryIdx;
57
58     /**
59      * Is the commong connection to access the persistent support
60      */

61     Object JavaDoc connection;
62
63     QueryEvalContext[] qecs;
64
65     int row = 0;
66     boolean hasResult = false;
67
68
69     public QueriesUnion(ParameterOperand[] pos,
70                         ProxyManager pm,
71                         Object JavaDoc connection,
72                         QueryEvalContext[] qecs,
73                         QueryDefinition userqd) throws MedorException {
74         queryIdx = -1;
75         this.pos = pos;
76         this.pm = pm;
77         this.connection = connection;
78         this.qecs = qecs;
79         calculateNext();
80     }
81
82
83     private boolean calculateNext() throws EvaluationException, MedorException {
84         queryIdx++;
85         row++;
86         currentTC = null;
87         if (qecs.length <= queryIdx) {
88             return false;
89         }
90         currentTC = qecs[queryIdx].eval(pm, pos, connection, null);
91         if (currentTC == null || currentTC.isEmpty()) {
92             calculateNext();
93         }
94         hasResult |= currentTC!= null;
95         if (currentTC == null) {
96             row = 0;
97         }
98         return currentTC != null;
99     }
100
101     //IMPLEMENTATION OF THE TupleCollection INTERFACE //
102
//------------------------------------------------//
103

104     public boolean next() throws MedorException {
105         return currentTC.next() || calculateNext();
106     }
107
108     public void first() throws MedorException {
109         if (row != 1) {
110             queryIdx = -1;
111             row = 0;
112             calculateNext();
113         }
114     }
115
116     public Tuple getTuple() throws MedorException {
117         if (currentTC == null) {
118             throw new MedorException("No more result");
119         }
120         return currentTC.getTuple();
121     }
122
123     public boolean isEmpty() throws MedorException {
124         return !hasResult;
125     }
126
127     public void close() throws MedorException {
128         if (currentTC != null) {
129             currentTC.close();
130         }
131     }
132
133     public TupleStructure getMetaData() throws MedorException {
134         return currentTC.getMetaData();
135     }
136
137     public boolean isLast() throws MedorException {
138         throw new MedorException("IsLast not yet suuported");
139     }
140
141     public Tuple getTuple(int row) throws MedorException {
142         throw new MedorException("getTuple(int) not yet suuported");
143     }
144
145     public boolean row(int row) throws MedorException {
146         throw new MedorException("row(int) not yet suuported");
147     }
148     public int getRow() throws MedorException {
149         throw new MedorException("getRow not yet suuported");
150     }
151 }
152
Popular Tags