001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049: /*
050: * TableBean.java
051: *
052: * Created on March 19, 2003, 10:04 PM
053: */
054:
055: package org.jaffa.tools.domainmeta.common;
056:
057: import java.beans.*;
058: import java.io.PrintWriter;
059: import org.jaffa.util.StringHelper;
060: import java.util.Iterator;
061: import java.util.ArrayList;
062: import java.util.LinkedHashMap;
063: import org.apache.log4j.Logger;
064: import org.jaffa.datatypes.DateOnly;
065:
066: /** Java Bean to store information about a domain object
067: * @verion 1.0
068: * @author PaulE
069: */
070: public class TableBean extends Object implements java.io.Serializable {
071:
072: private static Logger log = Logger.getLogger(TableBean.class);
073:
074: private PropertyChangeSupport propertySupport;
075:
076: /** Holds value of property id. */
077: private String id;
078:
079: /** Holds value of property tableName. */
080: private String tableName;
081:
082: /** Holds value of property fields. */
083: private LinkedHashMap fields = new LinkedHashMap();
084:
085: /** Holds value of property domainName. */
086: private String domainName;
087:
088: /** Holds value of property domainPackage. */
089: private String domainPackage;
090:
091: /** Holds value of property relationships. */
092: private java.util.ArrayList relationships = new ArrayList();
093: private java.util.ArrayList oneRelationships = new ArrayList();
094:
095: /**
096: * Holds value of property label.
097: */
098: private String label;
099:
100: /**
101: * Holds value of property description.
102: */
103: private String description;
104:
105: /** Creates new TableBean */
106: public TableBean() {
107: propertySupport = new PropertyChangeSupport(this );
108: }
109:
110: /**
111: * Write out an XML verion of the bean, including all
112: * the fields beans in this table bean.
113: * @param mappingPackage mapping packge for this table to use
114: * @param patternTemplate pattern template this this is based on
115: * @param out XMl representation as a string
116: */
117: public void write(PrintWriter out, String mappingPackage,
118: String patternTemplate) {
119: out
120: .println(" <DomainObject>" + domainName
121: + "</DomainObject>");
122: out.println(" <DomainPackage>" + domainPackage
123: + "</DomainPackage>");
124: out.println(" <DatabaseTable>" + tableName
125: + "</DatabaseTable>");
126: out.println(" <MappingPackage>" + mappingPackage
127: + "</MappingPackage>");
128: out.println(" <PatternTemplate>" + patternTemplate
129: + "</PatternTemplate>");
130: out.println(" <Description>Reverse Engineered on "
131: + (new DateOnly()) + "</Description>");
132: out.println(" <LabelToken>" + (label == null ? "" : label)
133: + "</LabelToken>");
134:
135: if (fields != null && !fields.isEmpty()) {
136: out.println(" <Fields>");
137: for (Iterator i = fields.keySet().iterator(); i.hasNext();) {
138: String key = (String) i.next();
139: FieldBean fb = (FieldBean) fields.get(key);
140: fb.write(out);
141: }
142: out.println(" </Fields>");
143: }
144:
145: if (relationships != null && !relationships.isEmpty()) {
146: out.println(" <Relationships>");
147: for (Iterator i = relationships.iterator(); i.hasNext();) {
148: TableBean tb = (TableBean) i.next();
149:
150: out.println(" <Relationship>");
151: out.println(" <ToDomainObject>"
152: + tb.getDomainName() + "</ToDomainObject>");
153: out.println(" <ToDomainPackage>"
154: + tb.getDomainPackage() + "</ToDomainPackage>");
155: out
156: .println(" <FromCardinality>1</FromCardinality>");
157: if (oneRelationships.contains(tb))
158: out
159: .println(" <ToCardinality>1</ToCardinality>");
160: else
161: out
162: .println(" <ToCardinality>0..*</ToCardinality>");
163: out.println(" <Type>association</Type>");
164: out.println(" <FromFields>");
165:
166: for (Iterator i2 = fields.keySet().iterator(); i2
167: .hasNext();) {
168: String key = (String) i2.next();
169: FieldBean fb = (FieldBean) fields.get(key);
170: if (fb.isPrimaryKey())
171: out
172: .println(" <RelationshipField><Name>"
173: + fb.getPropertyName()
174: + "</Name></RelationshipField>");
175: }
176:
177: out.println(" </FromFields>");
178: out.println(" <ToFields>");
179:
180: for (Iterator i2 = fields.keySet().iterator(); i2
181: .hasNext();) {
182: String key = (String) i2.next();
183: FieldBean fb = (FieldBean) fields.get(key);
184: if (fb.isPrimaryKey())
185: out
186: .println(" <RelationshipField><Name>"
187: + fb.getPropertyName()
188: + "</Name></RelationshipField>");
189: }
190:
191: out.println(" </ToFields>");
192: out.println(" </Relationship>");
193: }
194: out.println(" </Relationships>");
195: }
196: }
197:
198: /** Getter for property id.
199: * @return Value of property id.
200: */
201: public String getId() {
202: return this .id;
203: }
204:
205: /** Setter for property id.
206: * @param id New value of property id.
207: */
208: public void setId(String id) {
209: this .id = id;
210: }
211:
212: /** Getter for property tableName.
213: * @return Value of property tableName.
214: */
215: public String getTableName() {
216: return this .tableName;
217: }
218:
219: /** Setter for property tableName.
220: * @param tableName New value of property tableName.
221: */
222: public void setTableName(String tableName) {
223: this .tableName = tableName;
224: }
225:
226: /** Getter for property fields.
227: * @return Value of property fields.
228: */
229: public java.util.HashMap getFields() {
230: return this .fields;
231: }
232:
233: /** Setter for property fields.
234: * @param fields New value of property fields.
235: */
236: public void setFields(java.util.LinkedHashMap fields) {
237: this .fields = fields;
238: }
239:
240: /** Getter for property domainName.
241: * @return Value of property domainName.
242: */
243: public String getDomainName() {
244: return this .domainName;
245: }
246:
247: /** Setter for property domainName.
248: * @param domainName New value of property domainName.
249: */
250: public void setDomainName(String domainName) {
251: this .domainName = formatName(domainName);
252: }
253:
254: /** Getter for property domainPackage.
255: * @return Value of property domainPackage.
256: */
257: public String getDomainPackage() {
258: return this .domainPackage;
259: }
260:
261: /** Setter for property domainPackage.
262: * @param domainPackage New value of property domainPackage.
263: */
264: public void setDomainPackage(String domainPackage) {
265: this .domainPackage = domainPackage;
266: }
267:
268: /** Getter for property relationships.
269: * @return Value of property relationships.
270: */
271: public java.util.ArrayList getRelationships() {
272: return this .relationships;
273: }
274:
275: /**
276: * Add a relationship from this table to another one. It assumes that the key of
277: * this table are the same fields names that relate the two tables
278: * @param table table this one is related to
279: * @param cardinalityMany true if this is a one to many table, false if its one-to-one
280: */
281: public void addRelationship(TableBean table, boolean cardinalityMany) {
282: if (relationships.contains(table))
283: // ignore duplicates...
284: return;
285: this .relationships.add(table);
286: if (!cardinalityMany)
287: this .oneRelationships.add(table);
288: }
289:
290: /**
291: * Format a field / table name so it complies with Jaffa. Typically names have
292: * no spaces and each word is caplitalized.
293: * @param name original database name
294: * @return Jaffa compatible name
295: */
296: public static String formatName(String name) {
297: if (name.toUpperCase().endsWith("_B"))
298: name = name.substring(0, name.length() - 2);
299: name = StringHelper.replace(name, "_", " ");
300: String[] l = StringHelper.parseString(name, " ");
301: StringBuffer n = new StringBuffer();
302: if (l != null && l.length != 0)
303: for (int i = 0; i < l.length; i++)
304: if (l[i].equals(l[i].toUpperCase()))
305: // Word is all upper case, Convert it to just capitalise the first char
306: n
307: .append(StringHelper.getUpper1(l[i]
308: .toLowerCase()));
309: else
310: // Word is mixed case, make sure first letter is capitalized
311: n.append(StringHelper.getUpper1(l[i]));
312: return n.toString();
313: }
314:
315: /**
316: * Getter for property label.
317: * @return Value of property label.
318: */
319: public String getLabel() {
320: return this .label;
321: }
322:
323: /**
324: * Setter for property label.
325: * @param label New value of property label.
326: */
327: public void setLabel(String label) {
328: this .label = label;
329: }
330:
331: /**
332: * Getter for property description.
333: * @return Value of property description.
334: */
335: public String getDescription() {
336: return this .description;
337: }
338:
339: /**
340: * Setter for property description.
341: * @param description New value of property description.
342: */
343: public void setDescription(String description) {
344: this .description = description;
345: }
346:
347: // public static void main(String[] args) {
348: // String x = "Hello World";
349: // System.out.println("Name '"+x+"' maps to '"+formatName(x)+"'");
350: // x = "HELLO WORLD";
351: // System.out.println("Name '"+x+"' maps to '"+formatName(x)+"'");
352: // x = "HELLO_WORLD";
353: // System.out.println("Name '"+x+"' maps to '"+formatName(x)+"'");
354: // x = "HELLO_WORLD_B";
355: // System.out.println("Name '"+x+"' maps to '"+formatName(x)+"'");
356: // x = "HelloWorld";
357: // System.out.println("Name '"+x+"' maps to '"+formatName(x)+"'");
358: // x = "Helloworld";
359: // System.out.println("Name '"+x+"' maps to '"+formatName(x)+"'");
360: // x = "helloworld";
361: // System.out.println("Name '"+x+"' maps to '"+formatName(x)+"'");
362: // x = "hello world";
363: // System.out.println("Name '"+x+"' maps to '"+formatName(x)+"'");
364: // x = "hello world";
365: // System.out.println("Name '"+x+"' maps to '"+formatName(x)+"'");
366: // x = "hello___world";
367: // System.out.println("Name '"+x+"' maps to '"+formatName(x)+"'");
368: // x = "hello_ _world";
369: // System.out.println("Name '"+x+"' maps to '"+formatName(x)+"'");
370: // }
371: }
|