KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > modeler > schemagenerator > SchemaGenerator


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.modeler.schemagenerator;
31
32 import java.io.BufferedWriter JavaDoc;
33 import java.io.FileWriter JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Collections JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38
39 import com.genimen.djeneric.language.Messages;
40 import com.genimen.djeneric.repository.DjExtent;
41 import com.genimen.djeneric.repository.DjPersistenceManager;
42 import com.genimen.djeneric.repository.DjProperty;
43 import com.genimen.djeneric.repository.DjRelation;
44 import com.genimen.djeneric.util.DjStringReplacer;
45
46 public abstract class SchemaGenerator
47 {
48   DjPersistenceManager _mgr;
49
50   String JavaDoc LINE_SEPARATOR = System.getProperty("line.separator", "\n");
51   String JavaDoc _warnings = "";
52
53   public SchemaGenerator(DjPersistenceManager mgr)
54   {
55     _mgr = mgr;
56   }
57
58   private void writeFile(String JavaDoc fileName, String JavaDoc code) throws Exception JavaDoc
59   {
60     BufferedWriter JavaDoc w = new BufferedWriter JavaDoc(new FileWriter JavaDoc(fileName));
61     w.write(code);
62     w.close();
63   }
64
65   protected void resolveColumnAlias(HashMap JavaDoc propsByAlias, DjProperty prop)
66   {
67     DjProperty org = (DjProperty) propsByAlias.get(prop.getName());
68
69     // if one of them is not required, they should both be 'not required'
70
if (!prop.isRequired() || !org.isRequired())
71     {
72       org.setRequired(false);
73       prop.setRequired(false);
74     }
75
76     // If the length of this property exceeds the original, use the new one
77
if (prop.getLength() > org.getLength())
78     {
79       propsByAlias.put(prop.getName(), prop);
80     }
81     if (!prop.getNativeType().equals(org.getNativeType()))
82     {
83       _warnings += Messages.getString("SchemaGenerator.Mergeonflict", prop.getName(), prop.getName(), prop.getExtent()
84           .getName())
85                    + "\n";
86     }
87   }
88
89   protected String JavaDoc getTableName(DjExtent extent)
90   {
91     DjExtent se = extent;
92     while (se.getSuper() != null)
93       se = se.getSuper();
94     return se.getName();
95   }
96
97   protected void addAllProps(DjExtent extent, HashMap JavaDoc propsByAlias, boolean isSubExtent)
98   {
99     for (int p = 0; p < extent.getPropertyCount(); p++)
100     {
101       // Properties are possibly updated by the generator so use copies!
102
DjProperty prop = (DjProperty) extent.getProperty(p).clone();
103
104       // Properties derived from a sub extent must be nullable
105
// (probably not used for super types or siblings)
106
if (isSubExtent)
107       {
108         prop.setRequired(false);
109       }
110       if (propsByAlias.containsKey(prop.getName()))
111       {
112         resolveColumnAlias(propsByAlias, prop);
113       }
114       else
115       {
116         propsByAlias.put(prop.getName(), prop);
117       }
118     }
119   }
120
121   public void generate(String JavaDoc filename) throws Exception JavaDoc
122   {
123     _warnings = "";
124     StringBuffer JavaDoc code = new StringBuffer JavaDoc(10000);
125
126     DjExtent[] extents = _mgr.getExtentsSorted();
127     for (int i = 0; i < extents.length; i++)
128     {
129       // Ignore sub classes; they are stored in the superclass extent:
130
if (extents[i].getSuper() != null) continue;
131
132       code.append(getTableCreateStart(extents[i]));
133
134       DjExtent[] subs = extents[i].getAllSpecializations();
135       HashMap JavaDoc propsByAlias = new HashMap JavaDoc();
136
137       addAllProps(extents[i], propsByAlias, false);
138       for (int s = 0; s < subs.length; s++)
139       {
140         addAllProps(subs[s], propsByAlias, true);
141       }
142
143       ArrayList JavaDoc sortedProps = new ArrayList JavaDoc();
144
145       Iterator JavaDoc it = propsByAlias.values().iterator();
146       while (it.hasNext())
147         sortedProps.add(it.next());
148
149       Collections.sort(sortedProps, DjProperty.getDefaultComparator());
150
151       String JavaDoc typeColDef = "";
152       boolean first = true;
153       if (extents[i].isPartOfInheritanceChain())
154       {
155         typeColDef = getTypeColumnCreate(extents[i]);
156         first = false;
157       }
158       code.append(typeColDef);
159
160       for (int c = 0; c < sortedProps.size(); c++)
161       {
162         code.append(getColumnCreate((DjProperty) sortedProps.get(c), first, c == sortedProps.size() - 1));
163         first = false;
164       }
165       code.append(getTableCreateEnd(extents[i]));
166     }
167
168     // create Primary keys now
169
for (int i = 0; i < extents.length; i++)
170     {
171       // Ignore sub classes; they are stored in the superclass extent:
172
if (extents[i].getSuper() != null) continue;
173       code.append(getTablePk(extents[i], i));
174     }
175
176     // create Foreign keys now
177
int counter = 0;
178     for (int i = 0; i < extents.length; i++)
179     {
180       // Ignore sub classes; they are stored in the superclass extent:
181
if (extents[i].getSuper() != null) continue;
182       DjRelation[] rels = extents[i].getMasterRelations();
183       for (int r = 0; r < rels.length; r++)
184       {
185         code.append(getRelation(rels[r], counter++, r));
186       }
187     }
188
189     // create Indexes keys now
190
counter = 0;
191     for (int i = 0; i < extents.length; i++)
192     {
193       // Ignore sub classes; they are stored in the superclass extent:
194
if (extents[i].getSuper() != null) continue;
195       DjRelation[] rels = extents[i].getMasterRelations();
196       for (int r = 0; r < rels.length; r++)
197       {
198         code.append(getIndex(rels[r], counter++, r));
199       }
200     }
201
202     DjStringReplacer sr = new DjStringReplacer(code.toString());
203     sr.replace("\n", LINE_SEPARATOR);
204     writeFile(filename, _warnings + sr.toString());
205   }
206
207   public abstract String JavaDoc getTableCreateStart(DjExtent extent) throws Exception JavaDoc;
208
209   public abstract String JavaDoc getTypeColumnCreate(DjExtent extent) throws Exception JavaDoc;
210
211   public abstract String JavaDoc getTableCreateEnd(DjExtent extent) throws Exception JavaDoc;
212
213   public abstract String JavaDoc getTablePk(DjExtent extent, int idx) throws Exception JavaDoc;
214
215   public abstract String JavaDoc getRelation(DjRelation relation, int idx, int idxInTable) throws Exception JavaDoc;
216
217   public abstract String JavaDoc getIndex(DjRelation relation, int idx, int idxInTable) throws Exception JavaDoc;
218
219   public abstract String JavaDoc getColumnCreate(DjProperty prop, boolean isFirst, boolean isLast) throws Exception JavaDoc;
220 }
Popular Tags