KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > tuple > lib > GeneralTupleLoader


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

23
24 package org.objectweb.medor.tuple.lib;
25
26 import org.objectweb.jorm.type.api.PType;
27 import org.objectweb.medor.api.MedorException;
28 import org.objectweb.medor.api.TupleStructure;
29 import org.objectweb.medor.expression.api.Operand;
30 import org.objectweb.medor.expression.api.ParameterOperand;
31 import org.objectweb.medor.expression.api.ExpressionException;
32 import org.objectweb.medor.expression.api.VariableOperand;
33 import org.objectweb.medor.expression.api.Expression;
34 import org.objectweb.medor.query.api.CalculatedField;
35 import org.objectweb.medor.tuple.api.Tuple;
36 import org.objectweb.medor.tuple.api.TupleLoader;
37 import org.objectweb.medor.type.lib.QTypeTuple;
38 import org.objectweb.medor.lib.Log;
39 import org.objectweb.util.monolog.api.Logger;
40 import org.objectweb.util.monolog.api.BasicLevel;
41
42 public class GeneralTupleLoader implements TupleLoader {
43
44     private Logger logger;
45
46     public GeneralTupleLoader(int[] ranges, TupleStructure ts) {
47         indexes = ranges;
48         this.ts = ts;
49         logger = Log.getLoggerFactory().getLogger(getClass().getName());
50     }
51
52     //indexes is the array of attribute ranks to be loaded from the source Tuple
53
private int[] indexes;
54     TupleStructure ts;
55
56     public void loadTuple(Tuple sourceBuffer,
57                           VariableOperand[] temp,
58                           ParameterOperand[] parameters)
59             throws MedorException, ExpressionException {
60         boolean debug = logger != null && logger.isLoggable(BasicLevel.DEBUG);
61         PType type;
62         for (int i = 0; (i < temp.length); i++) {
63             type = ts.getField(i + 1).getType();
64             Expression cField = null;
65             if (indexes[i] == -1) {
66                 //calculated field
67
cField = ((CalculatedField) ts.getField(i + 1)).getExpression();
68             }
69             switch (type.getTypeCode()) {
70             case QTypeTuple.TYPECODE_INT:
71                 if (indexes[i] != -1) {
72                     //propagated field
73
temp[i].setValue(sourceBuffer.getInt(indexes[i]));
74                     temp[i].setIsDefined(sourceBuffer.isDefined(indexes[i]));
75                 } else {
76                     synchronized(cField) {
77                         cField.compileExpression();
78                         Operand op = cField.evaluate(parameters, sourceBuffer);
79                         temp[i].setValue(op.getInt());
80                         temp[i].setIsDefined(op.isDefined());
81                     }
82                 }
83                 if (debug) {
84                     logger.log(BasicLevel.DEBUG, "Value assigned [" + i + "]= " + temp[i].getInt());
85                 }
86                 break;
87             case QTypeTuple.TYPECODE_SHORT:
88                 if (indexes[i] != -1) {
89                     //propagated field
90
temp[i].setValue(sourceBuffer.getShort(indexes[i]));
91                     temp[i].setIsDefined(sourceBuffer.isDefined(indexes[i]));
92                 } else {
93                     synchronized(cField) {
94                         cField.compileExpression();
95                         Operand op = cField.evaluate(parameters, sourceBuffer);
96                         temp[i].setValue(op.getShort());
97                         temp[i].setIsDefined(op.isDefined());
98                     }
99                 }
100                 if (debug) {
101                     logger.log(BasicLevel.DEBUG, "Value assigned [" + i + "]= " + temp[i].getShort());
102                 }
103                 break;
104             case QTypeTuple.TYPECODE_BYTE:
105                 if (indexes[i] != -1) {
106                     //propagated field
107
temp[i].setValue(sourceBuffer.getByte(indexes[i]));
108                     temp[i].setIsDefined(sourceBuffer.isDefined(indexes[i]));
109                 } else {
110                     synchronized(cField) {
111                         cField.compileExpression();
112                         Operand op = cField.evaluate(parameters, sourceBuffer);
113                         temp[i].setValue(op.getByte());
114                         temp[i].setIsDefined(op.isDefined());
115                     }
116                 }
117                 if (debug) {
118                     logger.log(BasicLevel.DEBUG, "Value assigned [" + i +"]= " + temp[i].getByte());
119                 }
120                 break;
121             case QTypeTuple.TYPECODE_LONG:
122                 if (indexes[i] != -1) {
123                     //propagated field
124
temp[i].setValue(sourceBuffer.getLong(indexes[i]));
125                     temp[i].setIsDefined(sourceBuffer.isDefined(indexes[i]));
126                 } else {
127                     synchronized(cField) {
128                         cField.compileExpression();
129                         Operand op = cField.evaluate(parameters, sourceBuffer);
130                         temp[i].setValue(op.getLong());
131                         temp[i].setIsDefined(op.isDefined());
132                     }
133                 }
134                 if (debug) {
135                     logger.log(BasicLevel.DEBUG, "Value assigned [" + i +"]= " + temp[i].getLong());
136                 }
137                 break;
138             case QTypeTuple.TYPECODE_DOUBLE:
139                 if (indexes[i] != -1) {
140                     //propagated field
141
temp[i].setValue(sourceBuffer.getDouble(indexes[i]));
142                     temp[i].setIsDefined(sourceBuffer.isDefined(indexes[i]));
143                 } else {
144                     synchronized(cField) {
145                         cField.compileExpression();
146                         Operand op = cField.evaluate(parameters, sourceBuffer);
147                         temp[i].setValue(op.getDouble());
148                         temp[i].setIsDefined(op.isDefined());
149                     }
150                 }
151                 if (debug) {
152                     logger.log(BasicLevel.DEBUG, "Value assigned [" + i +"]= " + temp[i].getDouble());
153                 }
154                 break;
155             case QTypeTuple.TYPECODE_BOOLEAN:
156                 if (indexes[i] != -1) {
157                     //propagated field
158
temp[i].setValue(sourceBuffer.getBoolean(indexes[i]));
159                     temp[i].setIsDefined(sourceBuffer.isDefined(indexes[i]));
160                 } else {
161                     synchronized(cField) {
162                         cField.compileExpression();
163                         Operand op = cField.evaluate(parameters, sourceBuffer);
164                         temp[i].setValue(op.getBoolean());
165                         temp[i].setIsDefined(op.isDefined());
166                     }
167                 }
168                 if (debug) {
169                     logger.log(BasicLevel.DEBUG, "Value assigned [" + i +"]= " + temp[i].getBoolean());
170                 }
171                 break;
172             case QTypeTuple.TYPECODE_FLOAT:
173                 if (indexes[i] != -1) {
174                     //propagated field
175
temp[i].setValue(sourceBuffer.getFloat(indexes[i]));
176                     temp[i].setIsDefined(sourceBuffer.isDefined(indexes[i]));
177                 } else {
178                     synchronized(cField) {
179                         cField.compileExpression();
180                         Operand op = cField.evaluate(parameters, sourceBuffer);
181                         temp[i].setValue(op.getFloat());
182                         temp[i].setIsDefined(op.isDefined());
183                     }
184                 }
185                 break;
186             case QTypeTuple.TYPECODE_STRING:
187                 if (indexes[i] != -1) {
188                     //propagated field
189
temp[i].setValue(sourceBuffer.getString(indexes[i]));
190                     temp[i].setIsDefined(sourceBuffer.isDefined(indexes[i]));
191                 } else {
192                     synchronized(cField) {
193                         cField.compileExpression();
194                         Operand op = cField.evaluate(parameters, sourceBuffer);
195                         temp[i].setValue(op.getString());
196                         temp[i].setIsDefined(op.isDefined());
197                     }
198                 }
199                 if (debug) {
200                     logger.log(BasicLevel.DEBUG, "Value assigned [" + i +"]= " + temp[i].getString());
201                 }
202                 break;
203             case QTypeTuple.TYPECODE_CHAR:
204                 if (indexes[i] != -1) {
205                     //propagated field
206
temp[i].setValue(sourceBuffer.getChar(indexes[i]));
207                     temp[i].setIsDefined(sourceBuffer.isDefined(indexes[i]));
208                 } else {
209                     synchronized(cField) {
210                         cField.compileExpression();
211                         Operand op = cField.evaluate(parameters, sourceBuffer);
212                         temp[i].setValue(op.getChar());
213                         temp[i].setIsDefined(op.isDefined());
214                     }
215                 }
216                 if (debug) {
217                     logger.log(BasicLevel.DEBUG, "Value assigned [" + i +"]= " + temp[i].getChar());
218                 }
219                 break;
220             case QTypeTuple.TYPECODE_DATE:
221                 if (indexes[i] != -1) {
222                     //propagated field
223
temp[i].setValue(sourceBuffer.getDate(indexes[i]));
224                     temp[i].setIsDefined(sourceBuffer.isDefined(indexes[i]));
225                 } else {
226                     synchronized(cField) {
227                         cField.compileExpression();
228                         Operand op = cField.evaluate(parameters, sourceBuffer);
229                         temp[i].setValue(op.getDate());
230                         temp[i].setIsDefined(op.isDefined());
231                     }
232                 }
233                 if (debug) {
234                     logger.log(BasicLevel.DEBUG, "Value assigned [" + i +"]= " + temp[i].getDate());
235                 }
236                 break;
237                 // TO DO: Add TupleCollection type and Tuple Types
238
case QTypeTuple.TYPECODE_OBJBOOLEAN:
239             case QTypeTuple.TYPECODE_OBJBYTE:
240             case QTypeTuple.TYPECODE_OBJCHAR:
241             case QTypeTuple.TYPECODE_OBJDOUBLE:
242             case QTypeTuple.TYPECODE_OBJFLOAT:
243             case QTypeTuple.TYPECODE_OBJINT:
244             case QTypeTuple.TYPECODE_OBJLONG:
245             case QTypeTuple.TYPECODE_OBJSHORT:
246             case QTypeTuple.TYPECODE_BIGDECIMAL:
247             case QTypeTuple.TYPECODE_BIGINTEGER:
248             default:
249                 if (indexes[i] != -1) {
250                     //propagated field
251
temp[i].setValue(sourceBuffer.getObject(indexes[i]));
252                     temp[i].setIsDefined(sourceBuffer.isDefined(indexes[i]));
253                 } else {
254                     synchronized(cField) {
255                         cField.compileExpression();
256                         Operand op = cField.evaluate(parameters, sourceBuffer);
257                         temp[i].setValue(op.getObject());
258                         temp[i].setIsDefined(op.isDefined());
259                     }
260                 }
261                 if (debug) {
262                     logger.log(BasicLevel.DEBUG, "Value assigned [" + i +"]= " + temp[i].getObject());
263                 }
264                 break;
265             }
266         }
267     }
268
269     public int[] getFieldIndexes() {
270         return indexes;
271     }
272
273     // NO INTEGRITY VERIFING
274
public void setFieldIndexes(int[] indexes) {
275         this.indexes = indexes;
276     }
277
278     public TupleStructure getTupleStructure() {
279         return ts;
280     }
281 }
282
Popular Tags