/**
   * 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 model to the request attributes.
   *
   * @param enrollment the model to bind from
   * @param mv the model and view to bind to
   * @param readOnly true if the view is read only
   */
  public void bindToPage(
      CMSUser user, EnrollmentType enrollment, Map<String, Object> mv, boolean readOnly) {
    attr(mv, "bound", "Y");
    ProviderInformationType provider = XMLUtility.nsGetProvider(enrollment);

    ProviderStatementType statement = XMLUtility.nsGetProviderStatement(enrollment);
    attr(mv, "name", statement.getName());
    attr(mv, "title", statement.getTitle());
    attr(mv, "date", statement.getSignDate());

    AcceptedAgreementsType acceptedAgreements = provider.getAcceptedAgreements();

    ProviderType pt =
        getLookupService().findLookupByDescription(ProviderType.class, provider.getProviderType());
    List<AgreementDocument> docs = getLookupService().findRequiredDocuments(pt.getCode());
    int i = 0;
    for (AgreementDocument doc : docs) {
      attr(mv, "documentId", i, "" + doc.getId());
      attr(mv, "documentName", i, doc.getTitle());

      boolean agreed = false;
      boolean updatedVersion = false;

      if (acceptedAgreements != null) {
        List<ProviderAgreementType> agreements = acceptedAgreements.getProviderAgreement();
        for (ProviderAgreementType agreement : agreements) {
          if (doc.getType().equals(agreement.getAgreementDocumentType())) {
            if (String.valueOf(doc.getVersion()).equals(agreement.getAgreementDocumentVersion())) {
              agreed = true;
            } else {
              updatedVersion = true;
            }
            break;
          }
        }
      }

      attr(mv, "accepted", i, agreed ? "Y" : "N");
      attr(mv, "updatedVersion", i, updatedVersion ? "Y" : "N");
      i++;
    }

    attr(mv, "requiredAgreementsSize", docs.size());
  }