private CustomSeriesListDTO convertHibernateObjectToCustomSeriesListDTO(
     List<CustomSeriesList> daos) {
   List<CustomSeriesListDTO> returnList = new ArrayList<CustomSeriesListDTO>();
   // if (daos != null) {
   // this should contain only 1 record for customSeriesList
   for (int i = 0; i < daos.size(); i++) {
     CustomSeriesListDTO dto = new CustomSeriesListDTO();
     CustomSeriesList series = (CustomSeriesList) daos.get(0);
     Set<CustomSeriesListAttribute> children = series.getCustomSeriesListAttributes();
     List<String> seriesUidsList = new ArrayList<String>();
     for (Iterator<CustomSeriesListAttribute> iter = children.iterator(); iter.hasNext(); ) {
       CustomSeriesListAttribute attr = iter.next();
       String seriesInstanceUid = attr.getSeriesInstanceUid();
       seriesUidsList.add(seriesInstanceUid);
     }
     dto.setName(series.getName());
     dto.setComment(series.getComment());
     dto.setHyperlink(series.getHyperlink());
     dto.setDate(series.getCustomSeriesListTimestamp());
     dto.setId(series.getId());
     dto.setSeriesInstanceUIDs(seriesUidsList);
     dto.setUserName(series.getUserName());
     returnList.add(dto);
   }
   /*} else {
   	throw new Exception("No record found the query.");
   }*/
   return returnList.get(0);
 }
 private List<CustomSeriesListDTO> convertHibernateObjectToCustomSeriesListDTOList(
     List<CustomSeriesList> daos) {
   List<CustomSeriesListDTO> returnList = new ArrayList<CustomSeriesListDTO>();
   if (daos != null) {
     for (CustomSeriesList dataRow : daos) {
       Date d = dataRow.getCustomSeriesListTimestamp();
       CustomSeriesListDTO dto = new CustomSeriesListDTO();
       dto.setName(dataRow.getName());
       dto.setComment(dataRow.getComment());
       dto.setHyperlink((String) dataRow.getHyperlink());
       dto.setDate(d);
       dto.setId(dataRow.getId());
       dto.setUserName(dataRow.getUserName());
       Set<CustomSeriesListAttribute> children = dataRow.getCustomSeriesListAttributes();
       List<String> seriesUidsList = new ArrayList<String>();
       for (Iterator<CustomSeriesListAttribute> iter = children.iterator(); iter.hasNext(); ) {
         CustomSeriesListAttribute attr = iter.next();
         String seriesInstanceUid = attr.getSeriesInstanceUid();
         seriesUidsList.add(seriesInstanceUid);
       }
       dto.setSeriesInstanceUIDs(seriesUidsList);
       returnList.add(dto);
     }
   }
   return returnList;
 }
  private List<CustomSeriesListAttributeDTO> convertHibernateObjectToCustomSeriesListAttributeDTO(
      List<CustomSeriesListAttribute> daos) {
    List<CustomSeriesListAttributeDTO> returnList = new ArrayList<CustomSeriesListAttributeDTO>();

    if (daos != null) {
      // this should contain only 1 record for customSeriesList
      for (int i = 0; i < daos.size(); i++) {
        CustomSeriesListAttribute rowData = daos.get(i);
        CustomSeriesListAttributeDTO dto =
            new CustomSeriesListAttributeDTO(rowData.getId(), rowData.getSeriesInstanceUid());
        returnList.add(dto);
      }
    } /* else {
      	throw new Exception("No record found for the query.");
      }*/
    return returnList;
  }
  /**
   * insert a new record for the custom series list
   *
   * @param customList
   */
  @Transactional(propagation = Propagation.REQUIRED)
  public long insert(CustomSeriesListDTO customList, String username) throws DataAccessException {
    CustomSeriesList seriesList = new CustomSeriesList();
    seriesList.setName(customList.getName());
    seriesList.setComment(customList.getComment());
    seriesList.setHyperlink(customList.getHyperlink());
    seriesList.setUserName(username);
    seriesList.setCustomSeriesListTimestamp(new Date());
    List<String> seriesUids = customList.getSeriesInstanceUIDs();

    for (String seriesInstanceUid : seriesUids) {
      CustomSeriesListAttribute attr = new CustomSeriesListAttribute();
      attr.setSeriesInstanceUid(seriesInstanceUid);
      seriesList.addChild(attr);
    }
    getHibernateTemplate().saveOrUpdate(seriesList);

    return seriesList.getId();
  }
  /** update database with data in the dto */
  @Transactional(propagation = Propagation.REQUIRED)
  public long update(CustomSeriesListDTO editList, String userName, Boolean updatedSeries)
      throws DataAccessException {
    CustomSeriesList seriesList = new CustomSeriesList();
    seriesList.setName(editList.getName());
    seriesList.setComment(editList.getComment());
    seriesList.setHyperlink(editList.getHyperlink());
    seriesList.setId(editList.getId());
    seriesList.setCustomSeriesListTimestamp(new Date());
    seriesList.setUserName(userName);
    List<CustomSeriesListAttributeDTO> attributeDtos = editList.getSeriesInstanceUidsList();

    if (updatedSeries) {
      logger.debug("updatedSeries = " + updatedSeries + "  ... delete then insert... ");
      CustomSeriesList existingList =
          (CustomSeriesList) getHibernateTemplate().load(CustomSeriesList.class, editList.getId());
      Set<CustomSeriesListAttribute> children = existingList.getCustomSeriesListAttributes();
      logger.debug("total children to delete: " + children.size());
      existingList.getCustomSeriesListAttributes().removeAll(children);
      for (Iterator<CustomSeriesListAttribute> itr = children.iterator(); itr.hasNext(); ) {
        getHibernateTemplate().delete((CustomSeriesListAttribute) itr.next());
      }
      getHibernateTemplate().flush();
      existingList.setName(editList.getName());
      existingList.setComment(editList.getComment());
      existingList.setHyperlink(editList.getHyperlink());
      existingList.setCustomSeriesListTimestamp(new Date());
      existingList.setUserName(userName);

      for (CustomSeriesListAttributeDTO dto : attributeDtos) {
        CustomSeriesListAttribute attr = new CustomSeriesListAttribute();
        attr.setSeriesInstanceUid(dto.getSeriesInstanceUid());
        attr.setCustomSeriesListPkId(existingList.getId());
        existingList.addChild(attr);
      }
      getHibernateTemplate().update(existingList);
    } else {
      logger.debug("updatedSeries = " + updatedSeries + " ... updating..");
      getHibernateTemplate().update(seriesList);
    }

    return 1L;
  }