/**
* Speedo: an implementation of JDO compliant personality on top of JORM generic
* I/O sub-system.
* Copyright (C) 2001-2006 France Telecom
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact: speedo@objectweb.org
*
* Authors: A. Lefebvre
*/
package org.objectweb.speedo.query.ejb.parser;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import org.objectweb.medor.api.Field;
import org.objectweb.medor.query.jorm.lib.QueryBuilder;
import org.objectweb.speedo.api.SpeedoException;
/**
* @author Alexandre Lefebvre
*/
public class EJBQLVariableVisitor extends EJBQLAbstractVisitor {
/**
* track which paths are collections
*/
private HashSet collections = new HashSet();
private QueryBuilder qb = new QueryBuilder();
/**
* field for each defined identifiers of the query.
*/
private HashMap fields = new HashMap();
public EJBQLVariableVisitor(SimpleNode ejbql) throws SpeedoException {
// parse the ejbql and build the ids structure
visit(ejbql);
try {
for (Iterator it = ids.keySet().iterator(); it.hasNext(); ) {
String id = (String) it.next();
IdValue idv = (IdValue) ids.get(id);
fields.put(id + "." + Field.PNAMENAME, qb.project(define(qb, idv.alias, idv.alias)));
for (int i = 0; i < idv.getDeclaredPathLength(); i++) {
String path = idv.getMergedPath(i);
if (!collections.contains(path)) {
fields.put(path, qb.project(path, define(qb, path, null)));
}
}
}
} catch (Exception e) {
throw new SpeedoException("Error during the parsing of EJBQL:", e);
}
}
/**
* get the Map that was built from visiting the lexical query tree This map
* allows to get the org.objectweb.medor.api.Field from its name (ident or
* path).
* @return the Fields map
*/
public Map getFields() {
return fields;
}
}