/**
   * Create the ValueObject collection to hold the set of DomainObjects.
   *
   * @param map - maps DomainObjects to created ValueObjects
   * @param domainObjectSet - Set of ims.core.domain.objects.TransportBooking objects.
   */
  public static ims.RefMan.vo.TransportBookingShortVoCollection
      createTransportBookingShortVoCollectionFromTransportBooking(
          DomainObjectMap map, java.util.Set domainObjectSet) {
    ims.RefMan.vo.TransportBookingShortVoCollection voList =
        new ims.RefMan.vo.TransportBookingShortVoCollection();
    if (null == domainObjectSet) {
      return voList;
    }
    int rieCount = 0;
    int activeCount = 0;
    java.util.Iterator iterator = domainObjectSet.iterator();
    while (iterator.hasNext()) {
      ims.core.domain.objects.TransportBooking domainObject =
          (ims.core.domain.objects.TransportBooking) iterator.next();
      ims.RefMan.vo.TransportBookingShortVo vo = create(map, domainObject);

      if (vo != null) voList.add(vo);

      if (domainObject != null) {
        if (domainObject.getIsRIE() != null && domainObject.getIsRIE().booleanValue() == true)
          rieCount++;
        else activeCount++;
      }
    }
    voList.setRieCount(rieCount);
    voList.setActiveCount(activeCount);
    return voList;
  }
  /**
   * Create the ValueObject collection to hold the list of DomainObjects.
   *
   * @param map - maps DomainObjects to created ValueObjects
   * @param domainObjectList - List of ims.core.domain.objects.TransportBooking objects.
   */
  public static ims.RefMan.vo.TransportBookingShortVoCollection
      createTransportBookingShortVoCollectionFromTransportBooking(
          DomainObjectMap map, java.util.List domainObjectList) {
    ims.RefMan.vo.TransportBookingShortVoCollection voList =
        new ims.RefMan.vo.TransportBookingShortVoCollection();
    if (null == domainObjectList) {
      return voList;
    }
    int rieCount = 0;
    int activeCount = 0;
    for (int i = 0; i < domainObjectList.size(); i++) {
      ims.core.domain.objects.TransportBooking domainObject =
          (ims.core.domain.objects.TransportBooking) domainObjectList.get(i);
      ims.RefMan.vo.TransportBookingShortVo vo = create(map, domainObject);

      if (vo != null) voList.add(vo);

      if (domainObject != null) {
        if (domainObject.getIsRIE() != null && domainObject.getIsRIE().booleanValue() == true)
          rieCount++;
        else activeCount++;
      }
    }

    voList.setRieCount(rieCount);
    voList.setActiveCount(activeCount);
    return voList;
  }
  /**
   * Create the ValueObject from the ims.core.domain.objects.TransportBooking object.
   *
   * @param map DomainObjectMap of DomainObjects to already created ValueObjects.
   * @param domainObject
   */
  public static ims.RefMan.vo.TransportBookingShortVo create(
      DomainObjectMap map, ims.core.domain.objects.TransportBooking domainObject) {
    if (null == domainObject) {
      return null;
    }
    // check if the domainObject already has a valueObject created for it
    ims.RefMan.vo.TransportBookingShortVo valueObject =
        (ims.RefMan.vo.TransportBookingShortVo)
            map.getValueObject(domainObject, ims.RefMan.vo.TransportBookingShortVo.class);
    if (null == valueObject) {
      valueObject =
          new ims.RefMan.vo.TransportBookingShortVo(
              domainObject.getId(), domainObject.getVersion());
      map.addValueObject(domainObject, valueObject);

      valueObject = insert(map, valueObject, domainObject);
    }
    return valueObject;
  }
  public static ims.core.domain.objects.TransportBooking extractTransportBooking(
      ims.domain.ILightweightDomainFactory domainFactory,
      ims.RefMan.vo.TransportBookingShortVo valueObject,
      HashMap domMap) {
    if (null == valueObject) {
      return null;
    }
    Integer id = valueObject.getID_TransportBooking();
    ims.core.domain.objects.TransportBooking domainObject = null;
    if (null == id) {
      if (domMap.get(valueObject) != null) {
        return (ims.core.domain.objects.TransportBooking) domMap.get(valueObject);
      }
      // ims.RefMan.vo.TransportBookingShortVo ID_TransportBooking field is unknown
      domainObject = new ims.core.domain.objects.TransportBooking();
      domMap.put(valueObject, domainObject);
    } else {
      String key = (valueObject.getClass().getName() + "__" + valueObject.getID_TransportBooking());
      if (domMap.get(key) != null) {
        return (ims.core.domain.objects.TransportBooking) domMap.get(key);
      }
      domainObject =
          (ims.core.domain.objects.TransportBooking)
              domainFactory.getDomainObject(ims.core.domain.objects.TransportBooking.class, id);

      // TODO: Not sure how this should be handled. Effectively it must be a staleobject exception,
      // but maybe should be handled as that further up.
      if (domainObject == null) return null;

      domMap.put(key, domainObject);
    }
    domainObject.setVersion(valueObject.getVersion_TransportBooking());

    ims.scheduling.vo.Booking_AppointmentRefVoCollection refCollection1 =
        valueObject.getAppointments();
    int size1 = (null == refCollection1) ? 0 : refCollection1.size();
    java.util.Set domainAppointments1 = domainObject.getAppointments();
    if (domainAppointments1 == null) {
      domainAppointments1 = new java.util.HashSet();
    }
    java.util.Set newSet1 = new java.util.HashSet();
    for (int i = 0; i < size1; i++) {
      ims.scheduling.vo.Booking_AppointmentRefVo vo = refCollection1.get(i);
      ims.scheduling.domain.objects.Booking_Appointment dom = null;
      if (null != vo) {
        if (vo.getBoId() == null) {
          if (domMap.get(vo) != null) {
            dom = (ims.scheduling.domain.objects.Booking_Appointment) domMap.get(vo);
          }
        } else {
          dom =
              (ims.scheduling.domain.objects.Booking_Appointment)
                  domainFactory.getDomainObject(
                      ims.scheduling.domain.objects.Booking_Appointment.class, vo.getBoId());
        }
      }

      // Trying to avoid the hibernate collection being marked as dirty via its public interface
      // methods. (like add)
      if (!domainAppointments1.contains(dom)) {
        domainAppointments1.add(dom);
      }
      newSet1.add(dom);
    }
    java.util.Set removedSet1 = new java.util.HashSet();
    java.util.Iterator iter1 = domainAppointments1.iterator();
    // Find out which objects need to be removed
    while (iter1.hasNext()) {
      ims.domain.DomainObject o = (ims.domain.DomainObject) iter1.next();
      if ((o == null || o.getIsRIE() == null || !o.getIsRIE().booleanValue())
          && !newSet1.contains(o)) {
        removedSet1.add(o);
      }
    }
    iter1 = removedSet1.iterator();
    // Remove the unwanted objects
    while (iter1.hasNext()) {
      domainAppointments1.remove(iter1.next());
    }

    domainObject.setAppointments(domainAppointments1);
    java.util.Date value2 = null;
    ims.framework.utils.Date date2 = valueObject.getDateBooked();
    if (date2 != null) {
      value2 = date2.getDate();
    }
    domainObject.setDateBooked(value2);
    // This is to overcome a bug in both Sybase and Oracle which prevents them from storing an empty
    // string correctly
    // Sybase stores it as a single space, Oracle stores it as NULL. This fix will make them
    // consistent at least.
    if (valueObject.getBookingReference() != null && valueObject.getBookingReference().equals("")) {
      valueObject.setBookingReference(null);
    }
    domainObject.setBookingReference(valueObject.getBookingReference());
    ims.core.admin.domain.objects.CareContext value5 = null;
    if (null != valueObject.getCareContext()) {
      if (valueObject.getCareContext().getBoId() == null) {
        if (domMap.get(valueObject.getCareContext()) != null) {
          value5 =
              (ims.core.admin.domain.objects.CareContext) domMap.get(valueObject.getCareContext());
        }
      } else if (valueObject.getBoVersion()
          == -1) // RefVo was not modified since obtained from the Assembler, no need to update the
                 // BO field
      {
        value5 = domainObject.getCareContext();
      } else {
        value5 =
            (ims.core.admin.domain.objects.CareContext)
                domainFactory.getDomainObject(
                    ims.core.admin.domain.objects.CareContext.class,
                    valueObject.getCareContext().getBoId());
      }
    }
    domainObject.setCareContext(value5);

    return domainObject;
  }
  /**
   * Update the ValueObject with the Domain Object.
   *
   * @param map DomainObjectMap of DomainObjects to already created ValueObjects.
   * @param valueObject to be updated
   * @param domainObject ims.core.domain.objects.TransportBooking
   */
  public static ims.RefMan.vo.TransportBookingShortVo insert(
      DomainObjectMap map,
      ims.RefMan.vo.TransportBookingShortVo valueObject,
      ims.core.domain.objects.TransportBooking domainObject) {
    if (null == domainObject) {
      return valueObject;
    }
    if (null == map) {
      map = new DomainObjectMap();
    }

    valueObject.setID_TransportBooking(domainObject.getId());
    valueObject.setIsRIE(domainObject.getIsRIE());

    // If this is a recordedInError record, and the domainObject
    // value isIncludeRecord has not been set, then we return null and
    // not the value object
    if (valueObject.getIsRIE() != null
        && valueObject.getIsRIE().booleanValue() == true
        && !domainObject.isIncludeRecord()) return null;

    // If this is not a recordedInError record, and the domainObject
    // value isIncludeRecord has been set, then we return null and
    // not the value object
    if ((valueObject.getIsRIE() == null || valueObject.getIsRIE().booleanValue() == false)
        && domainObject.isIncludeRecord()) return null;

    // Appointments
    ims.scheduling.vo.Booking_AppointmentRefVoCollection Appointments =
        new ims.scheduling.vo.Booking_AppointmentRefVoCollection();
    for (java.util.Iterator iterator = domainObject.getAppointments().iterator();
        iterator.hasNext(); ) {
      ims.scheduling.domain.objects.Booking_Appointment tmp =
          (ims.scheduling.domain.objects.Booking_Appointment) iterator.next();
      if (tmp != null)
        Appointments.add(
            new ims.scheduling.vo.Booking_AppointmentRefVo(tmp.getId(), tmp.getVersion()));
    }
    valueObject.setAppointments(Appointments);
    // DateBooked
    java.util.Date DateBooked = domainObject.getDateBooked();
    if (null != DateBooked) {
      valueObject.setDateBooked(new ims.framework.utils.Date(DateBooked));
    }
    // BookingReference
    valueObject.setBookingReference(domainObject.getBookingReference());
    // SysInfo
    // set system information
    valueObject.setSysInfo(
        ims.vo.domain.SystemInformationAssembler.create(domainObject.getSystemInformation()));
    // CareContext
    if (domainObject.getCareContext() != null) {
      if (domainObject.getCareContext()
          instanceof
          HibernateProxy) // If the proxy is set, there is no need to lazy load, the proxy knows the
                          // id already.
      {
        HibernateProxy p = (HibernateProxy) domainObject.getCareContext();
        int id = Integer.parseInt(p.getHibernateLazyInitializer().getIdentifier().toString());
        valueObject.setCareContext(new ims.core.admin.vo.CareContextRefVo(id, -1));
      } else {
        valueObject.setCareContext(
            new ims.core.admin.vo.CareContextRefVo(
                domainObject.getCareContext().getId(), domainObject.getCareContext().getVersion()));
      }
    }
    return valueObject;
  }