KickJava   Java API By Example, From Geeks To Geeks.

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


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 modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes 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,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.strongtyper;
31
32 import com.genimen.djeneric.repository.DjDomain;
33 import com.genimen.djeneric.repository.DjExtent;
34 import com.genimen.djeneric.repository.DjPersistenceManager;
35 import com.genimen.djeneric.repository.DjProperty;
36
37 /**
38  * Description of the Class
39  *
40  *@author Wido Riezebos
41  *@created 28 mei 2002
42  */

43 public class QbeGenerator extends Generator
44 {
45   DjExtent _extent;
46   DjPersistenceManager _mgr;
47
48   /**
49    * Constructor for the QbeGenerator object
50    *
51    *@param mgr Description of the Parameter
52    */

53   public QbeGenerator(DjPersistenceManager mgr)
54   {
55     _mgr = mgr;
56   }
57
58   /**
59    * Returns the extent of the QbeGenerator object
60    *
61    *@return The extent value
62    */

63   public DjExtent getExtent()
64   {
65     return _extent;
66   }
67
68   /**
69    * Sets the extent of the QbeGenerator object
70    *
71    *@param extent The new extent value
72    */

73   public void setExtent(DjExtent extent)
74   {
75     _extent = extent;
76   }
77
78   /**
79    * Returns the qbeInterfaceName of the QbeGenerator object
80    *
81    *@return The qbeInterfaceName value
82    */

83   public String JavaDoc getQbeInterfaceName()
84   {
85     return getQbeInterfaceName(getExtent());
86   }
87
88   /**
89    * Returns the className of the QbeGenerator object
90    *
91    *@return The className value
92    */

93   public String JavaDoc getClassName()
94   {
95     return getClassName(getExtent()) + (isAbstract() ? getGeneratedSuffix() : "");
96   }
97
98   /**
99    * Returns the className of the QbeGenerator object
100    *
101    *@param extent Description of the Parameter
102    *@return The className value
103    */

104   public String JavaDoc getClassName(DjExtent extent)
105   {
106     return getQbeClassName(extent);
107   }
108
109   /**
110    * Returns the packageName of the QbeGenerator object
111    *
112    *@return The packageName value
113    */

114   public String JavaDoc getPackageName()
115   {
116     return getImplPackageName();
117   }
118
119   /**
120    * Returns the code of the QbeGenerator object
121    *
122    *@return The code value
123    *@exception Exception Description of the Exception
124    */

125   public String JavaDoc getCode() throws Exception JavaDoc
126   {
127     StringBuffer JavaDoc code = new StringBuffer JavaDoc(5000);
128     if (getPackageName().trim().length() > 0) code.append("package " + getPackageName() + ";\n\n");
129
130     if (containsBigDecimal(getExtent())) code.append("import java.math.*;\n");
131     code.append("import " + getItfPackageName() + ".*;\n");
132     code.append("import com.genimen.djeneric.repository.*;\n\n");
133     code.append("import com.genimen.djeneric.repository.exceptions.*;\n\n");
134     code.append(StrongTyper.getRegenerationTags(1));
135
136     code.append("public class " + getClassName());
137     if (getExtent().getSuper() != null)
138     {
139       code.append(" extends " + getClassName(getExtent().getSuper()));
140     }
141     else
142     {
143       code.append(" extends DjQueryByExample");
144     }
145
146     code.append(" implements " + getQbeInterfaceName());
147
148     code.append("\n{\n");
149
150     code.append(" private static boolean propidx_set = false;\n");
151
152     for (int i = 0; i < _extent.getPropertyCount(); i++)
153     {
154       DjProperty prop = _extent.getProperty(i);
155       if (_extent.isInherited(prop)) continue;
156
157       code.append(" private static int PROPIDX_" + prop.getName().toUpperCase() + ";\n");
158     }
159
160     code.append("\n protected " + getClassName() + "(DjSession session, DjExtent extent) throws DjenericException\n"
161                 + " {\n" + " super(session, extent);\n" + " if(!propidx_set)\n" + " {\n"
162                 + " propidx_set = true;\n");
163
164     for (int i = 0; i < _extent.getPropertyCount(); i++)
165     {
166       DjProperty prop = _extent.getProperty(i);
167       if (_extent.isInherited(prop)) continue;
168       code.append(" PROPIDX_" + prop.getName().toUpperCase() + " = extent.getPropertyIndex(\"" + prop.getName()
169                   + "\");\n");
170     }
171
172     code.append(" }\n" + " }\n\n");
173
174     code.append(" public void setQueryOperator(String propertyName, String operator) throws "
175                 + getExceptionClassName() + "\n" + " {\n" + " try\n" + " {\n"
176                 + " setOperator(propertyName, operator);\n" + " }\n" + " catch(Exception x)\n" + " {\n"
177                 + " throw new " + getExceptionClassName() + "(x);\n" + " }\n" + " }\n\n");
178
179     for (int i = 0; i < _extent.getPropertyCount(); i++)
180     {
181       DjProperty prop = _extent.getProperty(i);
182       if (_extent.isInherited(prop)) continue;
183
184       String JavaDoc propInitcap = initCap(prop.getName());
185       String JavaDoc typeInitcap = initCap(prop.getNativeType());
186
187       // remove the [] from the type
188
if (prop.getTypeCode() == DjDomain.BYTE_TYPE) typeInitcap = "Bytes";
189
190       if (prop.getType() instanceof DjDomain)
191       {
192         code.append(" public " + translateNativeType(prop.getNativeType()) + " get" + propInitcap + "()\n" + " {\n"
193                     + " return get" + typeInitcap + "(PROPIDX_" + prop.getName().toUpperCase() + ");\n" + " }\n\n");
194
195         code.append(" public void set" + propInitcap + "(" + translateNativeType(prop.getNativeType())
196                     + " value) throws " + getExceptionClassName() + "\n" + " {\n" + " try\n" + " {\n"
197                     + " set" + typeInitcap + "(PROPIDX_" + prop.getName().toUpperCase() + ", value);\n"
198                     + " }\n" + " catch(Exception x)\n" + " {\n" + " throw new " + getExceptionClassName()
199                     + "(x);\n" + " }\n" + " }\n\n");
200       }
201       else
202       {
203         DjExtent extent = (DjExtent) prop.getType();
204         code.append(" public " + getInterfaceName(extent) + " get" + propInitcap + "() throws "
205                     + getExceptionClassName() + "\n" + " {\n" + " try\n" + " {\n" + " return " + "("
206                     + getInterfaceName(extent) + ")get(PROPIDX_" + prop.getName().toUpperCase() + ");\n" + " }\n"
207                     + " catch(Exception x)\n" + " {\n" + " throw new " + getExceptionClassName() + "(x);\n"
208                     + " }\n" + " }\n\n");
209
210         code.append(" public void set" + propInitcap + "(" + getInterfaceName(extent) + " value) throws "
211                     + getExceptionClassName() + "\n" + " {\n" + " try\n" + " {\n" +
212                     // PAS OP; _SUPER_ GEBRUIKEN (NIET HET QBE OBJ)
213
" set(PROPIDX_" + prop.getName().toUpperCase() + ", (" + super.getClassName(extent)
214                     + ")value);\n" + " }\n" + " catch(Exception x)\n" + " {\n" + " throw new "
215                     + getExceptionClassName() + "(x);\n" + " }\n" + " }\n\n");
216       }
217     }
218
219     code.append(StrongTyper.getRegenerationTags(0));
220     code.append("}\n");
221
222     return code.toString();
223   }
224
225 }
Popular Tags