KickJava   Java API By Example, From Geeks To Geeks.

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


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
10  * materialsgetGeneratedSuffix() provided with the distribution. - All
11  * advertising materials mentioning features or use of this software must
12  * display the 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
15  * BV. - Redistributions of any form whatsoever must retain the following
16  * acknowledgment: "This 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, 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 InterfaceGenerator extends Generator
42 {
43   DjExtent _extent;
44
45   DjPersistenceManager _mgr;
46
47   public String JavaDoc getPackageName()
48   {
49     return getItfPackageName();
50   }
51
52   public InterfaceGenerator(DjPersistenceManager mgr)
53   {
54     _mgr = mgr;
55   }
56
57   public DjExtent getExtent()
58   {
59     return _extent;
60   }
61
62   public void setExtent(DjExtent extent)
63   {
64     _extent = extent;
65   }
66
67   public String JavaDoc getClassName()
68   {
69     return getInterfaceName(getExtent()) + (isAbstract() ? getGeneratedSuffix() : "");
70   }
71
72   public String JavaDoc getInterfaceName()
73   {
74     return getInterfaceName(getExtent());
75   }
76
77   public String JavaDoc getCode() throws Exception JavaDoc
78   {
79     StringBuffer JavaDoc code = new StringBuffer JavaDoc(5000);
80     if (getPackageName().trim().length() > 0) code.append("package " + getPackageName() + ";\n\n");
81
82     if (containsBigDecimal(getExtent())) code.append("import java.math.*;\n");
83     code.append(StrongTyper.getRegenerationTags(1));
84
85     code.append("public interface " + getClassName());
86     if (getExtent().getSuper() != null)
87     {
88       code.append(" extends " + getInterfaceName(getExtent().getSuper()));
89     }
90     else
91     {
92       code.append(" extends " + getParentInterfaceClassName());
93     }
94
95     code.append("\n{\n");
96
97     code.append(" public long determineObjectId() throws " + getExceptionClassName() + ";\n");
98
99     for (int i = 0; i < _extent.getPropertyCount(); i++)
100     {
101       DjProperty prop = _extent.getProperty(i);
102       if (_extent.isInherited(prop)) continue;
103
104       String JavaDoc propInitcap = initCap(prop.getName());
105
106       if (prop.getType() instanceof DjDomain)
107       {
108         code.append(" public " + translateNativeType(prop.getNativeType()) + " get" + propInitcap + "();\n");
109         code.append(" public boolean isNull" + propInitcap + "();\n");
110         if (_extent.getIdProperty() != prop)
111         {
112           code.append(" public void set" + propInitcap + "(" + translateNativeType(prop.getNativeType())
113                       + " value) throws " + getExceptionClassName() + ";\n");
114           code.append(" public void setNull" + propInitcap + "() throws " + getExceptionClassName() + ";\n");
115         }
116       }
117       else
118       {
119         DjExtent extent = (DjExtent) prop.getType();
120         code.append(" public " + getInterfaceName(extent) + " get" + propInitcap + "() throws "
121                     + getExceptionClassName() + ";\n");
122         code.append(" public boolean isNull" + propInitcap + "();\n");
123         code.append(" public void set" + propInitcap + "(" + getInterfaceName(extent) + " value) throws "
124                     + getExceptionClassName() + ";\n");
125         code.append(" public void setNull" + propInitcap + "() throws " + getExceptionClassName() + ";\n");
126       }
127       code.append("\n");
128     }
129
130     DjRelation[] rels = _extent.getDetailRelations();
131     boolean first = true;
132     for (int i = 0; i < rels.length; i++)
133     {
134       if (_extent.isInherited(rels[i])) continue;
135
136       if (!isInSelectedSet(rels[i].getDetailExtent())) continue;
137
138       if (first) code.append("\n // Set operations:\n\n");
139       first = false;
140       String JavaDoc propInitcap = initCap(rels[i].getName());
141       String JavaDoc varName = propInitcap.toLowerCase();
142
143       code.append(" public " + getInterfaceName(rels[i].getDetailExtent()) + "[] get" + propInitcap + "() throws "
144                   + getExceptionClassName() + ";\n" + " public " + getInterfaceName(rels[i].getDetailExtent())
145                   + "[] get" + propInitcap + "(" + getQbeInterfaceName(rels[i].getDetailExtent()) + " qbe) throws "
146                   + getExceptionClassName() + ";\n" + " public " + getCursorInterfaceName(rels[i].getDetailExtent())
147                   + " get" + propInitcap + "Cursor() throws " + getExceptionClassName() + ";\n" + " public "
148                   + getCursorInterfaceName(rels[i].getDetailExtent()) + " get" + propInitcap + "Cursor("
149                   + getQbeInterfaceName(rels[i].getDetailExtent()) + " qbe) throws " + getExceptionClassName() + ";\n");
150
151       ArrayList JavaDoc allDetailsList = new ArrayList JavaDoc();
152       allDetailsList.add(rels[i].getDetailExtent());
153       allDetailsList.addAll(Arrays.asList(_mgr.getSubExtentsOf(rels[i].getDetailExtent())));
154       DjExtent[] allDetails = (DjExtent[]) allDetailsList.toArray(new DjExtent[0]);
155
156       for (int d = 0; d < allDetails.length; d++)
157       {
158         if (!isInSelectedSet(allDetails[d])) continue;
159
160         code.append(" public " + getInterfaceName(allDetails[d]) + " create" + allDetails[d].getObjectType() + "In"
161                     + propInitcap + "() throws " + getExceptionClassName() + ";\n");
162       }
163     }
164
165     code.append(StrongTyper.getRegenerationTags(0));
166     code.append("}\n");
167     return code.toString();
168   }
169
170 }
Popular Tags