01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software 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 software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.ejb.plugins.cmp.ejbql;
23:
24: import java.util.HashMap;
25: import java.util.List;
26: import java.util.Map;
27:
28: import org.jboss.ejb.plugins.cmp.bridge.EntityBridge;
29: import org.jboss.ejb.plugins.cmp.bridge.CMRFieldBridge;
30:
31: /**
32: * This class manages a symbol table for the EJB-QL parser.
33: *
34: * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
35: * @version $Revision: 57209 $
36: */
37: public final class IdentifierManager {
38: private final Catalog catalog;
39: private final Map pathLists = new HashMap();
40: private final Map fieldLists = new HashMap();
41: private final Map identifiers = new HashMap();
42:
43: public IdentifierManager(Catalog catalog) {
44: this .catalog = catalog;
45: }
46:
47: public void declareRangeVariable(String identifier,
48: String abstractSchemaName) {
49:
50: identifiers.put(identifier, catalog
51: .getEntityByAbstractSchemaName(abstractSchemaName));
52: }
53:
54: public void declareCollectionMember(String identifier, String path) {
55:
56: List fieldList = (List) fieldLists.get(path);
57: Object field = fieldList.get(fieldList.size() - 1);
58: if (!(field instanceof CMRFieldBridge)) {
59: throw new IllegalArgumentException(
60: "Path is collection valued: " + path);
61: }
62: CMRFieldBridge cmrField = (CMRFieldBridge) field;
63: if (cmrField.isSingleValued()) {
64: throw new IllegalArgumentException(
65: "Path is collection valued: " + path);
66: }
67: identifiers.put(identifier, cmrField.getRelatedEntity());
68: }
69:
70: public EntityBridge getEntity(String identificationVariable) {
71: return (EntityBridge) identifiers.get(identificationVariable);
72: }
73:
74: public void registerPath(String path, List pathList, List fieldList) {
75:
76: if (pathList.size() != fieldList.size()) {
77: throw new IllegalArgumentException(
78: "Path list and field list must "
79: + "have the same size: pathList.size="
80: + pathList.size() + " fieldList.size="
81: + fieldList.size());
82: }
83: pathLists.put(path, pathList);
84: fieldLists.put(path, fieldList);
85: }
86:
87: public List getPathList(String path) {
88: return (List) pathLists.get(path);
89: }
90:
91: public List getFieldList(String path) {
92: return (List) fieldLists.get(path);
93: }
94:
95: }
|