KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > strongtyper > ObjectGenerator


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.strongtyper;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.Arrays JavaDoc;
34
35 import com.genimen.djeneric.repository.DjDomain;
36 import com.genimen.djeneric.repository.DjExtent;
37 import com.genimen.djeneric.repository.DjPersistenceManager;
38 import com.genimen.djeneric.repository.DjProperty;
39 import com.genimen.djeneric.repository.DjRelation;
40
41 public class ObjectGenerator extends Generator
42 {
43   DjExtent _extent;
44   DjPersistenceManager _mgr;
45
46   public ObjectGenerator(DjPersistenceManager mgr)
47   {
48     _mgr = mgr;
49   }
50
51   public DjExtent getExtent()
52   {
53     return _extent;
54   }
55
56   public void setExtent(DjExtent extent)
57   {
58     _extent = extent;
59   }
60
61   public String JavaDoc getInterfaceName()
62   {
63     return getInterfaceName(getExtent());
64   }
65
66   public String JavaDoc getClassName()
67   {
68     return getClassName(getExtent());
69   }
70
71   public String JavaDoc getClassName(DjExtent extent)
72   {
73     return super.getClassName(extent) + (extent == getExtent() && isAbstract() ? getGeneratedSuffix() : "");
74   }
75
76   public String JavaDoc getPackageName()
77   {
78     return getImplPackageName();
79   }
80
81   public String JavaDoc getCode() throws Exception JavaDoc
82   {
83     StringBuffer JavaDoc code = new StringBuffer JavaDoc(5000);
84     if (getPackageName().trim().length() > 0) code.append("package " + getPackageName() + ";\n\n");
85
86     code.append("import " + getItfPackageName() + ".*;\n");
87     if (containsBigDecimal(getExtent())) code.append("import java.math.*;\n");
88     code.append("import com.genimen.djeneric.repository.*;\n");
89     code.append("import com.genimen.djeneric.repository.exceptions.*;\n\n");
90     code.append(StrongTyper.getRegenerationTags(1));
91
92     code.append("public class " + getClassName());
93     if (getExtent().getSuper() != null)
94     {
95       code.append(" extends " + getClassName(getExtent().getSuper()));
96     }
97     else
98     {
99       code.append(" extends " + getParentObjectClassName());
100     }
101     code.append(" implements " + getInterfaceName());
102
103     code.append("\n{\n");
104
105     code.append(" private static boolean propidx_set = false;\n");
106
107     for (int i = 0; i < _extent.getPropertyCount(); i++)
108     {
109       DjProperty prop = _extent.getProperty(i);
110       if (_extent.isInherited(prop)) continue;
111
112       code.append(" private static int PROPIDX_" + prop.getName().toUpperCase() + ";\n");
113     }
114
115     code.append("\n protected " + getClassName() + "(DjSession session, DjExtent extent) throws DjenericException\n"
116                 + " {\n" + " super(session, extent);\n" + " if(!propidx_set)\n" + " {\n"
117                 + " propidx_set = true;\n");
118
119     for (int i = 0; i < _extent.getPropertyCount(); i++)
120     {
121       DjProperty prop = _extent.getProperty(i);
122       if (_extent.isInherited(prop)) continue;
123       code.append(" PROPIDX_" + prop.getName().toUpperCase() + " = extent.getPropertyIndex(\"" + prop.getName()
124                   + "\");\n");
125     }
126
127     code.append(" }\n" + " }\n\n");
128
129     code.append(" public long determineObjectId() throws " + getExceptionClassName() + "\n" + " {\n" + " try\n"
130                 + " {\n" + " return getObjectId();\n" + " }\n" + " catch(Exception x)\n" + " {\n"
131                 + " throw new " + getExceptionClassName() + "(x);\n" + " }\n" + " }\n\n");
132
133     for (int i = 0; i < _extent.getPropertyCount(); i++)
134     {
135       DjProperty prop = _extent.getProperty(i);
136       if (_extent.isInherited(prop)) continue;
137
138       String JavaDoc propInitcap = initCap(prop.getName());
139       String JavaDoc typeInitcap = initCap(prop.getNativeType());
140
141       // remove the [] from the type
142
if (prop.getTypeCode() == DjDomain.BYTE_TYPE) typeInitcap = "Bytes";
143
144       if (prop.getType() instanceof DjDomain)
145       {
146         code.append(" public " + translateNativeType(prop.getNativeType()) + " get" + propInitcap + "()\n" + " {\n"
147                     + " return get" + typeInitcap + "(PROPIDX_" + prop.getName().toUpperCase() + ");\n" + " }\n\n");
148
149         code.append(" public boolean isNull" + propInitcap + "()\n" + " {\n" + " return isNull(PROPIDX_"
150                     + prop.getName().toUpperCase() + ");\n" + " }\n\n");
151
152         if (_extent.getIdProperty() != prop)
153         {
154           code.append(" public void set" + propInitcap + "(" + translateNativeType(prop.getNativeType())
155                       + " value) throws " + getExceptionClassName() + "\n" + " {\n" + " try\n" + " {\n"
156                       + " set" + typeInitcap + "(PROPIDX_" + prop.getName().toUpperCase() + ", value);\n"
157                       + " }\n" + " catch(Exception x)\n" + " {\n" + " throw new "
158                       + getExceptionClassName() + "(x);\n" + " }\n" + " }\n\n");
159           code.append(" public void setNull" + propInitcap + "() throws " + getExceptionClassName() + "\n" + " {\n"
160                       + " try\n" + " {\n" + " setNull(PROPIDX_" + prop.getName().toUpperCase() + ");\n"
161                       + " }\n" + " catch(Exception x)\n" + " {\n" + " throw new "
162                       + getExceptionClassName() + "(x);\n" + " }\n" + " }\n\n");
163         }
164       }
165       else
166       {
167         DjExtent extent = (DjExtent) prop.getType();
168         code.append(" public " + getInterfaceName(extent) + " get" + propInitcap + "() throws "
169                     + getExceptionClassName() + "\n" + " {\n" + " try\n" + " {\n" + " return " + "("
170                     + getInterfaceName(extent) + ")get(PROPIDX_" + prop.getName().toUpperCase() + ");\n" + " }\n"
171                     + " catch(Exception x)\n" + " {\n" + " throw new " + getExceptionClassName() + "(x);\n"
172                     + " }\n" + " }\n\n");
173         code.append(" public boolean isNull" + propInitcap + "()\n" + " {\n" + " return isNull(PROPIDX_"
174                     + prop.getName().toUpperCase() + ");\n" + " }\n\n");
175
176         code.append(" public void set" + propInitcap + "(" + getInterfaceName(extent) + " value) throws "
177                     + getExceptionClassName() + "\n" + " {\n" + " try\n" + " {\n" + " set(PROPIDX_"
178                     + prop.getName().toUpperCase() + ", (" + getClassName(extent) + ")value);\n" + " }\n"
179                     + " catch(Exception x)\n" + " {\n" + " throw new " + getExceptionClassName() + "(x);\n"
180                     + " }\n" + " }\n\n");
181
182         code.append(" public void setNull" + propInitcap + "() throws " + getExceptionClassName() + "\n" + " {\n"
183                     + " try\n" + " {\n" + " setNull(PROPIDX_" + prop.getName().toUpperCase() + ");\n"
184                     + " }\n" + " catch(Exception x)\n" + " {\n" + " throw new " + getExceptionClassName()
185                     + "(x);\n" + " }\n" + " }\n\n");
186       }
187     }
188
189     DjRelation[] rels = _extent.getDetailRelations();
190     boolean first = true;
191     for (int i = 0; i < rels.length; i++)
192     {
193       if (_extent.isInherited(rels[i])) continue;
194
195       if (!isInSelectedSet(rels[i].getDetailExtent())) continue;
196
197       if (first) code.append("\n // Set operations:\n\n");
198       first = false;
199       String JavaDoc propInitcap = initCap(rels[i].getName());
200       String JavaDoc detailPropName = initCap(rels[i].getDetailProperty().getName());
201       String JavaDoc varName = propInitcap.toLowerCase();
202
203       code
204           .append(" public " + getInterfaceName(rels[i].getDetailExtent()) + "[] get" + propInitcap + "() throws "
205                   + getExceptionClassName() + "\n" + " {\n" + " return get" + propInitcap + "(null);\n" + " }\n\n");
206
207       code.append(" public " + getInterfaceName(rels[i].getDetailExtent()) + "[] get" + propInitcap + "("
208                   + getQbeInterfaceName(rels[i].getDetailExtent()) + " qbeItf) throws " + getExceptionClassName()
209                   + "\n" + " {\n" + " try\n" + " {\n" + " DjList result;\n"
210                   + " DjQueryByExample qbe = (DjQueryByExample) qbeItf;\n"
211                   + " DjAssociation assoc = getDetailAssociationByName(\"" + rels[i].getName() + "\");\n"
212                   + " if(qbe == null) result = assoc.getObjects();\n"
213                   + " else result = assoc.getObjects(qbe, false);\n"
214                   + " result.sort(assoc.getDetailExtent());\n" + " return ("
215                   + getInterfaceName(rels[i].getDetailExtent()) + "[])result.toArray(new "
216                   + getInterfaceName(rels[i].getDetailExtent()) + "[0]);\n" + " }\n" + " catch(Exception x)\n"
217                   + " {\n" + " throw new " + getExceptionClassName() + "(x);\n" + " }\n" + " }\n\n");
218
219       code.append(" public " + getCursorInterfaceName(rels[i].getDetailExtent()) + " get" + propInitcap + "Cursor("
220                   + getQbeInterfaceName(rels[i].getDetailExtent()) + " qbeItf) throws " + getExceptionClassName()
221                   + "\n" + " {\n" + " try\n" + " {\n"
222                   + " DjQueryByExample qbe = (DjQueryByExample) qbeItf;\n" + " return new "
223                   + getCursorClassName(rels[i].getDetailExtent()) + "(getDetailAssociationByName(\""
224                   + rels[i].getName() + "\").getObjectsCursor(qbe, false));\n" + " }\n" + " catch(Exception x)\n"
225                   + " {\n" + " throw new " + getExceptionClassName() + "(x);\n" + " }\n" + " }\n\n");
226
227       code.append(" public " + getCursorInterfaceName(rels[i].getDetailExtent()) + " get" + propInitcap
228                   + "Cursor() throws " + getExceptionClassName() + "\n" + " {\n" + " try\n" + " {\n"
229                   + " return new " + getCursorClassName(rels[i].getDetailExtent())
230                   + "(getDetailAssociationByName(\"" + rels[i].getName() + "\").getObjectsCursor(false));\n"
231                   + " }\n" + " catch(Exception x)\n" + " {\n" + " throw new " + getExceptionClassName()
232                   + "(x);\n" + " }\n" + " }\n\n");
233
234       ArrayList JavaDoc allDetailsList = new ArrayList JavaDoc();
235       allDetailsList.add(rels[i].getDetailExtent());
236       allDetailsList.addAll(Arrays.asList(_mgr.getSubExtentsOf(rels[i].getDetailExtent())));
237       DjExtent[] allDetails = (DjExtent[]) allDetailsList.toArray(new DjExtent[0]);
238
239       for (int d = 0; d < allDetails.length; d++)
240       {
241         if (!isInSelectedSet(allDetails[d])) continue;
242
243         code.append(" public " + getInterfaceName(allDetails[d]) + " create" + allDetails[d].getObjectType() + "In"
244                     + propInitcap + "() throws " + getExceptionClassName() + "\n" + " {\n" + " try\n" + " {\n"
245                     + " " + getSessionClassName() + " ses = (" + getSessionClassName() + ") getSession();\n"
246                     + " " + getInterfaceName(allDetails[d]) + " obj = ses.create" + allDetails[d].getObjectType()
247                     + "();\n" + " obj.set" + detailPropName + "(this);\n" + " return obj;\n" + " }\n"
248                     + " catch(Exception x)\n" + " {\n" + " throw new " + getExceptionClassName() + "(x);\n"
249                     + " }\n" + " }\n\n");
250       }
251     }
252
253     code.append(StrongTyper.getRegenerationTags(0));
254     code.append("}\n");
255     return code.toString();
256   }
257
258 }
Popular Tags