Replaced calls to deprecated methods by calls to the new ones.
authorAntonio Moreno <antonio.moreno@openbravo.com>
Thu, 02 Apr 2009 10:38:41 +0000
changeset 147 7865a358caba
parent 146 3560990eebd0
child 148 dce403afa9d3
Replaced calls to deprecated methods by calls to the new ones.
.classpath
src/org/apache/ddlutils/alteration/DataComparator.java
src/org/apache/ddlutils/io/DataWriter.java
src/org/apache/ddlutils/model/Database.java
src/org/apache/ddlutils/platform/ModelLoaderBase.java
src/org/openbravo/ddlutils/task/ExportDataXMLMod.java
src/org/openbravo/ddlutils/task/ExportDatabase.java
--- a/.classpath	Thu Mar 26 17:00:07 2009 +0000
+++ b/.classpath	Thu Apr 02 10:38:41 2009 +0000
@@ -20,6 +20,5 @@
 	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
 	<classpathentry combineaccessrules="false" kind="src" path="/openbravo"/>
 	<classpathentry kind="lib" path="/openbravo/src-db/database/lib/dbmanager.jar"/>
-	<classpathentry combineaccessrules="false" kind="src" path="/OpenbravoCore"/>
 	<classpathentry kind="output" path="nbproject/build/classes"/>
 </classpath>
--- a/src/org/apache/ddlutils/alteration/DataComparator.java	Thu Mar 26 17:00:07 2009 +0000
+++ b/src/org/apache/ddlutils/alteration/DataComparator.java	Thu Apr 02 10:38:41 2009 +0000
@@ -70,13 +70,13 @@
 
     DataSetService service = DataSetService.getInstance();
     dataset = service.getDataSetByValue(datasetName);
-    List<DataSetTable> tableList = service.getDataSetTables(dataset);
+    List<DataSetTable> tableList = dataset.getDataSetTableList();
     tableMap = new HashMap<String, DataSetTable>();
     columnMap = new HashMap<String, HashMap<String, DataSetColumn>>();
     for (DataSetTable table : tableList) {
       tableMap.put(table.getTable().getDBTableName().toUpperCase(), table);
       HashMap<String, DataSetColumn> columnsT = new HashMap<String, DataSetColumn>();
-      List<DataSetColumn> columnList = service.getDataSetColumns(table);
+      List<DataSetColumn> columnList = table.getDataSetColumnList();
       for (DataSetColumn column : columnList)
         columnsT.put(column.getColumn().getDBColumnName().toUpperCase(), column);
       columnMap.put(table.getTable().getDBTableName().toUpperCase(), columnsT);
@@ -161,8 +161,7 @@
               while (answer != null && answer.hasNext()) {
                 BaseOBObject db = (BaseOBObject) answer.next();
                 List<Property> exportableProperties = service.getExportableProperties(db, tableMap
-                    .get(tableC.getName()), service.getDataSetColumns(tableMap
-                    .get(tableC.getName())));
+                    .get(tableC.getName()), tableMap.get(tableC.getName()).getDataSetColumnList());
                 Object value = null;
                 for (Property property : exportableProperties)
                   if (property.getColumnName().equalsIgnoreCase(columnC.getName()))
@@ -226,13 +225,13 @@
       _log.error("Error: dataset " + datasetName + " not found in database.");
       return;
     }
