/**
   * Create the ValueObject collection to hold the list of DomainObjects.
   *
   * @param map - maps DomainObjects to created ValueObjects
   * @param domainObjectList - List of ims.scheduling.domain.objects.Sch_Session objects.
   */
  public static ims.scheduling.vo.SessionManagementVoCollection
      createSessionManagementVoCollectionFromSch_Session(
          DomainObjectMap map, java.util.List domainObjectList) {
    ims.scheduling.vo.SessionManagementVoCollection voList =
        new ims.scheduling.vo.SessionManagementVoCollection();
    if (null == domainObjectList) {
      return voList;
    }
    int rieCount = 0;
    int activeCount = 0;
    for (int i = 0; i < domainObjectList.size(); i++) {
      ims.scheduling.domain.objects.Sch_Session domainObject =
          (ims.scheduling.domain.objects.Sch_Session) domainObjectList.get(i);
      ims.scheduling.vo.SessionManagementVo 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 set of DomainObjects.
   *
   * @param map - maps DomainObjects to created ValueObjects
   * @param domainObjectSet - Set of ims.scheduling.domain.objects.Sch_Session objects.
   */
  public static ims.scheduling.vo.SessionManagementVoCollection
      createSessionManagementVoCollectionFromSch_Session(
          DomainObjectMap map, java.util.Set domainObjectSet) {
    ims.scheduling.vo.SessionManagementVoCollection voList =
        new ims.scheduling.vo.SessionManagementVoCollection();
    if (null == domainObjectSet) {
      return voList;
    }
    int rieCount = 0;
    int activeCount = 0;
    java.util.Iterator iterator = domainObjectSet.iterator();
    while (iterator.hasNext()) {
      ims.scheduling.domain.objects.Sch_Session domainObject =
          (ims.scheduling.domain.objects.Sch_Session) iterator.next();
      ims.scheduling.vo.SessionManagementVo 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.scheduling.domain.objects.Sch_Session object.
   *
   * @param map DomainObjectMap of DomainObjects to already created ValueObjects.
   * @param domainObject
   */
  public static ims.scheduling.vo.SessionManagementVo create(
      DomainObjectMap map, ims.scheduling.domain.objects.Sch_Session domainObject) {
    if (null == domainObject) {
      return null;
    }
    // check if the domainObject already has a valueObject created for it
    ims.scheduling.vo.SessionManagementVo valueObject =
        (ims.scheduling.vo.SessionManagementVo)
            map.getValueObject(domainObject, ims.scheduling.vo.SessionManagementVo.class);
    if (null == valueObject) {
      valueObject =
          new ims.scheduling.vo.SessionManagementVo(
              domainObject.getId(), domainObject.getVersion());
      map.addValueObject(domainObject, valueObject);

      valueObject = insert(map, valueObject, domainObject);
    }
    return valueObject;
  }
  public static ims.scheduling.domain.objects.Sch_Session extractSch_Session(
      ims.domain.ILightweightDomainFactory domainFactory,
      ims.scheduling.vo.SessionManagementVo valueObject,
      HashMap domMap) {
    if (null == valueObject) {
      return null;
    }
    Integer id = valueObject.getID_Sch_Session();
    ims.scheduling.domain.objects.Sch_Session domainObject = null;
    if (null == id) {
      if (domMap.get(valueObject) != null) {
        return (ims.scheduling.domain.objects.Sch_Session) domMap.get(valueObject);
      }
      // ims.scheduling.vo.SessionManagementVo ID_Sch_Session field is unknown
      domainObject = new ims.scheduling.domain.objects.Sch_Session();
      domMap.put(valueObject, domainObject);
    } else {
      String key = (valueObject.getClass().getName() + "__" + valueObject.getID_Sch_Session());
      if (domMap.get(key) != null) {
        return (ims.scheduling.domain.objects.Sch_Session) domMap.get(key);
      }
      domainObject =
          (ims.scheduling.domain.objects.Sch_Session)
              domainFactory.getDomainObject(ims.scheduling.domain.objects.Sch_Session.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_Sch_Session());

    // 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.getName() != null && valueObject.getName().equals("")) {
      valueObject.setName(null);
    }
    domainObject.setName(valueObject.getName());
    java.util.Date value2 = null;
    ims.framework.utils.Date date2 = valueObject.getSessionDate();
    if (date2 != null) {
      value2 = date2.getDate();
    }
    domainObject.setSessionDate(value2);
    ims.framework.utils.Time time3 = valueObject.getStartTm();
    String value3 = null;
    if (time3 != null) {
      value3 = time3.toString();
    }
    domainObject.setStartTm(value3);
    ims.framework.utils.Time time4 = valueObject.getEndTm();
    String value4 = null;
    if (time4 != null) {
      value4 = time4.toString();
    }
    domainObject.setEndTm(value4);
    // SaveAsRefVO - treated as a refVo in extract methods
    ims.core.clinical.domain.objects.Service value5 = null;
    if (null != valueObject.getService()) {
      if (valueObject.getService().getBoId() == null) {
        if (domMap.get(valueObject.getService()) != null) {
          value5 = (ims.core.clinical.domain.objects.Service) domMap.get(valueObject.getService());
        }
      } else {
        value5 =
            (ims.core.clinical.domain.objects.Service)
                domainFactory.getDomainObject(
                    ims.core.clinical.domain.objects.Service.class,
                    valueObject.getService().getBoId());
      }
    }
    domainObject.setService(value5);
    // create LookupInstance from vo LookupType
    ims.domain.lookups.LookupInstance value6 = null;
    if (null != valueObject.getSessionStatus()) {
      value6 = domainFactory.getLookupInstance(valueObject.getSessionStatus().getID());
    }
    domainObject.setSessionStatus(value6);
    domainObject.setRemainingSlots(valueObject.getRemainingSlots());
    domainObject.setTotalSlots(valueObject.getTotalSlots());
    domainObject.setRemainingTime(valueObject.getRemainingTime());
    // SaveAsRefVO - treated as a refVo in extract methods
    ims.core.resource.place.domain.objects.Location value10 = null;
    if (null != valueObject.getSchLocation()) {
      if (valueObject.getSchLocation().getBoId() == null) {
        if (domMap.get(valueObject.getSchLocation()) != null) {
          value10 =
              (ims.core.resource.place.domain.objects.Location)
                  domMap.get(valueObject.getSchLocation());
        }
      } else {
        value10 =
            (ims.core.resource.place.domain.objects.Location)
                domainFactory.getDomainObject(
                    ims.core.resource.place.domain.objects.Location.class,
                    valueObject.getSchLocation().getBoId());
      }
    }
    domainObject.setSchLocation(value10);
    // create LookupInstance from vo LookupType
    ims.domain.lookups.LookupInstance value11 = null;
    if (null != valueObject.getSessionType()) {
      value11 = domainFactory.getLookupInstance(valueObject.getSessionType().getID());
    }
    domainObject.setSessionType(value11);

    // SaveAsRefVO treated as RefValueObject
    ims.scheduling.vo.Session_ListOwnerRefVoCollection refCollection12 =
        new ims.scheduling.vo.Session_ListOwnerRefVoCollection();
    if (valueObject.getListOwners() != null) {
      for (int i12 = 0; i12 < valueObject.getListOwners().size(); i12++) {
        ims.scheduling.vo.Session_ListOwnerRefVo ref12 =
            (ims.scheduling.vo.Session_ListOwnerRefVo) valueObject.getListOwners().get(i12);
        refCollection12.add(ref12);
      }
    }
    int size12 = (null == refCollection12) ? 0 : refCollection12.size();
    java.util.Set domainListOwners12 = domainObject.getListOwners();
    if (domainListOwners12 == null) {
      domainListOwners12 = new java.util.HashSet();
    }
    java.util.Set newSet12 = new java.util.HashSet();
    for (int i = 0; i < size12; i++) {
      ims.scheduling.vo.Session_ListOwnerRefVo vo = refCollection12.get(i);
      ims.scheduling.domain.objects.Session_ListOwner dom = null;
      if (null != vo) {
        if (vo.getBoId() == null) {
          if (domMap.get(vo) != null) {
            dom = (ims.scheduling.domain.objects.Session_ListOwner) domMap.get(vo);
          }
        } else {
          dom =
              (ims.scheduling.domain.objects.Session_ListOwner)
                  domainFactory.getDomainObject(
                      ims.scheduling.domain.objects.Session_ListOwner.class, vo.getBoId());
        }
      }

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

    domainObject.setListOwners(domainListOwners12);

    // SaveAsRefVO treated as RefValueObject
    ims.scheduling.vo.DirectoryofServiceRefVoCollection refCollection13 =
        new ims.scheduling.vo.DirectoryofServiceRefVoCollection();
    if (valueObject.getDirectoryofServices() != null) {
      for (int i13 = 0; i13 < valueObject.getDirectoryofServices().size(); i13++) {
        ims.scheduling.vo.DirectoryofServiceRefVo ref13 =
            (ims.scheduling.vo.DirectoryofServiceRefVo)
                valueObject.getDirectoryofServices().get(i13);
        refCollection13.add(ref13);
      }
    }
    int size13 = (null == refCollection13) ? 0 : refCollection13.size();
    java.util.Set domainDirectoryofServices13 = domainObject.getDirectoryofServices();
    if (domainDirectoryofServices13 == null) {
      domainDirectoryofServices13 = new java.util.HashSet();
    }
    java.util.Set newSet13 = new java.util.HashSet();
    for (int i = 0; i < size13; i++) {
      ims.scheduling.vo.DirectoryofServiceRefVo vo = refCollection13.get(i);
      ims.scheduling.domain.objects.DirectoryofService dom = null;
      if (null != vo) {
        if (vo.getBoId() == null) {
          if (domMap.get(vo) != null) {
            dom = (ims.scheduling.domain.objects.DirectoryofService) domMap.get(vo);
          }
        } else {
          dom =
              (ims.scheduling.domain.objects.DirectoryofService)
                  domainFactory.getDomainObject(
                      ims.scheduling.domain.objects.DirectoryofService.class, vo.getBoId());
        }
      }

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

    domainObject.setDirectoryofServices(domainDirectoryofServices13);
    domainObject.setIsTheatreSession(valueObject.getIsTheatreSession());

    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.scheduling.domain.objects.Sch_Session
   */
  public static ims.scheduling.vo.SessionManagementVo insert(
      DomainObjectMap map,
      ims.scheduling.vo.SessionManagementVo valueObject,
      ims.scheduling.domain.objects.Sch_Session domainObject) {
    if (null == domainObject) {
      return valueObject;
    }
    if (null == map) {
      map = new DomainObjectMap();
    }

    valueObject.setID_Sch_Session(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;

    // Name
    valueObject.setName(domainObject.getName());
    // SessionDate
    java.util.Date SessionDate = domainObject.getSessionDate();
    if (null != SessionDate) {
      valueObject.setSessionDate(new ims.framework.utils.Date(SessionDate));
    }
    // StartTm
    String StartTm = domainObject.getStartTm();
    if (null != StartTm) {
      valueObject.setStartTm(new ims.framework.utils.Time(StartTm));
    }
    // EndTm
    String EndTm = domainObject.getEndTm();
    if (null != EndTm) {
      valueObject.setEndTm(new ims.framework.utils.Time(EndTm));
    }
    // Service
    valueObject.setService(
        ims.core.vo.domain.ServiceShortVoAssembler.create(map, domainObject.getService()));
    // SessionStatus
    ims.domain.lookups.LookupInstance instance6 = domainObject.getSessionStatus();
    if (null != instance6) {
      ims.framework.utils.ImagePath img = null;
      ims.framework.utils.Color color = null;
      img = null;
      if (instance6.getImage() != null) {
        img =
            new ims.framework.utils.ImagePath(
                instance6.getImage().getImageId(), instance6.getImage().getImagePath());
      }
      color = instance6.getColor();
      if (color != null) color.getValue();

      ims.scheduling.vo.lookups.Session_Status_and_Reason voLookup6 =
          new ims.scheduling.vo.lookups.Session_Status_and_Reason(
              instance6.getId(), instance6.getText(), instance6.isActive(), null, img, color);
      ims.scheduling.vo.lookups.Session_Status_and_Reason parentVoLookup6 = voLookup6;
      ims.domain.lookups.LookupInstance parent6 = instance6.getParent();
      while (parent6 != null) {
        if (parent6.getImage() != null) {
          img =
              new ims.framework.utils.ImagePath(
                  parent6.getImage().getImageId(), parent6.getImage().getImagePath());
        } else {
          img = null;
        }
        color = parent6.getColor();
        if (color != null) color.getValue();
        parentVoLookup6.setParent(
            new ims.scheduling.vo.lookups.Session_Status_and_Reason(
                parent6.getId(), parent6.getText(), parent6.isActive(), null, img, color));
        parentVoLookup6 = parentVoLookup6.getParent();
        parent6 = parent6.getParent();
      }
      valueObject.setSessionStatus(voLookup6);
    }
    // RemainingSlots
    valueObject.setRemainingSlots(domainObject.getRemainingSlots());
    // TotalSlots
    valueObject.setTotalSlots(domainObject.getTotalSlots());
    // RemainingTime
    valueObject.setRemainingTime(domainObject.getRemainingTime());
    // SchLocation
    valueObject.setSchLocation(
        ims.core.vo.domain.LocationLiteVoAssembler.create(map, domainObject.getSchLocation()));
    // SessionType
    ims.domain.lookups.LookupInstance instance11 = domainObject.getSessionType();
    if (null != instance11) {
      ims.framework.utils.ImagePath img = null;
      ims.framework.utils.Color color = null;
      img = null;
      if (instance11.getImage() != null) {
        img =
            new ims.framework.utils.ImagePath(
                instance11.getImage().getImageId(), instance11.getImage().getImagePath());
      }
      color = instance11.getColor();
      if (color != null) color.getValue();

      ims.scheduling.vo.lookups.Sched_Profile_Type voLookup11 =
          new ims.scheduling.vo.lookups.Sched_Profile_Type(
              instance11.getId(), instance11.getText(), instance11.isActive(), null, img, color);
      ims.scheduling.vo.lookups.Sched_Profile_Type parentVoLookup11 = voLookup11;
      ims.domain.lookups.LookupInstance parent11 = instance11.getParent();
      while (parent11 != null) {
        if (parent11.getImage() != null) {
          img =
              new ims.framework.utils.ImagePath(
                  parent11.getImage().getImageId(), parent11.getImage().getImagePath());
        } else {
          img = null;
        }
        color = parent11.getColor();
        if (color != null) color.getValue();
        parentVoLookup11.setParent(
            new ims.scheduling.vo.lookups.Sched_Profile_Type(
                parent11.getId(), parent11.getText(), parent11.isActive(), null, img, color));
        parentVoLookup11 = parentVoLookup11.getParent();
        parent11 = parent11.getParent();
      }
      valueObject.setSessionType(voLookup11);
    }
    // ListOwners
    valueObject.setListOwners(
        ims.scheduling.vo.domain.Session_ListOwnerVoAssembler
            .createSession_ListOwnerVoCollectionFromSession_ListOwner(
                map, domainObject.getListOwners()));
    // DirectoryofServices
    valueObject.setDirectoryofServices(
        ims.scheduling.vo.domain.DirectoryOfServiceVoAssembler
            .createDirectoryOfServiceVoCollectionFromDirectoryofService(
                map, domainObject.getDirectoryofServices()));
    // IsTheatreSession
    valueObject.setIsTheatreSession(domainObject.isIsTheatreSession());
    return valueObject;
  }