English

Introduction

This article will explain how to develop a Java program executable from DB-MAIN. This module will access the DB-MAIN repository and generate a dictionary (in CSV format), which contains information about all properties of objects in a schema (see example below).

To do this, you need to install Eclipse with Java 1.8 and DB-MAIN on your computer.

 

schema DB-Main

 

Creating the Eclipse project

Launch Eclipse and go to the menu [New/Java project…]. Enter a project name (« Dictionary » in our example) and click the [Next] button.

new java project

Click the [Libraries] tab and add the jidbm.jar library (located in the bin directory of the installation directory of DB-MAIN) by clicking the [Add external JAR…] button.

Java settings

Finish the creation of your project by clicking the [Finish] button.

Creating the Dictionary plug-in

In your « Dictionary » project, got to the [New/Class] menu and enter the name « DictionaryGeneration » as class name.

Java class

Complete the creation of the class by clicking the [Finish] button. The complete source code for the Dictionary class is located at : http://www.rever.eu/db-main/files/Dictionary.java.

Edit the DictionaryGeneration class and create the method « runDBM() » (see below). This is the method called by DB-MAIN when executing a Java plug-in. If this method is not in the program, the execution will stop. The corresponding code is :

public class Dictionary {
  private static PrintWriter fw = null;
  /**
   * Method called by DB-MAIN to generate a CSV file containing an attribute
   * dictionary of current schema.
   * 
   * @throws IOException
   */
  public static void runDBM() throws IOException {
    // Open the DB-MAIN console
    new DBMConsole();
    // Create the DBMLibrary instance
    DBMLibrary lib = new DBMLibrary();
    // Get the current schema
    DBMSchema sch = lib.getCurrentDBMSchema();
    if (sch == null) {
      System.out.println("Error: Select a schema before.");
      return;
    } else {
      // Create a new CSV file
      JFileChooser fc = new JFileChooser("");
      int rVal = fc.showSaveDialog(null);
      if (rVal == JFileChooser.APPROVE_OPTION) {
        try {
          fw = new PrintWriter(fc.getSelectedFile());
        } catch (Exception wri) {
          System.out.println("Error: Impossible to write in the file "
              + fc.getSelectedFile().getName() + ".");
          return;
        }
      } else {
        return;
      }
      // Write labels of columns in the CSV file
      fw.println(
          "Owner,Attribute,Cardinalities,Type,Lenght,Decimal number");
      // For each entity type of the current schema, displays its
      // attributes
      DBMEntityType ent = sch.getFirstDataObjectEntityType();
      while (ent != null) {
        displayEntity(ent);
        ent = sch.getNextDataObjectEntityType(ent);
      }
      // Close the CSV file
      fw.close();
    }
  }
}

The instruction « new DBMConsole(); » allows to open a console in which will be displayed the different messages (System.out.println(...)). The instruction « DBMSchema sch = lib.getCurrentDBMSchema() » returns the current schema (the one that is visible in DB-MAIN).

To better understand how to navigate the DB-MAIN repository, here is the part that interests us to write the module :

schema

A schema is composed of data objects (« DBMDataObject ») among which :

  • entity types (« DBMEntityType »)
  • relationship types (« DBMRelationshipType »)
  • simple attributes (« DBMSimpleAttribute »)
  • and compound attributes (attributes decomposed into sub-attributes, « DBMCompoundAttribute »).

To browse the entity types of a schema, you must use the methods « getFirstDataObjectEntityType » (which gives the first entity type of the schema) and « getNextDataObjectEntityType » (which gives the next entity type of a given entity type).

To compile the code of the « runDBM » method, it is necessary to create the « displayEntity » method taking as argument the current entity type. Here is the corresponding code :

  /**
   * Method that for a given entity type browse all these attributes.
   * 
   * @param ent
   *            entity type to be processed
   */
  private static void displayEntity(DBMEntityType ent) {
    // Print the entity type name in console
    System.out.println("Entity type: " + ent.getName());
    // For each attribute, displays its properies
    DBMAttribute att = ent.getFirstAttribute();
    while (att != null) {
      displayAttribute(att, "", ent);
      att = ent.getNextAttribute(att);
    }
  }

