/** Sets Interactor External References. Filters out any redundant external references. */
  private XrefType createExternalRefs(AttributeBag bag) {
    HashSet set = new HashSet();
    ExternalReference[] refs = bag.getExternalRefs();
    XrefType xref = new XrefType();

    if ((refs != null) && (refs.length > 0)) {
      //  Add Primary Reference
      createPrimaryKey(refs[0], xref);

      //  All others become Secondary References
      if (refs.length > 1) {
        for (int i = 1; i < refs.length; i++) {
          String key = this.generateXRefKey(refs[i]);

          if (!set.contains(key)) {
            createSecondaryKey(refs[i], xref);
            set.add(key);
          }
        }
      }
    }

    if (xref.getPrimaryRef() != null) {
      return xref;
    } else {
      return null;
    }
  }
  /**
   * Creates a Primary Reference.
   *
   * @param database Database.
   * @param id ID String.
   * @return Castor XRef object
   */
  private XrefType createXRef(String database, String id) {
    XrefType xref = new XrefType();
    DbReferenceType primaryRef = new DbReferenceType();
    primaryRef.setDb(database);
    primaryRef.setId(id);
    xref.setPrimaryRef(primaryRef);

    return xref;
  }
 /**
  * Creates Secondary Key.
  *
  * @param ref External Reference
  * @param xref Castro XRef.
  */
 private void createSecondaryKey(ExternalReference ref, XrefType xref) {
   DbReferenceType secondaryRef = new DbReferenceType();
   secondaryRef.setDb(ref.getDatabase());
   secondaryRef.setId(ref.getId());
   xref.getSecondaryRef().add(secondaryRef);
 }
 /**
  * Creates Primary Key.
  *
  * @param ref External Reference.
  * @param xref Castor XRef.
  */
 private void createPrimaryKey(ExternalReference ref, XrefType xref) {
   DbReferenceType primaryRef = new DbReferenceType();
   primaryRef.setDb(ref.getDatabase());
   primaryRef.setId(ref.getId());
   xref.setPrimaryRef(primaryRef);
 }