/**
  * This function returns the role Id from hibernate mapping and returns the value
  *
  * @param attributeName
  * @return roleKeyId
  */
 public static String getRoleKeyId(String attributeName) {
   net.sf.hibernate.mapping.Collection col1 = cfg.getCollectionMapping(attributeName);
   Iterator colIt = col1.getElement().getColumnIterator();
   while (colIt.hasNext()) {
     Column col = (Column) colIt.next();
     return (col.getName());
   }
   return "";
 }
  /**
   * This Function finds all the relations in i.e Many-To-Many and One-To-Many All the relations are
   * kept in HashMap where key is formed as table1@table2@table_name@attributeName and value is
   * Many-To-Many or One-To-Many
   *
   * @return Map
   */
  private static void findRelations() {
    try {
      Iterator itr1 = cfg.getCollectionMappings();

      while (itr1.hasNext()) {
        Collection col = (Collection) itr1.next();

        if (col.getElement().getClass().getName().equals("net.sf.hibernate.mapping.ManyToOne")) {
          saveRelations(col, "ManyToMany");
        } else {
          saveRelations(col, "OneToMany");
        }
      }
    } catch (Exception e) {
      Logger.out.info("Error occured in fildAllRelations Function:" + e);
    }
  }
  public static void getDATA(Class classObj) {
    net.sf.hibernate.mapping.Collection coll =
        cfg.getCollectionMapping(
            "edu.wustl.catissuecore.domain.CollectionProtocolEvent.specimenRequirementCollection");
    // System.out.println(map);

    System.out.println(coll.getCollectionTable().getName());
    System.out.println(coll.getTable().getName());
    // System.out.println();

    Iterator it = coll.getColumnIterator();

    while (it.hasNext()) {
      // net.sf.hibernate.mapping.Set set = (net.sf.hibernate.mapping.Set)it.next();
      System.out.println(it.next());
    }
  }
  /**
   * This function saves the relation data in HashSet.
   *
   * @param col this is the collection which contains all data
   * @param rel_type this is Many-To-Many ot Many-To-One
   * @throws Exception
   */
  private static void saveRelations(Collection col, String rel_type) throws Exception {
    String className = col.getOwnerClass().getName();
    String relatedClassName = col.getElement().getType().getName();
    String roleAttribute = col.getRole();
    String relationType = rel_type;
    String relationTable = col.getElement().getTable().getName();
    String keyId = getKeyId(roleAttribute);
    String roleId = getRoleKeyId(roleAttribute);

    ClassRelationshipData hmc =
        new ClassRelationshipData(
            className, relatedClassName, roleAttribute, relationType, relationTable, keyId, roleId);
    mappings.add(hmc);

    List list1 = HibernateMetaData.getSubClassList(col.getOwnerClass().getName());
    for (int i = 0; i < list1.size(); i++) {
      hmc =
          new ClassRelationshipData(
              list1.get(i).toString(),
              relatedClassName,
              roleAttribute,
              relationType,
              relationTable,
              keyId,
              roleId);
      mappings.add(hmc);
    }

    List list2 = HibernateMetaData.getSubClassList(col.getElement().getType().getName());
    for (int i = 0; i < list2.size(); i++) {
      hmc =
          new ClassRelationshipData(
              className,
              list2.get(i).toString(),
              roleAttribute,
              relationType,
              relationTable,
              keyId,
              roleId);
      mappings.add(hmc);
    }
  }