KickJava   Java API By Example, From Geeks To Geeks.

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


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.DjExtent;
33
34 public class AbstractSessionGenerator extends Generator
35 {
36   DjExtent[] _extents = null;
37
38   public AbstractSessionGenerator(DjExtent[] extents) throws Exception JavaDoc
39   {
40     _extents = extents;
41     if (extents == null || extents.length == 0) throw new Exception JavaDoc(
42         "Can not generate a session without any Extents selected");
43   }
44
45   public String JavaDoc getClassName()
46   {
47     return getAbstractSessionClassName() + (isAbstract() ? getGeneratedSuffix() : "");
48   }
49
50   public String JavaDoc getPackageName()
51   {
52     return getImplPackageName();
53   }
54
55   public String JavaDoc getCode() throws Exception JavaDoc
56   {
57     StringBuffer JavaDoc code = new StringBuffer JavaDoc(5000);
58     if (getPackageName().trim().length() > 0) code.append("package " + getPackageName() + ";\n\n");
59
60     code.append("import java.util.*;\n");
61     code.append("import " + getItfPackageName() + ".*;\n");
62     code.append("import com.genimen.djeneric.repository.*;\n");
63     code.append("import com.genimen.djeneric.repository.rdbms.*;\n");
64     code.append("import com.genimen.djeneric.repository.exceptions.*;\n\n");
65     code.append(StrongTyper.getRegenerationTags(1));
66     code.append("public class " + getClassName() + " extends RdbmsSession implements " + getSessionInterfaceName()
67                 + "\n{\n");
68
69     code.append("public static String NO_OBJECT_FOUND = \"NO_OBJECT_FOUND\";\n");
70     code.append("public static String MORE_THAN_1_OBJECT_FOUND = \"MORE_THAN_1_OBJECT_FOUND\";\n");
71
72     for (int i = 0; i < _extents.length; i++)
73     {
74       code.append(" protected DjExtent _" + _extents[i].getName() + "Extent;\n");
75     }
76     code.append("\n" + " protected " + getClassName() + "(" + getManagerClassName()
77                 + " mgr) throws ObjectNotDefinedException\n" + " { \n" + " super(mgr);\n");
78
79     for (int i = 0; i < _extents.length; i++)
80     {
81       code.append(" _" + _extents[i].getName() + "Extent = mgr.getExtentByObjectType(\""
82                   + _extents[i].getObjectType() + "\");\n");
83     }
84     code.append(" }\n\n" + " public DjObject createObject(DjExtent extent) throws DjenericException\n" + " { \n"
85                 + " if(!isValid())\n" + " throw new DjenericException(\"Session closed\");\n"
86                 + " DjObject po = null;\n");
87     for (int i = 0; i < _extents.length; i++)
88     {
89       if (i != 0) code.append(" ");
90       code.append(" if(extent == _" + _extents[i].getName() + "Extent) po = new " + getClassName(_extents[i])
91                   + "(this, extent);\n" + " else\n");
92     }
93     code.append(" po = super.createObject(extent);\n" + " return po;\n" + " }\n\n");
94
95     code.append(" public DjQueryByExample createQueryByExample(DjExtent extent) throws DjenericException\n" + " { \n"
96                 + " if(!isValid())\n" + " throw new DjenericException(\"Session closed\");\n"
97                 + " DjQueryByExample qbe = null;\n");
98     for (int i = 0; i < _extents.length; i++)
99     {
100       if (i != 0) code.append(" ");
101       code.append(" if(extent == _" + _extents[i].getName() + "Extent) qbe = new " + getQbeClassName(_extents[i])
102                   + "(this, extent);\n" + " else\n");
103     }
104     code.append(" qbe = super.createQueryByExample(extent);\n" + " return qbe;\n" + " }\n\n");
105
106     for (int i = 0; i < _extents.length; i++)
107     {
108       code.append(" ");
109
110       code.append("public " + getInterfaceName(_extents[i]) + " create" + _extents[i].getObjectType() + "() throws "
111                   + getExceptionClassName() + "\n" + " {\n" + " try\n" + " {\n" + " return ("
112                   + getInterfaceName(_extents[i]) + ") createObject(_" + _extents[i].getName() + "Extent);\n"
113                   + " }\n" + " catch(Exception x)\n" + " {\n" + " throw new " + getExceptionClassName()
114                   + "(x);\n" + " }\n" + " }\n\n");
115       code.append(" public " + getQbeInterfaceName(_extents[i]) + " create" + _extents[i].getObjectType()
116                   + "Qbe() throws " + getExceptionClassName() + "\n" + " {\n" + " try\n" + " {\n"
117                   + " return (" + getQbeInterfaceName(_extents[i]) + ") createQueryByExample(_"
118                   + _extents[i].getName() + "Extent);\n" + " }\n" + " catch(Exception x)\n" + " {\n"
119                   + " throw new " + getExceptionClassName() + "(x);\n" + " }\n" + " }\n\n");
120       code.append(" public " + getOqlInterfaceName() + " create" + _extents[i].getObjectType() + "Oql() throws "
121                   + getExceptionClassName() + "\n" + " {\n" + " try\n" + " {\n" + " return ("
122                   + getOqlInterfaceName() + ") new " + getOqlClassName() + "(_" + _extents[i].getName() + "Extent);\n"
123                   + " }\n" + " catch(Exception x)\n" + " {\n" + " throw new " + getExceptionClassName()
124                   + "(x);\n" + " }\n" + " }\n\n");
125     }
126
127     for (int i = 0; i < _extents.length; i++)
128     {
129       code.append(" public " + getInterfaceName(_extents[i]) + " get" + _extents[i].getObjectType()
130                   + "(long objectId) throws " + getExceptionClassName() + "\n" + " {\n" + " try\n" + " {\n"
131                   + " return (" + getInterfaceName(_extents[i]) + ") getObject(_" + _extents[i].getName()
132                   + "Extent, objectId);\n" + " }\n" + " catch(Exception x)\n" + " {\n" + " throw new "
133                   + getExceptionClassName() + "(x);\n" + " }\n" + " }\n\n");
134
135       code.append(" public " + getInterfaceName(_extents[i]) + "[] get" + _extents[i].getObjectType()
136                   + "Set() throws " + getExceptionClassName() + "\n" + " {\n" + " try\n" + " {\n"
137                   + " return (" + getInterfaceName(_extents[i]) + "[]) getObjects(_" + _extents[i].getName()
138                   + "Extent).toArray(new " + getInterfaceName(_extents[i]) + "[0]);\n" + " }\n"
139                   + " catch(Exception x)\n" + " {\n" + " throw new " + getExceptionClassName() + "(x);\n"
140                   + " }\n" + " }\n\n");
141
142       code.append(" public " + getCursorInterfaceName(_extents[i]) + " get" + _extents[i].getObjectType()
143                   + "Cursor() throws " + getExceptionClassName() + "\n" + " {\n" + " try\n" + " {\n"
144                   + " return new " + getCursorClassName(_extents[i]) + "(getObjectsCursor(_"
145                   + _extents[i].getName() + "Extent));\n" + " }\n" + " catch(Exception x)\n" + " {\n"
146                   + " throw new " + getExceptionClassName() + "(x);\n" + " }\n" + " }\n\n");
147
148       code.append(" public " + getCursorInterfaceName(_extents[i]) + " get" + _extents[i].getObjectType() + "Cursor("
149                   + getQbeInterfaceName(_extents[i]) + " qbe) throws " + getExceptionClassName() + "\n" + " {\n"
150                   + " try\n" + " {\n" + " return new " + getCursorClassName(_extents[i])
151                   + "(getObjectsCursor((DjQueryByExample)qbe));\n" + " }\n" + " catch(Exception x)\n" + " {\n"
152                   + " throw new " + getExceptionClassName() + "(x);\n" + " }\n" + " }\n\n");
153
154       code.append(" public " + getCursorInterfaceName(_extents[i]) + " get" + _extents[i].getObjectType() + "Cursor("
155                   + getOqlInterfaceName() + " oql) throws " + getExceptionClassName() + "\n" + " {\n" + " try\n"
156                   + " {\n" + " return new " + getCursorClassName(_extents[i])
157                   + "(getObjectsCursor((DjOql)oql));\n" + " }\n" + " catch(Exception x)\n" + " {\n"
158                   + " throw new " + getExceptionClassName() + "(x);\n" + " }\n" + " }\n\n");
159
160       code.append(" public " + getInterfaceName(_extents[i]) + "[] get" + _extents[i].getObjectType() + "Set("
161                   + getQbeInterfaceName(_extents[i]) + " qbe) throws " + getExceptionClassName() + "\n" + " {\n"
162                   + " try\n" + " {\n" + " return (" + getInterfaceName(_extents[i]) + "[]) getObjects(("
163                   + getQbeClassName(_extents[i]) + ")qbe).toArray(new " + getInterfaceName(_extents[i]) + "[0]);\n"
164                   + " }\n" + " catch(Exception x)\n" + " {\n" + " throw new " + getExceptionClassName()
165                   + "(x);\n" + " }\n" + " }\n\n");
166
167       code.append(" public " + getInterfaceName(_extents[i]) + "[] get" + _extents[i].getObjectType() + "Set("
168                   + getOqlInterfaceName() + " oql) throws " + getExceptionClassName() + "\n" + " {\n" + " try\n"
169                   + " {\n" + " return (" + getInterfaceName(_extents[i])
170                   + "[]) getObjects((DjOql)oql).toArray(new " + getInterfaceName(_extents[i]) + "[0]);\n" + " }\n"
171                   + " catch(Exception x)\n" + " {\n" + " throw new " + getExceptionClassName() + "(x);\n"
172                   + " }\n" + " }\n\n");
173
174       code.append(" public " + getInterfaceName(_extents[i]) + " get" + _extents[i].getObjectType() + "("
175                   + getQbeInterfaceName(_extents[i]) + " qbe) throws " + getExceptionClassName() + ", \n"
176                   + getNotFoundExceptionClassName() + ", \n" + getMultipleFoundExceptionClassName() + " \n" + " {\n"
177                   + " DjList result = null;\n" + " try\n" + " {\n" + " result = getObjects(("
178                   + getQbeClassName(_extents[i]) + ")qbe);\n" + " }\n" + " catch(Exception x)\n" + " {\n"
179                   + " throw new " + getExceptionClassName() + "(x);\n" + " }\n"
180                   + " if(result.size() == 0) throw new " + getNotFoundExceptionClassName() + "(\""
181                   + getInterfaceName(_extents[i]) + " not found\");\n" + " if(result.size() > 1) throw new "
182                   + getMultipleFoundExceptionClassName() + "(\"More than 1 " + getInterfaceName(_extents[i])
183                   + " found\");\n" + " return (" + getInterfaceName(_extents[i])
184                   + ")result.getDjenericObjectAt(0);\n" + " }\n\n");
185
186       code.append(" public " + getInterfaceName(_extents[i]) + " get" + _extents[i].getObjectType() + "("
187                   + getOqlInterfaceName() + " oql) throws " + getExceptionClassName() + ", \n"
188                   + getNotFoundExceptionClassName() + ", \n" + getMultipleFoundExceptionClassName() + " \n" + " {\n"
189                   + " DjList result = null;\n" + " try\n" + " {\n"
190                   + " result = getObjects((DjOql)oql);\n" + " }\n" + " catch(Exception x)\n" + " {\n"
191                   + " throw new " + getExceptionClassName() + "(x);\n" + " }\n"
192                   + " if(result.size() == 0) throw new " + getNotFoundExceptionClassName() + "(\""
193                   + getInterfaceName(_extents[i]) + " not found\");\n" + " if(result.size() > 1) throw new "
194                   + getMultipleFoundExceptionClassName() + "(\"More than 1 " + getInterfaceName(_extents[i])
195                   + " found\");\n" + " return (" + getInterfaceName(_extents[i])
196                   + ")result.getDjenericObjectAt(0);\n" + " }\n\n");
197     }
198
199     code.append(" public HashMap[] getDomains() throws " + getExceptionClassName() + "\n" + " {\n" + " try\n"
200                 + " {\n" + " DjDomain[] domains = getPersistenceManager().getDomains();\n"
201                 + " HashMap[] result = new HashMap[domains.length];\n"
202                 + " for(int i = 0; i < domains.length; i++)\n" + " {\n"
203                 + " result[i] = doGetDomainDefinition(domains[i]);\n" + " }\n"
204                 + " return result;\n" + " }\n" + " catch(Exception x)\n" + " {\n"
205                 + " throw new " + getExceptionClassName() + "(x);\n" + " }\n" + " }\n" + "\n"
206                 + " public HashMap getDomain(String domainName) throws " + getExceptionClassName() + "\n" + " {\n"
207                 + " try\n" + " {\n"
208                 + " return doGetDomainDefinition(getPersistenceManager().getDomain(domainName));\n"
209                 + " }\n" + " catch(Exception x)\n" + " {\n" + " throw new "
210                 + getExceptionClassName() + "(x);\n" + " }\n" + " }\n" + "\n"
211                 + " private Object[] getDomainValues(DjDomain domain) throws DjenericException\n" + " {\n"
212                 + " DjDomainValue[] validValues = domain.getValidValues();\n"
213                 + " Object[] result = new Object[validValues.length];\n" + "\n"
214                 + " for(int i = 0; i < validValues.length; i++)\n"
215                 + " { result[i] = validValues[i].getValue();\n" + " }\n" + " return result;\n" + " }\n"
216                 + "\n" + " private String[] getDomainValueDescriptions(DjDomain domain) throws DjenericException\n"
217                 + " {\n" + " DjDomainValue[] validValues = domain.getValidValues();\n"
218                 + " String[] result = new String[validValues.length];\n" + "\n"
219                 + " for(int i = 0; i < validValues.length; i++)\n"
220                 + " { result[i] = validValues[i].getDescription();\n" + " }\n" + " return result;\n"
221                 + " }\n" + "\n"
222                 + " private HashMap doGetDomainDefinition(DjDomain domain) throws DjenericException\n" + " {\n"
223                 + " HashMap result = new HashMap();\n" + " result.put(" + getSessionInterfaceName()
224                 + ".DOMAIN_NAME, domain.getName());\n" + " result.put(" + getSessionInterfaceName()
225                 + ".DOMAIN_DECIMALS, new Integer(domain.getDecimals()));\n" + " result.put("
226                 + getSessionInterfaceName() + ".DOMAIN_DESCRIPTION, domain.getDescription());\n" + " result.put("
227                 + getSessionInterfaceName() + ".DOMAIN_ENFORCE, new Boolean(domain.isEnforced()));\n"
228                 + " result.put(" + getSessionInterfaceName() + ".DOMAIN_FORMAT_MASK, domain.getFormatMask());\n"
229                 + " result.put(" + getSessionInterfaceName() + ".DOMAIN_LENGTH, new Integer(domain.getLength()));\n"
230                 + " result.put(" + getSessionInterfaceName() + ".DOMAIN_TYPE, domain.getNativeTypeAsObject());\n"
231                 + " result.put(" + getSessionInterfaceName() + ".DOMAIN_VALUES, getDomainValues(domain));\n"
232                 + " result.put(" + getSessionInterfaceName()
233                 + ".DOMAIN_VALUE_DESCRIPTIONS, getDomainValueDescriptions(domain));\n" + " return result;\n"
234                 + " }\n");
235
236     code.append(StrongTyper.getRegenerationTags(0));
237     code.append("\n}\n");
238
239     return code.toString();
240   }
241
242 }
Popular Tags