-    List<DataSetTable> tableList = service.getDataSetTables(dataset);
+    List<DataSetTable> tableList = dataset.getDataSetTableList();
     tableMap = new HashMap<String, DataSetTable>();
     columnMap = new HashMap<String, HashMap<String, DataSetColumn>>();
     for (DataSetTable table : tableList) {
       tableMap.put(table.getTable().getDBTableName().toUpperCase(), table);
       HashMap<String, DataSetColumn> columnsT = new HashMap<String, DataSetColumn>();
-      List<DataSetColumn> columnList = service.getDataSetColumns(table);
+      List<DataSetColumn> columnList = table.getDataSetColumnList();
 
       for (DataSetColumn column : columnList)
         columnsT.put(column.getColumn().getDBColumnName().toUpperCase(), column);
@@ -698,7 +697,7 @@
     SqlDynaClass dynaClass = model.getDynaClassFor(dbOrg);
     SqlDynaProperty[] primaryKeys = dynaClass.getPrimaryKeyProperties();
     DataSetTable datasetTable = tableMap.get(dynaClass.getTable().getName());
-    List<DataSetColumn> allColumns = DataSetService.getInstance().getDataSetColumns(datasetTable);
+    List<DataSetColumn> allColumns = datasetTable.getDataSetColumnList();
     List<Property> properties = DataSetService.getInstance().getExportableProperties(dbNew,
         datasetTable, allColumns);
 
@@ -824,7 +823,7 @@
     SqlDynaClass dynaClass = model.getDynaClassFor(dbNew);
     SqlDynaProperty[] primaryKeys = dynaClass.getPrimaryKeyProperties();
 
-    List<DataSetColumn> allColumns = DataSetService.getInstance().getDataSetColumns(datasetTable);
+    List<DataSetColumn> allColumns = datasetTable.getDataSetColumnList();
     List<Property> properties = DataSetService.getInstance().getExportableProperties(dbOrg,
         datasetTable, allColumns);
     int obPending = 1;
@@ -930,7 +929,7 @@
     SqlDynaClass dynaClass = model.getDynaClassFor(dbNew);
     SqlDynaProperty[] primaryKeys = dynaClass.getPrimaryKeyProperties();
 
-    List<DataSetColumn> allColumns = DataSetService.getInstance().getDataSetColumns(datasetTable);
+    List<DataSetColumn> allColumns = datasetTable.getDataSetColumnList();
     List<Property> properties = DataSetService.getInstance().getExportableProperties(dbOrg,
         datasetTable, allColumns);
 
--- a/src/org/apache/ddlutils/io/DataWriter.java	Thu Mar 26 17:00:07 2009 +0000
+++ b/src/org/apache/ddlutils/io/DataWriter.java	Thu Apr 02 10:38:41 2009 +0000
@@ -58,563 +58,535 @@
  * @version $Revision: 289996 $
  */
 public class DataWriter {
-    /**
-     * String values with a size not bigger than this value will be written to
-     * attributes; if their size is longer, then a sub element is generated
-     * instead.
-     */
-    private static final int MAX_ATTRIBUTE_LENGTH = 255;
-    /** The indentation string. */
-    private static final String INDENT_STRING = "  ";
+  /**
+   * String values with a size not bigger than this value will be written to attributes; if their
+   * size is longer, then a sub element is generated instead.
+   */
+  private static final int MAX_ATTRIBUTE_LENGTH = 255;
+  /** The indentation string. */
+  private static final String INDENT_STRING = "  ";
 
-    /** Our log. */
-    private final Log _log = LogFactory.getLog(DataWriter.class);
+  /** Our log. */
+  private final Log _log = LogFactory.getLog(DataWriter.class);
 
-    /** The converters. */
-    private ConverterConfiguration _converterConf = new ConverterConfiguration();
-    /** The output stream. */
-    private PrintWriter _output;
-    /** The xml writer. */
-    private XMLStreamWriter _writer;
-    /** The output encoding. */
-    private String _encoding;
-    /** Whether we're pretty-printing. */
-    private boolean _prettyPrinting = true;
+  /** The converters. */
+  private ConverterConfiguration _converterConf = new ConverterConfiguration();
+  /** The output stream. */
+  private PrintWriter _output;
+  /** The xml writer. */
+  private XMLStreamWriter _writer;
+  /** The output encoding. */
+  private String _encoding;
+  /** Whether we're pretty-printing. */
+  private boolean _prettyPrinting = true;
 
-    /**
-     * Creates a data writer instance using UTF-8 encoding.
-     * 
-     * @param output
-     *            The target to write the data XML to
-     */
-    public DataWriter(OutputStream output) throws DataWriterException {
-        this(output, null);
+  /**
+   * Creates a data writer instance using UTF-8 encoding.
+   * 
+   * @param output
+   *          The target to write the data XML to
+   */
+  public DataWriter(OutputStream output) throws DataWriterException {
+    this(output, null);
+  }
+
+  /**
+   * Creates a data writer instance.
+   * 
+   * @param output
+   *          The target to write the data XML to
+   * @param encoding
+   *          The encoding of the XML file
+   */
+  public DataWriter(OutputStream output, String encoding) throws DataWriterException {
+    _output = new PrintWriter(output);
+    if ((encoding == null) || (encoding.length() == 0)) {
+      _encoding = "UTF-8";
+    } else {
+      _encoding = encoding;
     }
 
-    /**
-     * Creates a data writer instance.
-     * 
-     * @param output
-     *            The target to write the data XML to
-     * @param encoding
-     *            The encoding of the XML file
-     */
-    public DataWriter(OutputStream output, String encoding)
-            throws DataWriterException {
-        _output = new PrintWriter(output);
-        if ((encoding == null) || (encoding.length() == 0)) {
-            _encoding = "UTF-8";
-        } else {
-            _encoding = encoding;
+    try {
+      XMLOutputFactory factory = XMLOutputFactory.newInstance();
+
+      _writer = factory.createXMLStreamWriter(output, _encoding);
+    } catch (XMLStreamException ex) {
+      throw new DataWriterException(ex);
+    }
+  }
+
+  /**
+   * Creates a data writer instance using the specified writer. Note that the writer needs to be
+   * configured using the specified encoding.
+   * 
+   * @param output
+   *          The target to write the data XML to
+   * @param encoding
+   *          The encoding of the writer
+   */
+  public DataWriter(Writer output, String encoding) throws DataWriterException {
+    _output = new PrintWriter(output);
+    _encoding = encoding;
+    try {
+      XMLOutputFactory factory = XMLOutputFactory.newInstance();
+
+      _writer = factory.createXMLStreamWriter(_output);
+    } catch (XMLStreamException ex) {
+      throw new DataWriterException(ex);
+    }
+  }
+
+  /**
+   * Determines whether the output shall be pretty-printed.
+   * 
+   * @return <code>true</code> if the output is pretty-printed
+   */
+  public boolean isPrettyPrinting() {
+    return _prettyPrinting;
+  }
+
+  /**
+   * Specifies whether the output shall be pretty-printed.
+   * 
+   * @param prettyPrinting
+   *          <code>true</code> if the output is pretty-printed
+   */
+  public void setPrettyPrinting(boolean prettyPrinting) {
+    _prettyPrinting = prettyPrinting;
+  }
+
+  /**
+   * Returns the converter configuration of this data reader.
+   * 
+   * @return The converter configuration
+   */
+  public ConverterConfiguration getConverterConfiguration() {
+    return _converterConf;
+  }
+
+  /**
+   * Prints a newline if we're pretty-printing.
+   */
+  private void printlnIfPrettyPrinting() throws DataWriterException {
+    if (_prettyPrinting) {
+      try {
+        _writer.writeCharacters("\n");
+      } catch (XMLStreamException ex) {
+        throw new DataWriterException(ex);
+      }
+    }
+  }
+
+  /**
+   * Prints the indentation if we're pretty-printing.
+   * 
+   * @param level
+   *          The indentation level
+   */
+  private void indentIfPrettyPrinting(int level) throws DataWriterException {
+    if (_prettyPrinting) {
+      try {
+        for (int idx = 0; idx < level; idx++) {
+          _writer.writeCharacters(INDENT_STRING);
         }
+      } catch (XMLStreamException ex) {
+        throw new DataWriterException(ex);
+      }
+    }
+  }
 
-        try {
-            XMLOutputFactory factory = XMLOutputFactory.newInstance();
+  /**
+   * Writes the start of the XML document, i.e. the "<?xml?>" section and the start of the root
+   * node.
+   */
+  public void writeDocumentStart() throws DataWriterException {
+    try {
+      _writer.writeStartDocument(_encoding, "1.0");
+      printlnIfPrettyPrinting();
+      _writer.writeStartElement("data");
+      printlnIfPrettyPrinting();
+    } catch (XMLStreamException ex) {
+      throw new DataWriterException(ex);
+    }
+  }
 
-            _writer = factory.createXMLStreamWriter(output, _encoding);
-        } catch (XMLStreamException ex) {
-            throw new DataWriterException(ex);
-        }
+  /**
+   * Writes the end of the XML document, i.e. end of the root node.
+   */
+  public void writeDocumentEnd() throws DataWriterException {
+    try {
+      _writer.writeEndElement();
+      printlnIfPrettyPrinting();
+      _writer.writeEndDocument();
+      _writer.flush();
+      _writer.close();
+      _output.close();
+    } catch (XMLStreamException ex) {
+      throw new DataWriterException(ex);
+    }
+  }
+
+  /**
+   * Writes the given bean.
+   * 
+   * @param bean
+   *          The bean to write
+   */
+  public void write(SqlDynaBean bean) throws DataWriterException {
+    SqlDynaClass dynaClass = (SqlDynaClass) bean.getDynaClass();
+    Table table = dynaClass.getTable();
+    Vector subElements = new Vector();
+    Vector elementValues = new Vector();
+    Column[] pks = table.getPrimaryKeyColumns();
+    String comment = "";
+    for (int i = 0; i < pks.length; i++) {
+      if (i > 0)
+        comment += " ";
+      comment += bean.get(pks[i].getName()).toString();
     }
 
-    /**
-     * Creates a data writer instance using the specified writer. Note that the
-     * writer needs to be configured using the specified encoding.
-     * 
-     * @param output
-     *            The target to write the data XML to
-     * @param encoding
-     *            The encoding of the writer
-     */
-    public DataWriter(Writer output, String encoding)
-            throws DataWriterException {
-        _output = new PrintWriter(output);
-        _encoding = encoding;
-        try {
-            XMLOutputFactory factory = XMLOutputFactory.newInstance();
+    try {
+      _writer.writeComment(comment + "**************");
+      // indentIfPrettyPrinting(5);
+      _writer.writeStartElement(table.getName());
+      for (int idx = 0; idx < table.getColumnCount(); idx++) {
+        Column column = table.getColumn(idx);
+        Object value = bean.get(column.getName());
+        SqlTypeConverter converter = _converterConf.getRegisteredConverter(table, column);
+        String valueAsText = null;
 
-            _writer = factory.createXMLStreamWriter(_output);
-        } catch (XMLStreamException ex) {
-            throw new DataWriterException(ex);
+        if (converter == null) {
+          if (value != null) {
+            valueAsText = value.toString();
+          }
+        } else {
+          valueAsText = converter.convertToString(value, column.getTypeCode());
         }
+        if (valueAsText != null) {
+          // we create an attribute only if the text is not too long
+          // and if it does not contain special characters
+          if (true)// (valueAsText.length() > MAX_ATTRIBUTE_LENGTH)
+          // || analyzeText(valueAsText, null))
+          {
+            // we defer writing the sub elements
+            subElements.add(column.getName());
+            elementValues.add(valueAsText);
+          } else {
+            _writer.writeAttribute(column.getName(), valueAsText);
+          }
+        }
+      }
+      if (!subElements.isEmpty()) {
+        List cutPoints = new ArrayList();
+
+        int i = 0;
+        while (i < subElements.size()) {
+          String entry = (String) subElements.get(i);
+          String content = (String) elementValues.get(i);
+
+          printlnIfPrettyPrinting();
+          _writer.writeComment(comment + " " + entry);
+          /*
+           * indentIfPrettyPrinting(3); _writer.writeComment(entry);
+           */
+          printlnIfPrettyPrinting();
+          _writer.writeComment(comment);
+          indentIfPrettyPrinting(7);
+          _writer.writeStartElement(entry);
+
+          // if the content contains special characters, we have to
+          // apply base64 encoding to it
+          // if the content is too short, then it has to contain
+          // special characters (otherwise
+          // it would have been written as an attribute already),
+          // otherwise we check
+          cutPoints.clear();
+
+          boolean writeBase64Encoded = analyzeText(content, cutPoints);
+
+          if (writeBase64Encoded) {
+            _writer.writeAttribute(DatabaseIO.BASE64_ATTR_NAME, "true");
+            _writer.writeCData(new String(Base64.encodeBase64(content.getBytes())));
+          } else {
+            if (cutPoints.isEmpty()) {
+              _writer.writeCData(content);
+            } else {
+              int lastPos = 0;
+
+              for (Iterator cutPointIt = cutPoints.iterator(); cutPointIt.hasNext();) {
+                int curPos = ((Integer) cutPointIt.next()).intValue();
+
+                _writer.writeCData(content.substring(lastPos, curPos));
+                lastPos = curPos;
+              }
+              if (lastPos < content.length()) {
+                _writer.writeCData(content.substring(lastPos));
+              }
+            }
+          }
+
+          _writer.writeEndElement();
+          // We now write a comment with the primary keys of the
+          // element
+          i++;
+        }
+        printlnIfPrettyPrinting();
+        _writer.writeComment(comment + "**************");
+        // indentIfPrettyPrinting(5);
+      }
+      _writer.writeEndElement();
+      printlnIfPrettyPrinting();
+      printlnIfPrettyPrinting();
+    } catch (XMLStreamException ex) {
+      throw new DataWriterException(ex);
+    } catch (ConversionException ex) {
+      throw new DataWriterException(ex);
+    }
+  }
+
+  /**
+   * Writes the given bean.
+   * 
+   * @param bean
+   *          The bean to write
+   */
+  public void write(Database db, DataSetService datasetService, DataSetTable datasetTable,
+      List<DataSetColumn> datasetColumns, BaseOBObject object) throws DataWriterException {
+    Vector subElements = new Vector();
+    Vector elementValues = new Vector();
+    List<Property> pkProperties = object.getEntity().getIdProperties();
+    Table table = db.findTable(datasetTable.getTable().getDBTableName());
+
+    String comment = "";
+    for (int i = 0; i < pkProperties.size(); i++) {
+      if (i > 0)
+        comment += " ";
+      comment += object.get(pkProperties.get(i).getName()).toString();
     }
 
-    /**
-     * Determines whether the output shall be pretty-printed.
-     * 
-     * @return <code>true</code> if the output is pretty-printed
-     */
-    public boolean isPrettyPrinting() {
-        return _prettyPrinting;
+    List<Property> exportableColumns = datasetService.getExportableProperties(object, datasetTable,
+        datasetColumns); // object.getEntity().
+    // getProperties();
+    try {
+      _writer.writeComment(comment);
+      // indentIfPrettyPrinting(5);
+      _writer.writeStartElement(table.getName());
+      for (int i = 0; i < table.getColumnCount(); i++) {
+        Property prop = null;
+        for (int j = 0; j < exportableColumns.size(); j++)
+          if (exportableColumns.get(j).getColumnName() != null
+              && exportableColumns.get(j).getColumnName().equalsIgnoreCase(
+                  table.getColumn(i).getName()))
+            prop = exportableColumns.get(j);
+        if (prop != null) {
+          Column column = table.findColumn(prop.getColumnName());
+          Object primValue = object.get(prop.getName());
+          Object value;
+          if (primValue instanceof BaseOBObject)
+            value = DalUtil.getReferencedPropertyValue(prop, primValue);
+          else
+            value = primValue;
+          if (value instanceof Boolean) {
+            if (((Boolean) value).booleanValue())
+              value = "Y";
+            else
+              value = "N";
+          } else if (value instanceof String) {
+            if (value != null && value.equals(""))
+              value = null;
+          }
+
+          SqlTypeConverter converter = _converterConf.getRegisteredConverter(table, column);
+          String valueAsText = null;
+
+          if (converter == null) {
+            if (value != null) {
+              valueAsText = value.toString();
+            }
+          } else {
+            valueAsText = converter.convertToString(value, column.getTypeCode());
+          }
+          if (valueAsText != null) {
+            // we create an attribute only if the text is not too
+            // long
+            // and if it does not contain special characters
+            if (true)// (valueAsText.length() >
+            // MAX_ATTRIBUTE_LENGTH) ||
+            // analyzeText(valueAsText, null))
+            {
+              // we defer writing the sub elements
+              subElements.add(column.getName());
+              elementValues.add(valueAsText);
+            } else {
+              _writer.writeAttribute(column.getName(), valueAsText);
+            }
+          }
+        }
+      }
+      if (!subElements.isEmpty()) {
+        List cutPoints = new ArrayList();
+
+        int i = 0;
+        while (i < subElements.size()) {
+          String entry = (String) subElements.get(i);
+          String content = (String) elementValues.get(i);
+
+          // printlnIfPrettyPrinting();
+          // _writer.writeComment(comment + " " + entry);
+          /*
+           * indentIfPrettyPrinting(3); _writer.writeComment(entry);
+           */
+          printlnIfPrettyPrinting();
+          _writer.writeComment(comment);
+          indentIfPrettyPrinting(1);
+          _writer.writeStartElement(entry);
+
+          // if the content contains special characters, we have to
+          // apply base64 encoding to it
+          // if the content is too short, then it has to contain
+          // special characters (otherwise
+          // it would have been written as an attribute already),
+          // otherwise we check
+          cutPoints.clear();
+
+          boolean writeBase64Encoded = analyzeText(content, cutPoints);
+
+          if (writeBase64Encoded) {
+            _writer.writeAttribute(DatabaseIO.BASE64_ATTR_NAME, "true");
+            _writer.writeCData(new String(Base64.encodeBase64(content.getBytes())));
+          } else {
+            if (cutPoints.isEmpty()) {
+              _writer.writeCData(content);
+            } else {
+              int lastPos = 0;
+
+              for (Iterator cutPointIt = cutPoints.iterator(); cutPointIt.hasNext();) {
+                int curPos = ((Integer) cutPointIt.next()).intValue();
+
+                _writer.writeCData(content.substring(lastPos, curPos));
+                lastPos = curPos;
+              }
+              if (lastPos < content.length()) {
+                _writer.writeCData(content.substring(lastPos));
+              }
+            }
+          }
+
+          _writer.writeEndElement();
+          // We now write a comment with the primary keys of the
+          // element
+          i++;
+        }
+        printlnIfPrettyPrinting();
+        _writer.writeComment(comment);
+        // indentIfPrettyPrinting(5);
+      }
+      _writer.writeEndElement();
+      printlnIfPrettyPrinting();
+      printlnIfPrettyPrinting();
+    } catch (XMLStreamException ex) {
+      throw new DataWriterException(ex);
+    } catch (ConversionException ex) {
+      throw new DataWriterException(ex);
     }
+  }
 
-    /**
-     * Specifies whether the output shall be pretty-printed.
-     * 
-     * @param prettyPrinting
-     *            <code>true</code> if the output is pretty-printed
-     */
-    public void setPrettyPrinting(boolean prettyPrinting) {
-        _prettyPrinting = prettyPrinting;
+  /**
+   * Determines whether the given string contains special characters that cannot be used in XML, and
+   * if not, finds the cut points where to split the text when writing it in a CDATA section.
+   * 
+   * @param text
+   *          The text
+   * @param cutPoints
+   *          Will be filled with cut points to split the text when writing it in a CDATA section
+   *          (only if the method returns <code>false</code>)
+   * @return <code>true</code> if the text contains special characters
+   */
+  private boolean analyzeText(String text, List cutPoints) {
+    List tmpCutPoints = cutPoints == null ? null : new ArrayList();
+    int numChars = text.length();
+    int numFoundCDataEndChars = 0;
+
+    for (int charPos = 0; charPos < numChars; charPos++) {
+      char c = text.charAt(charPos);
+
+      if ((c < 0x0020) && (c != '\n') && (c != '\r') && (c != '\t')) {
+        return true;
+      } else if (cutPoints != null) {
+        if ((c == ']') && ((numFoundCDataEndChars == 0) || (numFoundCDataEndChars == 1))) {
+          numFoundCDataEndChars++;
+        } else if ((c == '>') && (numFoundCDataEndChars == 2)) {
+          // we have to split the CDATA right here before the '>' (see
+          // DDLUTILS-174)
+          tmpCutPoints.add(new Integer(charPos));
+          numFoundCDataEndChars = 0;
+        } else {
+          numFoundCDataEndChars = 0;
+        }
+      }
     }
+    if (cutPoints != null) {
+      cutPoints.addAll(tmpCutPoints);
+    }
+    return false;
+  }
 
-    /**
-     * Returns the converter configuration of this data reader.
-     * 
-     * @return The converter configuration
-     */
-    public ConverterConfiguration getConverterConfiguration() {
-        return _converterConf;
+  /**
+   * Writes the beans contained in the given iterator.
+   * 
+   * @param beans
+   *          The beans iterator
+   */
+  public void write(Iterator beans) throws DataWriterException {
+    try {
+      boolean first = true;
+      while (beans.hasNext()) {
+        DynaBean bean = (DynaBean) beans.next();
+
+        if (bean instanceof SqlDynaBean) {
+          SqlDynaClass dynaClass = (SqlDynaClass) bean.getDynaClass();
+
+          write((SqlDynaBean) bean);
+        } else {
+          _log.warn("Cannot write normal dyna beans (type: " + bean.getDynaClass().getName() + ")");
+        }
+      }
+    } catch (ConversionException ex) {
+      throw new DataWriterException(ex);
     }
+  }
 
-    /**
-     * Prints a newline if we're pretty-printing.
-     */
-    private void printlnIfPrettyPrinting() throws DataWriterException {
-        if (_prettyPrinting) {
-            try {
-                _writer.writeCharacters("\n");
-            } catch (XMLStreamException ex) {
-                throw new DataWriterException(ex);
-            }
-        }
+  public boolean write(Database db, DataSetService datasetService, DataSetTable datasetTable,
+      String moduleID) throws DataWriterException {
+    try {
+      List<DataSetColumn> datasetColumns = datasetTable.getDataSetColumnList();
+      List<BaseOBObject> objectList = null;
+      boolean b = false;
+      try {
+        objectList = datasetService.getExportableObjects(datasetTable, moduleID);
+      } catch (Exception e) {
+        _log.error("Table " + datasetTable.getTable().getDBTableName()
+            + " not found in runtime model");
+        e.printStackTrace();
+        return b;
+      }
+      for (BaseOBObject object : objectList) {
+        write(db, datasetService, datasetTable, datasetColumns, object);
+        b = true; // we wrote something
+      }
+      return b;
+    } catch (ConversionException ex) {
+      throw new DataWriterException(ex);
     }
+  }
 
-    /**
-     * Prints the indentation if we're pretty-printing.
-     * 
-     * @param level
-     *            The indentation level
-     */
-    private void indentIfPrettyPrinting(int level) throws DataWriterException {
-        if (_prettyPrinting) {
-            try {
-                for (int idx = 0; idx < level; idx++) {
-                    _writer.writeCharacters(INDENT_STRING);
-                }
-            } catch (XMLStreamException ex) {
-                throw new DataWriterException(ex);
-            }
-        }
-    }
-
-    /**
-     * Writes the start of the XML document, i.e. the "<?xml?>" section and the
-     * start of the root node.
-     */
-    public void writeDocumentStart() throws DataWriterException {
-        try {
-            _writer.writeStartDocument(_encoding, "1.0");
-            printlnIfPrettyPrinting();
-            _writer.writeStartElement("data");
-            printlnIfPrettyPrinting();
-        } catch (XMLStreamException ex) {
-            throw new DataWriterException(ex);
-        }
-    }
-
-    /**
-     * Writes the end of the XML document, i.e. end of the root node.
-     */
-    public void writeDocumentEnd() throws DataWriterException {
-        try {
-            _writer.writeEndElement();
-            printlnIfPrettyPrinting();
-            _writer.writeEndDocument();
-            _writer.flush();
-            _writer.close();
-            _output.close();
-        } catch (XMLStreamException ex) {
-            throw new DataWriterException(ex);
-        }
-    }
-
-    /**
-     * Writes the given bean.
-     * 
-     * @param bean
-     *            The bean to write
-     */
-    public void write(SqlDynaBean bean) throws DataWriterException {
-        SqlDynaClass dynaClass = (SqlDynaClass) bean.getDynaClass();
-        Table table = dynaClass.getTable();
-        Vector subElements = new Vector();
-        Vector elementValues = new Vector();
-        Column[] pks = table.getPrimaryKeyColumns();
-        String comment = "";
-        for (int i = 0; i < pks.length; i++) {
-            if (i > 0)
-                comment += " ";
-            comment += bean.get(pks[i].getName()).toString();
-        }
-
-        try {
-            _writer.writeComment(comment + "**************");
-            // indentIfPrettyPrinting(5);
-            _writer.writeStartElement(table.getName());
-            for (int idx = 0; idx < table.getColumnCount(); idx++) {
-                Column column = table.getColumn(idx);
-                Object value = bean.get(column.getName());
-                SqlTypeConverter converter = _converterConf
-                        .getRegisteredConverter(table, column);
-                String valueAsText = null;
-
-                if (converter == null) {
-                    if (value != null) {
-                        valueAsText = value.toString();
-                    }
-                } else {
-                    valueAsText = converter.convertToString(value, column
-                            .getTypeCode());
-                }
-                if (valueAsText != null) {
-                    // we create an attribute only if the text is not too long
-                    // and if it does not contain special characters
-                    if (true)// (valueAsText.length() > MAX_ATTRIBUTE_LENGTH)
-                    // || analyzeText(valueAsText, null))
-                    {
-                        // we defer writing the sub elements
-                        subElements.add(column.getName());
-                        elementValues.add(valueAsText);
-                    } else {
-                        _writer.writeAttribute(column.getName(), valueAsText);
-                    }
-                }
-            }
-            if (!subElements.isEmpty()) {
-                List cutPoints = new ArrayList();
-
-                int i = 0;
-                while (i < subElements.size()) {
-                    String entry = (String) subElements.get(i);
-                    String content = (String) elementValues.get(i);
-
-                    printlnIfPrettyPrinting();
-                    _writer.writeComment(comment + " " + entry);
-                    /*
-                     * indentIfPrettyPrinting(3); _writer.writeComment(entry);
-                     */
-                    printlnIfPrettyPrinting();
-                    _writer.writeComment(comment);
-                    indentIfPrettyPrinting(7);
-                    _writer.writeStartElement(entry);
-
-                    // if the content contains special characters, we have to
-                    // apply base64 encoding to it
-                    // if the content is too short, then it has to contain
-                    // special characters (otherwise
-                    // it would have been written as an attribute already),
-                    // otherwise we check
-                    cutPoints.clear();
-
-                    boolean writeBase64Encoded = analyzeText(content, cutPoints);
-
-                    if (writeBase64Encoded) {
-                        _writer.writeAttribute(DatabaseIO.BASE64_ATTR_NAME,
-                                "true");
-                        _writer.writeCData(new String(Base64
-                                .encodeBase64(content.getBytes())));
-                    } else {
-                        if (cutPoints.isEmpty()) {
-                            _writer.writeCData(content);
-                        } else {
-                            int lastPos = 0;
-
-                            for (Iterator cutPointIt = cutPoints.iterator(); cutPointIt
-                                    .hasNext();) {
-                                int curPos = ((Integer) cutPointIt.next())
-                                        .intValue();
-
-                                _writer.writeCData(content.substring(lastPos,
-                                        curPos));
-                                lastPos = curPos;
-                            }
-                            if (lastPos < content.length()) {
-                                _writer.writeCData(content.substring(lastPos));
-                            }
-                        }
-                    }
-
-                    _writer.writeEndElement();
-                    // We now write a comment with the primary keys of the
-                    // element
-                    i++;
-                }
-                printlnIfPrettyPrinting();
-                _writer.writeComment(comment + "**************");
-                // indentIfPrettyPrinting(5);
-            }
-            _writer.writeEndElement();
-            printlnIfPrettyPrinting();
-            printlnIfPrettyPrinting();
-        } catch (XMLStreamException ex) {
-            throw new DataWriterException(ex);
-        } catch (ConversionException ex) {
-            throw new DataWriterException(ex);
-        }
-    }
-
-    /**
-     * Writes the given bean.
-     * 
-     * @param bean
-     *            The bean to write
-     */
-    public void write(Database db, DataSetService datasetService,
-            DataSetTable datasetTable, List<DataSetColumn> datasetColumns,
-            BaseOBObject object) throws DataWriterException {
-        Vector subElements = new Vector();
-        Vector elementValues = new Vector();
-        List<Property> pkProperties = object.getEntity().getIdProperties();
-        Table table = db.findTable(datasetTable.getTable().getDBTableName());
-
-        String comment = "";
-        for (int i = 0; i < pkProperties.size(); i++) {
-            if (i > 0)
-                comment += " ";
-            comment += object.get(pkProperties.get(i).getName()).toString();
-        }
-
-        List<Property> exportableColumns = datasetService.getExportableProperties(
-                object, datasetTable, datasetColumns); // object.getEntity().
-        // getProperties();
-        try {
-            _writer.writeComment(comment);
-            // indentIfPrettyPrinting(5);
-            _writer.writeStartElement(table.getName());
-            for (int i = 0; i < table.getColumnCount(); i++) {
-                Property prop = null;
-                for (int j = 0; j < exportableColumns.size(); j++)
-                    if (exportableColumns.get(j).getColumnName() != null
-                            && exportableColumns.get(j).getColumnName()
-                                    .equalsIgnoreCase(
-                                            table.getColumn(i).getName()))
-                        prop = exportableColumns.get(j);
-                if (prop != null) {
-                    Column column = table.findColumn(prop.getColumnName());
-                    Object primValue = object.get(prop.getName());
-                    Object value;
-                    if (primValue instanceof BaseOBObject)
-                        value = DalUtil.getReferencedPropertyValue(prop,
-                                primValue);
-                    else
-                        value = primValue;
-                    if (value instanceof Boolean) {
-                        if (((Boolean) value).booleanValue())
-                            value = "Y";
-                        else
-                            value = "N";
-                    } else if (value instanceof String) {
-                        if (value != null && value.equals(""))
-                            value = null;
-                    }
-
-                    SqlTypeConverter converter = _converterConf
-                            .getRegisteredConverter(table, column);
-                    String valueAsText = null;
-
-                    if (converter == null) {
-                        if (value != null) {
-                            valueAsText = value.toString();
-                        }
-                    } else {
-                        valueAsText = converter.convertToString(value, column
-                                .getTypeCode());
-                    }
-                    if (valueAsText != null) {
-                        // we create an attribute only if the text is not too
-                        // long
-                        // and if it does not contain special characters
-                        if (true)// (valueAsText.length() >
-                        // MAX_ATTRIBUTE_LENGTH) ||
-                        // analyzeText(valueAsText, null))
-                        {
-                            // we defer writing the sub elements
-                            subElements.add(column.getName());
-                            elementValues.add(valueAsText);
-                        } else {
-                            _writer.writeAttribute(column.getName(),
-                                    valueAsText);
-                        }
-                    }
-                }
-            }
-            if (!subElements.isEmpty()) {
-                List cutPoints = new ArrayList();
-
-                int i = 0;
-                while (i < subElements.size()) {
-                    String entry = (String) subElements.get(i);
-                    String content = (String) elementValues.get(i);
-
-                    // printlnIfPrettyPrinting();
-                    // _writer.writeComment(comment + " " + entry);
-                    /*
-                     * indentIfPrettyPrinting(3); _writer.writeComment(entry);
-                     */
-                    printlnIfPrettyPrinting();
-                    _writer.writeComment(comment);
-                    indentIfPrettyPrinting(1);
-                    _writer.writeStartElement(entry);
-
-                    // if the content contains special characters, we have to
-                    // apply base64 encoding to it
-                    // if the content is too short, then it has to contain
-                    // special characters (otherwise
-                    // it would have been written as an attribute already),
-                    // otherwise we check
-                    cutPoints.clear();
-
-                    boolean writeBase64Encoded = analyzeText(content, cutPoints);
-
-                    if (writeBase64Encoded) {
-                        _writer.writeAttribute(DatabaseIO.BASE64_ATTR_NAME,
-                                "true");
-                        _writer.writeCData(new String(Base64
-                                .encodeBase64(content.getBytes())));
-                    } else {
-                        if (cutPoints.isEmpty()) {
-                            _writer.writeCData(content);
-                        } else {
-                            int lastPos = 0;
-
-                            for (Iterator cutPointIt = cutPoints.iterator(); cutPointIt
-                                    .hasNext();) {
-                                int curPos = ((Integer) cutPointIt.next())
-                                        .intValue();
-
-                                _writer.writeCData(content.substring(lastPos,
-                                        curPos));
-                                lastPos = curPos;
-                            }
-                            if (lastPos < content.length()) {
-                                _writer.writeCData(content.substring(lastPos));
-                            }
-                        }
-                    }
-
-                    _writer.writeEndElement();
-                    // We now write a comment with the primary keys of the
-                    // element
-                    i++;
-                }
-                printlnIfPrettyPrinting();
-                _writer.writeComment(comment);
-                // indentIfPrettyPrinting(5);
-            }
-            _writer.writeEndElement();
-            printlnIfPrettyPrinting();
-            printlnIfPrettyPrinting();
-        } catch (XMLStreamException ex) {
-            throw new DataWriterException(ex);
-        } catch (ConversionException ex) {
-            throw new DataWriterException(ex);
-        }
-    }
-
-    /**
-     * Determines whether the given string contains special characters that
-     * cannot be used in XML, and if not, finds the cut points where to split
-     * the text when writing it in a CDATA section.
-     * 
-     * @param text
-     *            The text
-     * @param cutPoints
-     *            Will be filled with cut points to split the text when writing
-     *            it in a CDATA section (only if the method returns
-     *            <code>false</code>)
-     * @return <code>true</code> if the text contains special characters
-     */
-    private boolean analyzeText(String text, List cutPoints) {
-        List tmpCutPoints = cutPoints == null ? null : new ArrayList();
-        int numChars = text.length();
-        int numFoundCDataEndChars = 0;
-
-        for (int charPos = 0; charPos < numChars; charPos++) {
-            char c = text.charAt(charPos);
-
-            if ((c < 0x0020) && (c != '\n') && (c != '\r') && (c != '\t')) {
-                return true;
-            } else if (cutPoints != null) {
-                if ((c == ']')
-                        && ((numFoundCDataEndChars == 0) || (numFoundCDataEndChars == 1))) {
-                    numFoundCDataEndChars++;
-                } else if ((c == '>') && (numFoundCDataEndChars == 2)) {
-                    // we have to split the CDATA right here before the '>' (see
-                    // DDLUTILS-174)
-                    tmpCutPoints.add(new Integer(charPos));
-                    numFoundCDataEndChars = 0;
-                } else {
-                    numFoundCDataEndChars = 0;
-                }
-            }
-        }
-        if (cutPoints != null) {
-            cutPoints.addAll(tmpCutPoints);
-        }
-        return false;
-    }
-
-    /**
-     * Writes the beans contained in the given iterator.
-     * 
-     * @param beans
-     *            The beans iterator
-     */
-    public void write(Iterator beans) throws DataWriterException {
-        try {
-            boolean first = true;
-            while (beans.hasNext()) {
-                DynaBean bean = (DynaBean) beans.next();
-
-                if (bean instanceof SqlDynaBean) {
-                    SqlDynaClass dynaClass = (SqlDynaClass) bean.getDynaClass();
-
-                    write((SqlDynaBean) bean);
-                } else {
-                    _log.warn("Cannot write normal dyna beans (type: "
-                            + bean.getDynaClass().getName() + ")");
-                }
-            }
-        } catch (ConversionException ex) {
-            throw new DataWriterException(ex);
-        }
-    }
-
-    public boolean write(Database db, DataSetService datasetService,
-            DataSetTable datasetTable, String moduleID)
-            throws DataWriterException {
-        try {
-            List<DataSetColumn> datasetColumns = datasetService
-                    .getDataSetColumns(datasetTable);
-            List<BaseOBObject> objectList = null;
-            boolean b = false;
-            try {
-                objectList = datasetService.getExportableObjects(datasetTable,
-                        moduleID);
-            } catch (Exception e) {
-                _log.error("Table " + datasetTable.getTable().getDBTableName()
-                        + " not found in runtime model");
-                e.printStackTrace();
-                return b;
-            }
-            for (BaseOBObject object : objectList) {
-                write(db, datasetService, datasetTable, datasetColumns, object);
-                b = true; // we wrote something
-            }
-            return b;
-        } catch (ConversionException ex) {
-            throw new DataWriterException(ex);
-        }
-    }
-
-    /**
-     * Writes the beans contained in the given collection.
-     * 
-     * @param beans
-     *            The beans
-     */
-    public void write(Collection beans) throws DataWriterException {
-        write(beans.iterator());
-    }
+  /**
+   * Writes the beans contained in the given collection.
+   * 
+   * @param beans
+   *          The beans
+   */
+  public void write(Collection beans) throws DataWriterException {
+    write(beans.iterator());
+  }
 }
--- a/src/org/apache/ddlutils/model/Database.java	Thu Mar 26 17:00:07 2009 +0000
+++ b/src/org/apache/ddlutils/model/Database.java	Thu Apr 02 10:38:41 2009 +0000
@@ -40,1657 +40,1554 @@
 import org.openbravo.service.dataset.DataSetService;
 
 /**
- * Represents the database model, ie. the tables in the database. It also
- * contains the corresponding dyna classes for creating dyna beans for the
- * objects stored in the tables.
+ * Represents the database model, ie. the tables in the database. It also contains the corresponding
+ * dyna classes for creating dyna beans for the objects stored in the tables.
  * 
  * @version $Revision: 504014 $
  */
 public class Database implements Serializable, Cloneable {
-    /** Unique ID for serialization purposes. */
-    private static final long serialVersionUID = -3160443396757573868L;
+  /** Unique ID for serialization purposes. */
+  private static final long serialVersionUID = -3160443396757573868L;
 
-    /** The name of the database model. */
-    private String _name;
-    /** The method for generating primary keys (currently ignored). */
-    private String _idMethod;
-    /** The version of the model. */
-    private String _version;
-    /** The tables. */
-    private ArrayList _tables = new ArrayList();
-    /** The sequences. */
-    private ArrayList _sequences = new ArrayList();
-    /** The views. */
-    private ArrayList _views = new ArrayList();
-    /** The functions. */
-    private ArrayList _functions = new ArrayList();
-    /** The functions. */
-    private ArrayList _triggers = new ArrayList();
-    /** The dyna class cache for this model. */
-    private transient DynaClassCache _dynaClassCache = null;
+  /** The name of the database model. */
+  private String _name;
+  /** The method for generating primary keys (currently ignored). */
+  private String _idMethod;
+  /** The version of the model. */
+  private String _version;
+  /** The tables. */
+  private ArrayList _tables = new ArrayList();
+  /** The sequences. */
+  private ArrayList _sequences = new ArrayList();
+  /** The views. */
+  private ArrayList _views = new ArrayList();
+  /** The functions. */
+  private ArrayList _functions = new ArrayList();
+  /** The functions. */
+  private ArrayList _triggers = new ArrayList();
+  /** The dyna class cache for this model. */
+  private transient DynaClassCache _dynaClassCache = null;
 
-    private ArrayList _modifiedTables = new ArrayList();
+  private ArrayList _modifiedTables = new ArrayList();
 
-    /**
-     * Adds all tables from the other database to this database. Note that the
-     * other database is not changed.
-     * 
-     * @param otherDb
-     *            The other database model
-     */
-    public void mergeWith(Database otherDb) throws ModelException {
-        for (Iterator it = otherDb._tables.iterator(); it.hasNext();) {
-            Table table = (Table) it.next();
+  /**
+   * Adds all tables from the other database to this database. Note that the other database is not
+   * changed.
+   * 
+   * @param otherDb
+   *          The other database model
+   */
+  public void mergeWith(Database otherDb) throws ModelException {
+    for (Iterator it = otherDb._tables.iterator(); it.hasNext();) {
+      Table table = (Table) it.next();
 
-            if (findTable(table.getName()) != null) {
-                // We will merge the table with the corresponding one in the
-                // target model
-                findTable(table.getName()).mergeWith(table);
-            } else {
-                try {
-                    addTable((Table) table.clone());
-                } catch (CloneNotSupportedException ex) {
-                    // won't happen
-                }
-            }
+      if (findTable(table.getName()) != null) {
+        // We will merge the table with the corresponding one in the
+        // target model
+        findTable(table.getName()).mergeWith(table);
+      } else {
+        try {
+          addTable((Table) table.clone());
+        } catch (CloneNotSupportedException ex) {
+          // won't happen
+        }
+      }
+    }
+
+    for (Iterator it = otherDb._sequences.iterator(); it.hasNext();) {
+      Sequence sequence = (Sequence) it.next();
+
+      if (findSequence(sequence.getName()) != null) {
+        // TODO: It might make more sense to log a warning and overwrite
+        // the sequence (or merge them) ?
+        throw new ModelException("Cannot merge the models because sequence " + sequence.getName()
+            + " already defined in this model");
+      }
+      try {
+        addSequence((Sequence) sequence.clone());
+      } catch (CloneNotSupportedException ex) {
+        // won't happen
+      }
+    }
+
+    for (Iterator it = otherDb._views.iterator(); it.hasNext();) {
+      View view = (View) it.next();
+
+      if (findView(view.getName()) != null) {
+        // TODO: It might make more sense to log a warning and overwrite
+        // the view (or merge them) ?
+        throw new ModelException("Cannot merge the models because view " + view.getName()
+            + " already defined in this model");
+      }
+      try {
+        addView((View) view.clone());
+      } catch (CloneNotSupportedException ex) {
+        // won't happen
+      }
+    }
+
+    for (Iterator it = otherDb._functions.iterator(); it.hasNext();) {
+      Function function = (Function) it.next();
+      Function oldFunction = findFunction(function.getName());
+      if (oldFunction != null) {
+        // TODO: It might make more sense to log a warning and overwrite
+        // the function (or merge them) ?
+        /*
+         * throw new ModelException( "Cannot merge the models because function " +
+         * function.getName() + " already defined in this model");
+         */
+        this.removeFunction(oldFunction);
+      }
+      try {
+        addFunction((Function) function.clone());
+      } catch (CloneNotSupportedException ex) {
+        // won't happen
+      }
+    }
+
+    for (Iterator it = otherDb._triggers.iterator(); it.hasNext();) {
+      Trigger trigger = (Trigger) it.next();
+      Trigger oldTrigger = findTrigger(trigger.getName());
+      if (oldTrigger != null) {
+        // TODO: It might make more sense to log a warning and overwrite
+        // the trigger (or merge them) ?
+        /*
+         * throw new ModelException( "Cannot merge the models because trigger " + trigger.getName()
+         * + " already defined in this model");
+         */
+        this.removeTrigger(oldTrigger);
+      }
+      try {
+        addTrigger((Trigger) trigger.clone());
+      } catch (CloneNotSupportedException ex) {
+        // won't happen
+      }
+    }
+
+    for (Iterator it = otherDb._modifiedTables.iterator(); it.hasNext();) {
+      Table mtable = (Table) it.next();
+      this._modifiedTables.add(mtable);
+    }
+
+    Vector<Table> toRemove = new Vector<Table>();
+    for (Iterator it = this._modifiedTables.iterator(); it.hasNext();) {
+      Table mtable = (Table) it.next();
+      Table table = findTable(mtable.getName());
+      if (table != null) {
+        toRemove.add(mtable);
+        table.mergeWith(mtable);
+      }
+    }
+
+    for (Table table : toRemove)
+      _modifiedTables.remove(table);
+  }
+
+  /**
+   * Returns the name of this database model.
+   * 
+   * @return The name
+   */
+  public String getName() {
+    return _name;
+  }
+
+  /**
+   * Sets the name of this database model.
+   * 
+   * @param name
+   *          The name
+   */
+  public void setName(String name) {
+    _name = name;
+  }
+
+  /**
+   * Returns the version of this database model.
+   * 
+   * @return The version
+   */
+  public String getVersion() {
+    return _version;
+  }
+
+  /**
+   * Sets the version of this database model.
+   * 
+   * @param version
+   *          The version
+   */
+  public void setVersion(String version) {
+    _version = version;
+  }
+
+  /**
+   * Returns the method for generating primary key values.
+   * 
+   * @return The method
+   */
+  public String getIdMethod() {
+    return _idMethod;
+  }
+
+  /**
+   * Sets the method for generating primary key values. Note that this value is ignored by DdlUtils
+   * and only for compatibility with Torque.
+   * 
+   * @param idMethod
+   *          The method
+   */
+  public void setIdMethod(String idMethod) {
+    _idMethod = idMethod;
+  }
+
+  /**
+   * Returns the number of tables in this model.
+   * 
+   * @return The number of tables
+   */
+  public int getTableCount() {
+    return _tables.size();
+  }
+
+  public int getModifiedTableCount() {
+    return _modifiedTables.size();
+  }
+
+  /**
+   * Returns the tables in this model.
+   * 
+   * @return The tables
+   */
+  public Table[] getTables() {
+    return (Table[]) _tables.toArray(new Table[_tables.size()]);
+  }
+
+  public Table[] getModifiedTables() {
+    return (Table[]) _modifiedTables.toArray(new Table[_modifiedTables.size()]);
+  }
+
+  /**
+   * Returns the table at the specified position.
+   * 
+   * @param idx
+   *          The index of the table
+   * @return The table
+   */
+  public Table getTable(int idx) {
+    return (Table) _tables.get(idx);
+  }
+
+  public Table getModifiedTable(int idx) {
+    return (Table) _modifiedTables.get(idx);
+  }
+
+  /**
+   * Adds a table.
+   * 
+   * @param table
+   *          The table to add
+   */
+  public void addTable(Table table) {
+    if (table != null) {
+      _tables.add(table);
+    }
+  }
+
+  public void addModifiedTable(Table table) {
+    if (table != null) {
+      _modifiedTables.add(table);
+    }
+  }
+
+  /**
+   * Adds a table at the specified position.
+   * 
+   * @param idx
+   *          The index where to insert the table
+   * @param table
+   *          The table to add
+   */
+  public void addTable(int idx, Table table) {
+    if (table != null) {
+      _tables.add(idx, table);
+    }
+  }
+
+  public void addModifiedTable(int idx, Table table) {
+    if (table != null) {
+      _modifiedTables.add(idx, table);
+    }
+  }
+
+  /**
+   * Adds the given tables.
+   * 
+   * @param tables
+   *          The tables to add
+   */
+  public void addTables(Collection tables) {
+    for (Iterator it = tables.iterator(); it.hasNext();) {
+      addTable((Table) it.next());
+    }
+  }
+
+  public void addModifiedTables(Collection tables) {
+    for (Iterator it = tables.iterator(); it.hasNext();) {
+      addModifiedTable((Table) it.next());
+    }
+  }
+
+  /**
+   * Removes the given table.
+   * 
+   * @param table
+   *          The table to remove
+   */
+  public void removeTable(Table table) {
+    if (table != null) {
+      _tables.remove(table);
+    }
+  }
+
+  public void removeModifiedTable(Table table) {
+    if (table != null) {
+      _modifiedTables.remove(table);
+    }
+  }
+
+  /**
+   * Removes the indicated table.
+   * 
+   * @param idx
+   *          The index of the table to remove
+   */
+  public void removeTable(int idx) {
+    _tables.remove(idx);
+  }
+
+  public void removeModifiedTable(int idx) {
+    _modifiedTables.remove(idx);
+  }
+
+  /**
+   * Returns the number of sequences in this model.
+   * 
+   * @return The number of sequences
+   */
+  public int getSequenceCount() {
+    return _sequences.size();
+  }
+
+  /**
+   * Returns the sequences in this model.
+   * 
+   * @return The sequences
+   */
+  public Sequence[] getSequences() {
+    return (Sequence[]) _sequences.toArray(new Sequence[_sequences.size()]);
+  }
+
+  /**
+   * Returns the sequence at the specified position.
+   * 
+   * @param idx
+   *          The index of the sequence
+   * @return The sequence
+   */
+  public Sequence getSequence(int idx) {
+    return (Sequence) _sequences.get(idx);
+  }
+
+  /**
+   * Adds a sequence.
+   * 
+   * @param sequence
+   *          The sequence to add
+   */
+  public void addSequence(Sequence sequence) {
+    if (sequence != null) {
+      _sequences.add(sequence);
+    }
+  }
+
+  /**
+   * Adds a sequence at the specified position.
+   * 
+   * @param idx
+   *          The index where to insert the sequence
+   * @param sequence
+   *          The sequence to add
+   */
+  public void addSequence(int idx, Sequence sequence) {
+    if (sequence != null) {
+      _sequences.add(idx, sequence);
+    }
+  }
+
+  /**
+   * Adds the given sequences.
+   * 
+   * @param sequences
+   *          The sequences to add
+   */
+  public void addSequences(Collection sequences) {
+    for (Iterator it = sequences.iterator(); it.hasNext();) {
+      addSequence((Sequence) it.next());
+    }
+  }
+
+  /**
+   * Removes the given sequence.
+   * 
+   * @param sequence
+   *          The sequence to remove
+   */
+  public void removeSequence(Sequence sequence) {
+    if (sequence != null) {
+      _sequences.remove(sequence);
+    }
+  }
+
+  /**
+   * Removes the indicated sequence.
+   * 
+   * @param idx
+   *          The index of the sequence to remove
+   */
+  public void removeSequence(int idx) {
+    _sequences.remove(idx);
+  }
+
+  /**
+   * Returns the number of views in this model.
+   * 
+   * @return The number of views
+   */
+  public int getViewCount() {
+    return _views.size();
+  }
+
+  /**
+   * Returns the views in this model.
+   * 
+   * @return The views
+   */
+  public View[] getViews() {
+    return (View[]) _views.toArray(new View[_views.size()]);
+  }
+
+  /**
+   * Returns the view at the specified position.
+   * 
+   * @param idx
+   *          The index of the view
+   * @return The view
+   */
+  public View getView(int idx) {
+    return (View) _views.get(idx);
+  }
+
+  /**
+   * Adds a view.
+   * 
+   * @param view
+   *          The view to add
+   */
+  public void addView(View view) {
+    if (view != null) {
+      _views.add(view);
+    }
+  }
+
+  /**
+   * Adds a view at the specified position.
+   * 
+   * @param idx
+   *          The index where to insert the view
+   * @param view
+   *          The view to add
+   */
+  public void addView(int idx, View view) {
+    if (view != null) {
+      _views.add(idx, view);
+    }
+  }
+
+  /**
+   * Adds the given views.
+   * 
+   * @param views
+   *          The views to add
+   */
+  public void addViews(Collection views) {
+    for (Iterator it = views.iterator(); it.hasNext();) {
+      addView((View) it.next());
+    }
+  }
+
+  /**
+   * Removes the given view.
+   * 
+   * @param view
+   *          The view to remove
+   */
+  public void removeView(View view) {
+    if (view != null) {
+      _views.remove(view);
+    }
+  }
+
+  /**
+   * Removes the indicated view.
+   * 
+   * @param idx
+   *          The index of the view to remove
+   */
+  public void removeView(int idx) {
+    _views.remove(idx);
+  }
+
+  /**
+   * Returns the number of functions in this model.
+   * 
+   * @return The number of functions
+   */
+  public int getFunctionCount() {
+    return _functions.size();
+  }
+
+  /**
+   * Returns the functions in this model.
+   * 
+   * @return The functions
+   */
+  public Function[] getFunctions() {
+    return (Function[]) _functions.toArray(new Function[_functions.size()]);
+  }
+
+  /**
+   * Returns the function at the specified position.
+   * 
+   * @param idx
+   *          The index of the function
+   * @return The function
+   */
+  public Function getFunction(int idx) {
+    return (Function) _functions.get(idx);
+  }
+
+  /**
+   * Adds a function.
+   * 
+   * @param function
+   *          The function to add
+   */
+  public void addFunction(Function function) {
+    if (function != null) {
+      _functions.add(function);
+    }
+  }
+
+  /**
+   * Adds a function at the specified position.
+   * 
+   * @param idx
+   *          The index where to insert the function
+   * @param function
+   *          The function to add
+   */
+  public void addFunction(int idx, Function function) {
+    if (function != null) {
+      _functions.add(idx, function);
+    }
+  }
+
+  /**
+   * Adds the given functions.
+   * 
+   * @param functions
+   *          The functions to add
+   */
+  public void addFunctions(Collection functions) {
+    for (Iterator it = functions.iterator(); it.hasNext();) {
+      addFunction((Function) it.next());
+    }
+  }
+
+  /**
+   * Removes the given function.
+   * 
+   * @param function
+   *          The function to remove
+   */
+  public void removeFunction(Function function) {
+    if (function != null) {
+      _functions.remove(function);
+    }
+  }
+
+  /**
+   * Removes the indicated function.
+   * 
+   * @param idx
+   *          The index of the function to remove
+   */
+  public void removeFunction(int idx) {
+    _functions.remove(idx);
+  }
+
+  /**
+   * Returns the number of triggers in this model.
+   * 
+   * @return The number of triggers
+   */
+  public int getTriggerCount() {
+    return _triggers.size();
+  }
+
+  /**
+   * Returns the triggers in this model.
+   * 
+   * @return The triggers
+   */
+  public Trigger[] getTriggers() {
+    return (Trigger[]) _triggers.toArray(new Trigger[_triggers.size()]);
+  }
+
+  /**
+   * Returns the trigger at the specified position.
+   * 
+   * @param idx
+   *          The index of the trigger
+   * @return The trigger
+   */
+  public Trigger getTrigger(int idx) {
+    return (Trigger) _triggers.get(idx);
+  }
+
+  /**
+   * Adds a trigger.
+   * 
+   * @param trigger
+   *          The trigger to add
+   */
+  public void addTrigger(Trigger trigger) {
+    if (trigger != null) {
+      _triggers.add(trigger);
+    }
+  }
+
+  /**
+   * Adds a trigger at the specified position.
+   * 
+   * @param idx
+   *          The index where to insert the trigger
+   * @param trigger
+   *          The trigger to add
+   */
+  public void addTrigger(int idx, Trigger trigger) {
+    if (trigger != null) {
+      _triggers.add(idx, trigger);
+    }
+  }
+
+  /**
+   * Adds the given triggers.
+   * 
+   * @param triggers
+   *          The triggers to add
+   */
+  public void addTriggers(Collection triggers) {
+    for (Iterator it = triggers.iterator(); it.hasNext();) {
+      addTrigger((Trigger) it.next());
+    }
+  }
+
+  /**
+   * Removes the given trigger.
+   * 
+   * @param trigger
+   *          The trigger to remove
+   */
+  public void removeTrigger(Trigger trigger) {
+    if (trigger != null) {
+      _triggers.remove(trigger);
+    }
+  }
+
+  /**
+   * Removes the indicated trigger.
+   * 
+   * @param idx
+   *          The index of the trigger to remove
+   */
+  public void removeTrigger(int idx) {
+    _triggers.remove(idx);
+  }
+
+  // Helper methods
+
+  /**
+   * Initializes the model by establishing the relationships between elements in this model encoded
+   * eg. in foreign keys etc. Also checks that the model elements are valid (table and columns have
+   * a name, foreign keys rference existing tables etc.)
+   */
+  public void initialize() throws ModelException {
+    // we have to setup
+    // * target tables in foreign keys
+    // * columns in foreign key references
+    // * columns in indices
+    // * columns in uniques
+    HashSet namesOfProcessedTables = new HashSet();
+    HashSet namesOfProcessedColumns = new HashSet();
+    HashSet namesOfProcessedFks = new HashSet();
+    HashSet namesOfProcessedIndices = new HashSet();
+    HashSet namesOfProcessedChecks = new HashSet();
+    int tableIdx = 0;
+
+    // if ((getName() == null) || (getName().length() == 0))
+    // {
+    // throw new ModelException("The database model has no name");
+    // }
+
+    for (Iterator tableIt = _tables.iterator(); tableIt.hasNext(); tableIdx++) {
+      Table curTable = (Table) tableIt.next();
+
+      if ((curTable.getName() == null) || (curTable.getName().length() == 0)) {
+        throw new ModelException("The table nr. " + tableIdx + " has no name");
+      }
+      if (namesOfProcessedTables.contains(curTable.getName())) {
+        throw new ModelException("There are multiple tables with the name " + curTable.getName());
+      }
+      namesOfProcessedTables.add(curTable.getName());
+
+      namesOfProcessedColumns.clear();
+      namesOfProcessedFks.clear();
+      namesOfProcessedIndices.clear();
+      namesOfProcessedChecks.clear();
+
+      for (int idx = 0; idx < curTable.getColumnCount(); idx++) {
+        Column column = curTable.getColumn(idx);
+
+        if ((column.getName() == null) || (column.getName().length() == 0)) {
+          throw new ModelException("The column nr. " + idx + " in table " + curTable.getName()
+              + " has no name");
+        }
+        if (namesOfProcessedColumns.contains(column.getName())) {
+          throw new ModelException("There are multiple column with the name " + column.getName()
+              + " in the table " + curTable.getName());
+        }
+        namesOfProcessedColumns.add(column.getName());
+
+        if ((column.getType() == null) || (column.getType().length() == 0)) {
+          throw new ModelException("The column nr. " + idx + " in table " + curTable.getName()
+              + " has no type");
+        }
+        if ((column.getTypeCode() == Types.OTHER) && !"OTHER".equalsIgnoreCase(column.getType())) {
+          throw new ModelException("The column nr. " + idx + " in table " + curTable.getName()
+              + " has an unknown type " + column.getType());
+        }
+        namesOfProcessedColumns.add(column.getName());
+      }
+
+      for (int idx = 0; idx < curTable.getForeignKeyCount(); idx++) {
+        ForeignKey fk = curTable.getForeignKey(idx);
+        String fkName = (fk.getName() == null ? "" : fk.getName());
+        String fkDesc = (fkName.length() == 0 ? "nr. " + idx : fkName);
+
+        if (fkName.length() > 0) {
+          if (namesOfProcessedFks.contains(fkName)) {
+            throw new ModelException("There are multiple foreign keys in table "
+                + curTable.getName() + " with the name " + fkName);
+          }
+          namesOfProcessedFks.add(fkName);
         }
 
-        for (Iterator it = otherDb._sequences.iterator(); it.hasNext();) {
-            Sequence sequence = (Sequence) it.next();
+        if (fk.getForeignTable() == null) {
+          Table targetTable = findTable(fk.getForeignTableName(), true);
 
-            if (findSequence(sequence.getName()) != null) {
-                // TODO: It might make more sense to log a warning and overwrite
-                // the sequence (or merge them) ?
-                throw new ModelException(
-                        "Cannot merge the models because sequence "
-                                + sequence.getName()
-                                + " already defined in this model");
+          if (targetTable == null) {
+            throw new ModelException("The foreignkey " + fkDesc + " in table " + curTable.getName()
+                + " references the undefined table " + fk.getForeignTableName());
+          } else {
+            fk.setForeignTable(targetTable);
+          }
+        }
+        for (int refIdx = 0; refIdx < fk.getReferenceCount(); refIdx++) {
+          Reference ref = fk.getReference(refIdx);
+
+          if (ref.getLocalColumn() == null) {
+            Column localColumn = curTable.findColumn(ref.getLocalColumnName(), true);
+
+            if (localColumn == null) {
+              throw new ModelException("The foreignkey " + fkDesc + " in table "
+                  + curTable.getName() + " references the undefined local column "
+                  + ref.getLocalColumnName());
+            } else {
+              ref.setLocalColumn(localColumn);
             }
-            try {
-                addSequence((Sequence) sequence.clone());
-            } catch (CloneNotSupportedException ex) {
-                // won't happen
+          }
+          if (ref.getForeignColumn() == null) {
+            Column foreignColumn = fk.getForeignTable()
+                .findColumn(ref.getForeignColumnName(), true);
+
+            if (foreignColumn == null) {
+              throw new ModelException("The foreignkey " + fkDesc + " in table "
+                  + curTable.getName() + " references the undefined local column "
+                  + ref.getForeignColumnName() + " in table " + fk.getForeignTable().getName());
+            } else {
+              ref.setForeignColumn(foreignColumn);
             }
+          }
+        }
+      }
+
+      for (int idx = 0; idx < curTable.getIndexCount(); idx++) {
+        Index index = curTable.getIndex(idx);
+        String indexName = (index.getName() == null ? "" : index.getName());
+        String indexDesc = (indexName.length() == 0 ? "nr. " + idx : indexName);
+
+        if (indexName.length() > 0) {
+          if (namesOfProcessedIndices.contains(indexName)) {
+            throw new ModelException("There are multiple indices in table " + curTable.getName()
+                + " with the name " + indexName);
+          }
+          namesOfProcessedIndices.add(indexName);
         }
 
-        for (Iterator it = otherDb._views.iterator(); it.hasNext();) {
-            View view = (View) it.next();
+        for (int indexColumnIdx = 0; indexColumnIdx < index.getColumnCount(); indexColumnIdx++) {
+          IndexColumn indexColumn = index.getColumn(indexColumnIdx);
+          Column column = curTable.findColumn(indexColumn.getName(), true);
 
-            if (findView(view.getName()) != null) {
-                // TODO: It might make more sense to log a warning and overwrite
-                // the view (or merge them) ?
-                throw new ModelException(
-                        "Cannot merge the models because view "
-                                + view.getName()
-                                + " already defined in this model");
-            }
-            try {
-                addView((View) view.clone());
-            } catch (CloneNotSupportedException ex) {
-                // won't happen
-            }
+          if (column == null) {
+            throw new ModelException("The index " + indexDesc + " in table " + curTable.getName()
+                + " references the undefined column " + indexColumn.getName());
+          } else {
+            indexColumn.setColumn(column);
+          }
+        }
+      }
+
+      for (int idx = 0; idx < curTable.getUniqueCount(); idx++) {
+        Unique unique = curTable.getUnique(idx);
+        String uniqueName = (unique.getName() == null ? "" : unique.getName());
+        String uniqueDesc = (uniqueName.length() == 0 ? "nr. " + idx : uniqueName);
+
+        if (uniqueName.length() > 0) {
+          if (namesOfProcessedIndices.contains(uniqueName)) {
+            throw new ModelException("There are multiple uniques or indices in table "
+                + curTable.getName() + " with the name " + uniqueName);
+          }
+          namesOfProcessedIndices.add(uniqueName);
         }
 
-        for (Iterator it = otherDb._functions.iterator(); it.hasNext();) {
-            Function function = (Function) it.next();
-            Function oldFunction = findFunction(function.getName());
-            if (oldFunction != null) {
-                // TODO: It might make more sense to log a warning and overwrite
-                // the function (or merge them) ?
-                /*
-                 * throw new ModelException(
-                 * "Cannot merge the models because function " +
-                 * function.getName() + " already defined in this model");
-                 */
-                this.removeFunction(oldFunction);
-            }
-            try {
-                addFunction((Function) function.clone());
-            } catch (CloneNotSupportedException ex) {
-                // won't happen
-            }
+        for (int uniqueColumnIdx = 0; uniqueColumnIdx < unique.getColumnCount(); uniqueColumnIdx++) {
+          IndexColumn uniqueColumn = unique.getColumn(uniqueColumnIdx);
+          Column column = curTable.findColumn(uniqueColumn.getName(), true);
+
+          if (column == null) {
+            throw new ModelException("The unique " + uniqueDesc + " in table " + curTable.getName()
+                + " references the undefined column " + uniqueColumn.getName());
+          } else {
+            uniqueColumn.setColumn(column);
+          }
         }
+      }
 
-        for (Iterator it = otherDb._triggers.iterator(); it.hasNext();) {
-            Trigger trigger = (Trigger) it.next();
-            Trigger oldTrigger = findTrigger(trigger.getName());
-            if (oldTrigger != null) {
-                // TODO: It might make more sense to log a warning and overwrite
-                // the trigger (or merge them) ?
-                /*
-                 * throw new ModelException(
-                 * "Cannot merge the models because trigger " +
-                 * trigger.getName() + " already defined in this model");
-                 */
-                this.removeTrigger(oldTrigger);
-            }
-            try {
-                addTrigger((Trigger) trigger.clone());
-            } catch (CloneNotSupportedException ex) {
-                // won't happen
-            }
+      for (int idx = 0; idx < curTable.getCheckCount(); idx++) {
+
+        Check check = curTable.getCheck(idx);
+
+        if ((check.getName() == null) || (check.getName().length() == 0)) {
+          throw new ModelException("The check nr. " + idx + " in table " + curTable.getName()
+              + " has no name");
         }
+        if (namesOfProcessedChecks.contains(check.getName())) {
+          throw new ModelException("There are multiple checks with the name " + check.getName()
+              + " in the table " + curTable.getName());
+        }
+        namesOfProcessedChecks.add(check.getName());
 
-        for (Iterator it = otherDb._modifiedTables.iterator(); it.hasNext();) {
-            Table mtable = (Table) it.next();
-            this._modifiedTables.add(mtable);
+        if ((check.getCondition() == null) || (check.getCondition().length() == 0)) {
+          throw new ModelException("The check " + check.getName() + " in table "
+              + curTable.getName() + " has no condition defined");
         }
-
-        Vector<Table> toRemove = new Vector<Table>();
-        for (Iterator it = this._modifiedTables.iterator(); it.hasNext();) {
-            Table mtable = (Table) it.next();
-            Table table = findTable(mtable.getName());
-            if (table != null) {
-                toRemove.add(mtable);
-                table.mergeWith(mtable);
-            }
-        }
-
-        for (Table table : toRemove)
-            _modifiedTables.remove(table);
+      }
     }
 
-    /**
-     * Returns the name of this database model.
-     * 
-     * @return The name
-     */
-    public String getName() {
-        return _name;
+    HashSet namesOfProcessedSequences = new HashSet();
+    int sequenceIdx = 0;
+
+    for (Iterator sequenceIt = _sequences.iterator(); sequenceIt.hasNext();) {
+      Sequence curSequence = (Sequence) sequenceIt.next();
+
+      if ((curSequence.getName() == null) || (curSequence.getName().length() == 0)) {
+        throw new ModelException("The sequence nr. " + sequenceIdx + "has no name");
+      }
+      if (namesOfProcessedSequences.contains(curSequence.getName())) {
+        throw new ModelException("There are multiple sequences with the name "
+            + curSequence.getName());
+      }
+      namesOfProcessedSequences.add(curSequence.getName());
     }
 
-    /**
-     * Sets the name of this database model.
-     * 
-     * @param name
-     *            The name
-     */
-    public void setName(String name) {
-        _name = name;
+    HashSet namesOfProcessedViews = new HashSet();
+    int viewIdx = 0;
+
+    for (Iterator viewIt = _views.iterator(); viewIt.hasNext();) {
+      View curView = (View) viewIt.next();
+
+      if ((curView.getName() == null) || (curView.getName().length() == 0)) {
+        throw new ModelException("The view nr. " + viewIdx + "has no name");
+      }
+      if (namesOfProcessedViews.contains(curView.getName())) {
+        throw new ModelException("There are multiple views with the name " + curView.getName());
+      }
+      namesOfProcessedViews.add(curView.getName());
+
+      if ((curView.getStatement() == null) || (curView.getStatement().length() == 0)) {
+        throw new ModelException("The view " + curView.getName() + " has no statement defined");
+      }
     }
 
-    /**
-     * Returns the version of this database model.
-     * 
-     * @return The version
-     */
-    public String getVersion() {
-        return _version;
+    HashSet namesOfProcessedFunctions = new HashSet();
+    HashSet namesOfProcessedParameters = new HashSet();
+    int functionIdx = 0;
+
+    for (Iterator functionIt = _functions.iterator(); functionIt.hasNext();) {
+      Function curFunction = (Function) functionIt.next();
+
+      if ((curFunction.getName() == null) || (curFunction.getName().length() == 0)) {
+        throw new ModelException("The function nr. " + functionIdx + "has no name");
+      }
+      if (namesOfProcessedFunctions.contains(curFunction.getNotation())) {
+        throw new ModelException("There are multiple functions with the same notation "
+            + curFunction.getNotation());
+      }
+      namesOfProcessedFunctions.add(curFunction.getNotation());
+
+      if ((curFunction.getBody() == null) || (curFunction.getBody().length() == 0)) {
+        throw new ModelException("The function " + curFunction.getName() + " has no body defined");
+      }
+
+      namesOfProcessedParameters.clear();
+      for (int idx = 0; idx < curFunction.getParameterCount(); idx++) {
+        Parameter curParameter = curFunction.getParameter(idx);
+
+        if ((curParameter.getName() != null) && (curParameter.getName().length() == 0)) {
+          if (namesOfProcessedParameters.contains(curParameter.getName())) {
+            throw new ModelException("There are multiple parameters with the name "
+                + curParameter.getName() + " in the function " + curFunction.getName());
+          }
+        }
+      }
     }
 
-    /**
-     * Sets the version of this database model.
-     * 
-     * @param version
-     *            The version
-     */
-    public void setVersion(String version) {
-        _version = version;
+    HashSet namesOfProcessedTriggers = new HashSet();
+    int triggerIdx = 0;
+
+    for (Iterator triggerIt = _triggers.iterator(); triggerIt.hasNext();) {
+      Trigger curTrigger = (Trigger) triggerIt.next();
+
+      if ((curTrigger.getName() == null) || (curTrigger.getName().length() == 0)) {
+        throw new ModelException("The trigger nr. " + triggerIdx + "has no name");
+      }
+      if (namesOfProcessedTriggers.contains(curTrigger.getName())) {
+        throw new ModelException("There are multiple triggers with the name "
+            + curTrigger.getName());
+      }
+      namesOfProcessedTriggers.add(curTrigger.getName());
+
+      if (!namesOfProcessedTables.contains(curTrigger.getTable())) {
+        throw new ModelException("The trigger " + curTrigger.getName()
+            + " references the undefined table " + curTrigger.getTable());
+      }
+
+      if ((curTrigger.getBody() == null) || (curTrigger.getBody().length() == 0)) {
+        throw new ModelException("The trigger " + curTrigger.getName() + " has no body defined");
+      }
+    }
+  }
+
+  /**
+   * Finds the table with the specified name, using case insensitive matching. Note that this method
+   * is not called getTable to avoid introspection problems.
+   * 
+   * @param name
+   *          The name of the table to find
+   * @return The table or <code>null</code> if there is no such table
+   */
+  public Table findTable(String name) {
+    return findTable(name, false);
+  }
+
+  /**
+   * Finds the table with the specified name, using case insensitive matching. Note that this method
+   * is not called getTable) to avoid introspection problems.
+   * 
+   * @param name
+   *          The name of the table to find
+   * @param caseSensitive
+   *          Whether case matters for the names
+   * @return The table or <code>null</code> if there is no such table
+   */
+  public Table findTable(String name, boolean caseSensitive) {
+    for (Iterator iter = _tables.iterator(); iter.hasNext();) {
+      Table table = (Table) iter.next();
+
+      if (caseSensitive) {
+        if (table.getName().equals(name)) {
+          return table;
+        }
+      } else {
+        if (table.getName().equalsIgnoreCase(name)) {
+          return table;
+        }
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Finds the sequence with the specified name, using case insensitive matching. Note that this
+   * method is not called getSequence to avoid introspection problems.
+   * 
+   * @param name
+   *          The name of the sequence to find
+   * @return The sequence or <code>null</code> if there is no such sequence
+   */
+  public Sequence findSequence(String name) {
+    return findSequence(name, false);
+  }
+
+  /**
+   * Finds the sequence with the specified name, using case insensitive matching. Note that this
+   * method is not called getSequence) to avoid introspection problems.
+   * 
+   * @param name
+   *          The name of the sequence to find
+   * @param caseSensitive
+   *          Whether case matters for the names
+   * @return The sequence or <code>null</code> if there is no such sequence
+   */
+  public Sequence findSequence(String name, boolean caseSensitive) {
+    for (Iterator iter = _sequences.iterator(); iter.hasNext();) {
+      Sequence sequence = (Sequence) iter.next();
+
+      if (caseSensitive) {
+        if (sequence.getName().equals(name)) {
+          return sequence;
+        }
+      } else {
+        if (sequence.getName().equalsIgnoreCase(name)) {
+          return sequence;
+        }
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Finds the view with the specified name, using case insensitive matching. Note that this method
+   * is not called getView to avoid introspection problems.
+   * 
+   * @param name
+   *          The name of the view to find
+   * @return The view or <code>null</code> if there is no such view
+   */
+  public View findView(String name) {
+    return findView(name, false);
+  }
+
+  /**
+   * Finds the view with the specified name, using case insensitive matching. Note that this method
+   * is not called getView) to avoid introspection problems.
+   * 
+   * @param name
+   *          The name of the view to find
+   * @param caseSensitive
+   *          Whether case matters for the names
+   * @return The view or <code>null</code> if there is no such view
+   */
+  public View findView(String name, boolean caseSensitive) {
+    for (Iterator iter = _views.iterator(); iter.hasNext();) {
+      View view = (View) iter.next();
+
+      if (caseSensitive) {
+        if (view.getName().equals(name)) {
+          return view;
+        }
+      } else {
+        if (view.getName().equalsIgnoreCase(name)) {
+          return view;
+        }
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Finds the function with the specified name, using case insensitive matching. Note that this
+   * method is not called getFunction to avoid introspection problems.
+   * 
+   * @param name
+   *          The name of the function to find
+   * @return The function or <code>null</code> if there is no such function
+   */
+  public Function findFunction(String name) {
+    return findFunction(name, false);
+  }
+
+  /**
+   * Finds the function with the specified name and parameters, using case sensitive matching.
+   * 
+   * @param name
+   *          The name of the function to find
+   * @param caseSensitive
+   *          Whether case matters for the names
+   * @return The function or <code>null</code> if there is no such function
+   */
+  public Function findFunctionWithParams(String name, Parameter[] params) {
+    return findFunctionWithParams(name, params, false);
+  }
+
+  /**
+   * Finds the function with the specified name, using case sensitive matching. Note that this
+   * method is not called getFunction) to avoid introspection problems.
+   * 
+   * @param name
+   *          The name of the function to find
+   * @param caseSensitive
+   *          Whether case matters for the names
+   * @return The function or <code>null</code> if there is no such function
+   */
+  public Function findFunction(String name, boolean caseSensitive) {
+    for (Iterator iter = _functions.iterator(); iter.hasNext();) {
+      Function function = (Function) iter.next();
+
+      if (caseSensitive) {
+        if (function.getName().equals(name)) {
+          return function;
+        }
+      } else {
+        if (function.getName().equalsIgnoreCase(name)) {
+          return function;
+        }
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Finds the function with the specified name and parameters, using case insensitive matching.
+   * 
+   * @param name
+   *          The name of the function to find
+   * @param params
+   *          The parameters of the function to find
+   * @param caseSensitive
+   *          Whether case matters for the names
+   * @return The function or <code>null</code> if there is no such function
+   */
+  public Function findFunctionWithParams(String name, Parameter[] params, boolean caseSensitive) {
+    for (Iterator iter = _functions.iterator(); iter.hasNext();) {
+      Function function = (Function) iter.next();
+
+      if (caseSensitive) {
+        if (function.getName().equals(name)) {
+          if (function.getParameterCount() == params.length) {
+            boolean eqPara = true;
+            int i = 0;
+            Parameter[] params2 = function.getParameters();
+            while (eqPara && i < params.length) {
+              if (!params[i].equals(params2[i]))
+                eqPara = false;
+              i++;
+            }
+            if (eqPara)
+              return function;
+          }
+        }
+      } else {
+        if (function.getName().equalsIgnoreCase(name)) {
+          Parameter[] params2 = function.getParameters();
+          if (params2.length == params.length) {
+            boolean eqPara = true;
+            int i = 0;
+            while (eqPara && i < params.length) {
+              int type1 = params[i].getTypeCode();
+              int type2 = params2[i].getTypeCode();
+
+              int typeCode2 = type1 == ExtTypes.NVARCHAR ? Types.VARCHAR : type1;
+              typeCode2 = typeCode2 == ExtTypes.NCHAR ? Types.CHAR : typeCode2;
+              int othertypeCode2 = type2 == ExtTypes.NVARCHAR ? Types.VARCHAR : type2;
+              othertypeCode2 = othertypeCode2 == ExtTypes.NCHAR ? Types.CHAR : othertypeCode2;
+              if (typeCode2 != othertypeCode2) {
+                eqPara = false;
+              }
+              i++;
+            }
+            if (eqPara) {
+              return function;
+            }
+          }
+        }
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Finds the trigger with the specified name, using case insensitive matching. Note that this
+   * method is not called getTrigger to avoid introspection problems.
+   * 
+   * @param name
+   *          The name of the trigger to find
+   * @return The trigger or <code>null</code> if there is no such trigger
+   */
+  public Trigger findTrigger(String name) {
+    return findTrigger(name, false);
+  }
+
+  /**
+   * Finds the trigger with the specified name, using case insensitive matching. Note that this
+   * method is not called getTrigger) to avoid introspection problems.
+   * 
+   * @param name
+   *          The name of the trigger to find
+   * @param caseSensitive
+   *          Whether case matters for the names
+   * @return The trigger or <code>null</code> if there is no such trigger
+   */
+  public Trigger findTrigger(String name, boolean caseSensitive) {
+    for (Iterator iter = _triggers.iterator(); iter.hasNext();) {
+      Trigger trigger = (Trigger) iter.next();
+
+      if (caseSensitive) {
+        if (trigger.getName().equals(name)) {
+          return trigger;
+        }
+      } else {
+        if (trigger.getName().equalsIgnoreCase(name)) {
+          return trigger;
+        }
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Returns the dyna class cache. If none is available yet, a new one will be created.
+   * 
+   * @return The dyna class cache
+   */
+  private DynaClassCache getDynaClassCache() {
+    if (_dynaClassCache == null) {
+      _dynaClassCache = new DynaClassCache();
+    }
+    return _dynaClassCache;
+  }
+
+  /**
+   * Resets the dyna class cache. This should be done for instance when a column has been added or
+   * removed to a table.
+   */
+  public void resetDynaClassCache() {
+    _dynaClassCache = null;
+  }
+
+  /**
+   * Returns the {@link org.apache.ddlutils.dynabean.SqlDynaClass} for the given table name. If the
+   * it does not exist yet, a new one will be created based on the Table definition.
+   * 
+   * @param tableName
+   *          The name of the table to create the bean for
+   * @return The <code>SqlDynaClass</code> for the indicated table or <code>null</code> if the model
+   *         contains no such table
+   */
+  public SqlDynaClass getDynaClassFor(String tableName) {
+    Table table = findTable(tableName);
+
+    return table != null ? getDynaClassCache().getDynaClass(table) : null;
+  }
+
+  /**
+   * Returns the {@link org.apache.ddlutils.dynabean.SqlDynaClass} for the given dyna bean.
+   * 
+   * @param bean
+   *          The dyna bean
+   * @return The <code>SqlDynaClass</code> for the given bean
+   */
+  public SqlDynaClass getDynaClassFor(DynaBean bean) {
+    return getDynaClassCache().getDynaClass(bean);
+  }
+
+  /**
+   * Creates a new dyna bean for the given table.
+   * 
+   * @param table
+   *          The table to create the bean for
+   * @return The new dyna bean
+   */
+  public DynaBean createDynaBeanFor(Table table) throws SqlDynaException {
+    return getDynaClassCache().createNewInstance(table);
+  }
+
+  /**
+   * Convenience method that combines {@link #createDynaBeanFor(Table)} and
+   * {@link #findTable(String, boolean)}.
+   * 
+   * @param tableName
+   *          The name of the table to create the bean for
+   * @param caseSensitive
+   *          Whether case matters for the names
+   * @return The new dyna bean
+   */
+  public DynaBean createDynaBeanFor(String tableName, boolean caseSensitive)
+      throws SqlDynaException {
+    return getDynaClassCache().createNewInstance(findTable(tableName, caseSensitive));
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public Object clone() throws CloneNotSupportedException {
+    Database result = (Database) super.clone();
+
+    result._name = _name;
+    result._idMethod = _idMethod;
+    result._version = _version;
+    result._tables = new ArrayList();
+    result._views = new ArrayList();
+    result._functions = new ArrayList();
+    result._triggers = new ArrayList();
+    result._sequences = new ArrayList();
+    result._modifiedTables = new ArrayList();
+    Iterator it = _tables.iterator();
+    while (it.hasNext())
+      result._tables.add(((Table) it.next()).clone());
+    it = _views.iterator();
+    while (it.hasNext())
+      result._views.add(((View) it.next()).clone());
+    it = _functions.iterator();
+    while (it.hasNext())
+      result._functions.add(((Function) it.next()).clone());
+    it = _triggers.iterator();
+    while (it.hasNext())
+      result._triggers.add(((Trigger) it.next()).clone());
+    it = _sequences.iterator();
+    while (it.hasNext())
+      result._sequences.add(((Sequence) it.next()).clone());
+    it = _modifiedTables.iterator();
+    while (it.hasNext())
+      result._modifiedTables.add(((Table) it.next()).clone());
+
+    return result;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public boolean equals(Object obj) {
+    if (obj instanceof Database) {
+      Database other = (Database) obj;
+
+      // Note that this compares case sensitive
+      return new EqualsBuilder().append(_name, other._name).append(_tables, other._tables).append(
+          _views, other._views).append(_functions, other._functions).append(_triggers,
+          other._triggers).isEquals();
+    } else {
+      return false;
+    }
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public int hashCode() {
+    return new HashCodeBuilder(17, 37).append(_name).append(_tables).append(_views).append(
+        _functions).append(_triggers).toHashCode();
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public String toString() {
+    StringBuffer result = new StringBuffer();
+
+    result.append("Database [name=");
+    result.append(getName());
+    result.append("; ");
+    result.append(getTableCount());
+    result.append(" tables");
+    result.append("; ");
+    result.append(getSequenceCount());
+    result.append(" sequences");
+    result.append("; ");
+    result.append(getViewCount());
+    result.append(" views");
+    result.append("; ");
+    result.append(getFunctionCount());
+    result.append(" functions");
+    result.append("; ");
+    result.append(getTriggerCount());
+    result.append(" triggers]");
+
+    return result.toString();
+  }
+
+  /**
+   * Returns a verbose string representation of this database.
+   * 
+   * @return The string representation
+   */
+  public String toVerboseString() {
+    StringBuffer result = new StringBuffer();
+
+    result.append("Database [");
+    result.append(getName());
+    result.append("] tables:");
+    for (int idx = 0; idx < getTableCount(); idx++) {
+      result.append(" ");
+      result.append(getTable(idx).toVerboseString());
+    }
+    result.append(" sequences:");
+    for (int idx = 0; idx < getSequenceCount(); idx++) {
+      result.append(" ");
+      result.append(getSequence(idx).toString());
+    }
+    result.append(" views:");
+    for (int idx = 0; idx < getViewCount(); idx++) {
+      result.append(" ");
+      result.append(getView(idx).toVerboseString());
+    }
+    result.append(" functions:");
+    for (int idx = 0; idx < getFunctionCount(); idx++) {
+      result.append(" ");
+      result.append(getFunction(idx).toString());
+    }
+    result.append(" triggers:");
+    for (int idx = 0; idx < getTriggerCount(); idx++) {
+      result.append(" ");
+      result.append(getTrigger(idx).toString());
+    }
+    return result.toString();
+  }
+
+  public void applyNamingConventionFilter(ExcludeFilter filter) {
+    for (int i = 0; i < _tables.size(); i++) {
+      Table t = (Table) _tables.get(i);
+      t.applyNamingConventionFilter(filter);
+      if (t.isEmpty()) {
+        _tables.remove(t);
+        i--;
+      }
     }
 
-    /**
-     * Returns the method for generating primary key values.
-     * 
-     * @return The method
-     */
-    public String getIdMethod() {
-        return _idMethod;
+    // We will now move incomplete tables to their specific arraylist
+    for (int i = 0; i < _tables.size(); i++) {
+      Table t = (Table) _tables.get(i);
+      if (!filter.compliesWithNamingRuleObject(t.getName())) {
+        _tables.remove(t);
+        _modifiedTables.add(t);
+        i--;
+      }
     }
 
-    /**
-     * Sets the method for generating primary key values. Note that this value
-     * is ignored by DdlUtils and only for compatibility with Torque.
-     * 
-     * @param idMethod
-     *            The method
-     */
-    public void setIdMethod(String idMethod) {
-        _idMethod = idMethod;
+    applyNamingConventionToRest(filter);
+  }
+
+  public void applyNamingConventionToUpdate(ExcludeFilter filter) {
+    try {
+      for (int i = 0; i < _tables.size(); i++) {
+        Table t = (Table) _tables.get(i);
+        Table clonedT = (Table) t.clone();
+        clonedT.applyNamingConventionFilter(filter);
+        if (clonedT.isEmpty()) {
+          _tables.remove(t);
+          i--;
+        }
+      }
+      applyNamingConventionToRest(filter);
+    } catch (Exception e) {
+      e.printStackTrace();
     }
+  }
 
-    /**
-     * Returns the number of tables in this model.
-     * 
-     * @return The number of tables
-     */
-    public int getTableCount() {
-        return _tables.size();
+  private void applyNamingConventionToRest(ExcludeFilter filter) {
+
+    for (int i = 0; i < _sequences.size(); i++) {
+      Sequence v = (Sequence) _sequences.get(i);
+      if (!filter.compliesWithNamingRuleObject(v.getName())) {
+        _sequences.remove(v);
+        i--;
+      }
     }
+    for (int i = 0; i < _views.size(); i++) {
+      View v = (View) _views.get(i);
+      if (!filter.compliesWithNamingRuleObject(v.getName())) {
+        _views.remove(v);
+        i--;
+      }
+    }
+    for (int i = 0; i < _functions.size(); i++) {
+      Function v = (Function) _functions.get(i);
+      if (!filter.compliesWithNamingRuleObject(v.getName())) {
+        _functions.remove(v);
+        i--;
+      }
+    }
+    for (int i = 0; i < _triggers.size(); i++) {
+      Trigger v = (Trigger) _triggers.get(i);
+      if (!filter.compliesWithNamingRuleObject(v.getName())) {
+        _triggers.remove(v);
+        i--;
+      }
+    }
+  }
 
-    public int getModifiedTableCount() {
-        return _modifiedTables.size();
+  public void moveTablesToModified() {
+    _modifiedTables.addAll(_tables);
+    _tables = new ArrayList();
+  }
+
+  public void moveModifiedToTables() {
+    _tables.addAll(_modifiedTables);
+    _modifiedTables = new ArrayList();
+  }
+
+  public void removeAllTables() {
+    _tables = new ArrayList();
+  }
+
+  public void filterByDataset(String datasetName) {
+    ArrayList oldTables = _tables;
+    _tables = new ArrayList();
+    DataSetService ds = DataSetService.getInstance();
+    List<DataSetTable> tables = ds.getDataSetByValue(datasetName).getDataSetTableList();
+    for (DataSetTable table : tables) {
+      for (int i = 0; i < oldTables.size(); i++) {
+        Table t = (Table) oldTables.get(i);
+        if (t.getName().equalsIgnoreCase(table.getTable().getDBTableName())) {
+          _tables.add(t);
+        }
+      }
     }
-
-    /**
-     * Returns the tables in this model.
-     * 
-     * @return The tables
-     */
-    public Table[] getTables() {
-        return (Table[]) _tables.toArray(new Table[_tables.size()]);
+    _views = new ArrayList();
+    ArrayList oldTriggers = _triggers;
+    _triggers = new ArrayList();
+    for (int i = 0; i < oldTriggers.size(); i++) {
+      Trigger trigger = (Trigger) oldTriggers.get(i);
+      if (findTable(trigger.getTable()) != null)
+        _triggers.add(trigger);
     }
-
-    public Table[] getModifiedTables() {
-        return (Table[]) _modifiedTables.toArray(new Table[_modifiedTables
-                .size()]);
-    }
-
-    /**
-     * Returns the table at the specified position.
-     * 
-     * @param idx
-     *            The index of the table
-     * @return The table
-     */
-    public Table getTable(int idx) {
-        return (Table) _tables.get(idx);
-    }
-
-    public Table getModifiedTable(int idx) {
-        return (Table) _modifiedTables.get(idx);
-    }
-
-    /**
-     * Adds a table.
-     * 
-     * @param table
-     *            The table to add
-     */
-    public void addTable(Table table) {
-        if (table != null) {
-            _tables.add(table);
-        }
-    }
-
-    public void addModifiedTable(Table table) {
-        if (table != null) {
-            _modifiedTables.add(table);
-        }
-    }
-
-    /**
-     * Adds a table at the specified position.
-     * 
-     * @param idx
-     *            The index where to insert the table
-     * @param table
-     *            The table to add
-     */
-    public void addTable(int idx, Table table) {
-        if (table != null) {
-            _tables.add(idx, table);
-        }
-    }
-
-    public void addModifiedTable(int idx, Table table) {
-        if (table != null) {
-            _modifiedTables.add(idx, table);
-        }
-    }
-
-    /**
-     * Adds the given tables.
-     * 
-     * @param tables
-     *            The tables to add
-     */
-    public void addTables(Collection tables) {
-        for (Iterator it = tables.iterator(); it.hasNext();) {
-            addTable((Table) it.next());
-        }
-    }
-
-    public void addModifiedTables(Collection tables) {
-        for (Iterator it = tables.iterator(); it.hasNext();) {
-            addModifiedTable((Table) it.next());
-        }
-    }
-
-    /**
-     * Removes the given table.
-     * 
-     * @param table
-     *            The table to remove
-     */
-    public void removeTable(Table table) {
-        if (table != null) {
-            _tables.remove(table);
-        }
-    }
-
-    public void removeModifiedTable(Table table) {
-        if (table != null) {
-            _modifiedTables.remove(table);
-        }
-    }
-
-    /**
-     * Removes the indicated table.
-     * 
-     * @param idx
-     *            The index of the table to remove
-     */
-    public void removeTable(int idx) {
-        _tables.remove(idx);
-    }
-
-    public void removeModifiedTable(int idx) {
-        _modifiedTables.remove(idx);
-    }
-
-    /**
-     * Returns the number of sequences in this model.
-     * 
-     * @return The number of sequences
-     */
-    public int getSequenceCount() {
-        return _sequences.size();
-    }
-
-    /**
-     * Returns the sequences in this model.
-     * 
-     * @return The sequences
-     */
-    public Sequence[] getSequences() {
-        return (Sequence[]) _sequences.toArray(new Sequence[_sequences.size()]);
-    }
-
-    /**
-     * Returns the sequence at the specified position.
-     * 
-     * @param idx
-     *            The index of the sequence
-     * @return The sequence
-     */
-    public Sequence getSequence(int idx) {
-        return (Sequence) _sequences.get(idx);
-    }
-
-    /**
-     * Adds a sequence.
-     * 
-     * @param sequence
-     *            The sequence to add
-     */
-    public void addSequence(Sequence sequence) {
-        if (sequence != null) {
-            _sequences.add(sequence);
-        }
-    }
-
-    /**
-     * Adds a sequence at the specified position.
-     * 
-     * @param idx
-     *            The index where to insert the sequence
-     * @param sequence
-     *            The sequence to add
-     */
-    public void addSequence(int idx, Sequence sequence) {
-        if (sequence != null) {
-            _sequences.add(idx, sequence);
-        }
-    }
-
-    /**
-     * Adds the given sequences.
-     * 
-     * @param sequences
-     *            The sequences to add
-     */
-    public void addSequences(Collection sequences) {
-        for (Iterator it = sequences.iterator(); it.hasNext();) {
-            addSequence((Sequence) it.next());
-        }
-    }
-
-    /**
-     * Removes the given sequence.
-     * 
-     * @param sequence
-     *            The sequence to remove
-     */
-    public void removeSequence(Sequence sequence) {
-        if (sequence != null) {
-            _sequences.remove(sequence);
-        }
-    }
-
-    /**
-     * Removes the indicated sequence.
-     * 
-     * @param idx
-     *            The index of the sequence to remove
-     */
-    public void removeSequence(int idx) {
-        _sequences.remove(idx);
-    }
-
-    /**
-     * Returns the number of views in this model.
-     * 
-     * @return The number of views
-     */
-    public int getViewCount() {
-        return _views.size();
-    }
-
-    /**
-     * Returns the views in this model.
-     * 
-     * @return The views
-     */
-    public View[] getViews() {
-        return (View[]) _views.toArray(new View[_views.size()]);
-    }
-
-    /**
-     * Returns the view at the specified position.
-     * 
-     * @param idx
-     *            The index of the view
-     * @return The view
-     */
-    public View getView(int idx) {
-        return (View) _views.get(idx);
-    }
-
-    /**
-     * Adds a view.
-     * 
-     * @param view
-     *            The view to add
-     */
-    public void addView(View view) {
-        if (view != null) {
-            _views.add(view);
-        }
-    }
-
-    /**
-     * Adds a view at the specified position.
-     * 
-     * @param idx
-     *            The index where to insert the view
-     * @param view
-     *            The view to add
-     */
-    public void addView(int idx, View view) {
-        if (view != null) {
-            _views.add(idx, view);
-        }
-    }
-
-    /**
-     * Adds the given views.
-     * 
-     * @param views
-     *            The views to add
-     */
-    public void addViews(Collection views) {
-        for (Iterator it = views.iterator(); it.hasNext();) {
-            addView((View) it.next());
-        }
-    }
-
-    /**
-     * Removes the given view.
-     * 
-     * @param view
-     *            The view to remove
-     */
-    public void removeView(View view) {
-        if (view != null) {
-            _views.remove(view);
-        }
-    }
-
-    /**
-     * Removes the indicated view.
-     * 
-     * @param idx
-     *            The index of the view to remove
-     */
-    public void removeView(int idx) {
-        _views.remove(idx);
-    }
-
-    /**
-     * Returns the number of functions in this model.
-     * 
-     * @return The number of functions
-     */
-    public int getFunctionCount() {
-        return _functions.size();
-    }
-
-    /**
-     * Returns the functions in this model.
-     * 
-     * @return The functions
-     */
-    public Function[] getFunctions() {
-        return (Function[]) _functions.toArray(new Function[_functions.size()]);
-    }
-
-    /**
-     * Returns the function at the specified position.
-     * 
-     * @param idx
-     *            The index of the function
-     * @return The function
-     */
-    public Function getFunction(int idx) {
-        return (Function) _functions.get(idx);
-    }
-
-    /**
-     * Adds a function.
-     * 
-     * @param function
-     *            The function to add
-     */
-    public void addFunction(Function function) {
-        if (function != null) {
-            _functions.add(function);
-        }
-    }
-
-    /**
-     * Adds a function at the specified position.
-     * 
-     * @param idx
-     *            The index where to insert the function
-     * @param function
-     *            The function to add
-     */
-    public void addFunction(int idx, Function function) {
-        if (function != null) {
-            _functions.add(idx, function);
-        }
-    }
-
-    /**
-     * Adds the given functions.
-     * 
-     * @param functions
-     *            The functions to add
-     */
-    public void addFunctions(Collection functions) {
-        for (Iterator it = functions.iterator(); it.hasNext();) {
-            addFunction((Function) it.next());
-        }
-    }
-
-    /**
-     * Removes the given function.
-     * 
-     * @param function
-     *            The function to remove
-     */
-    public void removeFunction(Function function) {
-        if (function != null) {
-            _functions.remove(function);
-        }
-    }
-
-    /**
-     * Removes the indicated function.
-     * 
-     * @param idx
-     *            The index of the function to remove
-     */
-    public void removeFunction(int idx) {
-        _functions.remove(idx);
-    }
-
-    /**
-     * Returns the number of triggers in this model.
-     * 
-     * @return The number of triggers
-     */
-    public int getTriggerCount() {
-        return _triggers.size();
-    }
-
-    /**
-     * Returns the triggers in this model.
-     * 
-     * @return The triggers
-     */
-    public Trigger[] getTriggers() {
-        return (Trigger[]) _triggers.toArray(new Trigger[_triggers.size()]);
-    }
-
-    /**
-     * Returns the trigger at the specified position.
-     * 
-     * @param idx
-     *            The index of the trigger
-     * @return The trigger
-     */
-    public Trigger getTrigger(int idx) {
-        return (Trigger) _triggers.get(idx);
-    }
-
-    /**
-     * Adds a trigger.
-     * 
-     * @param trigger
-     *            The trigger to add
-     */
-    public void addTrigger(Trigger trigger) {
-        if (trigger != null) {
-            _triggers.add(trigger);
-        }
-    }
-
-    /**
-     * Adds a trigger at the specified position.
-     * 
-     * @param idx
-     *            The index where to insert the trigger
-     * @param trigger
-     *            The trigger to add
-     */
-    public void addTrigger(int idx, Trigger trigger) {
-        if (trigger != null) {
-            _triggers.add(idx, trigger);
-        }
-    }
-
-    /**
-     * Adds the given triggers.
-     * 
-     * @param triggers
-     *            The triggers to add
-     */
-    public void addTriggers(Collection triggers) {
-        for (Iterator it = triggers.iterator(); it.hasNext();) {
-            addTrigger((Trigger) it.next());
-        }
-    }
-
-    /**
-     * Removes the given trigger.
-     * 
-     * @param trigger
-     *            The trigger to remove
-     */
-    public void removeTrigger(Trigger trigger) {
-        if (trigger != null) {
-            _triggers.remove(trigger);
-        }
-    }
-
-    /**
-     * Removes the indicated trigger.
-     * 
-     * @param idx
-     *            The index of the trigger to remove
-     */
-    public void removeTrigger(int idx) {
-        _triggers.remove(idx);
-    }
-
-    // Helper methods
-
-    /**
-     * Initializes the model by establishing the relationships between elements
-     * in this model encoded eg. in foreign keys etc. Also checks that the model
-     * elements are valid (table and columns have a name, foreign keys rference
-     * existing tables etc.)
-     */
-    public void initialize() throws ModelException {
-        // we have to setup
-        // * target tables in foreign keys
-        // * columns in foreign key references
-        // * columns in indices
-        // * columns in uniques
-        HashSet namesOfProcessedTables = new HashSet();
-        HashSet namesOfProcessedColumns = new HashSet();
-        HashSet namesOfProcessedFks = new HashSet();
-        HashSet namesOfProcessedIndices = new HashSet();
-        HashSet namesOfProcessedChecks = new HashSet();
-        int tableIdx = 0;
-
-        // if ((getName() == null) || (getName().length() == 0))
-        // {
-        // throw new ModelException("The database model has no name");
-        // }
-
-        for (Iterator tableIt = _tables.iterator(); tableIt.hasNext(); tableIdx++) {
-            Table curTable = (Table) tableIt.next();
-
-            if ((curTable.getName() == null)
-                    || (curTable.getName().length() == 0)) {
-                throw new ModelException("The table nr. " + tableIdx
-                        + " has no name");
-            }
-            if (namesOfProcessedTables.contains(curTable.getName())) {
-                throw new ModelException(
-                        "There are multiple tables with the name "
-                                + curTable.getName());
-            }
-            namesOfProcessedTables.add(curTable.getName());
-
-            namesOfProcessedColumns.clear();
-            namesOfProcessedFks.clear();
-            namesOfProcessedIndices.clear();
-            namesOfProcessedChecks.clear();
-
-            for (int idx = 0; idx < curTable.getColumnCount(); idx++) {
-                Column column = curTable.getColumn(idx);
-
-                if ((column.getName() == null)
-                        || (column.getName().length() == 0)) {
-                    throw new ModelException("The column nr. " + idx
-                            + " in table " + curTable.getName()
-                            + " has no name");
-                }
-                if (namesOfProcessedColumns.contains(column.getName())) {
-                    throw new ModelException(
-                            "There are multiple column with the name "
-                                    + column.getName() + " in the table "
-                                    + curTable.getName());
-                }
-                namesOfProcessedColumns.add(column.getName());
-
-                if ((column.getType() == null)
-                        || (column.getType().length() == 0)) {
-                    throw new ModelException("The column nr. " + idx
-                            + " in table " + curTable.getName()
-                            + " has no type");
-                }
-                if ((column.getTypeCode() == Types.OTHER)
-                        && !"OTHER".equalsIgnoreCase(column.getType())) {
-                    throw new ModelException("The column nr. " + idx
-                            + " in table " + curTable.getName()
-                            + " has an unknown type " + column.getType());
-                }
-                namesOfProcessedColumns.add(column.getName());
-            }
-
-            for (int idx = 0; idx < curTable.getForeignKeyCount(); idx++) {
-                ForeignKey fk = curTable.getForeignKey(idx);
-                String fkName = (fk.getName() == null ? "" : fk.getName());
-                String fkDesc = (fkName.length() == 0 ? "nr. " + idx : fkName);
-
-                if (fkName.length() > 0) {
-                    if (namesOfProcessedFks.contains(fkName)) {
-                        throw new ModelException(
-                                "There are multiple foreign keys in table "
-                                        + curTable.getName()
-                                        + " with the name " + fkName);
-                    }
-                    namesOfProcessedFks.add(fkName);
-                }
-
-                if (fk.getForeignTable() == null) {
-                    Table targetTable = findTable(fk.getForeignTableName(),
-                            true);
-
-                    if (targetTable == null) {
-                        throw new ModelException("The foreignkey " + fkDesc
-                                + " in table " + curTable.getName()
-                                + " references the undefined table "
-                                + fk.getForeignTableName());
-                    } else {
-                        fk.setForeignTable(targetTable);
-                    }
-                }
-                for (int refIdx = 0; refIdx < fk.getReferenceCount(); refIdx++) {
-                    Reference ref = fk.getReference(refIdx);
-
-                    if (ref.getLocalColumn() == null) {
-                        Column localColumn = curTable.findColumn(ref
-                                .getLocalColumnName(), true);
-
-                        if (localColumn == null) {
-                            throw new ModelException("The foreignkey " + fkDesc
-                                    + " in table " + curTable.getName()
-                                    + " references the undefined local column "
-                                    + ref.getLocalColumnName());
-                        } else {
-                            ref.setLocalColumn(localColumn);
-                        }
-                    }
-                    if (ref.getForeignColumn() == null) {
-                        Column foreignColumn = fk.getForeignTable().findColumn(
-                                ref.getForeignColumnName(), true);
-
-                        if (foreignColumn == null) {
-                            throw new ModelException("The foreignkey " + fkDesc
-                                    + " in table " + curTable.getName()
-                                    + " references the undefined local column "
-                                    + ref.getForeignColumnName() + " in table "
-                                    + fk.getForeignTable().getName());
-                        } else {
-                            ref.setForeignColumn(foreignColumn);
-                        }
-                    }
-                }
-            }
-
-            for (int idx = 0; idx < curTable.getIndexCount(); idx++) {
-                Index index = curTable.getIndex(idx);
-                String indexName = (index.getName() == null ? "" : index
-                        .getName());
-                String indexDesc = (indexName.length() == 0 ? "nr. " + idx
-                        : indexName);
-
-                if (indexName.length() > 0) {
-                    if (namesOfProcessedIndices.contains(indexName)) {
-                        throw new ModelException(
-                                "There are multiple indices in table "
-                                        + curTable.getName()
-                                        + " with the name " + indexName);
-                    }
-                    namesOfProcessedIndices.add(indexName);
-                }
-
-                for (int indexColumnIdx = 0; indexColumnIdx < index
-                        .getColumnCount(); indexColumnIdx++) {
-                    IndexColumn indexColumn = index.getColumn(indexColumnIdx);
-                    Column column = curTable.findColumn(indexColumn.getName(),
-                            true);
-
-                    if (column == null) {
-                        throw new ModelException("The index " + indexDesc
-                                + " in table " + curTable.getName()
-                                + " references the undefined column "
-                                + indexColumn.getName());
-                    } else {
-                        indexColumn.setColumn(column);
-                    }
-                }
-            }
-
-            for (int idx = 0; idx < curTable.getUniqueCount(); idx++) {
-                Unique unique = curTable.getUnique(idx);
-                String uniqueName = (unique.getName() == null ? "" : unique
-                        .getName());
-                String uniqueDesc = (uniqueName.length() == 0 ? "nr. " + idx
-                        : uniqueName);
-
-                if (uniqueName.length() > 0) {
-                    if (namesOfProcessedIndices.contains(uniqueName)) {
-                        throw new ModelException(
-                                "There are multiple uniques or indices in table "
-                                        + curTable.getName()
-                                        + " with the name " + uniqueName);
-                    }
-                    namesOfProcessedIndices.add(uniqueName);
-                }
-
-                for (int uniqueColumnIdx = 0; uniqueColumnIdx < unique
-                        .getColumnCount(); uniqueColumnIdx++) {
-                    IndexColumn uniqueColumn = unique
-                            .getColumn(uniqueColumnIdx);
-                    Column column = curTable.findColumn(uniqueColumn.getName(),
-                            true);
-
-                    if (column == null) {
-                        throw new ModelException("The unique " + uniqueDesc
-                                + " in table " + curTable.getName()
-                                + " references the undefined column "
-                                + uniqueColumn.getName());
-                    } else {
-                        uniqueColumn.setColumn(column);
-                    }
-                }
-            }
-
-            for (int idx = 0; idx < curTable.getCheckCount(); idx++) {
-
-                Check check = curTable.getCheck(idx);
-
-                if ((check.getName() == null)
-                        || (check.getName().length() == 0)) {
-                    throw new ModelException("The check nr. " + idx
-                            + " in table " + curTable.getName()
-                            + " has no name");
-                }
-                if (namesOfProcessedChecks.contains(check.getName())) {
-                    throw new ModelException(
-                            "There are multiple checks with the name "
-                                    + check.getName() + " in the table "
-                                    + curTable.getName());
-                }
-                namesOfProcessedChecks.add(check.getName());
-
-                if ((check.getCondition() == null)
-                        || (check.getCondition().length() == 0)) {
-                    throw new ModelException("The check " + check.getName()
-                            + " in table " + curTable.getName()
-                            + " has no condition defined");
-                }
-            }
-        }
-
-        HashSet namesOfProcessedSequences = new HashSet();
-        int sequenceIdx = 0;
-
-        for (Iterator sequenceIt = _sequences.iterator(); sequenceIt.hasNext();) {
-            Sequence curSequence = (Sequence) sequenceIt.next();
-
-            if ((curSequence.getName() == null)
-                    || (curSequence.getName().length() == 0)) {
-                throw new ModelException("The sequence nr. " + sequenceIdx
-                        + "has no name");
-            }
-            if (namesOfProcessedSequences.contains(curSequence.getName())) {
-                throw new ModelException(
-                        "There are multiple sequences with the name "
-                                + curSequence.getName());
-            }
-            namesOfProcessedSequences.add(curSequence.getName());
-        }
-
-        HashSet namesOfProcessedViews = new HashSet();
-        int viewIdx = 0;
-
-        for (Iterator viewIt = _views.iterator(); viewIt.hasNext();) {
-            View curView = (View) viewIt.next();
-
-            if ((curView.getName() == null)
-                    || (curView.getName().length() == 0)) {
-                throw new ModelException("The view nr. " + viewIdx
-                        + "has no name");
-            }
-            if (namesOfProcessedViews.contains(curView.getName())) {
-                throw new ModelException(
-                        "There are multiple views with the name "
-                                + curView.getName());
-            }
-            namesOfProcessedViews.add(curView.getName());
-
-            if ((curView.getStatement() == null)
-                    || (curView.getStatement().length() == 0)) {
-                throw new ModelException("The view " + curView.getName()
-                        + " has no statement defined");
-            }
-        }
-
-        HashSet namesOfProcessedFunctions = new HashSet();
-        HashSet namesOfProcessedParameters = new HashSet();
-        int functionIdx = 0;
-
-        for (Iterator functionIt = _functions.iterator(); functionIt.hasNext();) {
-            Function curFunction = (Function) functionIt.next();
-
-            if ((curFunction.getName() == null)
-                    || (curFunction.getName().length() == 0)) {
-                throw new ModelException("The function nr. " + functionIdx
-                        + "has no name");
-            }
-            if (namesOfProcessedFunctions.contains(curFunction.getNotation())) {
-                throw new ModelException(
-                        "There are multiple functions with the same notation "
-                                + curFunction.getNotation());
-            }
-            namesOfProcessedFunctions.add(curFunction.getNotation());
-
-            if ((curFunction.getBody() == null)
-                    || (curFunction.getBody().length() == 0)) {
-                throw new ModelException("The function "
-                        + curFunction.getName() + " has no body defined");
-            }
-
-            namesOfProcessedParameters.clear();
-            for (int idx = 0; idx < curFunction.getParameterCount(); idx++) {
-                Parameter curParameter = curFunction.getParameter(idx);
-
-                if ((curParameter.getName() != null)
-                        && (curParameter.getName().length() == 0)) {
-                    if (namesOfProcessedParameters.contains(curParameter
-                            .getName())) {
-                        throw new ModelException(
-                                "There are multiple parameters with the name "
-                                        + curParameter.getName()
-                                        + " in the function "
-                                        + curFunction.getName());
-                    }
-                }
-            }
-        }
-
-        HashSet namesOfProcessedTriggers = new HashSet();
-        int triggerIdx = 0;
-
-        for (Iterator triggerIt = _triggers.iterator(); triggerIt.hasNext();) {
-            Trigger curTrigger = (Trigger) triggerIt.next();
-
-            if ((curTrigger.getName() == null)
-                    || (curTrigger.getName().length() == 0)) {
-                throw new ModelException("The trigger nr. " + triggerIdx
-                        + "has no name");
-            }
-            if (namesOfProcessedTriggers.contains(curTrigger.getName())) {
-                throw new ModelException(
-                        "There are multiple triggers with the name "
-                                + curTrigger.getName());
-            }
-            namesOfProcessedTriggers.add(curTrigger.getName());
-
-            if (!namesOfProcessedTables.contains(curTrigger.getTable())) {
-                throw new ModelException("The trigger " + curTrigger.getName()
-                        + " references the undefined table "
-                        + curTrigger.getTable());
-            }
-
-            if ((curTrigger.getBody() == null)
-                    || (curTrigger.getBody().length() == 0)) {
-                throw new ModelException("The trigger " + curTrigger.getName()
-                        + " has no body defined");
-            }
-        }
-    }
-
-    /**
-     * Finds the table with the specified name, using case insensitive matching.
-     * Note that this method is not called getTable to avoid introspection
-     * problems.
-     * 
-     * @param name
-     *            The name of the table to find
-     * @return The table or <code>null</code> if there is no such table
-     */
-    public Table findTable(String name) {
-        return findTable(name, false);
-    }
-
-    /**
-     * Finds the table with the specified name, using case insensitive matching.
-     * Note that this method is not called getTable) to avoid introspection
-     * problems.
-     * 
-     * @param name
-     *            The name of the table to find
-     * @param caseSensitive
-     *            Whether case matters for the names
-     * @return The table or <code>null</code> if there is no such table
-     */
-    public Table findTable(String name, boolean caseSensitive) {
-        for (Iterator iter = _tables.iterator(); iter.hasNext();) {
-            Table table = (Table) iter.next();
-
-            if (caseSensitive) {
-                if (table.getName().equals(name)) {
-                    return table;
-                }
-            } else {
-                if (table.getName().equalsIgnoreCase(name)) {
-                    return table;
-                }
-            }
-        }
-        return null;
-    }
-
-    /**
-     * Finds the sequence with the specified name, using case insensitive
-     * matching. Note that this method is not called getSequence to avoid
-     * introspection problems.
-     * 
-     * @param name
-     *            The name of the sequence to find
-     * @return The sequence or <code>null</code> if there is no such sequence
-     */
-    public Sequence findSequence(String name) {
-        return findSequence(name, false);
-    }
-
-    /**
-     * Finds the sequence with the specified name, using case insensitive
-     * matching. Note that this method is not called getSequence) to avoid
-     * introspection problems.
-     * 
-     * @param name
-     *            The name of the sequence to find
-     * @param caseSensitive
-     *            Whether case matters for the names
-     * @return The sequence or <code>null</code> if there is no such sequence
-     */
-    public Sequence findSequence(String name, boolean caseSensitive) {
-        for (Iterator iter = _sequences.iterator(); iter.hasNext();) {
-            Sequence sequence = (Sequence) iter.next();
-
-            if (caseSensitive) {
-                if (sequence.getName().equals(name)) {
-                    return sequence;
-                }
-            } else {
-                if (sequence.getName().equalsIgnoreCase(name)) {
-                    return sequence;
-                }
-            }
-        }
-        return null;
-    }
-
-    /**
-     * Finds the view with the specified name, using case insensitive matching.
-     * Note that this method is not called getView to avoid introspection
-     * problems.
-     * 
-     * @param name
-     *            The name of the view to find
-     * @return The view or <code>null</code> if there is no such view
-     */
-    public View findView(String name) {
-        return findView(name, false);
-    }
-
-    /**
-     * Finds the view with the specified name, using case insensitive matching.
-     * Note that this method is not called getView) to avoid introspection
-     * problems.
-     * 
-     * @param name
-     *            The name of the view to find
-     * @param caseSensitive
-     *            Whether case matters for the names
-     * @return The view or <code>null</code> if there is no such view
-     */
-    public View findView(String name, boolean caseSensitive) {
-        for (Iterator iter = _views.iterator(); iter.hasNext();) {
-            View view = (View) iter.next();
-
-            if (caseSensitive) {
-                if (view.getName().equals(name)) {
-                    return view;
-                }
-            } else {
-                if (view.getName().equalsIgnoreCase(name)) {
-                    return view;
-                }
-            }
-        }
-        return null;
-    }
-
-    /**
-     * Finds the function with the specified name, using case insensitive
-     * matching. Note that this method is not called getFunction to avoid
-     * introspection problems.
-     * 
-     * @param name
-     *            The name of the function to find
-     * @return The function or <code>null</code> if there is no such function
-     */
-    public Function findFunction(String name) {
-        return findFunction(name, false);
-    }
-
-    /**
-     * Finds the function with the specified name and parameters, using case
-     * sensitive matching.
-     * 
-     * @param name
-     *            The name of the function to find
-     * @param caseSensitive
-     *            Whether case matters for the names
-     * @return The function or <code>null</code> if there is no such function
-     */
-    public Function findFunctionWithParams(String name, Parameter[] params) {
-        return findFunctionWithParams(name, params, false);
-    }
-
-    /**
-     * Finds the function with the specified name, using case sensitive
-     * matching. Note that this method is not called getFunction) to avoid
-     * introspection problems.
-     * 
-     * @param name
-     *            The name of the function to find
-     * @param caseSensitive
-     *            Whether case matters for the names
-     * @return The function or <code>null</code> if there is no such function
-     */
-    public Function findFunction(String name, boolean caseSensitive) {
-        for (Iterator iter = _functions.iterator(); iter.hasNext();) {
-            Function function = (Function) iter.next();
-
-            if (caseSensitive) {
-                if (function.getName().equals(name)) {
-                    return function;
-                }
-            } else {
-                if (function.getName().equalsIgnoreCase(name)) {
-                    return function;
-                }
-            }
-        }
-        return null;
-    }
-
-    /**
-     * Finds the function with the specified name and parameters, using case
-     * insensitive matching.
-     * 
-     * @param name
-     *            The name of the function to find
-     * @param params
-     *            The parameters of the function to find
-     * @param caseSensitive
-     *            Whether case matters for the names
-     * @return The function or <code>null</code> if there is no such function
-     */
-    public Function findFunctionWithParams(String name, Parameter[] params,
-            boolean caseSensitive) {
-        for (Iterator iter = _functions.iterator(); iter.hasNext();) {
-            Function function = (Function) iter.next();
-
-            if (caseSensitive) {
-                if (function.getName().equals(name)) {
-                    if (function.getParameterCount() == params.length) {
-                        boolean eqPara = true;
-                        int i = 0;
-                        Parameter[] params2 = function.getParameters();
-                        while (eqPara && i < params.length) {
-                            if (!params[i].equals(params2[i]))
-                                eqPara = false;
-                            i++;
-                        }
-                        if (eqPara)
-                            return function;
-                    }
-                }
-            } else {
-                if (function.getName().equalsIgnoreCase(name)) {
-                    Parameter[] params2 = function.getParameters();
-                    if (params2.length == params.length) {
-                        boolean eqPara = true;
-                        int i = 0;
-                        while (eqPara && i < params.length) {
-                            int type1 = params[i].getTypeCode();
-                            int type2 = params2[i].getTypeCode();
-
-                            int typeCode2 = type1 == ExtTypes.NVARCHAR ? Types.VARCHAR
-                                    : type1;
-                            typeCode2 = typeCode2 == ExtTypes.NCHAR ? Types.CHAR
-                                    : typeCode2;
-                            int othertypeCode2 = type2 == ExtTypes.NVARCHAR ? Types.VARCHAR
-                                    : type2;
-                            othertypeCode2 = othertypeCode2 == ExtTypes.NCHAR ? Types.CHAR
-                                    : othertypeCode2;
-                            if (typeCode2 != othertypeCode2) {
-                                eqPara = false;
-                            }
-                            i++;
-                        }
-                        if (eqPara) {
-                            return function;
-                        }
-                    }
-                }
-            }
-        }
-        return null;
-    }
-
-    /**
-     * Finds the trigger with the specified name, using case insensitive
-     * matching. Note that this method is not called getTrigger to avoid
-     * introspection problems.
-     * 
-     * @param name
-     *            The name of the trigger to find
-     * @return The trigger or <code>null</code> if there is no such trigger
-     */
-    public Trigger findTrigger(String name) {
-        return findTrigger(name, false);
-    }
-
-    /**
-     * Finds the trigger with the specified name, using case insensitive
-     * matching. Note that this method is not called getTrigger) to avoid
-     * introspection problems.
-     * 
-     * @param name
-     *            The name of the trigger to find
-     * @param caseSensitive
-     *            Whether case matters for the names
-     * @return The trigger or <code>null</code> if there is no such trigger
-     */
-    public Trigger findTrigger(String name, boolean caseSensitive) {
-        for (Iterator iter = _triggers.iterator(); iter.hasNext();) {
-            Trigger trigger = (Trigger) iter.next();
-
-            if (caseSensitive) {
-                if (trigger.getName().equals(name)) {
-                    return trigger;
-                }
-            } else {
-                if (trigger.getName().equalsIgnoreCase(name)) {
-                    return trigger;
-                }
-            }
-        }
-        return null;
-    }
-
-    /**
-     * Returns the dyna class cache. If none is available yet, a new one will be
-     * created.
-     * 
-     * @return The dyna class cache
-     */
-    private DynaClassCache getDynaClassCache() {
-        if (_dynaClassCache == null) {
-            _dynaClassCache = new DynaClassCache();
-        }
-        return _dynaClassCache;
-    }
-
-    /**
-     * Resets the dyna class cache. This should be done for instance when a
-     * column has been added or removed to a table.
-     */
-    public void resetDynaClassCache() {
-        _dynaClassCache = null;
-    }
-
-    /**
-     * Returns the {@link org.apache.ddlutils.dynabean.SqlDynaClass} for the
-     * given table name. If the it does not exist yet, a new one will be created
-     * based on the Table definition.
-     * 
-     * @param tableName
-     *            The name of the table to create the bean for
-     * @return The <code>SqlDynaClass</code> for the indicated table or
-     *         <code>null</code> if the model contains no such table
-     */
-    public SqlDynaClass getDynaClassFor(String tableName) {
-        Table table = findTable(tableName);
-
-        return table != null ? getDynaClassCache().getDynaClass(table) : null;
-    }
-
-    /**
-     * Returns the {@link org.apache.ddlutils.dynabean.SqlDynaClass} for the
-     * given dyna bean.
-     * 
-     * @param bean
-     *            The dyna bean
-     * @return The <code>SqlDynaClass</code> for the given bean
-     */
-    public SqlDynaClass getDynaClassFor(DynaBean bean) {
-        return getDynaClassCache().getDynaClass(bean);
-    }
-
-    /**
-     * Creates a new dyna bean for the given table.
-     * 
-     * @param table
-     *            The table to create the bean for
-     * @return The new dyna bean
-     */
-    public DynaBean createDynaBeanFor(Table table) throws SqlDynaException {
-        return getDynaClassCache().createNewInstance(table);
-    }
-
-    /**
-     * Convenience method that combines {@link #createDynaBeanFor(Table)} and
-     * {@link #findTable(String, boolean)}.
-     * 
-     * @param tableName
-     *            The name of the table to create the bean for
-     * @param caseSensitive
-     *            Whether case matters for the names
-     * @return The new dyna bean
-     */
-    public DynaBean createDynaBeanFor(String tableName, boolean caseSensitive)
-            throws SqlDynaException {
-        return getDynaClassCache().createNewInstance(
-                findTable(tableName, caseSensitive));
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public Object clone() throws CloneNotSupportedException {
-        Database result = (Database) super.clone();
-
-        result._name = _name;
-        result._idMethod = _idMethod;
-        result._version = _version;
-        result._tables = new ArrayList();
-        result._views = new ArrayList();
-        result._functions = new ArrayList();
-        result._triggers = new ArrayList();
-        result._sequences = new ArrayList();
-        result._modifiedTables = new ArrayList();
-        Iterator it = _tables.iterator();
-        while (it.hasNext())
-            result._tables.add(((Table) it.next()).clone());
-        it = _views.iterator();
-        while (it.hasNext())
-            result._views.add(((View) it.next()).clone());
-        it = _functions.iterator();
-        while (it.hasNext())
-            result._functions.add(((Function) it.next()).clone());
-        it = _triggers.iterator();
-        while (it.hasNext())
-            result._triggers.add(((Trigger) it.next()).clone());
-        it = _sequences.iterator();
-        while (it.hasNext())
-            result._sequences.add(((Sequence) it.next()).clone());
-        it = _modifiedTables.iterator();
-        while (it.hasNext())
-            result._modifiedTables.add(((Table) it.next()).clone());
-
-        return result;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public boolean equals(Object obj) {
-        if (obj instanceof Database) {
-            Database other = (Database) obj;
-
-            // Note that this compares case sensitive
-            return new EqualsBuilder().append(_name, other._name).append(
-                    _tables, other._tables).append(_views, other._views)
-                    .append(_functions, other._functions).append(_triggers,
-                            other._triggers).isEquals();
-        } else {
-            return false;
-        }
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public int hashCode() {
-        return new HashCodeBuilder(17, 37).append(_name).append(_tables)
-                .append(_views).append(_functions).append(_triggers)
-                .toHashCode();
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public String toString() {
-        StringBuffer result = new StringBuffer();
-
-        result.append("Database [name=");
-        result.append(getName());
-        result.append("; ");
-        result.append(getTableCount());
-        result.append(" tables");
-        result.append("; ");
-        result.append(getSequenceCount());
-        result.append(" sequences");
-        result.append("; ");
-        result.append(getViewCount());
-        result.append(" views");
-        result.append("; ");
-        result.append(getFunctionCount());
-        result.append(" functions");
-        result.append("; ");
-        result.append(getTriggerCount());
-        result.append(" triggers]");
-
-        return result.toString();
-    }
-
-    /**
-     * Returns a verbose string representation of this database.
-     * 
-     * @return The string representation
-     */
-    public String toVerboseString() {
-        StringBuffer result = new StringBuffer();
-
-        result.append("Database [");
-        result.append(getName());
-        result.append("] tables:");
-        for (int idx = 0; idx < getTableCount(); idx++) {
-            result.append(" ");
-            result.append(getTable(idx).toVerboseString());
-        }
-        result.append(" sequences:");
-        for (int idx = 0; idx < getSequenceCount(); idx++) {
-            result.append(" ");
-            result.append(getSequence(idx).toString());
-        }
-        result.append(" views:");
-        for (int idx = 0; idx < getViewCount(); idx++) {
-            result.append(" ");
-            result.append(getView(idx).toVerboseString());
-        }
-        result.append(" functions:");
-        for (int idx = 0; idx < getFunctionCount(); idx++) {
-            result.append(" ");
-            result.append(getFunction(idx).toString());
-        }
-        result.append(" triggers:");
-        for (int idx = 0; idx < getTriggerCount(); idx++) {
-            result.append(" ");
-            result.append(getTrigger(idx).toString());
-        }
-        return result.toString();
-    }
-
-    public void applyNamingConventionFilter(ExcludeFilter filter) {
-        for (int i = 0; i < _tables.size(); i++) {
-            Table t = (Table) _tables.get(i);
-            t.applyNamingConventionFilter(filter);
-            if (t.isEmpty()) {
-                _tables.remove(t);
-                i--;
-            }
-        }
-
-        // We will now move incomplete tables to their specific arraylist
-        for (int i = 0; i < _tables.size(); i++) {
-            Table t = (Table) _tables.get(i);
-            if (!filter.compliesWithNamingRuleObject(t.getName())) {
-                _tables.remove(t);
-                _modifiedTables.add(t);
-                i--;
-            }
-        }
-
-        applyNamingConventionToRest(filter);
-    }
-
-    public void applyNamingConventionToUpdate(ExcludeFilter filter) {
-        try {
-            for (int i = 0; i < _tables.size(); i++) {
-                Table t = (Table) _tables.get(i);
-                Table clonedT = (Table) t.clone();
-                clonedT.applyNamingConventionFilter(filter);
-                if (clonedT.isEmpty()) {
-                    _tables.remove(t);
-                    i--;
-                }
-            }
-            applyNamingConventionToRest(filter);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-    private void applyNamingConventionToRest(ExcludeFilter filter) {
-
-        for (int i = 0; i < _sequences.size(); i++) {
-            Sequence v = (Sequence) _sequences.get(i);
-            if (!filter.compliesWithNamingRuleObject(v.getName())) {
-                _sequences.remove(v);
-                i--;
-            }
-        }
-        for (int i = 0; i < _views.size(); i++) {
-            View v = (View) _views.get(i);
-            if (!filter.compliesWithNamingRuleObject(v.getName())) {
-                _views.remove(v);
-                i--;
-            }
-        }
-        for (int i = 0; i < _functions.size(); i++) {
-            Function v = (Function) _functions.get(i);
-            if (!filter.compliesWithNamingRuleObject(v.getName())) {
-                _functions.remove(v);
-                i--;
-            }
-        }
-        for (int i = 0; i < _triggers.size(); i++) {
-            Trigger v = (Trigger) _triggers.get(i);
-            if (!filter.compliesWithNamingRuleObject(v.getName())) {
-                _triggers.remove(v);
-                i--;
-            }
-        }
-    }
-
-    public void moveTablesToModified() {
-        _modifiedTables.addAll(_tables);
-        _tables = new ArrayList();
-    }
-
-    public void moveModifiedToTables() {
-        _tables.addAll(_modifiedTables);
-        _modifiedTables = new ArrayList();
-    }
-
-    public void removeAllTables() {
-        _tables = new ArrayList();
-    }
-
-    public void filterByDataset(String datasetName) {
-        ArrayList oldTables = _tables;
-        _tables = new ArrayList();
-        DataSetService ds = DataSetService.getInstance();
-        List<DataSetTable> tables = ds.getDataSetTables(ds
-                .getDataSetByValue(datasetName));
-        for (DataSetTable table : tables) {
-            for (int i = 0; i < oldTables.size(); i++) {
-                Table t = (Table) oldTables.get(i);
-                if (t.getName().equalsIgnoreCase(
-                        table.getTable().getDBTableName())) {
-                    _tables.add(t);
-                }
-            }
-        }
-        _views = new ArrayList();
-        ArrayList oldTriggers = _triggers;
-        _triggers = new ArrayList();
-        for (int i = 0; i < oldTriggers.size(); i++) {
-            Trigger trigger = (Trigger) oldTriggers.get(i);
-            if (findTable(trigger.getTable()) != null)
-                _triggers.add(trigger);
-        }
-        // _functions=new ArrayList();
-        _sequences = new ArrayList();
-        _modifiedTables = new ArrayList();
-    }
+    // _functions=new ArrayList();
+    _sequences = new ArrayList();
+    _modifiedTables = new ArrayList();
+  }
 }
--- a/src/org/apache/ddlutils/platform/ModelLoaderBase.java	Thu Mar 26 17:00:07 2009 +0000
+++ b/src/org/apache/ddlutils/platform/ModelLoaderBase.java	Thu Apr 02 10:38:41 2009 +0000
@@ -241,7 +241,7 @@
     Database db = new Database();
     DataSetService ds = DataSetService.getInstance();
     List tablenames = readTableNames();
-    List<DataSetTable> tables = ds.getDataSetTables(ds.getDataSetByValue(datasetName));
+    List<DataSetTable> tables = ds.getDataSetByValue(datasetName).getDataSetTableList();
     for (DataSetTable table : tables) {
       Iterator it = tablenames.iterator();
       while (it.hasNext()) {
--- a/src/org/openbravo/ddlutils/task/ExportDataXMLMod.java	Thu Mar 26 17:00:07 2009 +0000
+++ b/src/org/openbravo/ddlutils/task/ExportDataXMLMod.java	Thu Apr 02 10:38:41 2009 +0000
@@ -102,7 +102,7 @@
       for (final String dataSetCode : datasets) {
         final DataSet dataSet = datasetService.getDataSetByValue(dataSetCode);
         System.out.println(dataSet);
-        final List<DataSetTable> tableList = datasetService.getDataSetTables(dataSet);
+        final List<DataSetTable> tableList = dataSet.getDataSetTableList();
         for (int i = 0; i < util.getActiveModuleCount(); i++) {
           if (module == null || module.equals("%")
               || util.isIncludedInExportList(util.getActiveModule(i))) {
--- a/src/org/openbravo/ddlutils/task/ExportDatabase.java	Thu Mar 26 17:00:07 2009 +0000
+++ b/src/org/openbravo/ddlutils/task/ExportDatabase.java	Thu Apr 02 10:38:41 2009 +0000
@@ -42,330 +42,278 @@
  */
 public class ExportDatabase extends BaseDalInitializingTask {
 
-    private String excludeobjects = "org.apache.ddlutils.platform.ExcludeFilter";
-    private String codeRevision;
+  private String excludeobjects = "org.apache.ddlutils.platform.ExcludeFilter";
+  private String codeRevision;
 
-    private File model;
-    private File moduledir;
-    private String module;
-    private File output;
-    private String encoding = "UTF-8";
-    private boolean force = false;
-    private boolean validateModel = true;
+  private File model;
+  private File moduledir;
+  private String module;
+  private File output;
+  private String encoding = "UTF-8";
+  private boolean force = false;
+  private boolean validateModel = true;
 
-    /** Creates a new instance of ExportDatabase */
-    public ExportDatabase() {
+  /** Creates a new instance of ExportDatabase */
+  public ExportDatabase() {
+  }
+
+  @Override
+  public void doExecute() {
+    getLog().info("Database connection: " + getUrl() + ". User: " + getUser());
+
+    final BasicDataSource ds = new BasicDataSource();
+    ds.setDriverClassName(getDriver());
+    ds.setUrl(getUrl());
+    ds.setUsername(getUser());
+    ds.setPassword(getPassword());
+
+    final Platform platform = PlatformFactory.createNewPlatformInstance(ds);
+    // platform.setDelimitedIdentifierModeOn(true);
+
+    boolean hasBeenModified = DBSMOBUtil.getInstance().hasBeenModified(platform, true);
+    if (!hasBeenModified && !force) {
+      getLog()
+          .info(
+              "Database doesn't have local changes. We will not export changes. If you want to force the export, do: ant export.database -Dforce=yes");
+      return;
     }
 
-    @Override
-    public void doExecute() {
-        getLog().info(
-                "Database connection: " + getUrl() + ". User: " + getUser());
+    DBSMOBUtil.verifyRevision(platform, getCodeRevision(), getLog());
 
-        final BasicDataSource ds = new BasicDataSource();
-        ds.setDriverClassName(getDriver());
-        ds.setUrl(getUrl());
-        ds.setUsername(getUser());
-        ds.setPassword(getPassword());
+    try {
+      final DBSMOBUtil util = DBSMOBUtil.getInstance();
+      util.getModules(platform, excludeobjects);
+      if (util.getActiveModuleCount() == 0) {
+        getLog()
+            .info(
+                "No active modules. For a module to be exported, it needs to be set as 'InDevelopment'");
+        return;
+      }
+      Database db;
+      if (module == null || module.equals("%")) {
+        db = platform.loadModelFromDatabase(DatabaseUtils.getExcludeFilter(excludeobjects));
 
-        final Platform platform = PlatformFactory.createNewPlatformInstance(ds);
-        // platform.setDelimitedIdentifierModeOn(true);
+        for (int i = 0; i < util.getActiveModuleCount(); i++) {
+          getLog().info("Exporting module: " + util.getActiveModule(i).name);
+          Database dbI = null;
+          try {
+            dbI = (Database) db.clone();
+          } catch (final Exception e) {
+            System.out.println("Error while cloning the database model" + e.getMessage());
+            return;
+          }
+          dbI.applyNamingConventionFilter(util.getActiveModule(i).filter);
+          getLog().info(db.toString());
+          final DatabaseIO io = new DatabaseIO();
+          if (validateModel)
+            validateDatabaseForModule(util.getActiveModule(i).idMod, dbI);
 
-        boolean hasBeenModified = DBSMOBUtil.getInstance().hasBeenModified(
-                platform, true);
-        if (!hasBeenModified && !force) {
-            getLog()
-                    .info(
-                            "Database doesn't have local changes. We will not export changes. If you want to force the export, do: ant export.database -Dforce=yes");
-            return;
+          if (util.getActiveModule(i).name.equalsIgnoreCase("CORE")) {
+            getLog().info("Path: " + model.getAbsolutePath());
+            io.writeToDir(dbI, model);
+          } else {
+            final File path = new File(moduledir, util.getActiveModule(i).dir
+                + "/src-db/database/model/");
+            getLog().info("Path: " + path);
+            io.writeToDir(dbI, path);
+          }
         }
+      } else {
+        getLog().info("Loading models for AD and RD Datasets");
+        db = platform.loadModelFromDatabase(DatabaseUtils.getExcludeFilter(excludeobjects), "AD");
+        final Database dbrd = platform.loadModelFromDatabase(DatabaseUtils
+            .getExcludeFilter(excludeobjects), "ADRD");
+        db.mergeWith(dbrd);
 
-        DBSMOBUtil.verifyRevision(platform, getCodeRevision(), getLog());
+        if (module != null && !module.equals("%"))
+          util.getIncDependenciesForModuleList(module);
 
-        try {
-            final DBSMOBUtil util = DBSMOBUtil.getInstance();
-            util.getModules(platform, excludeobjects);
-            if (util.getActiveModuleCount() == 0) {
-                getLog()
-                        .info(
-                                "No active modules. For a module to be exported, it needs to be set as 'InDevelopment'");
-                return;
+        for (int i = 0; i < util.getActiveModuleCount(); i++) {
+          final ModuleRow row = util.getActiveModule(i);
+          if (util.isIncludedInExportList(row)) {
+            if (row == null)
+              throw new BuildException("Module not found in AD_MODULE table.");
+            if (row.prefixes.size() == 0) {
+              getLog().info("Module doesn't have dbprefix. We will not export structure for it.");
+              return;
             }
-            Database db;
-            if (module == null || module.equals("%")) {
-                db = platform.loadModelFromDatabase(DatabaseUtils
-                        .getExcludeFilter(excludeobjects));
+            getLog().info("Exporting module: " + row.name);
+            if (row.isInDevelopment != null && row.isInDevelopment.equalsIgnoreCase("Y")) {
+              getLog().info("Loading submodel from database...");
+              db = platform
+                  .loadModelFromDatabase(row.filter, row.prefixes.get(0), false, row.idMod);
 
-                for (int i = 0; i < util.getActiveModuleCount(); i++) {
-                    getLog()
-                            .info(
-                                    "Exporting module: "
-                                            + util.getActiveModule(i).name);
-                    Database dbI = null;
-                    try {
-                        dbI = (Database) db.clone();
-                    } catch (final Exception e) {
-                        System.out
-                                .println("Error while cloning the database model"
-                                        + e.getMessage());
-                        return;
-                    }
-                    dbI
-                            .applyNamingConventionFilter(util
-                                    .getActiveModule(i).filter);
-                    getLog().info(db.toString());
-                    final DatabaseIO io = new DatabaseIO();
-                    if (validateModel)
-                        validateDatabaseForModule(
-                                util.getActiveModule(i).idMod, dbI);
+              validateDatabaseForModule(row.idMod, db);
 
-                    if (util.getActiveModule(i).name.equalsIgnoreCase("CORE")) {
-                        getLog().info("Path: " + model.getAbsolutePath());
-                        io.writeToDir(dbI, model);
-                    } else {
-                        final File path = new File(moduledir, util
-                                .getActiveModule(i).dir
-                                + "/src-db/database/model/");
-                        getLog().info("Path: " + path);
-                        io.writeToDir(dbI, path);
-                    }
+              getLog().info("Submodel loaded");
+              final DatabaseIO io = new DatabaseIO();
+              final File path = new File(moduledir, row.dir + "/src-db/database/model/");
+              getLog().info("Path: " + path);
+              io.writeToDir(db, path);
+            } else {
+              getLog().info(
+                  "Module is not in development. Check that it is, before trying to export it.");
+            }
+          }
+        }
+      }
+
+      final Vector<String> datasets = new Vector<String>();
+      datasets.add("AD");
+      datasets.add("ADRD");
+
+      final DataSetService datasetService = DataSetService.getInstance();
+      int datasetI = 0;
+
+      for (final String dataSetCode : datasets) {
+        final DataSet dataSet = datasetService.getDataSetByValue(dataSetCode);
+        System.out.println(dataSet);
+        final List<DataSetTable> tableList = dataSet.getDataSetTableList();
+        for (int i = 0; i < util.getActiveModuleCount(); i++) {
+          if (module == null || module.equals("%")
+              || util.isIncludedInExportList(util.getActiveModule(i))) {
+            getLog().info("Exporting module: " + util.getActiveModule(i).name);
+            getLog().info(db.toString());
+            final DatabaseDataIO dbdio = new DatabaseDataIO();
+            dbdio.setEnsureFKOrder(false);
+            if (util.getActiveModule(i).name.equalsIgnoreCase("CORE")) {
+              getLog().info("Path: " + output.getAbsolutePath());
+              // First we delete all .xml files in the directory
+
+              if (datasetI == 0) {
+                final File[] filestodelete = DatabaseIO.readFileArray(getOutput());
+                for (final File filedelete : filestodelete) {
+                  filedelete.delete();
                 }
+              }
+
+              for (final DataSetTable table : tableList) {
+                getLog().info("Exporting table: " + table.getTable().getDBTableName() + " to Core");
+                final OutputStream out = new FileOutputStream(new File(getOutput(), table
+                    .getTable().getDBTableName().toUpperCase()
+                    + ".xml"));
+                dbdio.writeDataForTableToXML(db, datasetService, dataSetCode, table, out,
+                    getEncoding(), util.getActiveModule(i).idMod);
+                out.flush();
+              }
             } else {
-                getLog().info("Loading models for AD and RD Datasets");
-                db = platform.loadModelFromDatabase(DatabaseUtils
-                        .getExcludeFilter(excludeobjects), "AD");
-                final Database dbrd = platform.loadModelFromDatabase(
-                        DatabaseUtils.getExcludeFilter(excludeobjects), "ADRD");
-                db.mergeWith(dbrd);
+              if (dataSetCode.equals("AD")) {
+                final File path = new File(moduledir, util.getActiveModule(i).dir
+                    + "/src-db/database/sourcedata/");
+                getLog().info("Path: " + path);
+                path.mkdirs();
+                if (datasetI == 0) {
+                  final File[] filestodelete = DatabaseIO.readFileArray(path);
+                  for (final File filedelete : filestodelete) {
+                    filedelete.delete();
+                  }
+                }
+                for (final DataSetTable table : tableList) {
+                  getLog().info(
+                      "Exporting table: " + table.getTable().getDBTableName() + " to module "
+                          + util.getActiveModule(i).name);
+                  final File tableFile = new File(path, table.getTable().getDBTableName()
+                      .toUpperCase()
+                      + ".xml");
+                  final OutputStream out = new FileOutputStream(tableFile);
+                  final boolean b = dbdio.writeDataForTableToXML(db, datasetService, dataSetCode,
+                      table, out, getEncoding(), util.getActiveModule(i).idMod);
+                  if (!b)
+                    tableFile.delete();
+                  out.flush();
+                }
+              }
+            }
+          }
+        }
+        datasetI++;
+      }
+    } catch (Exception e) {
+      e.printStackTrace();
+    }
+  }
 
-                if (module != null && !module.equals("%"))
-                    util.getIncDependenciesForModuleList(module);
+  private void validateDatabaseForModule(String moduleId, Database dbI) {
+    getLog().info("Validating Module...");
+    final Module moduleToValidate = OBDal.getInstance().get(Module.class, moduleId);
+    final SystemValidationResult result = SystemService.getInstance().validateDatabase(
+        moduleToValidate, dbI);
+    SystemService.getInstance().logValidationResult(log, result);
+    if (result.getErrors().size() > 0) {
+      throw new OBException(
+          "Module validation failed, see the above list of errors for more information");
+    }
+  }
 
-                for (int i = 0; i < util.getActiveModuleCount(); i++) {
-                    final ModuleRow row = util.getActiveModule(i);
-                    if (util.isIncludedInExportList(row)) {
-                        if (row == null)
-                            throw new BuildException(
-                                    "Module not found in AD_MODULE table.");
-                        if (row.prefixes.size() == 0) {
-                            getLog()
-                                    .info(
-                                            "Module doesn't have dbprefix. We will not export structure for it.");
-                            return;
-                        }
-                        getLog().info("Exporting module: " + row.name);
-                        if (row.isInDevelopment != null
-                                && row.isInDevelopment.equalsIgnoreCase("Y")) {
-                            getLog().info("Loading submodel from database...");
-                            db = platform.loadModelFromDatabase(row.filter,
-                                    row.prefixes.get(0), false, row.idMod);
+  public String getExcludeobjects() {
+    return excludeobjects;
+  }
 
-                            validateDatabaseForModule(row.idMod, db);
+  public void setExcludeobjects(String excludeobjects) {
+    this.excludeobjects = excludeobjects;
+  }
 
-                            getLog().info("Submodel loaded");
-                            final DatabaseIO io = new DatabaseIO();
-                            final File path = new File(moduledir, row.dir
-                                    + "/src-db/database/model/");
-                            getLog().info("Path: " + path);
-                            io.writeToDir(db, path);
-                        } else {
-                            getLog()
-                                    .info(
-                                            "Module is not in development. Check that it is, before trying to export it.");
-                        }
-                    }
-                }
-            }
+  public File getModel() {
+    return model;
+  }
 
-            final Vector<String> datasets = new Vector<String>();
-            datasets.add("AD");
-            datasets.add("ADRD");
+  public void setModel(File model) {
+    this.model = model;
+  }
 
-            final DataSetService datasetService = DataSetService.getInstance();
-            int datasetI = 0;
+  public String getCodeRevision() {
+    return codeRevision;
+  }
 
-            for (final String dataSetCode : datasets) {
-                final DataSet dataSet = datasetService
-                        .getDataSetByValue(dataSetCode);
-                System.out.println(dataSet);
-                final List<DataSetTable> tableList = datasetService
-                        .getDataSetTables(dataSet);
-                for (int i = 0; i < util.getActiveModuleCount(); i++) {
-                    if (module == null
-                            || module.equals("%")
-                            || util.isIncludedInExportList(util
-                                    .getActiveModule(i))) {
-                        getLog().info(
-                                "Exporting module: "
-                                        + util.getActiveModule(i).name);
-                        getLog().info(db.toString());
-                        final DatabaseDataIO dbdio = new DatabaseDataIO();
-                        dbdio.setEnsureFKOrder(false);
-                        if (util.getActiveModule(i).name
-                                .equalsIgnoreCase("CORE")) {
-                            getLog().info("Path: " + output.getAbsolutePath());
-                            // First we delete all .xml files in the directory
+  public void setCodeRevision(String rev) {
+    codeRevision = rev;
+  }
 
-                            if (datasetI == 0) {
-                                final File[] filestodelete = DatabaseIO
-                                        .readFileArray(getOutput());
-                                for (final File filedelete : filestodelete) {
-                                    filedelete.delete();
-                                }
-                            }
+  public File getModuledir() {
+    return moduledir;
+  }
 
-                            for (final DataSetTable table : tableList) {
-                                getLog().info(
-                                        "Exporting table: "
-                                                + table.getTable()
-                                                        .getDBTableName()
-                                                + " to Core");
-                                final OutputStream out = new FileOutputStream(
-                                        new File(getOutput(), table.getTable()
-                                                .getDBTableName().toUpperCase()
-                                                + ".xml"));
-                                dbdio.writeDataForTableToXML(db,
-                                        datasetService, dataSetCode, table,
-                                        out, getEncoding(), util
-                                                .getActiveModule(i).idMod);
-                                out.flush();
-                            }
-                        } else {
-                            if (dataSetCode.equals("AD")) {
-                                final File path = new File(moduledir, util
-                                        .getActiveModule(i).dir
-                                        + "/src-db/database/sourcedata/");
-                                getLog().info("Path: " + path);
-                                path.mkdirs();
-                                if (datasetI == 0) {
-                                    final File[] filestodelete = DatabaseIO
-                                            .readFileArray(path);
-                                    for (final File filedelete : filestodelete) {
-                                        filedelete.delete();
-                                    }
-                                }
-                                for (final DataSetTable table : tableList) {
-                                    getLog()
-                                            .info(
-                                                    "Exporting table: "
-                                                            + table
-                                                                    .getTable()
-                                                                    .getDBTableName()
-                                                            + " to module "
-                                                            + util
-                                                                    .getActiveModule(i).name);
-                                    final File tableFile = new File(path, table
-                                            .getTable().getDBTableName()
-                                            .toUpperCase()
-                                            + ".xml");
-                                    final OutputStream out = new FileOutputStream(
-                                            tableFile);
-                                    final boolean b = dbdio
-                                            .writeDataForTableToXML(
-                                                    db,
-                                                    datasetService,
-                                                    dataSetCode,
-                                                    table,
-                                                    out,
-                                                    getEncoding(),
-                                                    util.getActiveModule(i).idMod);
-                                    if (!b)
-                                        tableFile.delete();
-                                    out.flush();
-                                }
-                            }
-                        }
-                    }
-                }
-                datasetI++;
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
+  public void setModuledir(File moduledir) {
+    this.moduledir = moduledir;
+  }
 
-    private void validateDatabaseForModule(String moduleId, Database dbI) {
-        getLog().info("Validating Module...");
-        final Module moduleToValidate = OBDal.getInstance().get(Module.class,
-                moduleId);
-        final SystemValidationResult result = SystemService.getInstance()
-                .validateDatabase(moduleToValidate, dbI);
-        SystemService.getInstance().logValidationResult(log, result);
-        if (result.getErrors().size() > 0) {
-            throw new OBException(
-                    "Module validation failed, see the above list of errors for more information");
-        }
-    }
+  public String getModule() {
+    return module;
+  }
 
-    public String getExcludeobjects() {
-        return excludeobjects;
-    }
+  public void setModule(String module) {
+    this.module = module;
+  }
 
-    public void setExcludeobjects(String excludeobjects) {
-        this.excludeobjects = excludeobjects;
-    }
+  public File getOutput() {
+    return output;
+  }
 
-    public File getModel() {
-        return model;
-    }
+  public void setOutput(File output) {
+    this.output = output;
+  }
 
-    public void setModel(File model) {
-        this.model = model;
-    }
+  public String getEncoding() {
+    return encoding;
+  }
 
-    public String getCodeRevision() {
-        return codeRevision;
-    }
+  public void setEncoding(String encoding) {
+    this.encoding = encoding;
+  }
 
-    public void setCodeRevision(String rev) {
-        codeRevision = rev;
-    }
+  public boolean isForce() {
+    return force;
+  }
 
-    public File getModuledir() {
-        return moduledir;
-    }
+  public void setForce(boolean force) {
+    this.force = force;
+  }
 
-    public void setModuledir(File moduledir) {
-        this.moduledir = moduledir;
-    }
+  public boolean isValidateModel() {
+    return validateModel;
+  }
 
-    public String getModule() {
-        return module;
-    }
-
-    public void setModule(String module) {
-        this.module = module;
-    }
-
-    public File getOutput() {
-        return output;
-    }
-
-    public void setOutput(File output) {
-        this.output = output;
-    }
-
-    public String getEncoding() {
-        return encoding;
-    }
-
-    public void setEncoding(String encoding) {
-        this.encoding = encoding;
-    }
-
-    public boolean isForce() {
-        return force;
-    }
-
-    public void setForce(boolean force) {
-        this.force = force;
-    }
-
-    public boolean isValidateModel() {
-        return validateModel;
-    }
-
-    public void setValidateModel(boolean validateModel) {
-        this.validateModel = validateModel;
-    }
+  public void setValidateModel(boolean validateModel) {
+    this.validateModel = validateModel;
+  }
 }