0001: /*
0002: * ====================================================================
0003: * JAFFA - Java Application Framework For All
0004: *
0005: * Copyright (C) 2002 JAFFA Development Group
0006: *
0007: * This library is free software; you can redistribute it and/or
0008: * modify it under the terms of the GNU Lesser General Public
0009: * License as published by the Free Software Foundation; either
0010: * version 2.1 of the License, or (at your option) any later version.
0011: *
0012: * This library is distributed in the hope that it will be useful,
0013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0015: * Lesser General Public License for more details.
0016: *
0017: * You should have received a copy of the GNU Lesser General Public
0018: * License along with this library; if not, write to the Free Software
0019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0020: *
0021: * Redistribution and use of this software and associated documentation ("Software"),
0022: * with or without modification, are permitted provided that the following conditions are met:
0023: * 1. Redistributions of source code must retain copyright statements and notices.
0024: * Redistributions must also contain a copy of this document.
0025: * 2. Redistributions in binary form must reproduce the above copyright notice,
0026: * this list of conditions and the following disclaimer in the documentation
0027: * and/or other materials provided with the distribution.
0028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
0029: * this Software without prior written permission. For written permission,
0030: * please contact mail to: jaffagroup@yahoo.com.
0031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
0032: * appear in their names without prior written permission.
0033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
0034: *
0035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
0036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
0038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
0039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
0042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
0043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
0044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
0045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0046: * SUCH DAMAGE.
0047: * ====================================================================
0048: */
0049:
0050: /*
0051: * UnifaceSchemaReader.java
0052: *
0053: * Created on April 24, 2002, 1:49 PM
0054: */
0055:
0056: package org.jaffa.tools.domainmeta.uniface;
0057:
0058: import java.util.*;
0059: import java.io.*;
0060: import java.sql.*;
0061: import org.jaffa.util.StringHelper;
0062: import org.jaffa.datatypes.Defaults;
0063: import org.jaffa.metadata.FieldMetaData;
0064: import org.jaffa.util.MessageHelper;
0065: import org.jaffa.patterns.library.domain_creator_1_1.domain.Root;
0066: import org.jaffa.patterns.library.domain_creator_1_1.domain.Field;
0067: import org.jaffa.patterns.library.domain_creator_1_1.domain.Relationship;
0068: import org.jaffa.patterns.library.domain_creator_1_1.domain.RelationshipField;
0069:
0070: /**
0071: * This class has is to be used for converting existing Uniface defintions into a XML format, which in turn can be used for code-generating Domain classes & mapping files.
0072: * @author GautamJ
0073: */
0074: public class UnifaceSchemaReader {
0075:
0076: /**
0077: * This will write the following XML into the input WRITER.
0078: * <?xml version="1.0"?>
0079: * <!DOCTYPE Root SYSTEM "dtd">
0080: * <Root>
0081: * <DomainObject(1)></DomainObject>
0082: * <DomainPackage(1)></DomainPackage>
0083: * <DatabaseTable(1)></DatabaseTable>
0084: * <MappingPackage(1)></MappingPackage>
0085: * <PatternTemplate(1)></PatternTemplate>
0086: * <Description(0..1)></Description>
0087: * <LabelToken(0..1)></LabelToken>
0088: * <Fields(1)>
0089: * <Field(1..*)>
0090: * <Name(1)></Name>
0091: * <DataType(1)></DataType>
0092: * <DatabaseFieldName(1)></DatabaseFieldName>
0093: * <DatabaseDataType(1)></DatabaseDataType>
0094: * <PrimaryKey(0..1, T/F)></PrimaryKey>
0095: * <Description(0..1)></Description>
0096: * <LabelToken(0..1)></LabelToken>
0097: * <Mandatory(0..1, T/F)></Mandatory>
0098: * <Width(0..1)></Width>
0099: * <IntSize(0..1)></IntSize>
0100: * <FracSize(0..1)></FracSize>
0101: * <MinValue(0..1)></MinValue>
0102: * <MaxValue(0..1)></MaxValue>
0103: * <Layout(0..1)></Layout>
0104: * <Pattern(0..1)></Pattern>
0105: * <CaseType(0..1, UpperCase/LowerCase/MixedCase)></CaseType>
0106: * <Ignore(0..1)></Ignore>
0107: * </Field>
0108: * </Fields>
0109: *
0110: * <Relationships(0..1)>
0111: * <Relationship(1..*)>
0112: * <ToDomainObject(1)></ToDomainObject>
0113: * <ToDomainPackage(1)></ToDomainPackage>
0114: * <FromCardinality(1)></FromCardinality>
0115: * <ToCardinality(1)></ToCardinality>
0116: * <Type(1)></Type>
0117: * <FromFields(1)>
0118: * <RelationshipField(1..*)>
0119: * <Name(1)></Name>
0120: * </RelationshipField>
0121: * </FromFields>
0122: * <ToFields(1)>
0123: * <RelationshipField(1..*)>
0124: * <Name(1)></Name>
0125: * </RelationshipField>
0126: * </ToFields>
0127: * </Relationship>
0128: * </Relationships>
0129: * </Root>
0130: *
0131: * @param table The TableInfoReader.Table object containing the information for the domain object for which the domain-creator XML is to be generated.
0132: * @param mappingPackage The value to used for the 'MappingPackage' element. This will determine the destination for mapping files.
0133: * @param patternTemplate The value to used for the 'PatternTemplate' element. This will be used for code generating the domain class.
0134: * @param dtd The dtd to be used for validating the generated xml.
0135: * @param tableInfo A TableInfoReader.TableInfo object with a List of TableInfoReader.Table objects which maps the various database tables to domain objects.
0136: * @param relationshipInfo A List of RelationshipInfoReader.Relationship objects which has useful info for the relationships; things that cannot be determined from the Uniface definitions.
0137: * @param writer The destination into which the XML is to be streamed.
0138: * @param connection The connection object to use for the database queries.
0139: * @param existingDomainObject The object representation of the existing file.
0140: * @param ignoreNewFields Determines the default value of the <ignore> element for the new fields.
0141: * @param doNotOverwriteExistingRelationships Determines if existing relationships will be retained or over-written with new data.
0142: * @throws IOException in case any IO error occurs.
0143: * @throws SQLException in case any database error occurs.
0144: */
0145: public static void parse(TableInfoReader.Table table,
0146: String mappingPackage, String patternTemplate, String dtd,
0147: TableInfoReader.TableInfo tableInfo, List relationshipInfo,
0148: Writer writer, Connection connection,
0149: Root existingDomainObject, boolean ignoreNewFields,
0150: boolean doNotOverwriteExistingRelationships)
0151: throws IOException, SQLException {
0152: // This method is now having ridiculous number of input parameters.
0153: // At some point, need to refactor the class to have getters/setters for the parameters and make this method non-static
0154:
0155: String domainObject = table.getDomainObject();
0156: String domainPackage = determineDomainPackage(table, tableInfo);
0157: String databaseTable = table.getDatabaseTable().toUpperCase();
0158: String schema = table.getSchema().toUpperCase();
0159: writer = new BufferedWriter(writer);
0160:
0161: // determine the description of the entity
0162: String description = determineDescription(databaseTable,
0163: schema, connection, existingDomainObject);
0164:
0165: // determine the labelToken of the entity
0166: String labelToken = determineDomainObjectLabelToken(tableInfo
0167: .getApplicationName(), table.getModuleName(),
0168: domainObject, existingDomainObject);
0169:
0170: // write the initial XML
0171: initialXml(domainObject, domainPackage, databaseTable,
0172: mappingPackage, patternTemplate, dtd, description,
0173: labelToken, writer);
0174:
0175: // get a list containing primary keys
0176: List primaryKeys = getPrimaryKeys(databaseTable, schema,
0177: connection);
0178:
0179: // add the field info
0180: writeFields(databaseTable, schema, writer, connection,
0181: primaryKeys, domainPackage, domainObject,
0182: existingDomainObject, ignoreNewFields, tableInfo
0183: .getApplicationName(), table.getModuleName());
0184:
0185: // add the relationship info
0186: if (doNotOverwriteExistingRelationships)
0187: writeExistingRelationships(writer, existingDomainObject);
0188: else
0189: writeRelationships(databaseTable, schema, tableInfo,
0190: relationshipInfo, writer, connection);
0191:
0192: // write the final XML
0193: finalXml(writer);
0194:
0195: writer.flush();
0196: }
0197:
0198: private static String determineDomainPackage(
0199: TableInfoReader.Table table,
0200: TableInfoReader.TableInfo tableInfo) {
0201: StringBuffer buf = new StringBuffer();
0202: if (table.getDomainPackage() != null) {
0203: buf.append(table.getDomainPackage());
0204: } else {
0205: buf.append(tableInfo.getPackagePrefix());
0206: if (tableInfo.isFullPackageNames())
0207: buf.append(".applications");
0208: buf.append('.');
0209: buf.append(tableInfo.getApplicationName());
0210: if (tableInfo.isFullPackageNames())
0211: buf.append(".modules");
0212: buf.append('.');
0213: buf.append(table.getModuleName());
0214: buf.append(".domain");
0215: }
0216: return buf.toString().toLowerCase();
0217: }
0218:
0219: private static String determineDescription(String databaseTable,
0220: String schema, Connection connection,
0221: Root existingDomainObject) throws IOException, SQLException {
0222: String description = existingDomainObject != null ? existingDomainObject
0223: .getDescription()
0224: : null;
0225: if (description == null) {
0226: String sql = "select UDESCR, TPLACTUAL from UCGROUP where U_TLAB = '"
0227: + databaseTable
0228: + "' and U_VLAB = '"
0229: + schema
0230: + "' and U_GLAB = '" + databaseTable + '\'';
0231: Statement stmnt = connection.createStatement();
0232: ResultSet rs = stmnt.executeQuery(sql);
0233: if (rs.next()) {
0234: String udescr = rs.getString("UDESCR");
0235: String tplactual = StringHelper.getString(rs
0236: .getCharacterStream("TPLACTUAL"));
0237:
0238: if (udescr != null)
0239: description = udescr.trim();
0240:
0241: if (tplactual != null) {
0242: int i = tplactual.indexOf("} 5");
0243: if (i >= 0) {
0244: String str = tplactual.substring(i + 3);
0245: i = str.indexOf("} 6");
0246: if (i >= 0)
0247: str = str.substring(0, i);
0248:
0249: if (str.length() > 0) {
0250: // handle the escaped '{' , '}', EOL characters
0251: str = replaceEscapedCharacters(str);
0252: if (description == null) {
0253: description = str;
0254: } else {
0255: if (description.length() > 0
0256: && !description.endsWith(".")
0257: && !description.endsWith("\n"))
0258: description += ". ";
0259: description += str;
0260: }
0261: }
0262: }
0263: }
0264: }
0265: }
0266: return description;
0267: }
0268:
0269: private static void initialXml(String domainObject,
0270: String domainPackage, String databaseTable,
0271: String mappingPackage, String patternTemplate, String dtd,
0272: String description, String labelToken, Writer writer)
0273: throws IOException {
0274: writer.write("<?xml version=\"1.0\"?>\n");
0275: if (dtd != null && dtd.length() > 0)
0276: writer.write("<!DOCTYPE Root " + dtd + ">\n");
0277:
0278: writer.write("\n<!--\n");
0279: writer
0280: .write("Non-null values for the following elements will be retained, the next time this file is regenerated, provided the merge option is set in the TableInfo file.\n");
0281: writer.write("1- Table Level: Description, LabelToken\n");
0282: writer
0283: .write("2- Field Level: Description, LabelToken, Layout, Pattern, CaseType, Ignore\n");
0284: writer.write("-->\n\n");
0285:
0286: writer.write("<Root>\n");
0287: writer.write(" <DomainObject>");
0288: if (domainObject != null)
0289: writer.write(domainObject);
0290: writer.write("</DomainObject>\n");
0291: writer.write(" <DomainPackage>");
0292: if (domainPackage != null)
0293: writer.write(domainPackage);
0294: writer.write("</DomainPackage>\n");
0295: writer.write(" <DatabaseTable>");
0296: if (databaseTable != null)
0297: writer.write(databaseTable);
0298: writer.write("</DatabaseTable>\n");
0299: writer.write(" <MappingPackage>");
0300: if (mappingPackage != null)
0301: writer.write(mappingPackage);
0302: writer.write("</MappingPackage>\n");
0303: writer.write(" <PatternTemplate>");
0304: if (patternTemplate != null)
0305: writer.write(patternTemplate);
0306: writer.write("</PatternTemplate>\n");
0307: if (description != null) {
0308: writer.write(" <Description>");
0309: writer.write(StringHelper.convertToHTML(description));
0310: writer.write("</Description>\n");
0311: }
0312: if (labelToken != null) {
0313: writer.write(" <LabelToken>");
0314: writer.write(StringHelper.convertToHTML(labelToken));
0315: writer.write("</LabelToken>\n");
0316: }
0317: }
0318:
0319: private static void finalXml(Writer writer) throws IOException {
0320: writer.write("</Root>\n");
0321: }
0322:
0323: private static void writeFields(String databaseTable,
0324: String schema, Writer writer, Connection connection,
0325: List primaryKeys, String domainPackage,
0326: String domainObject, Root existingDomainObject,
0327: boolean ignoreNewFields, String applicationName,
0328: String moduleName) throws IOException, SQLException {
0329:
0330: Statement stmnt = connection.createStatement();
0331: String sql = "SELECT A.U_FLAB \"U_FLAB\", A.U_DTYP \"U_DTYP\", A.UDESCR \"UDESCR\", A.U_DESC \"U_DESC\","
0332: + " A.U_INTF \"U_INTF\", B.U_MOD \"T_U_INTF\","
0333: + " A.U_SYN \"U_SYN\", C.U_MOD \"T_U_SYN\","
0334: + " A.U_LAY \"U_LAY\", D.U_MOD \"T_U_LAY\""
0335: + " FROM UCFIELD A, UGFIF B, UGFSYN C, UGFLAY D"
0336: + " WHERE A.TPLINTF = B.U_MLAB(+)"
0337: + " AND A.TPLSYN = C.U_MLAB(+)"
0338: + " AND A.TPLLAY = D.U_MLAB(+)"
0339: + " AND A.U_TLAB = '"
0340: + databaseTable
0341: + "'"
0342: + " AND A.U_VLAB = '"
0343: + schema
0344: + "'" + " AND U_INDB = 'Y'" + " order by U_FSEQ";
0345: ResultSet rs = stmnt.executeQuery(sql);
0346:
0347: writer.write(" <Fields>\n");
0348: while (rs.next()) {
0349: InnerField f = new InnerField(rs, primaryKeys,
0350: domainPackage, domainObject, existingDomainObject,
0351: ignoreNewFields, applicationName, moduleName);
0352: writer.write(" <Field>\n");
0353: writer.write(" <Name>");
0354: if (f.m_name != null)
0355: writer.write(f.m_name);
0356: writer.write("</Name>\n");
0357: writer.write(" <DataType>");
0358: if (f.m_dataType != null)
0359: writer.write(f.m_dataType);
0360: writer.write("</DataType>\n");
0361: writer.write(" <DatabaseFieldName>");
0362: if (f.m_databaseFieldName != null)
0363: writer.write(f.m_databaseFieldName);
0364: writer.write("</DatabaseFieldName>\n");
0365: writer.write(" <DatabaseDataType>");
0366: if (f.m_databaseDataType != null)
0367: writer.write(f.m_databaseDataType);
0368: writer.write("</DatabaseDataType>\n");
0369: if (f.m_primaryKey != null) {
0370: writer.write(" <PrimaryKey>");
0371: writer.write(f.m_primaryKey);
0372: writer.write("</PrimaryKey>\n");
0373: }
0374: if (f.m_description != null) {
0375: writer.write(" <Description>");
0376: writer.write(StringHelper
0377: .convertToHTML(f.m_description));
0378: writer.write("</Description>\n");
0379: }
0380: if (f.m_labelToken != null) {
0381: writer.write(" <LabelToken>");
0382: writer
0383: .write(StringHelper
0384: .convertToHTML(f.m_labelToken));
0385: writer.write("</LabelToken>\n");
0386: }
0387: if (f.m_mandatory != null) {
0388: writer.write(" <Mandatory>");
0389: writer.write(f.m_mandatory);
0390: writer.write("</Mandatory>\n");
0391: }
0392: if (f.m_width != null) {
0393: writer.write(" <Width>");
0394: writer.write(f.m_width);
0395: writer.write("</Width>\n");
0396: }
0397: if (f.m_intSize != null) {
0398: writer.write(" <IntSize>");
0399: writer.write(f.m_intSize);
0400: writer.write("</IntSize>\n");
0401: }
0402: if (f.m_fracSize != null) {
0403: writer.write(" <FracSize>");
0404: writer.write(f.m_fracSize);
0405: writer.write("</FracSize>\n");
0406: }
0407: if (f.m_minValue != null) {
0408: writer.write(" <MinValue>");
0409: writer.write(f.m_minValue);
0410: writer.write("</MinValue>\n");
0411: }
0412: if (f.m_maxValue != null) {
0413: writer.write(" <MaxValue>");
0414: writer.write(f.m_maxValue);
0415: writer.write("</MaxValue>\n");
0416: }
0417: if (f.m_layout != null) {
0418: writer.write(" <Layout>");
0419: writer.write(StringHelper.convertToHTML(f.m_layout));
0420: writer.write("</Layout>\n");
0421: }
0422: if (f.m_pattern != null) {
0423: writer.write(" <Pattern>");
0424: writer.write(StringHelper.convertToHTML(f.m_pattern));
0425: writer.write("</Pattern>\n");
0426: }
0427: if (f.m_caseType != null) {
0428: writer.write(" <CaseType>");
0429: writer.write(f.m_caseType);
0430: writer.write("</CaseType>\n");
0431: }
0432: if (f.m_ignore != null) {
0433: writer.write(" <Ignore>");
0434: writer.write(f.m_ignore);
0435: writer.write("</Ignore>\n");
0436: }
0437: writer.write(" </Field>\n");
0438: }
0439: writer.write(" </Fields>\n");
0440: stmnt.close();
0441: }
0442:
0443: private static void writeExistingRelationships(Writer writer,
0444: Root existingDomainObject) throws IOException {
0445: if (existingDomainObject != null
0446: && existingDomainObject.getRelationships() != null
0447: && existingDomainObject.getRelationships()
0448: .getRelationship() != null
0449: && existingDomainObject.getRelationships()
0450: .getRelationship().size() > 0) {
0451: writer.write(" <Relationships>\n");
0452: for (Iterator itr = existingDomainObject.getRelationships()
0453: .getRelationship().iterator(); itr.hasNext();) {
0454: Relationship r = (Relationship) itr.next();
0455: writer.write(" <Relationship>\n");
0456: if (r.getName() != null) {
0457: writer.write(" <Name>");
0458: writer.write(r.getName());
0459: writer.write("</Name>\n");
0460: }
0461: writer.write(" <ToDomainObject>");
0462: if (r.getToDomainObject() != null)
0463: writer.write(r.getToDomainObject());
0464: writer.write("</ToDomainObject>\n");
0465: writer.write(" <ToDomainPackage>");
0466: if (r.getToDomainPackage() != null)
0467: writer.write(r.getToDomainPackage());
0468: writer.write("</ToDomainPackage>\n");
0469: writer.write(" <FromCardinality>");
0470: if (r.getFromCardinality() != null)
0471: writer.write(r.getFromCardinality());
0472: writer.write("</FromCardinality>\n");
0473: writer.write(" <ToCardinality>");
0474: if (r.getToCardinality() != null)
0475: writer.write(r.getToCardinality());
0476: writer.write("</ToCardinality>\n");
0477: writer.write(" <Type>");
0478: if (r.getType() != null)
0479: writer.write(r.getType());
0480: writer.write("</Type>\n");
0481:
0482: writer.write(" <FromFields>\n");
0483: for (Iterator i = r.getFromFields()
0484: .getRelationshipField().iterator(); i.hasNext();) {
0485: RelationshipField relationshipField = (RelationshipField) i
0486: .next();
0487: writer
0488: .write(" <RelationshipField>\n");
0489: writer.write(" <Name>");
0490: writer.write(relationshipField.getName());
0491: writer.write("</Name>\n");
0492: writer
0493: .write(" </RelationshipField>\n");
0494: }
0495: writer.write(" </FromFields>\n");
0496:
0497: writer.write(" <ToFields>\n");
0498: for (Iterator i = r.getToFields()
0499: .getRelationshipField().iterator(); i.hasNext();) {
0500: RelationshipField relationshipField = (RelationshipField) i
0501: .next();
0502: writer
0503: .write(" <RelationshipField>\n");
0504: writer.write(" <Name>");
0505: writer.write(relationshipField.getName());
0506: writer.write("</Name>\n");
0507: writer
0508: .write(" </RelationshipField>\n");
0509: }
0510: writer.write(" </ToFields>\n");
0511:
0512: writer.write(" </Relationship>\n");
0513: }
0514: writer.write(" </Relationships>\n");
0515: }
0516: }
0517:
0518: private static void writeRelationships(String databaseTable,
0519: String schema, TableInfoReader.TableInfo tableInfo,
0520: List relationshipInfo, Writer writer, Connection connection)
0521: throws IOException, SQLException {
0522: Statement stmnt = connection.createStatement();
0523: String sql = "select * from ucrelsh where" + " (u_glab = '"
0524: + databaseTable + "' and u_vlab = '" + schema + "')"
0525: + " or" + " (u_rglab = '" + databaseTable
0526: + "' and u_rvlab = '" + schema + "')"
0527: + " order by u_vlab, u_glab, u_rvlab, u_rglab";
0528: ResultSet rs = stmnt.executeQuery(sql);
0529: Collection relationships = new ArrayList();
0530: while (rs.next()) {
0531: InnerRelationship r = new InnerRelationship(databaseTable,
0532: schema, connection, rs, tableInfo, relationshipInfo);
0533: if (r.m_valid)
0534: relationships.add(r);
0535: }
0536: stmnt.close();
0537:
0538: if (relationships.size() > 0) {
0539: writer.write(" <Relationships>\n");
0540: for (Iterator itr = relationships.iterator(); itr.hasNext();) {
0541: InnerRelationship r = (InnerRelationship) itr.next();
0542: writer.write(" <Relationship>\n");
0543: writer.write(" <ToDomainObject>");
0544: if (r.m_toDomainObject != null)
0545: writer.write(r.m_toDomainObject);
0546: writer.write("</ToDomainObject>\n");
0547: writer.write(" <ToDomainPackage>");
0548: if (r.m_toDomainPackage != null)
0549: writer.write(r.m_toDomainPackage);
0550: writer.write("</ToDomainPackage>\n");
0551: writer.write(" <FromCardinality>");
0552: if (r.m_fromCardinality != null)
0553: writer.write(StringHelper
0554: .convertToHTML(r.m_fromCardinality));
0555: writer.write("</FromCardinality>\n");
0556: writer.write(" <ToCardinality>");
0557: if (r.m_toCardinality != null)
0558: writer.write(StringHelper
0559: .convertToHTML(r.m_toCardinality));
0560: writer.write("</ToCardinality>\n");
0561: writer.write(" <Type>");
0562: if (r.m_type != null)
0563: writer.write(StringHelper.convertToHTML(r.m_type));
0564: writer.write("</Type>\n");
0565:
0566: writer.write(" <FromFields>\n");
0567: if (r.m_fromFields != null) {
0568: for (Iterator i = r.m_fromFields.iterator(); i
0569: .hasNext();) {
0570: String fieldName = (String) i.next();
0571: writer
0572: .write(" <RelationshipField>\n");
0573: writer.write(" <Name>");
0574: if (fieldName != null)
0575: writer.write(fieldName);
0576: writer.write("</Name>\n");
0577: writer
0578: .write(" </RelationshipField>\n");
0579: }
0580: }
0581: writer.write(" </FromFields>\n");
0582:
0583: writer.write(" <ToFields>\n");
0584: if (r.m_toFields != null) {
0585: for (Iterator i = r.m_toFields.iterator(); i
0586: .hasNext();) {
0587: String fieldName = (String) i.next();
0588: writer
0589: .write(" <RelationshipField>\n");
0590: writer.write(" <Name>");
0591: if (fieldName != null)
0592: writer.write(fieldName);
0593: writer.write("</Name>\n");
0594: writer
0595: .write(" </RelationshipField>\n");
0596: }
0597: }
0598: writer.write(" </ToFields>\n");
0599:
0600: writer.write(" </Relationship>\n");
0601: }
0602: writer.write(" </Relationships>\n");
0603: }
0604: }
0605:
0606: private static List getPrimaryKeys(String databaseTable,
0607: String schema, Connection connection) throws IOException,
0608: SQLException {
0609: List primaryKeys = new ArrayList();
0610: String sql = "select U_DOC from UCKEY where U_TLAB = '"
0611: + databaseTable + "' and U_VLAB = '" + schema
0612: + "' and U_KTYP = 'P'";
0613: Statement stmnt = connection.createStatement();
0614: ResultSet rs = stmnt.executeQuery(sql);
0615: if (rs.next()) {
0616: String str = StringHelper.getString(rs
0617: .getCharacterStream("U_DOC"));
0618: if (str != null) {
0619: // the format of the string would be '} )} *PK1,PK2}....'
0620: str = str.trim(); //trim the string
0621: str = StringHelper.replace(str, " ", ""); //remove all the spaces
0622: StringTokenizer stkzr = new StringTokenizer(str, "}*,",
0623: true);
0624: boolean reachedStart = false;
0625: while (stkzr.hasMoreTokens()) {
0626: String token = stkzr.nextToken();
0627: if (token.equals("}")) {
0628: if (reachedStart)
0629: break;
0630: } else if (token.equals("*")) {
0631: reachedStart = true;
0632: } else if (token.equals(",")) {
0633: // do nothing
0634: } else {
0635: if (reachedStart)
0636: primaryKeys.add(token.toUpperCase());
0637: }
0638: }
0639: }
0640: }
0641: stmnt.close();
0642: return primaryKeys;
0643: }
0644:
0645: /** This method has been kept for future enhancements when we add support for candidate keys*/
0646: private static List getCandidateKeys(String databaseTable,
0647: String schema, Connection connection) throws IOException,
0648: SQLException {
0649: List candidateKeys = new ArrayList();
0650: String sql = "select U_DOC from UCKEY where U_TLAB = '"
0651: + databaseTable + "' and U_VLAB = '" + schema
0652: + "' and U_KTYP = 'C'";
0653: Statement stmnt = connection.createStatement();
0654: ResultSet rs = stmnt.executeQuery(sql);
0655: while (rs.next()) {
0656: String str = StringHelper.getString(rs
0657: .getCharacterStream("U_DOC"));
0658: if (str != null) {
0659: // the format of the string would be '} )} *CK1,CK2}....'
0660: str = str.trim(); //trim the string
0661: str = StringHelper.replace(str, " ", ""); //remove all the spaces
0662: StringTokenizer stkzr = new StringTokenizer(str, "}*,",
0663: true);
0664: boolean reachedStart = false;
0665: while (stkzr.hasMoreTokens()) {
0666: String token = stkzr.nextToken();
0667: if (token.equals("}")) {
0668: if (reachedStart)
0669: break;
0670: } else if (token.equals("*")) {
0671: reachedStart = true;
0672: } else if (token.equals(",")) {
0673: // do nothing
0674: } else {
0675: if (reachedStart)
0676: candidateKeys.add(token.toUpperCase());
0677: }
0678: }
0679: }
0680: }
0681: stmnt.close();
0682: return candidateKeys;
0683: }
0684:
0685: /** Convert a Database field name to a Java property name.
0686: * Convert the whole thing to lower case (except first character),
0687: * then remove all '_' and capitalize any letter following a '_'.
0688: * eg MAIN_TABLE_KEY_VALUE to MainTableKeyValue.
0689: * Drop the '_B' of any field (for booleans).
0690: * eg BYPASS_SECURITY_B to BypassSecurity.
0691: */
0692: private static String convertFieldName(String name) {
0693: // Capitalize just first character
0694: if (name.length() > 1)
0695: name = name.substring(0, 1).toUpperCase()
0696: + name.substring(1).toLowerCase();
0697: else
0698: name = name.toUpperCase();
0699:
0700: // Drop the _b if there...
0701: if (name.endsWith("_b") || name.endsWith("_B"))
0702: name = name.substring(0, name.length() - 2);
0703:
0704: // Replace any '_x' with 'X'
0705: int pos;
0706: while ((pos = name.indexOf('_')) != -1) {
0707: name = name.substring(0, pos)
0708: + name.substring(pos + 1, pos + 2).toUpperCase()
0709: + name.substring(pos + 2);
0710: }
0711:
0712: // replace known keywords
0713: if (name.equals("Default"))
0714: name = "DefaultFlag";
0715: else if (name.equals("Locked"))
0716: name = "LockedStatus";
0717: else if (name.equals("UOW"))
0718: name = "UowObject";
0719: else if (name.equals("DatabaseOccurence"))
0720: name = "DatabaseOccurenceFlag";
0721: else if (name.equals("Locking"))
0722: name = "LockingFlag";
0723: else if (name.equals("Modified"))
0724: name = "ModifiedFlag";
0725: else if (name.equals("Queued"))
0726: name = "QueuedFlag";
0727: return name;
0728: }
0729:
0730: private static class InnerField {
0731: private String m_name = null;
0732: private String m_dataType = null;
0733: private String m_databaseFieldName = null;
0734: private String m_databaseDataType = null;
0735: private String m_primaryKey = null;
0736: private String m_description = null;
0737: private String m_labelToken = null;
0738: private String m_mandatory = null;
0739: private String m_width = null;
0740: private String m_intSize = null;
0741: private String m_fracSize = null;
0742: private String m_minValue = null;
0743: private String m_maxValue = null;
0744: private String m_layout = null;
0745: private String m_pattern = null;
0746: private String m_caseType = null;
0747: private String m_ignore = null;
0748:
0749: private InnerField(ResultSet rs, List primaryKeys,
0750: String domainPackage, String domainObject,
0751: Root existingDomainObject, boolean ignoreNewFields,
0752: String applicationName, String moduleName)
0753: throws IOException, SQLException {
0754: // the following cannot be done
0755: m_minValue = null;
0756: m_maxValue = null;
0757:
0758: // the following will not be done for now.. too much work trying to convert uniface defintions into java
0759: m_layout = null;
0760: m_pattern = null;
0761:
0762: String fieldName = rs.getString("U_FLAB").trim();
0763: m_name = convertFieldName(fieldName);
0764: m_databaseFieldName = fieldName.toUpperCase();
0765: if (primaryKeys.contains(m_databaseFieldName))
0766: m_primaryKey = "T";
0767: else
0768: m_primaryKey = "F";
0769: determineDescriptionAndLabel(rs.getString("UDESCR"),
0770: StringHelper.getString(rs
0771: .getCharacterStream("U_DESC")));
0772: determineMandatoryAndCaseType(rs.getString("U_SYN"), rs
0773: .getString("T_U_SYN"), m_databaseFieldName);
0774: determineWidth(rs.getString("U_LAY"), rs
0775: .getString("T_U_LAY"));
0776: determineDataType(rs.getString("U_DTYP"), rs
0777: .getString("U_INTF"), rs.getString("T_U_INTF"));
0778: determineLabelToken(applicationName, moduleName,
0779: domainObject);
0780: m_ignore = ignoreNewFields ? "true" : "false";
0781:
0782: mergeWithExisitngElements(existingDomainObject);
0783: }
0784:
0785: private void determineDescriptionAndLabel(String description,
0786: String comments) {
0787: if (description != null) {
0788: description = description.trim();
0789: if (description.length() > 0)
0790: m_description = description;
0791: }
0792:
0793: if (comments != null) {
0794: // format of comments is '} 7Some Label} 8Some Comments} 9....'
0795: int i;
0796: i = comments.indexOf("} 7");
0797: if (i >= 0) {
0798: String str = comments.substring(i + 3);
0799: i = str.indexOf("} 8");
0800: if (i >= 0)
0801: str = str.substring(0, i);
0802:
0803: if (str.length() > 0) {
0804: // handle the escaped '{' , '}', EOL characters
0805: str = replaceEscapedCharacters(str);
0806:
0807: // this is no longer used
0808: //m_labelToken = str;
0809: }
0810: }
0811:
0812: i = comments.indexOf("} 8");
0813: if (i >= 0) {
0814: String str = comments.substring(i + 3);
0815: i = str.indexOf("} 9");
0816: if (i >= 0)
0817: str = str.substring(0, i);
0818:
0819: if (str.length() > 0) {
0820: // handle the escaped '{' , '}', EOL characters
0821: str = replaceEscapedCharacters(str);
0822: if (m_description == null) {
0823: m_description = str;
0824: } else {
0825: if (m_description.length() > 0
0826: && !m_description.endsWith(".")
0827: && !m_description.endsWith("\n"))
0828: m_description += ". ";
0829: m_description += str;
0830: }
0831: }
0832: }
0833: }
0834: }
0835:
0836: private void determineMandatoryAndCaseType(String syntax,
0837: String t_syntax, String databaseFieldName) {
0838: String shortHandCode = null;
0839: if (syntax != null)
0840: shortHandCode = syntax.trim();
0841: else if (t_syntax != null)
0842: shortHandCode = t_syntax.trim();
0843:
0844: if (shortHandCode != null) {
0845: StringTokenizer stknzr = new StringTokenizer(
0846: shortHandCode, ", ");
0847: while (stknzr.hasMoreTokens()) {
0848: String token = stknzr.nextToken();
0849: if (token.equals("MAN"))
0850: m_mandatory = "T";
0851: else if (token.equals("UPC"))
0852: m_caseType = FieldMetaData.UPPER_CASE;
0853: else if (token.equals("LOW"))
0854: m_caseType = FieldMetaData.LOWER_CASE;
0855: else if (token.startsWith("LEN(")) {
0856: // Sometimes the mandatory-ness can be enforced by the construct 'LEN(1-10)'
0857: String str = token.substring("LEN(".length());
0858: int i = str.indexOf('-');
0859: if (i > 0)
0860: str = str.substring(0, i);
0861: try {
0862: int length = Integer.parseInt(str);
0863: if (length > 0)
0864: m_mandatory = "T";
0865: } catch (Exception e) {
0866: // do nothing
0867: }
0868: }
0869: }
0870: }
0871:
0872: if (m_mandatory == null)
0873: m_mandatory = m_primaryKey;
0874:
0875: if (m_caseType == null)
0876: m_caseType = FieldMetaData.MIXED_CASE;
0877: }
0878:
0879: private void determineWidth(String layout, String t_layout) {
0880: String shortHandCode = null;
0881: if (layout != null)
0882: shortHandCode = layout.trim();
0883: else if (t_layout != null)
0884: shortHandCode = t_layout.trim();
0885:
0886: if (shortHandCode != null && shortHandCode.length() > 0) {
0887: int i = shortHandCode.indexOf("WID(");
0888: if (i >= 0) {
0889: String str = shortHandCode.substring(i + 4);
0890: i = str.indexOf(')');
0891: if (i >= 0) {
0892: str = str.substring(0, i).trim();
0893: if (str.length() > 0) {
0894: try {
0895: // ensure that its a number
0896: i = Integer.parseInt(str);
0897: m_width = str;
0898: } catch (Exception e) {
0899: // do nothing
0900: }
0901: }
0902: }
0903: }
0904: }
0905: }
0906:
0907: private void determineDataType(String dataType,
0908: String interfaceType, String t_interfaceType) {
0909: dataType = dataType.trim();
0910: if (interfaceType != null) {
0911: interfaceType = interfaceType.trim();
0912: if (interfaceType.length() == 0)
0913: interfaceType = null;
0914: }
0915: if (interfaceType == null && t_interfaceType != null) {
0916: interfaceType = t_interfaceType.trim();
0917: if (interfaceType.length() == 0)
0918: interfaceType = null;
0919: }
0920:
0921: if (dataType.equals("B")) {
0922: /* Here are the Uniface Packing codes
0923: * B = Optimum DBMS Boolean Default
0924: * B1 = ASCII Boolean (0|1)
0925: * B2 = ASCII Boolean (F|T)
0926: * B3 = ASCII Boolean (N|Y)
0927: * B4 = Binary Boolean (0|1)
0928: * B5 = Binary Boolean (0|-1) -- this is not supported. it will be treated as (0|1)
0929: */
0930: if (interfaceType == null || interfaceType.equals("B")) {
0931: m_databaseDataType = Defaults.BOOLEAN;
0932: } else if (interfaceType.equals("B1")) {
0933: m_databaseDataType = "BOOLEAN_10";
0934: } else if (interfaceType.equals("B2")) {
0935: m_databaseDataType = "BOOLEAN_TF";
0936: } else if (interfaceType.equals("B3")) {
0937: m_databaseDataType = "BOOLEAN_YN";
0938: } else {
0939: m_databaseDataType = "BOOLEAN_BIT";
0940: }
0941: } else if (dataType.equals("D") || dataType.equals("LD")) {
0942: m_databaseDataType = Defaults.DATEONLY;
0943: } else if (dataType.equals("E") || dataType.equals("T")
0944: || dataType.equals("LE") || dataType.equals("LT")) {
0945: m_databaseDataType = Defaults.DATETIME;
0946: } else if (dataType.equals("S") || dataType.equals("SS")) {
0947: if (interfaceType == null) {
0948: // cannot determine the lengths
0949: m_databaseDataType = Defaults.STRING;
0950: } else if (interfaceType.startsWith("SC")) {
0951: m_databaseDataType = Defaults.CLOB;
0952: } else if (interfaceType.startsWith("VC*")) {
0953: m_databaseDataType = Defaults.LONG_STRING;
0954: } else {
0955: m_databaseDataType = Defaults.STRING;
0956: determineLengths(interfaceType);
0957: }
0958: } else if (dataType.equals("R") || dataType.startsWith("I")) {
0959: if (interfaceType == null) {
0960: // cannot determine the lengths
0961: m_databaseDataType = Defaults.RAW;
0962: } else if (interfaceType.startsWith("SR")) {
0963: m_databaseDataType = Defaults.BLOB;
0964: } else if (interfaceType.startsWith("R*")
0965: || interfaceType.startsWith("VC*")) {
0966: m_databaseDataType = Defaults.LONG_RAW;
0967: } else {
0968: m_databaseDataType = Defaults.RAW;
0969: determineLengths(interfaceType);
0970: }
0971: } else if (dataType.equals("N") || dataType.equals("F")) {
0972: determineLengths(interfaceType);
0973: if ((m_intSize == null && m_fracSize == null)
0974: || m_fracSize != null)
0975: m_databaseDataType = Defaults.DECIMAL;
0976: else
0977: m_databaseDataType = Defaults.INTEGER;
0978: }
0979:
0980: m_dataType = m_databaseDataType.startsWith("BOOLEAN") ? Defaults
0981: .getClassString(Defaults.BOOLEAN)
0982: : Defaults.getClassString(m_databaseDataType);
0983: }
0984:
0985: private void determineLengths(String interfaceType) {
0986: if (interfaceType != null) {
0987: StringBuffer bufInt = new StringBuffer();
0988: StringBuffer bufFrac = new StringBuffer();
0989:
0990: boolean startedInt = false;
0991: boolean startedFrac = false;
0992: for (int i = 0; i < interfaceType.length(); i++) {
0993: char c = interfaceType.charAt(i);
0994: if (c == '.') {
0995: if (!startedInt || startedFrac) {
0996: // this should never happen
0997: break;
0998: } else {
0999: startedFrac = true;
1000: }
1001: } else if (c >= 48 && c <= 57) {
1002: // these are ascii values for 0 to 9
1003: if (!startedFrac) {
1004: startedInt = true;
1005: bufInt.append(c);
1006: } else {
1007: bufFrac.append(c);
1008: }
1009: } else {
1010: if (startedInt || startedFrac) {
1011: // this should never happen
1012: break;
1013: }
1014: }
1015: }
1016:
1017: if (bufInt.length() > 0) {
1018: m_intSize = bufInt.toString();
1019: if (bufFrac.length() > 0) {
1020: m_fracSize = bufFrac.toString();
1021: m_intSize = ""
1022: + (Integer.parseInt(m_intSize)
1023: - Integer.parseInt(m_fracSize) - 1);
1024: }
1025: }
1026: }
1027: }
1028:
1029: private void determineLabelToken(String applicationName,
1030: String moduleName, String domainObject) {
1031: m_labelToken = MessageHelper.tokenize("label."
1032: + StringHelper.getUpper1(applicationName) + '.'
1033: + StringHelper.getUpper1(moduleName) + '.'
1034: + StringHelper.getUpper1(domainObject) + '.'
1035: + m_name);
1036: }
1037:
1038: private void mergeWithExisitngElements(Root existingDomainObject) {
1039: if (existingDomainObject != null
1040: && existingDomainObject.getFields() != null
1041: && existingDomainObject.getFields().getField() != null) {
1042: for (Iterator itr = existingDomainObject.getFields()
1043: .getField().iterator(); itr.hasNext();) {
1044: Field existingField = (Field) itr.next();
1045: if (existingField.getName().equals(m_name)) {
1046: String value = null;
1047: value = existingField.getDescription();
1048: if (value != null)
1049: m_description = value;
1050:
1051: value = existingField.getLabelToken();
1052: if (value != null)
1053: m_labelToken = value;
1054:
1055: value = existingField.getLayout();
1056: if (value != null)
1057: m_layout = value;
1058:
1059: value = existingField.getPattern();
1060: if (value != null)
1061: m_pattern = value;
1062:
1063: value = existingField.getCaseType();
1064: if (value != null)
1065: m_caseType = value;
1066:
1067: m_ignore = Boolean.toString(existingField
1068: .isIgnore());
1069: }
1070: }
1071: }
1072: }
1073:
1074: }
1075:
1076: private static class InnerRelationship {
1077: private String m_toDomainObject = null;
1078: private String m_toDomainPackage = null;
1079: private String m_fromCardinality = null;
1080: private String m_toCardinality = null;
1081: private String m_type = null;
1082: private List m_fromFields = new ArrayList();
1083: private List m_toFields = new ArrayList();
1084: private List m_manyDatabaseFields = new ArrayList();
1085: private boolean m_valid = true;
1086:
1087: private InnerRelationship(String databaseTable, String schema,
1088: Connection connection, ResultSet rs,
1089: TableInfoReader.TableInfo tableInfo,
1090: List relationshipInfo) throws IOException, SQLException {
1091: String oneTableName = rs.getString("U_GLAB").trim()
1092: .toUpperCase();
1093: String oneSchema = rs.getString("U_VLAB").trim()
1094: .toUpperCase();
1095: String manyTableName = rs.getString("U_RGLAB").trim()
1096: .toUpperCase();
1097: String manySchema = rs.getString("U_RVLAB").trim()
1098: .toUpperCase();
1099: String manyFields = StringHelper.getString(
1100: rs.getCharacterStream("U_DOC")).trim();
1101: List oneFields = getPrimaryKeys(oneTableName, oneSchema,
1102: connection);
1103: if (databaseTable.equals(oneTableName)
1104: && schema.equals(oneSchema)) {
1105: // One-to-Many relationship
1106: determineToDomainObjectPackage(manyTableName,
1107: manySchema, tableInfo);
1108: if (m_valid) {
1109: for (Iterator i = oneFields.iterator(); i.hasNext();)
1110: m_fromFields.add(convertFieldName((String) i
1111: .next()));
1112: addManyFieldsToList(manyFields, m_toFields);
1113: determineCardinality(oneTableName, oneSchema,
1114: manyTableName, manySchema,
1115: relationshipInfo, connection, true);
1116: }
1117: } else {
1118: // Many-to-One relationship
1119: determineToDomainObjectPackage(oneTableName, oneSchema,
1120: tableInfo);
1121: if (m_valid) {
1122: for (Iterator i = oneFields.iterator(); i.hasNext();)
1123: m_toFields.add(convertFieldName((String) i
1124: .next()));
1125: addManyFieldsToList(manyFields, m_fromFields);
1126: determineCardinality(oneTableName, oneSchema,
1127: manyTableName, manySchema,
1128: relationshipInfo, connection, false);
1129: }
1130: }
1131: }
1132:
1133: private void determineToDomainObjectPackage(String toTableName,
1134: String toSchema, TableInfoReader.TableInfo tableInfo) {
1135: if (tableInfo.getTables() != null) {
1136: for (Iterator i = tableInfo.getTables().iterator(); i
1137: .hasNext();) {
1138: TableInfoReader.Table table = (TableInfoReader.Table) i
1139: .next();
1140: if (table.getDatabaseTable().equals(toTableName)
1141: && table.getSchema().equals(toSchema)) {
1142: m_toDomainObject = table.getDomainObject();
1143: m_toDomainPackage = determineDomainPackage(
1144: table, tableInfo);
1145: break;
1146: }
1147: }
1148: }
1149:
1150: // dirty !!!
1151: if (m_toDomainObject == null)
1152: m_valid = false;
1153: }
1154:
1155: private void addManyFieldsToList(String manyFields, List list) {
1156: // format of manyFields is '} 1} 2FLD1,FLD2} 3} 40} 5...'
1157: int i = manyFields.indexOf("} 2");
1158: if (i >= 0) {
1159: String str = manyFields.substring(i + 3);
1160: i = str.indexOf("} 3");
1161: if (i >= 0)
1162: str = str.substring(0, i);
1163: if (str.length() > 0) {
1164: StringTokenizer stknzr = new StringTokenizer(str,
1165: ", ");
1166: while (stknzr.hasMoreTokens()) {
1167: String token = stknzr.nextToken();
1168: list.add(convertFieldName(token));
1169: m_manyDatabaseFields.add(token.toUpperCase());
1170: }
1171: }
1172: }
1173: }
1174:
1175: private void determineCardinality(String oneTableName,
1176: String oneSchema, String manyTableName,
1177: String manySchema, List relationshipInfo,
1178: Connection connection, boolean oneToMany)
1179: throws IOException, SQLException {
1180: if (oneToMany) {
1181: if (relationshipInfo != null) {
1182: RelationshipInfoReader.Relationship relationship = new RelationshipInfoReader.Relationship(
1183: oneSchema, manySchema, oneTableName,
1184: manyTableName);
1185: int i = Collections.binarySearch(relationshipInfo,
1186: relationship);
1187: if (i >= 0) {
1188: relationship = (RelationshipInfoReader.Relationship) relationshipInfo
1189: .get(i);
1190: m_fromCardinality = relationship
1191: .getFromCardinality();
1192: m_toCardinality = relationship
1193: .getToCardinality();
1194: m_type = relationship.getType();
1195: }
1196: }
1197:
1198: if (m_fromCardinality == null)
1199: m_fromCardinality = "1";
1200:
1201: if (m_toCardinality == null) {
1202: List manyPrimaryKeys = getPrimaryKeys(
1203: manyTableName, manySchema, connection);
1204:
1205: //if (manyPrimaryKeys.equals(m_manyDatabaseFields))
1206: String[] primaryKeys = (String[]) manyPrimaryKeys
1207: .toArray(new String[0]);
1208: String[] foreignKeys = (String[]) m_manyDatabaseFields
1209: .toArray(new String[0]);
1210: Arrays.sort(primaryKeys);
1211: Arrays.sort(foreignKeys);
1212: if (Arrays.equals(primaryKeys, foreignKeys))
1213: m_toCardinality = "0..1";
1214: else
1215: m_toCardinality = "0..*";
1216: }
1217:
1218: } else {
1219: if (relationshipInfo != null) {
1220: RelationshipInfoReader.Relationship relationship = new RelationshipInfoReader.Relationship(
1221: manySchema, oneSchema, manyTableName,
1222: oneTableName);
1223: int i = Collections.binarySearch(relationshipInfo,
1224: relationship);
1225: if (i >= 0) {
1226: relationship = (RelationshipInfoReader.Relationship) relationshipInfo
1227: .get(i);
1228: m_toCardinality = relationship
1229: .getFromCardinality();
1230: m_fromCardinality = relationship
1231: .getToCardinality();
1232: m_type = relationship.getType();
1233: }
1234: }
1235:
1236: if (m_toCardinality == null)
1237: m_toCardinality = "1";
1238:
1239: if (m_fromCardinality == null) {
1240: List manyPrimaryKeys = getPrimaryKeys(
1241: manyTableName, manySchema, connection);
1242:
1243: //if (manyPrimaryKeys.equals(m_manyDatabaseFields))
1244: String[] primaryKeys = (String[]) manyPrimaryKeys
1245: .toArray(new String[0]);
1246: String[] foreignKeys = (String[]) m_manyDatabaseFields
1247: .toArray(new String[0]);
1248: Arrays.sort(primaryKeys);
1249: Arrays.sort(foreignKeys);
1250: if (Arrays.equals(primaryKeys, foreignKeys))
1251: m_fromCardinality = "0..1";
1252: else
1253: m_fromCardinality = "0..*";
1254: }
1255: }
1256:
1257: if (m_type == null)
1258: m_type = "association";
1259: }
1260: }
1261:
1262: // handle the escaped '{' , '}', EOL characters
1263: private static String replaceEscapedCharacters(String str) {
1264: if (str != null) {
1265: str = StringHelper.replace(str, "{A", "{");
1266: str = StringHelper.replace(str, "{C", "}");
1267: str = StringHelper.replace(str, "{.", " ");
1268: }
1269: return str;
1270: }
1271:
1272: private static String determineDomainObjectLabelToken(
1273: String applicationName, String moduleName,
1274: String domainObject, Root existingDomainObject) {
1275: String labelToken = existingDomainObject != null ? existingDomainObject
1276: .getLabelToken()
1277: : null;
1278: if (labelToken == null)
1279: labelToken = MessageHelper.tokenize("label."
1280: + StringHelper.getUpper1(applicationName) + '.'
1281: + StringHelper.getUpper1(moduleName) + '.'
1282: + StringHelper.getUpper1(domainObject));
1283: return labelToken;
1284: }
1285:
1286: }
|