01: /**
02: * Speedo: an implementation of JDO compliant personality on top of JORM generic
03: * I/O sub-system.
04: * Copyright (C) 2001-2006 France Telecom
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2 of the License, or (at your option) any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: *
20: * Contact: speedo@objectweb.org
21: *
22: * Authors: A. Lefebvre
23: */package org.objectweb.speedo.query.ejb.parser;
24:
25: import java.util.HashMap;
26: import java.util.HashSet;
27: import java.util.Iterator;
28: import java.util.Map;
29:
30: import org.objectweb.medor.api.Field;
31: import org.objectweb.medor.query.jorm.lib.QueryBuilder;
32: import org.objectweb.speedo.api.SpeedoException;
33:
34: /**
35: * @author Alexandre Lefebvre
36: */
37: public class EJBQLVariableVisitor extends EJBQLAbstractVisitor {
38:
39: /**
40: * track which paths are collections
41: */
42: private HashSet collections = new HashSet();
43:
44: private QueryBuilder qb = new QueryBuilder();
45:
46: /**
47: * field for each defined identifiers of the query.
48: */
49: private HashMap fields = new HashMap();
50:
51: public EJBQLVariableVisitor(SimpleNode ejbql)
52: throws SpeedoException {
53: // parse the ejbql and build the ids structure
54: visit(ejbql);
55:
56: try {
57: for (Iterator it = ids.keySet().iterator(); it.hasNext();) {
58: String id = (String) it.next();
59: IdValue idv = (IdValue) ids.get(id);
60: fields.put(id + "." + Field.PNAMENAME, qb
61: .project(define(qb, idv.alias, idv.alias)));
62: for (int i = 0; i < idv.getDeclaredPathLength(); i++) {
63: String path = idv.getMergedPath(i);
64: if (!collections.contains(path)) {
65: fields.put(path, qb.project(path, define(qb,
66: path, null)));
67: }
68: }
69: }
70: } catch (Exception e) {
71: throw new SpeedoException(
72: "Error during the parsing of EJBQL:", e);
73: }
74: }
75:
76: /**
77: * get the Map that was built from visiting the lexical query tree This map
78: * allows to get the org.objectweb.medor.api.Field from its name (ident or
79: * path).
80: * @return the Fields map
81: */
82: public Map getFields() {
83: return fields;
84: }
85:
86: }
|