/**
  * Maps the given list by id.
  *
  * @param list the list to be mapped
  * @return the lookup map
  */
 private Map<String, AcceptedAgreements> mapAgreementsByDocumentId(List<AcceptedAgreements> list) {
   Map<String, AcceptedAgreements> map = new HashMap<String, AcceptedAgreements>();
   if (list != null) {
     for (AcceptedAgreements item : list) {
       map.put(String.valueOf(item.getAgreementDocument().getId()), item);
     }
   }
   return map;
 }
 /**
  * Filters the inactive agreements from the given list.
  *
  * @param accepted the current agreements
  * @param documentMap the active agreements
  * @return the inactive agreements
  */
 private List<AcceptedAgreements> filterOnlyInactive(
     List<AcceptedAgreements> accepted, Map<String, AgreementDocument> documentMap) {
   List<AcceptedAgreements> inactiveList = new ArrayList<AcceptedAgreements>();
   if (accepted != null) {
     for (AcceptedAgreements agreement : accepted) {
       String documentId = String.valueOf(agreement.getAgreementDocument().getId());
       if (documentMap.get(documentId) == null) { // no longer required for this provider
         inactiveList.add(agreement);
       }
     }
   }
   return inactiveList;
 }
  /**
   * Binds the fields of the form to the persistence model.
   *
   * @param enrollment the front end model
   * @param ticket the persistent model
   */
  public void bindToHibernate(EnrollmentType enrollment, Enrollment ticket) {
    ProviderInformationType provider = XMLUtility.nsGetProvider(enrollment);
    ProviderProfile profile = ticket.getDetails();

    List<AcceptedAgreements> hList = profile.getAgreements();

    ProviderType pt =
        getLookupService().findLookupByDescription(ProviderType.class, provider.getProviderType());
    List<AgreementDocument> activeList = getLookupService().findRequiredDocuments(pt.getCode());
    Map<String, AgreementDocument> documentMap = mapDocumentsById(activeList);

    // Retain any previously accepted agreements that is no longer shown in the page
    List<AcceptedAgreements> mergedList = filterOnlyInactive(hList, documentMap);
    Map<String, AcceptedAgreements> agreementsMap = mapAgreementsByDocumentId(hList);

    AcceptedAgreementsType acceptedAgreements = XMLUtility.nsGetAcceptedAgreements(enrollment);
    List<ProviderAgreementType> xList = acceptedAgreements.getProviderAgreement();

    for (ProviderAgreementType xAgreement : xList) { // for every item checked by theuser
      String documentId = xAgreement.getAgreementDocumentId();
      if (agreementsMap.get(documentId) != null) {
        // already accepted, just retain
        mergedList.add(agreementsMap.get(documentId));

      } else {
        // newly accepted
        AcceptedAgreements agreement = new AcceptedAgreements();
        agreement.setAcceptedDate(new Date());
        agreement.setAgreementDocument(documentMap.get(documentId));
        mergedList.add(agreement);
      }
    }

    profile.setAgreements(mergedList);

    ProviderStatementType xStatement = XMLUtility.nsGetProviderStatement(enrollment);
    ProviderStatement hStatement = profile.getStatement();
    if (hStatement == null) {
      hStatement = new ProviderStatement();
      profile.setStatement(hStatement);
    }

    hStatement.setName(xStatement.getName());
    hStatement.setTitle(xStatement.getTitle());
    hStatement.setDate(BinderUtils.toDate(xStatement.getSignDate()));
  }
  /**
   * Binds the fields of the persistence model to the front end xml.
   *
   * @param ticket the persistent model
   * @param enrollment the front end model
   */
  public void bindFromHibernate(Enrollment ticket, EnrollmentType enrollment) {
    ProviderInformationType provider = XMLUtility.nsGetProvider(enrollment);
    ProviderProfile profile = ticket.getDetails();

    ProviderType pt =
        getLookupService().findLookupByDescription(ProviderType.class, provider.getProviderType());
    List<AgreementDocument> activeList = getLookupService().findRequiredDocuments(pt.getCode());
    Map<String, AgreementDocument> documentMap = mapDocumentsById(activeList);

    AcceptedAgreementsType acceptedAgreements = XMLUtility.nsGetAcceptedAgreements(enrollment);
    ArrayList<ProviderAgreementType> xlist = new ArrayList<ProviderAgreementType>();

    List<AcceptedAgreements> hList = profile.getAgreements();
    for (AcceptedAgreements agreements : hList) {
      // bind only active items
      AgreementDocument document = agreements.getAgreementDocument();
      String documentId = String.valueOf(document.getId());
      if (documentMap.get(documentId) != null) {
        ProviderAgreementType type = new ProviderAgreementType();
        type.setAcceptedDate(BinderUtils.toCalendar(agreements.getAcceptedDate()));
        type.setAgreementDocumentId(documentId);
        type.setAgreementDocumentTitle(document.getTitle());
        type.setAgreementDocumentVersion(String.valueOf(document.getVersion()));
        type.setAgreementDocumentType(document.getType());
        xlist.add(type);
      }
    }

    acceptedAgreements.getProviderAgreement().clear();
    acceptedAgreements.getProviderAgreement().addAll(xlist);

    ProviderStatement hStatement = profile.getStatement();
    if (hStatement != null) {
      ProviderStatementType xStatement = XMLUtility.nsGetProviderStatement(enrollment);
      xStatement.setName(hStatement.getName());
      xStatement.setTitle(hStatement.getTitle());
      xStatement.setSignDate(BinderUtils.toCalendar(hStatement.getDate()));
    }
  }