private void insertProjects(SrVector children, Db db) throws DbException {
   db.beginTrans(Db.READ_TRANS);
   DbObject parent = db.getRoot();
   DbEnumeration dbEnum = parent.getComponents().elements(DbProject.metaClass);
   while (dbEnum.hasMoreElements())
     children.addElement(createPrimaryNode(parent, dbEnum.nextElement()));
   dbEnum.close();
   db.commitTrans();
 }
 private void propagateCommonItemValues(DbORCommonItemModel model, int options)
     throws DbException {
   DbRelationN relN = model.getComponents();
   DbEnumeration dbEnum = relN.elements(DbORCommonItem.metaClass);
   while (dbEnum.hasMoreElements()) {
     DbORCommonItem item = (DbORCommonItem) dbEnum.nextElement();
     propagateCommonItemValues(item, options);
   } // end while
   dbEnum.close();
 } // end propagateCommonItemValues()
  // Overridden
  protected void insertComponents(SrVector children, DbObject dbParent) throws DbException {
    SemanticalModel model = ApplicationContext.getSemanticalModel();

    DbEnumeration dbEnum = dbParent.getComponents().elements();
    while (dbEnum.hasMoreElements()) {
      DbObject dbo = dbEnum.nextElement();
      boolean isVisible = model.isVisibleOnScreen(dbParent, dbo, Explorer.class);
      if (isVisible) {
        children.addElement(createPrimaryNode(dbParent, dbo));
      } // end if
    } // end while
    dbEnum.close();
  } // end insertComponents()
  // this update is always safe
  public final void updateSelectionAction() throws DbException {
    ApplicationDiagram diag = ApplicationContext.getFocusManager().getActiveDiagram();
    Object[] objects = ApplicationContext.getFocusManager().getSelectedSemanticalObjects();
    if ((diag == null) || (objects.length != 1) || !(objects[0] instanceof DbORAssociationEnd)) {
      setSelectedIndex(-1);
      setEnabled(false);
      return;
    }

    // update values
    dbos.clear();

    DbORAssociationEnd end = (DbORAssociationEnd) objects[0];
    DbORPrimaryUnique selKey = end.getReferencedConstraint();

    DbORAbsTable tableSo = (DbORAbsTable) end.getOppositeEnd().getClassifier();
    DbEnumeration dbEnum = tableSo.getComponents().elements(DbORPrimaryUnique.metaClass);
    while (dbEnum.hasMoreElements()) {
      DbORPrimaryUnique key = (DbORPrimaryUnique) dbEnum.nextElement();
      dbos.add(key);
    }
    dbEnum.close();

    int sizeadjustment = (dbos.size() == 0) ? 1 : 2; // adding a separator
    // and an item for
    // removing the
    // referenced
    // constraint
    // if no keys, do not add a separator
    Object[] items = new Object[dbos.size() + sizeadjustment];

    items[0] = kNoReferencedConstraint;
    if (sizeadjustment == 2) {
      items[1] = null;
    }

    String tableName = tableSo.getName();
    for (int i = 0; i < dbos.size(); i++) {
      String name = ((DbORPrimaryUnique) dbos.get(i)).getName();
      items[i + sizeadjustment] =
          MessageFormat.format(kKeyNamePatern, new Object[] {tableName, name});
    }
    setDomainValues(items);

    if (selKey == null) setSelectedIndex(0);
    else setSelectedIndex(dbos.indexOf(selKey) + sizeadjustment);

    setEnabled(true);
  }
  // find, if any, the column of 'table' whose name is 'columnName'
  private DbORColumn findColumn(DbORTable table, String columnName) throws DbException {
    DbORColumn namedColumn = null;
    DbRelationN relN = table.getComponents();
    DbEnumeration enu = relN.elements(DbORColumn.metaClass);
    while (enu.hasMoreElements()) {
      DbORColumn c = (DbORColumn) enu.nextElement();
      if (columnName.equals(c.getName())) {
        namedColumn = c;
        break;
      }
    } // end while
    enu.close();

    return namedColumn;
  }
  private DbJVPackage findPackageByName(DbOOAbsPackage parent, String packageName)
      throws DbException {
    DbJVPackage foundPackage = null;

    DbRelationN relN = parent.getComponents();
    DbEnumeration enu = relN.elements(DbJVPackage.metaClass);
    while (enu.hasMoreElements()) {
      DbJVPackage pack = (DbJVPackage) enu.nextElement();
      String name = pack.getName();
      if (packageName.equals(name)) {
        foundPackage = pack;
        break;
      }
    } // end while
    enu.close();

    return foundPackage;
  }
  private DbORTable findParentTable(DbORTable t1) throws DbException {
    DbORTable parent = null;
    DbRelationN relN = t1.getAssociationEnds();
    DbEnumeration enu = relN.elements(DbORAssociationEnd.metaClass);
    while (enu.hasMoreElements()) {
      DbORAssociationEnd end = (DbORAssociationEnd) enu.nextElement();
      int mult = end.getMultiplicity().getValue();
      if (mult == SMSMultiplicity.EXACTLY_ONE) {
        DbORAssociationEnd oppEnd = end.getOppositeEnd();
        DbORAbsTable t = oppEnd.getClassifier();

        if (t instanceof DbORTable) {
          parent = (DbORTable) t;
          break;
        }
      } // end if
    } // end while
    enu.close();

    return parent;
  }
  private DbOOAdt findClassByName(DbJVPackage pack, String classname) throws DbException {
    DbJVClass foundClass = null;

    DbRelationN relN = (pack == null) ? m_classModel.getComponents() : pack.getComponents();
    DbEnumeration enu = relN.elements(DbJVClass.metaClass);
    while (enu.hasMoreElements()) {
      DbJVClass claz = (DbJVClass) enu.nextElement();
      String name = claz.getName();
      if (classname.equals(name)) {
        foundClass = claz;
        break;
      }
    } // end while
    enu.close();

    if (foundClass == null) {
      JVClassCategory catg = JVClassCategory.getInstance(JVClassCategory.CLASS);
      foundClass = (pack == null) ? new DbJVClass(m_classModel, catg) : new DbJVClass(pack, catg);
      foundClass.setName(classname);
      types.put(classname, foundClass);
    }

    return foundClass;
  } // end findClassByName()
  protected final void refresh(DynamicNode parentNode) throws DbException {
    if (ApplicationContext.getFocusManager().isGuiLocked()) return;

    if (parentNode == null) return;
    if (!parentNode.hasLoaded() || parentNode.isLeaf()) {
      if (parentNode.getUserObject() instanceof DbObject)
        updateNode((DbObject) parentNode.getUserObject());
      return;
    }
    Object userObject = parentNode.getUserObject();
    if (userObject == ROOT) {
      int count = getChildCount(parentNode);
      for (int i = 0; i < count; i++) {
        DynamicNode node = (DynamicNode) getChild(parentNode, i);
        userObject = node.getUserObject();
        if (userObject == DB_RAM) refresh(node);
        else {
          if (((Db) userObject).isValid()) {
            ((Db) userObject).beginTrans(Db.READ_TRANS);
            refresh(node);
            ((Db) userObject).commitTrans();
          } else {
            removeNode(node);
          }
        }
      }
      return;
    }
    if (userObject == DB_RAM) {
      int count = getChildCount(parentNode);
      for (int i = count - 1; i >= 0; i--) {
        DynamicNode node = (DynamicNode) getChild(parentNode, i);
        Db db = ((DbObject) node.getUserObject()).getDb();
        if (!db.isValid()) {
          removeNode(node);
          continue;
        }
        db.beginTrans(Db.READ_TRANS);
        refresh(node);
        // refresh the display text for the projects - we do not want to
        // apply a full update
        // using update(dbo) because we want to preserve the expanded
        // state for projects.
        node.setDisplayText(getDisplayText(null, (DbObject) node.getUserObject()));
        db.commitTrans();
      }
      // check for missing Db
      Db[] dbs = Db.getDbs();
      for (int i = 0; i < dbs.length; i++) {
        if (!dbs[i].isValid() || !(dbs[i] instanceof DbRAM)) continue;
        DynamicNode dbNode = getDynamicNode(parentNode, dbs[i], 0);
        if (dbNode != null) continue;
        dbs[i].beginTrans(Db.READ_TRANS);
        DbEnumeration dbEnum = dbs[i].getRoot().getComponents().elements(DbProject.metaClass);
        if (dbEnum.hasMoreElements()) {
          getDynamicNode(dbEnum.nextElement(), false);
        }
        dbEnum.close();
        dbs[i].commitTrans();
      }
      return;
    }

    SrVector children = new SrVector(10);
    if (userObject instanceof Db) {
      insertProjects(children, (Db) userObject);
      children.sort();
    } else if (((DbObject) userObject).isAlive()) {
      insertComponents(children, (DbObject) userObject);
      if (childrenAreSorted((DbObject) userObject))
        children.sort(getComparator((DbObject) userObject));
    }

    DynamicNode groupNode = null;
    int index = 0;
    int iGroup = 0;
    for (int i = 0; i < children.size(); i++) {
      DynamicNode srcNode = (DynamicNode) children.elementAt(i);
      GroupParams group = srcNode.getGroupParams();
      if (group.name == null) {
        refreshNode(srcNode, parentNode, index);
        index++;
      } else {
        if (groupNode == null || !groupNode.toString().equals(group.name)) {
          if (groupNode != null) deleteNodes(groupNode, iGroup);
          groupNode = getGroupNode(parentNode, group, index);
          if (groupNode == null) {
            groupNode = createGroupNode(group);
            insertNodeInto(groupNode, parentNode, index);
          } else if (groupNode != getChild(parentNode, index)) {
            removeNodeFromParent(groupNode);
            insertNodeInto(groupNode, parentNode, index);
          }
          index++;
          iGroup = 0;
        }
        refreshNode(srcNode, groupNode, iGroup);
        iGroup++;
      }
    }
    if (groupNode != null) deleteNodes(groupNode, iGroup);
    deleteNodes(parentNode, index);

    // Refresh subnodes in a separate pass to avoid interference from
    // automatic
    // adding of a primary node when adding a secondary node.
    refreshChildren(parentNode);
  }
  private Map<Byte, DbJVPrimitiveType> getJavaTypes() throws DbException {

    // if first call, build the map
    if (m_javaTypes == null) {
      m_javaTypes = new HashMap<Byte, DbJVPrimitiveType>();
      DbRelationN relN = m_project.getComponents();
      DbEnumeration enu = relN.elements(DbSMSBuiltInTypeNode.metaClass);
      while (enu.hasMoreElements()) {
        DbSMSBuiltInTypeNode typeNode = (DbSMSBuiltInTypeNode) enu.nextElement();
        DbRelationN relN2 = typeNode.getComponents();
        DbEnumeration enu2 = relN2.elements(DbSMSBuiltInTypePackage.metaClass);
        while (enu2.hasMoreElements()) {
          DbSMSBuiltInTypePackage pack = (DbSMSBuiltInTypePackage) enu2.nextElement();
          DbSMSTargetSystem ts = pack.getTargetSystem();
          if ("Java".equals(ts.getName())) {
            DbRelationN relN3 = pack.getComponents();
            DbEnumeration enu3 = relN3.elements(DbJVPrimitiveType.metaClass);
            while (enu3.hasMoreElements()) {
              DbJVPrimitiveType type = (DbJVPrimitiveType) enu3.nextElement();
              String name = type.getName();
              if ("boolean".equals(name)) {
                m_javaTypes.put(Constants.T_BOOLEAN, type);
              } else if ("byte".equals(name)) {
                m_javaTypes.put(Constants.T_BYTE, type);
              } else if ("short".equals(name)) {
                m_javaTypes.put(Constants.T_SHORT, type);
              } else if ("char".equals(name)) {
                m_javaTypes.put(Constants.T_CHAR, type);
              } else if ("int".equals(name)) {
                m_javaTypes.put(Constants.T_INT, type);
              } else if ("long".equals(name)) {
                m_javaTypes.put(Constants.T_LONG, type);
              } else if ("double".equals(name)) {
                m_javaTypes.put(Constants.T_DOUBLE, type);
              } else if ("float".equals(name)) {
                m_javaTypes.put(Constants.T_FLOAT, type);
              } // end if
            } // end while
            enu3.close();
          } // end if
          pack.getAlias();
        } // end while
        enu2.close();
      } // end while
      enu.close();
    } // end if

    return m_javaTypes;
  }
  private void propagateCommonItemValues(DbORCommonItem item, int options) throws DbException {

    // get mandatory properties
    DbORTypeClassifier type = item.getType();
    Integer len = item.getLength();
    Integer dec = item.getNbDecimal();
    boolean isNull = item.getNull();

    // get optional properties
    String name = item.getName();
    String physicalName = item.getPhysicalName();
    String alias = item.getAlias();
    String desc = item.getDescription();
    DbSMSStereotype stereotype = item.getUmlStereotype();

    // for each column
    DbRelationN relN = item.getColumns();
    DbEnumeration dbEnum = relN.elements(DbORColumn.metaClass);
    while (dbEnum.hasMoreElements()) {

      m_changesmade = true;

      // set mandatory properties
      DbORColumn column = (DbORColumn) dbEnum.nextElement();
      column.setType(type);
      column.setLength(len);
      column.setNbDecimal(dec);
      column.setNull(Boolean.valueOf(isNull));

      // set optional properties
      if ((options & NAME) != 0) {
        column.setName(name);
      }

      if ((options & PHYSICAL_NAME) != 0) {
        column.setPhysicalName(physicalName);
      }

      if ((options & ALIAS) != 0) {
        column.setAlias(alias);
      }

      if ((options & DESCRIPTION) != 0) {
        column.setDescription(desc);
      }

      if ((options & STEREOTYPE) != 0) {
        column.setUmlStereotype(stereotype);
      }
    } // end while
    dbEnum.close();

    // get object properties
    DbOOAdt ooType = item.getOoType();
    String typeUse = item.getTypeUse();
    OOTypeUseStyle style = item.getTypeUseStyle();
    JVVisibility visib = (JVVisibility) item.getVisibility();
    boolean isStatic = item.isStatic();
    boolean isFinal = item.isFinal();
    boolean isTransient = item.isTransient();
    boolean isVolatile = item.isVolatile();

    // for each field
    relN = item.getFields();
    dbEnum = relN.elements(DbJVDataMember.metaClass);
    while (dbEnum.hasMoreElements()) {

      m_changesmade = true;

      // set mandatory oo properties
      DbJVDataMember field = (DbJVDataMember) dbEnum.nextElement();
      field.setType(ooType);
      field.setTypeUse(typeUse);
      field.setTypeUseStyle(style);
      field.setVisibility(visib);
      field.setStatic(Boolean.valueOf(isStatic));
      field.setFinal(new Boolean(isFinal));
      field.setTransient(new Boolean(isTransient));
      field.setVolatile(new Boolean(isVolatile));

      // set optional properties
      if ((options & NAME) != 0) {
        field.setName(name);
      }

      if ((options & ALIAS) != 0) {
        field.setAlias(alias);
      }

      if ((options & DESCRIPTION) != 0) {
        field.setDescription(desc);
      }

      if ((options & STEREOTYPE) != 0) {
        field.setUmlStereotype(stereotype);
      }
    } // end while
    dbEnum.close();
  } // end propagateCommonItemValues()
  //
  // PRIVATE METHODS
  //
  private void propagateCommonItems() {
    g_options = getOptions();

    m_changesmade = false;

    try {
      m_semObjs[0].getDb().beginWriteTrans(m_title);
      int nb = m_semObjs.length;
      for (int i = 0; i < nb; i++) {
        DbObject semObj = m_semObjs[i];
        if (semObj instanceof DbORCommonItemModel) {
          DbORCommonItemModel model = (DbORCommonItemModel) semObj;
          propagateCommonItemValues(model, g_options);
        } else if (semObj instanceof DbORCommonItem) {
          DbORCommonItem item = (DbORCommonItem) semObj;
          propagateCommonItemValues(item, g_options);
        } // end if
        else if (semObj instanceof DbORDataModel) {
          DbObject tableOrView = null;
          DbEnumeration orTableEnum = semObj.getComponents().elements(DbORAbsTable.metaClass);
          while (orTableEnum.hasMoreElements()) {
            tableOrView = orTableEnum.nextElement();
            DbEnumeration orColumnEnum = tableOrView.getComponents().elements(DbORColumn.metaClass);
            DbORCommonItem item = null;
            while (orColumnEnum.hasMoreElements()) {
              DbORColumn parm = (DbORColumn) orColumnEnum.nextElement();
              item = (DbORCommonItem) parm.get(DbORColumn.fCommonItem);
              if (item != null) propagateCommonItemValues(item, parm, false, g_options);
            }
            orColumnEnum.close();
          }
          orTableEnum.close();
        } else if (semObj instanceof DbORColumn) {
          DbORCommonItem item = (DbORCommonItem) semObj.get(DbORColumn.fCommonItem);
          if (item != null) propagateCommonItemValues(item, semObj, false, g_options);
        } else if (semObj instanceof DbORAbsTable) {
          DbEnumeration orColumnEnum = semObj.getComponents().elements(DbORColumn.metaClass);
          DbORCommonItem item = null;
          while (orColumnEnum.hasMoreElements()) {
            DbORColumn parm = (DbORColumn) orColumnEnum.nextElement();
            item = (DbORCommonItem) parm.get(DbORColumn.fCommonItem);
            if (item != null) propagateCommonItemValues(item, parm, false, g_options);
          }
          orColumnEnum.close();
        } else if (semObj instanceof DbOOClassModel) {
          DbObject tableOrView = null;
          DbEnumeration orTableEnum = semObj.getComponents().elements(DbOOClass.metaClass);
          while (orTableEnum.hasMoreElements()) {
            tableOrView = orTableEnum.nextElement();
            DbEnumeration orColumnEnum =
                tableOrView.getComponents().elements(DbJVDataMember.metaClass);
            DbORCommonItem item = null;
            while (orColumnEnum.hasMoreElements()) {
              DbJVDataMember parm = (DbJVDataMember) orColumnEnum.nextElement();
              item = (DbORCommonItem) parm.get(DbJVDataMember.fCommonItem);
              if (item != null) propagateCommonItemValues(item, parm, true, g_options);
            }
            orColumnEnum.close();
          }
          orTableEnum.close();
        } else if (semObj instanceof DbOOClass) {
          DbEnumeration orColumnEnum = semObj.getComponents().elements(DbJVDataMember.metaClass);
          DbORCommonItem item = null;
          while (orColumnEnum.hasMoreElements()) {
            DbJVDataMember parm = (DbJVDataMember) orColumnEnum.nextElement();
            item = (DbORCommonItem) parm.get(DbJVDataMember.fCommonItem);
            if (item != null) propagateCommonItemValues(item, parm, true, g_options);
          }
          orColumnEnum.close();
        } else if (semObj instanceof DbJVDataMember) {
          DbORCommonItem item = (DbORCommonItem) semObj.get(DbJVDataMember.fCommonItem);
          if (item != null) propagateCommonItemValues(item, semObj, true, g_options);
        }
      } // end for
      m_semObjs[0].getDb().commitTrans();

      if (m_changesmade == false)
        JOptionPane.showMessageDialog(
            this,
            LocaleMgr.screen.getString("NoCommonItemToPropagate"),
            ApplicationContext.getApplicationName(),
            JOptionPane.INFORMATION_MESSAGE);

    } catch (Exception e) {
      org.modelsphere.jack.util.ExceptionHandler.processUncatchedException(
          ApplicationContext.getDefaultMainFrame(), e);
    }
  } // end propagateCommonItems()
  // add childrenMetaClass to filter only children of that class
  // if null, scan each child
  protected boolean expandMetaRelationN(
      Writer output,
      DbObject object,
      MetaRelationship metaRelation,
      boolean state[],
      MetaClass childrenMetaClass,
      RuleOptions options)
      throws DbException, IOException, RuleException {

    boolean expanded = false;
    DbEnumeration dbEnumChildren = null;
    DbRelationN relationN = (DbRelationN) object.get(metaRelation);
    if (childrenMetaClass == null) {
      dbEnumChildren = relationN.elements();
    } else {
      dbEnumChildren = relationN.elements(childrenMetaClass);
    }

    // a first pass to find out the number of children
    int nbChildren = 0;
    try {
      while (dbEnumChildren.hasMoreElements()) {
        dbEnumChildren.nextElement();
        nbChildren++;

        // Cancel everything if the user has decided to stop the
        // operation
        if (options != null) {
          Controller controller = options.m_controller;
          if (controller != null) {
            boolean can_continue = controller.checkPoint();
            if (!can_continue) {
              throw new RuleException(controller.getAbortedString());
            }
          }
        } // end if
      } // end while
    } finally {
      dbEnumChildren.close();
    }

    // cannot rollback to the first element, so recreate the enumeration
    if (childrenMetaClass == null) {
      dbEnumChildren = relationN.elements();
    } else {
      dbEnumChildren = relationN.elements(childrenMetaClass);
    }

    try {
      while (dbEnumChildren.hasMoreElements()) {
        DbObject child = (DbObject) dbEnumChildren.nextElement();
        MetaClass metaClass = child.getMetaClass();
        String className = metaClass.getGUIName();
        boolean excluded = false;

        // check if child's class is excluded
        if (options != null) {
          excluded = options.isExcluded(className);

          // if not, check if parent's connection is excluded
          if (!excluded) {
            metaClass = object.getMetaClass();
            String parentClassName = metaClass.getGUIName();
            String connectionName = parentClassName + "." + metaRelation.getGUIName();
            excluded = options.isExcluded(connectionName);
          } // end if
        } // end if

        if (!excluded) {
          expanded |= expandChild(output, object, child, nbChildren, state, options);
        } // end if
      } // end while
    } finally {
      dbEnumChildren.close();
    }

    boolean atLeastOneChildPrinted = state[1];

    if ((atLeastOneChildPrinted) && (suffixModifier != null)) {
      expanded |= suffixModifier.expand(output, object, options);
    } else if ((!atLeastOneChildPrinted) && (nullModifier != null)) {
      expanded |= nullModifier.expand(output, object, options);
    }

    return expanded;
  } // end expandMetaRelationN