KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > mapper > rdb > generator > RdbGenRef


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * 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: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.mapper.rdb.generator;
25
26 import org.objectweb.jorm.metainfo.api.NameDef;
27 import org.objectweb.jorm.metainfo.api.PrimitiveElement;
28 import org.objectweb.jorm.api.PException;
29
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34
35 /**
36  * Defines the structure of a reference of a JORM class. This structure is
37  * either a simple column or a composite name.
38  * In the case of a simple column, only refColumn is defined (not null).
39  * In the case of a composite name, all variables are defined except refColumn
40  * that is null.
41  * @author P. Dechamboux
42  */

43 public class RdbGenRef {
44     /**
45      * The name of the composite name used, if it is a composite name.
46      */

47     public String JavaDoc cnName = null;
48     /**
49      * An index in [1..n] is associated with each composite name reference.
50      */

51     public int cnId = -1;
52     /**
53      * The package name of the composite name.
54      */

55     public String JavaDoc cnPackage = null;
56     /**
57      * It associates a projection column for each field of the composite name.
58      * < name of the field in the composite name (String) -> RdbGenColumn >
59      */

60     public Map JavaDoc cnFieldColumns = null;
61     /**
62      * The column associated with this reference (non composite name).
63      * Null if the namedef is composite, otherwise directly references
64      * the column encoding the name
65      */

66     public RdbGenColumn refColumn = null;
67
68     public NameDef nd = null;
69
70     /**
71      * name of the reference field. If null the object
72      * describes the mapping of the Id
73      */

74     public String JavaDoc fieldName = null;
75
76     public RdbGenJoin join = null;
77
78     public String JavaDoc getFieldName() {
79         return fieldName;
80     }
81
82     public NameDef getNd() {
83         return nd;
84     }
85     public String JavaDoc getCnName() {
86         return cnName;
87     }
88
89     public int getCnId() {
90         return cnId;
91     }
92
93     public String JavaDoc getCnPackage() {
94         return cnPackage;
95     }
96
97     public String JavaDoc getCnFQName() {
98         return (cnPackage == null ? cnName : cnPackage + "." + cnName);
99     }
100
101     public Map JavaDoc getCnFieldColumns() {
102         return cnFieldColumns;
103     }
104
105     public RdbGenColumn getRefColumn() {
106         return refColumn;
107     }
108
109     /**
110      * Gets the composite name field that is mapped to the given column.
111      * @param rgc The involved column.
112      * @return The field name of the composite name that is mapped to this
113      * column.
114      */

115     public String JavaDoc getFieldName(RdbGenColumn rgc) {
116         if (rgc.joinCol != null) {
117             return getFieldName(rgc.joinCol);
118         }
119         if (refColumn != null) {
120             return null;
121         }
122         Iterator JavaDoc it = cnFieldColumns.keySet().iterator();
123         while (it.hasNext()) {
124             String JavaDoc res = (String JavaDoc) it.next();
125             if (cnFieldColumns.get(res) == rgc) {
126                 return res;
127             }
128         }
129         return null;
130     }
131
132     public String JavaDoc getClassFieldName(String JavaDoc compositeFieldName) {
133         return (String JavaDoc) nd.getNameRef().getProjection().get(compositeFieldName);
134     }
135
136     public String JavaDoc getAccessorValue(boolean isSpecific) {
137         if (isSpecific) {
138             return "paccessor.paGet"
139                     + RdbGenInfos.commonHelper.upperFL(fieldName) + "(conn)";
140         } else {
141             return "paccessorGen.paGetRefField("
142                     + fieldName + "_NAME, conn)";
143         }
144
145     }
146
147     public RdbGenJoin getJoin(RdbGenColumn rgc) throws PException {
148         return getJoin(rgc, nd.getFieldName());
149     }
150
151     public RdbGenJoin getJoin(RdbGenColumn rgc,
152                               String JavaDoc fn) throws PException {
153         try {
154             if (rgc.pes == null) {
155                 throw new PException("No join needed to reach the field " + fn
156                                      + " over the " + rgc.columnName + " column.");
157             }
158             int i = 0;
159             while (i < rgc.pes.size()
160                     && !fn.equals(((PrimitiveElement) rgc.pes.get(i)).getName())) {
161                 i++;
162             }
163             if (i < rgc.pes.size()) {
164                 RdbGenJoin rgj = (RdbGenJoin) rgc.joins.get(i);
165                 if (rgj == null) {
166                     new PException("No RdbGenJoin found for the field '"
167                             + fn + "' and the column '"
168                             + rgc.columnName + "'.").printStackTrace();
169                 }
170                 return rgj;
171             } else {
172                 throw new PException("Field " + fn + " is not mapped over the "
173                                      + rgc.columnName + " column.");
174             }
175         } catch (RuntimeException JavaDoc e) {
176             e.printStackTrace();
177             throw e;
178         }
179     }
180     public Set JavaDoc getcnField2ColumnName() {
181         Map JavaDoc p = new HashMap JavaDoc();
182         try {
183         if (nd.isFieldName()) {
184             //No alias is need because the table containing identifier is the
185
//main table.
186
p.put("", refColumn.table.tableName + "." + refColumn.columnName);
187         } else if (nd.isNameRef()) {
188             
189             for (Iterator JavaDoc iter = cnFieldColumns.entrySet().iterator(); iter.hasNext();) {
190                 Map.Entry JavaDoc me = (Map.Entry JavaDoc) iter.next();
191                 RdbGenColumn rgc = (RdbGenColumn) me.getValue();
192                 //No alias is need because the table containing identifier is the
193
//main table.
194
p.put(me.getKey(), rgc.table.tableName + "." + rgc.columnName);
195             }
196         }
197         }catch(RuntimeException JavaDoc e){
198             System.out.println(this);
199             e.printStackTrace();throw e;};
200         return p.entrySet();
201         
202     }
203     
204     public String JavaDoc toString() {
205         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(super.toString());
206         sb.append(" / cnId=").append(cnId);
207         sb.append(" / cnPackage=").append(cnPackage);
208         sb.append(" / cnFieldColumns=").append(cnFieldColumns);
209         sb.append(" / refColumn=").append(refColumn);
210         sb.append(" / nd=").append(nd);
211         if (nd != null) {
212             if (nd.isNameRef()) {
213                 sb.append(" / nd.nr.cn=").append(nd.getNameRef().getCompositeName());
214                 sb.append(" / nd.nr.cn=").append(nd.getNameRef().getProjection());
215             }
216         }
217         sb.append(" / fieldName=").append(fieldName);
218         sb.append(" / join=").append(join);
219         return sb.toString();
220     }
221 }
222
Popular Tags