/**
   * Binds the request to the model.
   *
   * @param enrollment the model to bind to
   * @param request the request containing the form fields
   * @throws BinderException if the format of the fields could not be bound properly
   */
  public List<BinderException> bindFromPage(
      CMSUser user, EnrollmentType enrollment, HttpServletRequest request) {
    List<BinderException> exceptions = new ArrayList<BinderException>();
    ProviderInformationType provider = XMLUtility.nsGetProvider(enrollment);

    ProviderStatementType statement = XMLUtility.nsGetProviderStatement(enrollment);
    statement.setName(param(request, "name"));
    statement.setTitle(param(request, "title"));
    try {
      statement.setSignDate(BinderUtils.getAsCalendar(param(request, "date")));
    } catch (BinderException e) {
      e.setAttribute(name("date"), param(request, "date"));
      exceptions.add(e);
    }

    ProviderType pt =
        getLookupService().findLookupByDescription(ProviderType.class, provider.getProviderType());
    List<AgreementDocument> docs = getLookupService().findRequiredDocuments(pt.getCode());

    List<ProviderAgreementType> xList = new ArrayList<ProviderAgreementType>();
    HashSet<String> agreed = new HashSet<String>();
    for (int i = 0; i < docs.size(); i++) {
      String accepted = param(request, "accepted", i);
      String documentId = param(request, "documentId", i);
      if (accepted != null) {
        agreed.add(documentId);
      }
    }

    for (AgreementDocument doc : docs) {
      String documentId = String.valueOf(doc.getId());
      if (agreed.contains(documentId)) {
        ProviderAgreementType item = new ProviderAgreementType();
        item.setAgreementDocumentId(documentId);
        item.setAcceptedDate(Calendar.getInstance());
        item.setAgreementDocumentTitle(doc.getTitle());
        item.setAgreementDocumentType(doc.getType());
        item.setAgreementDocumentVersion(String.valueOf(doc.getVersion()));
        xList.add(item);
      }
    }

    AcceptedAgreementsType acceptedAgreements = XMLUtility.nsGetAcceptedAgreements(enrollment);
    List<ProviderAgreementType> providerAgreement = acceptedAgreements.getProviderAgreement();
    synchronized (providerAgreement) {
      providerAgreement.clear();
      providerAgreement.addAll(xList);
    }

    return exceptions;
  }
  /**
   * 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());
  }
  /**
   * 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()));
    }
  }