KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > eval > lib > TuplePNameGetter


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.eval.lib;
25
26 import org.objectweb.jorm.api.PExceptionIO;
27 import org.objectweb.jorm.api.PException;
28 import org.objectweb.jorm.naming.api.PNameGetter;
29 import org.objectweb.jorm.naming.api.PNameCoder;
30 import org.objectweb.jorm.type.api.PExceptionTyping;
31 import org.objectweb.medor.api.MedorException;
32 import org.objectweb.medor.tuple.api.Tuple;
33 import org.objectweb.medor.expression.api.Operand;
34 import org.objectweb.util.monolog.api.Logger;
35 import org.objectweb.util.monolog.api.BasicLevel;
36
37 import java.util.Date JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.math.BigDecimal JavaDoc;
41 import java.math.BigInteger JavaDoc;
42
43 /**
44  * This class implements a JORM PnameGetter. It relies on a Tuple, which
45  * contains values, and a Map which gives the index in the Tuple for a given
46  * field name.
47  *
48  * @author <A HREF="mailto:alia.mourad@rd.francetelecom.com><b>
49  * Mourad Alia
50  * </b></A>
51  * <A HREF="mailto:alexandre.lefebvre@rd.francetelecom.com><b>
52  * Alexandre Lefebvre
53  </b></A>
54  */

