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