/**
  * Test whether there is a refinement relationship between a pair of responsibilities
  *
  * @param resp The source responsibility
  * @param leaf The target responsibility
  */
 public boolean existRefinement(ArchEResponsibilityVO resp, ArchEResponsibilityVO leaf) {
   ArchERefinementRelationVO rel = null;
   for (Iterator<ArchERefinementRelationVO> it = refVOs.iterator(); it.hasNext(); ) {
     rel = it.next(); // TODO: Check if an equal 'by name' is needed below
     ArchEResponsibilityVO parent = rel.getParent();
     ArchEResponsibilityVO child = rel.getChild();
     if ((parent != null) && (child != null) && parent.equals(resp) && child.equals(leaf))
       return (true);
   }
   return (false);
 }
  /**
   * Test if a given responsibility has been refined
   *
   * @param responsibility The parent responsibility
   */
  public boolean isLeaf(ArchEResponsibility responsibility) {
    ArchERefinementRelationVO item = null;
    for (Iterator<ArchERefinementRelationVO> it = refVOs.iterator(); it.hasNext(); ) {
      item = it.next();
      // TODO: Check if an equal 'by name' works better here
      ArchEResponsibility parent = item.getParent();
      if ((parent != null) && parent.equals(responsibility) && item.getChild() != null)
        return (false);
    }

    return (true);
  }
  /**
   * Return the refinements (children responsibilities) of a given responsibility
   *
   * @param parentResponsibility The parent responsibility
   */
  public List<ArchEResponsibility> getChildrenResponsibilities(
      ArchEResponsibility parentResponsibility) {
    List<ArchEResponsibility> children = new ArrayList<ArchEResponsibility>();

    ArchERefinementRelationVO item = null;
    for (Iterator<ArchERefinementRelationVO> it = refVOs.iterator(); it.hasNext(); ) {
      item = it.next();
      // TODO: Check if an equal 'by name' works better here
      ArchEResponsibility parent = item.getParent();
      if ((parent != null) && parent.equals(parentResponsibility) && item.getChild() != null)
        children.add(item.getChild());
    }
    return (children);
  }
  /**
   * Establish a refinement relation between two existing responsibilities
   *
   * @param parent The source responsibility
   * @param child The target responsibility
   */
  public boolean refineResponsibility(ArchEResponsibilityVO parent, ArchEResponsibilityVO child) {

    ArchERelation rel = this.getRelation(parent, child, ArchERefinementRelationVO.class.getName());

    if (rel == null) { // There's no previous refinement between the two responsibilities

      ArchERefinementRelationVO refinement = new ArchERefinementRelationVO(this.version);
      refinement.setParent(parent);
      refinement.setChild(child);
      refVOs.add(refinement);

      // The child in the refinement relation "inherits" the scenario
      // translations from its parent responsibility
      ArchETranslationRelationVO translation = null;
      ArchETranslationRelationVO newTranslation = null;
      ArchEScenarioVO baseScenario = null;
      List<ArchETranslationRelationVO> derivedTranslations =
          new ArrayList<ArchETranslationRelationVO>();
      for (Iterator<ArchETranslationRelationVO> it = trVOs.iterator(); it.hasNext(); ) {
        translation = it.next();

        baseScenario = translation.getParent();
        if (parent.equals(translation.getChild()) && !this.existTranslation(baseScenario, child)) {
          // A translation relation exists for the parent responsibility
          newTranslation = new ArchETranslationRelationVO(this.version);
          newTranslation.setParent(baseScenario);
          newTranslation.setChild(child);
          derivedTranslations.add(newTranslation);
        }
      }

      for (Iterator<ArchETranslationRelationVO> it = derivedTranslations.iterator(); it.hasNext(); )
        trVOs.add(it.next());

      return (true);
    }

    return (false);
  }
  /**
   * Save AS all the VOs related to this core responsibility structure
   *
   * @param newVersion The version for the new VOs
   * @throws ArchEException
   */
  public void saveAs(ArchEVersionVO newVersion) throws ArchEException {
    Session openSession = ArchECoreDataProvider.getSessionFactory().getCurrentSession();
    if (openSession != null && openSession.isConnected() && openSession.isOpen()) {
      try {

        // Save the newly-created raw responsibility structure
        rawRSVO.setId(newVersion.getId());

        ArchEResponsibilityVO itemResp = null;
        for (Iterator<ArchEResponsibilityVO> it = rawRSVO.getResponsibilities().iterator();
            it.hasNext(); ) {
          itemResp = it.next();
          itemResp.setVersion(newVersion);
          openSession.save(itemResp);
          this.saveParametersForResponsibilityAs(newVersion, itemResp, openSession);
        }

        // Create a copy of the existing translation relations
        ArchETranslationRelationVO itemTr = null;
        for (Iterator<ArchETranslationRelationVO> it = trVOs.iterator(); it.hasNext(); ) {
          itemTr = it.next();
          itemTr.setVersion(newVersion);
          openSession.save(itemTr);
        }

        // Create a copy of the existing refinement relations
        ArchERefinementRelationVO itemRef = null;
        for (Iterator<ArchERefinementRelationVO> it = refVOs.iterator(); it.hasNext(); ) {
          itemRef = it.next();
          itemRef.setVersion(newVersion);
          openSession.save(itemRef);
        }

      } catch (HibernateException ex) {
        throw new ArchEException(ex.getMessage(), ex.getCause());
      }
    }
  }