55 //TODO move this class to a eval.jorm package since it is JORM specific.
56
public class TuplePNameGetter implements PNameGetter {
57
58     /**
59      * This map contains the association between the PName field name and
60      * the index in the tuple. This field describes the mapping between the
61      * PName structure and the fields availlable in the tuple.
62      *
63      * key: String PName field name
64      * value: Integer index in the Tuple
65      */

66     private Map JavaDoc fields;
67
68     private PNameGetter png;
69
70     private Operand pnc;
71
72     transient private Logger logger;
73
74     public TuplePNameGetter(TuplePNameGetter tpng) {
75         this.logger = tpng.logger;
76         this.fields = new HashMap JavaDoc(tpng.fields);
77         this.logger = tpng.logger;
78         this.pnc = tpng.pnc;
79     }
80     /**
81      * Constructs a TuplePNameGetter from a set of associations (field name,
82      * index in tuple).
83      * @param fieldName2index is the Map which contains the association field
84      * name -> index in tuple.
85      */

86     public TuplePNameGetter(Map JavaDoc fieldName2index, Operand nc, Logger logger) {
87         this.logger = logger;
88         this.fields = fieldName2index;
89         pnc = nc;
90     }
91
92     /**
93      * Sets the field name to index association map.
94      * @param fieldName2index is the Map which contains the association field
95      * name -> index in tuple.
96      */

97     public void setMap(Map JavaDoc fieldName2index) {
98         this.fields = fieldName2index;
99     }
100
101     //Methods from PNameGetter
102
/**
103      * Retrieves the value of a byte[] field.
104      * @param fn A String containing the name of the field.
105      * @param context A context used by PNameGetter to extract the related data.
106      * In in the case of RDB, it can be the result set.
107      * @return The byte[] value of the field as stored within the composite
108      * name.
109      * @exception PExceptionTyping There is no field with this name and
110      * this type within this composite name.
111      * @exception PExceptionIO It is raised when a problem occurs while
112      * potentially accessing the DSI.
113      */

114     public byte[] pngetByteArrayField(String JavaDoc fn, Object JavaDoc context)
115         throws PException {
116         Tuple currentTuple = (Tuple) context;
117         if (currentTuple == null) {
118             throw new PExceptionTyping("The context must be the tuple: " + context);
119         }
120         try {
121             Integer JavaDoc in = (Integer JavaDoc) fields.get(fn);
122             if (in == null)
123                 throw new PExceptionTyping("Field " + fn + " has not been found");
124             return currentTuple.getByteArray(in.intValue());
125         }
126         catch (MedorException e) {
127             throw new PException(e.getMessage());
128         }
129     }
130
131     /**
132      * It retrieves the value of a byte field.
133      * @param fn A String containing the name of the field.
134      * @param context A context used by PNameGetter to extract the related data.
135      * In in the case of RDB, it can be the result set.
136      * @return The byte value of the field as stored within the composite
137      * name.
138      * @exception PExceptionTyping There is no field with this name and
139      * this type within this composite name.
140      * @exception PExceptionIO It is raised when a problem occurs while
141      * potentially accessing the DSI.
142      */

143     public byte pngetByteField(String JavaDoc fn, Object JavaDoc context)
144         throws PException {
145         Tuple currentTuple = (Tuple) context;
146         if (currentTuple == null) {
147             throw new PExceptionTyping("The context must be the tuple: " + context);
148         }
149         try {
150             Integer JavaDoc in = (Integer JavaDoc) fields.get(fn);
151             if (in == null)
152                 throw new PExceptionTyping("Field " + fn + " has not been found");
153             if (currentTuple.isDefined(in.intValue())) {
154                 return currentTuple.getByte(in.intValue());
155             } else {
156                 if (png == null) {
157                     png = (PNameGetter)
158                         ((PNameCoder)pnc.getObject()).getNull().encodeAbstract();
159                 }
160                 return png.pngetByteField(fn, null);
161             }
162         }
163         catch (MedorException e) {
164             throw new PException(e.getMessage());
165         }
166     }
167
168     /**
169      * It retrieves the value of a char field.
170      * @param fn A String containing the name of the field.
171      * @param context A context used by PNameGetter to extract the related data.
172      * In in the case of RDB, it can be the result set.
173      * @return The char value of the field as stored within the composite
174      * name.
175      * @exception PExceptionTyping There is no field with this name and
176      * this type within this composite name.
177      * @exception PExceptionIO It is raised when a problem occurs while
178      * potentially accessing the DSI.
179      */

180     public char pngetCharField(String JavaDoc fn, Object JavaDoc context)
181         throws PException {
182         Tuple currentTuple = (Tuple) context;
183         if (currentTuple == null) {
184             throw new PExceptionTyping("The context must be the tuple: " + context);
185         }
186         try {
187             Integer JavaDoc in = (Integer JavaDoc) fields.get(fn);
188             if (in == null)
189                 throw new PExceptionTyping("Field " + fn + " has not been found");
190             if (currentTuple.isDefined(in.intValue())) {
191                 return currentTuple.getChar(in.intValue());
192             } else {
193                 return png.pngetCharField(fn, null);
194             }
195         }
196         catch (MedorException e) {
197             throw new PException(e.getMessage());
198         }
199     }
200
201     /**
202      * It retrieves the value of a char[] field.
203      * @param fn A String containing the name of the field.
204      * @param context A context used by PNameGetter to extract the related data.
205      * In in the case of RDB, it can be the result set.
206      * @return The char[] value of the field as stored within the composite
207      * name.
208      * @exception PExceptionTyping There is no field with this name and
209      * this type within this composite name.
210      * @exception PExceptionIO It is raised when a problem occurs while
211      * potentially accessing the DSI.
212      */

213     public char[] pngetCharArrayField(String JavaDoc fn, Object JavaDoc context)
214         throws PException {
215         Tuple currentTuple = (Tuple) context;
216         if (currentTuple == null) {
217             throw new PExceptionTyping("The context must be the tuple: " + context);
218         }
219         try {
220             Integer JavaDoc in = (Integer JavaDoc) fields.get(fn);
221             if (in == null)
222                 throw new PExceptionTyping("Field " + fn + " has not been found");
223             return currentTuple.getCharArray(in.intValue());
224         }
225         catch (MedorException e) {
226             throw new PException(e.getMessage());
227         }
228     }
229
230     /**
231      * It retrieves the value of a short field.
232      * @param fn A String containing the name of the field.
233      * @param context A context used by PNameGetter to extract the related data.
234      * In in the case of RDB, it can be the result set.
235      * @return The short value of the field as stored within the composite
236      * name.
237      * @exception PExceptionTyping There is no field with this name and
238      * this type within this composite name.
239      * @exception PExceptionIO It is raised when a problem occurs while
240      * potentially accessing the DSI.
241      */

242     public short pngetShortField(String JavaDoc fn, Object JavaDoc context)
243         throws PException {
244         Tuple currentTuple = (Tuple) context;
245         if (currentTuple == null) {
246             throw new PExceptionTyping("The context must be the tuple: " + context);
247         }
248         try {
249             Integer JavaDoc in = (Integer JavaDoc) fields.get(fn);
250             if (in == null)
251                 throw new PExceptionTyping("Field " + fn + " has not been found");
252             if (currentTuple.isDefined(in.intValue())) {
253                 return currentTuple.getShort(in.intValue());
254             } else {
255                 if (png == null) {
256                     png = (PNameGetter)
257                         ((PNameCoder)pnc.getObject()).getNull().encodeAbstract();
258                 }
259                 return png.pngetShortField(fn, null);
260             }
261         }
262         catch (MedorException e) {
263             throw new PException(e.getMessage());
264         }
265     }
266
267     /**
268      * It retrieves the value of a Byte field.
269      * @param fn A String containing the name of the field.
270      * @param ctxt A context used by PNameGetter to extract the related data.
271      * @return The Byte value of the field as stored within the composite
272      * name.
273      * @exception PExceptionTyping There is no field with this name and
274      * this type within this composite name.
275      * @exception PExceptionIO It is raised when a problem occurs while
276      * potentially accessing the DSI.
277      */

278     public Byte JavaDoc pngetObyteField(String JavaDoc fn, Object JavaDoc ctxt)
279         throws PException {
280         Tuple currentTuple = (Tuple) ctxt;
281         if (currentTuple == null) {
282             throw new PExceptionTyping("The context must be the tuple: " + ctxt);
283         }
284         try {
285             Integer JavaDoc in = (Integer JavaDoc) fields.get(fn);
286             if (in == null)
287                 throw new PExceptionTyping("Field " + fn + " has not been found");
288             return new Byte JavaDoc(currentTuple.getByte(in.intValue()));
289         }
290         catch (MedorException e) {
291             throw new PException(e.getMessage());
292         }
293
294     }
295
296     /**
297      * It retrieves the value of a Character field.
298      * @param fn A String containing the name of the field.
299      * @param ctxt A context used by PNameGetter to extract the related data.
300      * @return The Character value of the field as stored within the composite
301      * name.
302      * @exception PExceptionTyping There is no field with this name and
303      * this type within this composite name.
304      * @exception PExceptionIO It is raised when a problem occurs while
305      * potentially accessing the DSI.
306      */

307     public Character JavaDoc pngetOcharField(String JavaDoc fn, Object JavaDoc ctxt)
308         throws PException {
309         Tuple currentTuple = (Tuple) ctxt;
310         if (currentTuple == null) {
311             throw new PExceptionTyping("The context must be the tuple: " + ctxt);
312         }
313         try {
314             Integer JavaDoc in = (Integer JavaDoc) fields.get(fn);
315             if (in == null)
316                 throw new PExceptionTyping("Field " + fn + " has not been found");
317             return new Character JavaDoc(currentTuple.getChar(in.intValue()));
318         }
319         catch (MedorException e) {
320             throw new PException(e.getMessage());
321         }
322     }
323
324     /**
325      * It retrieves the value of a Short field.
326      * @param fn A String containing the name of the field.
327      * @param ctxt A context used by PNameGetter to extract the related data.
328      * @return The Short value of the field as stored within the composite
329      * name.
330      * @exception PExceptionTyping There is no field with this name and
331      * this type within this composite name.
332      * @exception PExceptionIO It is raised when a problem occurs while
333      * potentially accessing the DSI.
334      */

335     public Short JavaDoc pngetOshortField(String JavaDoc fn, Object JavaDoc ctxt)
336         throws PException {
337         Tuple currentTuple = (Tuple) ctxt;
338         if (currentTuple == null) {
339             throw new PExceptionTyping("The context must be the tuple: " + ctxt);
340         }
341         try {
342             Integer JavaDoc in = (Integer JavaDoc) fields.get(fn);
343             if (in == null)
344                 throw new PExceptionTyping("Field " + fn + " has not been found");
345             return new Short JavaDoc(currentTuple.getShort(in.intValue()));
346         }
347         catch (MedorException e) {
348             throw new PException(e.getMessage());
349         }
350     }
351
352     /**
353      * It retrieves the value of an Integer field.
354      * @param fn A String containing the name of the field.
355      * @param ctxt A context used by PNameGetter to extract the related data.
356      * @return The Integer value of the field as stored within the composite
357      * name.
358      * @exception PExceptionTyping There is no field with this name and
359      * this type within this composite name.
360      * @exception PExceptionIO It is raised when a problem occurs while
361      * potentially accessing the DSI.
362      */

363     public Integer JavaDoc pngetOintField(String JavaDoc fn, Object JavaDoc ctxt)
364         throws PException {
365         Tuple currentTuple = (Tuple) ctxt;
366         if (currentTuple == null) {
367             throw new PExceptionTyping("The context must be the tuple: " + ctxt);
368         }
369         try {
370             Integer JavaDoc in = (Integer JavaDoc) fields.get(fn);
371             if (in == null)
372                 throw new PExceptionTyping("Field " + fn + " has not been found");
373             return new Integer JavaDoc(currentTuple.getInt(in.intValue()));
374         }
375         catch (MedorException e) {
376             throw new PException(e.getMessage());
377         }
378     }
379
380     /**
381      * It retrieves the value of a Long field.
382      * @param fn A String containing the name of the field.
383      * @param ctxt A context used by PNameGetter to extract the related data.
384      * @return The Long value of the field as stored within the composite
385      * name.
386      * @exception PExceptionTyping There is no field with this name and
387      * this type within this composite name.
388      * @exception PExceptionIO It is raised when a problem occurs while
389      * potentially accessing the DSI.
390      */

391     public Long JavaDoc pngetOlongField(String JavaDoc fn, Object JavaDoc ctxt)
392         throws PExceptionTyping, PExceptionIO {
393         Tuple currentTuple = (Tuple) ctxt;
394         if (currentTuple == null) {
395             throw new PExceptionTyping("The context must be the tuple: " + ctxt);
396         }
397         try {
398             Integer JavaDoc in = (Integer JavaDoc) fields.get(fn);
399             if (in == null)
400                 throw new PExceptionTyping("Field " + fn + " has not been found");
401             return new Long JavaDoc(currentTuple.getLong(in.intValue()));
402         }
403         catch (MedorException e) {
404             throw new PExceptionTyping(e.getMessage());
405         }
406     }
407
408     /**
409      * It retrieves the value of a date field.
410      * @param fn A String containing the name of the field.
411      * @param ctxt A context used by PNameGetter to extract the related data.
412      * @return The java.util.Date value of the field as stored within the
413      * composite name.
414      * @exception PExceptionTyping There is no field with this name and
415      * this type within this composite name.
416      * @exception PExceptionIO It is raised when a problem occurs while
417      * potentially accessing the DSI.
418      */

419     public Date JavaDoc pngetDateField(String JavaDoc fn, Object JavaDoc ctxt)
420         throws PExceptionTyping, PExceptionIO {
421         Tuple currentTuple = (Tuple) ctxt;
422         if (currentTuple == null) {
423             throw new PExceptionTyping("The context must be the tuple: " + ctxt);
424         }
425         try {
426             Integer JavaDoc in = (Integer JavaDoc) fields.get(fn);
427             if (in == null)
428                 throw new PExceptionTyping("Field " + fn + " has not been found");
429             return currentTuple.getDate(in.intValue());
430         }
431         catch (MedorException e) {
432             throw new PExceptionTyping(e.getMessage());
433         }
434     }
435
436     /**
437      * It retrieves the value of an int field.
438      * @param context A context used by PNameGetter to extract the related data.
439      * In in the case of RDB, it can be the result set.
440      * @param fn A String containing the name of the field.
441      * @return The int value of the field as stored within the composite
442      * name.
443      * @exception PExceptionTyping There is no field with this name and
444      * this type within this composite name.
445      * @exception PExceptionIO It is raised when a problem occurs while
446      * potentially accessing the DSI.
447      */

448     public int pngetIntField(String JavaDoc fn, Object JavaDoc context)
449         throws PException {
450         Tuple currentTuple = (Tuple) context;
451         if (currentTuple == null) {
452             throw new PExceptionTyping("The context must be the tuple: " + context);
453         }
454         try {
455             Integer JavaDoc in = (Integer JavaDoc) fields.get(fn);
456             if (in == null)
457                 throw new PExceptionTyping("Field " + fn + " has not been found");
458             if (currentTuple.isDefined(in.intValue())) {
459                 return currentTuple.getInt(in.intValue());
460             } else {
461                 if (png == null) {
462                     png = (PNameGetter)
463                         ((PNameCoder)pnc.getObject()).getNull().encodeAbstract();
464                 }
465                 return png.pngetIntField(fn, null);
466             }
467         }
468         catch (MedorException e) {
469             throw new PException(e.getMessage());
470         }
471     }
472
473     /**
474      * It retrieves the value of an int field.
475      * @param context A context used by PNameGetter to extract the related data.
476      * In in the case of RDB, it can be the result set.
477      * @param fn A String containing the name of the field.
478      * @return The BigDecimal value of the field as stored within the composite
479      * name.
480      * @exception PExceptionTyping There is no field with this name and
481      * this type within this composite name.
482      * @exception PExceptionIO It is raised when a problem occurs while
483      * potentially accessing the DSI.
484      */

485     public BigDecimal JavaDoc pngetBigDecimalField(String JavaDoc fn, Object JavaDoc context)
486         throws PException {
487         Tuple currentTuple = (Tuple) context;
488         if (currentTuple == null) {
489             throw new PExceptionTyping("The context must be the tuple: " + context);
490         }
491         try {
492             Integer JavaDoc in = (Integer JavaDoc) fields.get(fn);
493             if (in == null)
494                 throw new PExceptionTyping("Field " + fn + " has not been found");
495             return currentTuple.getBigDecimal(in.intValue());
496         } catch (MedorException e) {
497             throw new PException(e.getMessage());
498         }
499     }
500
501     /**
502      * It retrieves the value of an int field.
503      * @param context A context used by PNameGetter to extract the related data.
504      * In in the case of RDB, it can be the result set.
505      * @param fn A String containing the name of the field.
506      * @return The BigInteger value of the field as stored within the composite
507      * name.
508      * @exception PExceptionTyping There is no field with this name and
509      * this type within this composite name.
510      * @exception PExceptionIO It is raised when a problem occurs while
511      * potentially accessing the DSI.
512      */

513     public BigInteger JavaDoc pngetBigIntegerField(String JavaDoc fn, Object JavaDoc context)
514         throws PException {
515         Tuple currentTuple = (Tuple) context;
516         if (currentTuple == null) {
517             throw new PExceptionTyping("The context must be the tuple: " + context);
518         }
519             try {
520                 Integer JavaDoc in = (Integer JavaDoc) fields.get(fn);
521                 if (in == null)
522                     throw new PExceptionTyping("Field " + fn + " has not been found");
523                 return currentTuple.getBigInteger(in.intValue());
524             }
525             catch (MedorException e) {
526                 throw new PException(e.getMessage());
527             }
528         }
529
530     /**
531      * It retrieves the value of a long field.
532      * @param context A context used by PNameGetter to extract the related data.
533      * In in the case of RDB, it can be the result set.
534      * @param fn A String containing the name of the field.
535      * @return The long value of the field as stored within the composite
536      * name.
537      * @exception PExceptionTyping There is no field with this name and
538      * this type within this composite name.
539      * @exception PExceptionIO It is raised when a problem occurs while
540      * potentially accessing the DSI.
541      */

542     public long pngetLongField(String JavaDoc fn, Object JavaDoc context)
543         throws PException {
544         Tuple currentTuple = (Tuple) context;
545         if (currentTuple == null) {
546             throw new PExceptionTyping("The context must be the tuple: " + context);
547         }
548         try {
549             Integer JavaDoc in = (Integer JavaDoc) fields.get(fn);
550             if (in == null)
551                 throw new PExceptionTyping("Field " + fn + " has not been found");
552             if (currentTuple.isDefined(in.intValue())) {
553                 long res = currentTuple.getLong(in.intValue());
554                 if (logger.isLoggable(BasicLevel.DEBUG)) {
555                     logger.log(BasicLevel.DEBUG, "PName field " + fn + ", value=" + res);
556                 }
557                 return res;
558             } else {
559                 if (png == null) {
560                     png = (PNameGetter)
561                         ((PNameCoder)pnc.getObject()).getNull().encodeAbstract();
562                 }
563                 return png.pngetLongField(fn, null);
564             }
565         }
566         catch (MedorException e) {
567             throw new PException(e.getMessage());
568         }
569     }
570
571     /**
572      * It retrieves the value of a string field.
573      * @param context A context used by PNameGetter to extract the related data.
574      * In in the case of RDB, it can be the result set.
575      * @param fn A String containing the name of the field.
576      * @return The java.lang.String value of the field as stored within the
577      * composite name.
578      * @exception PExceptionTyping There is no field with this name and
579      * this type within this composite name.
580      * @exception PExceptionIO It is raised when a problem occurs while
581      * potentially accessing the DSI.
582      */

583     public String JavaDoc pngetStringField(String JavaDoc fn, Object JavaDoc context)
584         throws PExceptionTyping, PExceptionIO {
585         Tuple currentTuple = (Tuple) context;
586         if (currentTuple == null) {
587             throw new PExceptionTyping("The context must be the tuple: " + context);
588         }
589         try {
590             Integer JavaDoc in = (Integer JavaDoc) fields.get(fn);
591             if (in == null)
592                 throw new PExceptionTyping("Field " + fn + " has not been found");
593             return currentTuple.getString(in.intValue());
594         }
595         catch (MedorException e) {
596             throw new PExceptionTyping(e.getMessage());
597         }
598     }
599 }
600
Popular Tags