KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > ql > PathExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.ql;
30
31 import com.caucho.amber.field.IdField;
32 import com.caucho.amber.type.EntityType;
33 import com.caucho.config.ConfigException;
34 import com.caucho.ejb.cfg.CmpField;
35 import com.caucho.ejb.cfg.CmrRelation;
36 import com.caucho.ejb.cfg.EjbEntityBean;
37 import com.caucho.util.CharBuffer;
38
39 import java.util.ArrayList JavaDoc;
40 import java.util.Collections JavaDoc;
41
42 /**
43  * Generated expression a relation path.
44  *
45  * tableA.x = tableB.y
46  */

47 abstract class PathExpr extends Expr {
48   protected EjbEntityBean _bean;
49
50   PathExpr(EjbEntityBean bean)
51   {
52     _bean = bean;
53
54     setJavaType(bean.getEJBClass());
55   }
56
57   /**
58    * Returns the bean.
59    */

60   EjbEntityBean getBean()
61   {
62     return _bean;
63   }
64
65   /**
66    * Gets the Java Type of the expression.
67    */

68   /*
69   public Class getJavaType()
70   {
71     return _bean.getLocal();
72   }
73   */

74   
75   /**
76    * Create field
77    */

78   Expr newField(String JavaDoc fieldName)
79     throws ConfigException
80   {
81     CmpField field = _bean.getCmpField(fieldName);
82
83     if (field != null)
84       return new FieldExpr(_query, this, fieldName, field);
85
86     CmrRelation relation = _bean.getRelation(fieldName);
87
88     if (relation == null)
89       throw error(L.l("{0}: `{1}' is an unknown cmp-field.",
90               _bean.getEJBClass().getName(),
91                       fieldName));
92
93     // _query.getPersistentBean().addBeanDepend(relation.getTargetBean().getName());
94

95     if (relation.isCollection())
96       return new CollectionExpr(_query, this, fieldName, relation);
97     else
98       return new RelationExpr(_query, this, fieldName, relation);
99   }
100
101   abstract String JavaDoc getKeyTable();
102   abstract String JavaDoc []getKeyFields();
103
104   abstract String JavaDoc getTable();
105
106   int getComponentCount()
107   {
108     return getKeyFields().length;
109   }
110
111   void setUsesField()
112   {
113   }
114
115   protected String JavaDoc generateKeyField(EntityType type, int index)
116   {
117     ArrayList JavaDoc<String JavaDoc> names = generateKeyFields(type);
118
119     return names.get(index);
120   }
121
122   protected ArrayList JavaDoc<String JavaDoc> generateKeyFields(EntityType type)
123   {
124     ArrayList JavaDoc<String JavaDoc> names = new ArrayList JavaDoc<String JavaDoc>();
125
126     for (IdField key : type.getId().getKeys()) {
127       String JavaDoc name = key.getName();
128
129       if (key.getType() instanceof EntityType) {
130     ArrayList JavaDoc<String JavaDoc> subNames;
131     subNames = generateKeyFields((EntityType) key.getType());
132
133     for (String JavaDoc subName : subNames) {
134       names.add(name + '.' + subName);
135     }
136       }
137       else
138     names.add(name);
139     }
140
141     Collections.sort(names);
142
143     return names;
144   }
145
146   void generateAmber(CharBuffer cb)
147   {
148     throw new UnsupportedOperationException JavaDoc(getClass().getName());
149   }
150 }
151
Popular Tags