This method scans the attributes of an entity type to display their properties. It uses the « displayAttribute » method taking as argument the current attribute, a prefix for the display of its name and of its owner. The code for this new method is :

  /**
   * Method that for a given attribute displays these properties and browse
   * its sub-attributes if it is a compound attribute.
   * 
   * @param att
   *            attribute to be processed
   * @param prefix
   *            prefix of attribute name
   * @param ent
   *            owner of attribute att
   */
  private static void displayAttribute(DBMAttribute att, String prefix,
      DBMEntityType ent) {
    // Write attribute owner name, its name with prefix and its
    // cardinalities in CSV file
    fw.print(ent.getName() + "," + prefix + att.getName() + ","
        + att.getMinimumCardinality() + "-"
        + att.getMaximumCardinality());
    if (att.isSimpleAttribute()) {// Simple attribute
      // Write simple attribute name in console
      System.out.println("Simple attribute: " + att.getName());
      // Write simple attribute type, lenght and number of decimals in CSV
      // file
      DBMSimpleAttribute siatt = (DBMSimpleAttribute) att;
      fw.println("," + siatt.getTypeName() + "," + siatt.getLength() + ","
          + siatt.getDecimalNumber());
    } else {
      // Write coumpound attribute type
      fw.println(",COMP_ATT");
      // Display sub-attributes of compound attribute
      displayCompoundAttribute((DBMCompoundAttribute) att, prefix, ent);
    }
  }

There are two types of attributes : simple attributes (« DBMSimpleAttribute ») and compound attributes (« DBMCompoundAttribute ») which in turn possess simple or compound attributes. The method « displayAttribute » writes the common properties of attributes in the CSV file and then tests the attribute type. If it's a simple attribute, the properties specific to this attribute type are written in the CSV file, otherwise, in the case of a compound attribute, these sub-attributes are reviewed and displayed in the CSV file. The code of the method « displayCompoundAttribute » is :

  /**
   * Method that, for a given compound attribute, browses its sub-attributes.
   * 
   * @param coatt
   *            compound attribute to be processed
   * @param prefix
   *            prefix used for sub-attributes
   * @param ent
   *            owner of coatt
   */
  private static void displayCompoundAttribute(DBMCompoundAttribute coatt,
      String prefix, DBMEntityType ent) {
    // Write compound attribute name in console
    System.out.println("Compound attribute: " + coatt.getName());
    // For each sub-attributes of coatt, display their properties
    DBMAttribute att = coatt.getFirstAttribute();
    while (att != null) {
      displayAttribute(att, prefix + coatt.getName() + ".", ent);
      att = coatt.getNextAttribute(att);
    }
  }

For each attribute of the compound attribute, you call on the method which displays the properties of the attributes in the CSV file.

The encoding of the program Java is now finished.

Running the plug-in

In order to test the Java program, you need to launch DB-MAIN. In the « About » window, click the [Library (Demo project)] button.

DB-Main launch box

Double-click the « Library/Conceptual] schema to open it (and thus make it current). To run the Java program, go to the [File/Execute plugin…] menu and open the « Dictionary.class » file which is in the « bin » directory of your « Dictionary » project.

DB-Main library

The DB-MAIN console will open with the window to create the CSV file. Give a name to the file and click the « OK » button. The Java program will write the debugging information in the console and create your CSV file with the list of all the current schema attributes as well as their properties.

DB-Main java console

Conclusion

You just created your first Java plug-in for DB-MAIN. Immediately, the possibilities of extending your favorite modeling tool come to mind.

To help you further develop plug-ins, you can consult the reference manual of the JIDBM (« JIDBM-reference-manual.pdf ») and its Javadoc (« JIDBM-javadoc ») distributed with DB-MAIN (either from the [Start/DB-MAIN…] menu under Windows, either from the « doc » directory of the installation directory of DB-MAIN under Linux).

In the plug-ins directory of the DB-MAIN distribution, there are also two Java plug-ins with their source code. They are :

« SQLGenerator.java » (« sql » directory) : simplified SQL script generator for database creation.

« StatisticGenerator.java » (« statistic » directory) : simplified generator of schema statistics.

You can try and modify these two plug-ins to suit your needs.

If you have any questions about this article or, more generally, about DB-MAIN, get in touch with us via our contact form or via our forum.

Topics: 

Writer: 

Jean Henrard, Jean-Marc Hick