/**
   * Finds the <code>Syn_GenesDAO</code> identified by <code>
   * id_syn_genesTarget</code> by searching the genesDAO's <code>Syn_GenesDAO
   * </code> collection. Returns the object if found; null otherwise.
   *
   * @param genesDAO the <code>GenesDAO</code>containing the <code>Syn_GenesDAO
   * </code> collection
   * @param id_syn the id_syn to match
   * @return The object if found; null otherwise.
   */
  public static Syn_GenesDAO findSyn_genesDAO(GenesDAO genesDAO, int id_syn) {
    if (genesDAO.getSynonyms() == null) return null;
    Iterator<Syn_GenesDAO> syn_genesIterator = genesDAO.getSynonyms().iterator();
    while (syn_genesIterator.hasNext()) {
      Syn_GenesDAO itSyn_genesDAO = syn_genesIterator.next();
      if (itSyn_genesDAO.getId_syn() == id_syn) {
        return itSyn_genesDAO;
      }
    }

    return null;
  }
  /**
   * Returns a <code>List&lt;String&gt;</code> of distinct gene ids suitable for autocomplete
   * sourcing.
   *
   * @return a <code>List&lt;String&gt;</code> of gene ids suitable for autocomplete sourcing.
   */
  public List<String> getGeneIds() {
    List<GenesDAO> genesDAOList = getGenes();
    List<String> targetList = new ArrayList();

    if (genesDAOList != null) {
      for (GenesDAO genesDAO : genesDAOList) {
        String sId_gene = Integer.toString(genesDAO.getId_gene());
        targetList.add(Utils.wrap(sId_gene, "\""));
      }
    }

    logger.debug("targetList count = " + targetList.size());
    return targetList;
  }
  /**
   * Adds a new synonym to the gene identified by <code>gene</code>.
   *
   * @param gene the <code>GenesDAO</code> instance to which the new synonym is to be added
   * @return the new <code>Syn_GenesDAO</code> instance.
   */
  public Syn_GenesDAO addSynonym(GenesDAO gene) {
    synchronized (gene) {
      Set<Syn_GenesDAO> syn_genesDAOSet = gene.getSynonyms();
      if (syn_genesDAOSet == null) {
        syn_genesDAOSet = new LinkedHashSet();
        gene.setSynonyms(syn_genesDAOSet);
      }
      Syn_GenesDAO syn_genesDAO = new Syn_GenesDAO();
      syn_genesDAO.setLast_change(new Date());
      syn_genesDAO.setUsername("EMMA");
      syn_genesDAO.setGenes(gene);
      gene.getSynonyms().add(syn_genesDAO);
      save(gene);

      return syn_genesDAO;
    }
  }
 /**
  * Saves the given <code>GenesDAO</code> gene
  *
  * @param gene the <code>GenesDAO</code> to be saved
  */
 public void save(GenesDAO gene) {
   Session session = factory.getCurrentSession();
   Integer centimorgan = Utils.tryParseInt(gene.getCentimorgan());
   gene.setCentimorgan(
       centimorgan == null
           ? null
           : centimorgan
               .toString()); // Centimorgans are numeric, nullable in the database, so re-map any
   // non-numeric values to null.
   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String currentTimestamp = dateFormat.format(new Date());
   gene.setLast_change(currentTimestamp);
   gene.setUsername("EMMA");
   try {
     session.beginTransaction();
     session.saveOrUpdate(gene);
     session.getTransaction().commit();
   } catch (HibernateException e) {
     session.getTransaction().rollback();
     throw e;
   }
 }
  /**
   * Deletes the synonym identified by the primary key <code>id_syn</code> from the <code>GenesDAO
   * </code> object identified by <code>gene</code>.
   *
   * @param gene the gene from which the synonym is to be deleted
   * @param id_syn the primary key of the synonym to be deleted
   */
  public void deleteSynonym(GenesDAO gene, int id_syn) {
    Session session = factory.getCurrentSession();
    if (gene == null) return;

    synchronized (gene) {
      Syn_GenesDAO syn_genesDAO = findSyn_genesDAO(gene, id_syn);
      if (syn_genesDAO != null) {
        gene.getSynonyms().remove(syn_genesDAO);

        try {
          session.beginTransaction();
          session.delete(syn_genesDAO);
          session.getTransaction().commit();
        } catch (HibernateException e) {
          session.getTransaction().rollback();
        }
      }
    }
  }
  /**
   * Remaps null fields to empty strings suitable for use in the client.
   *
   * @param genesDAO the instance to remap
   * @return the same instance, with nulls remapped to empty strings.
   */
  private GenesDAO remapNulls(GenesDAO genesDAO) {
    // Re-map null fields to empty strings.
    if (genesDAO != null) {
      if (genesDAO.getCentimorgan() == null) genesDAO.setCentimorgan("");
      if (genesDAO.getChromosome() == null) genesDAO.setChromosome("");
      if (genesDAO.getCytoband() == null) genesDAO.setCytoband("");
      if (genesDAO.getEnsembl_ref() == null) genesDAO.setEnsembl_ref("");
      if (genesDAO.getFounder_line_number() == null) genesDAO.setFounder_line_number("");
      if (genesDAO.getLast_change() == null) genesDAO.setLast_change("");
      if (genesDAO.getMgi_ref() == null) genesDAO.setMgi_ref("");
      if (genesDAO.getName() == null) genesDAO.setName("");
      if (genesDAO.getPlasmid_construct() == null) genesDAO.setPlasmid_construct("");
      if (genesDAO.getPromoter() == null) genesDAO.setPromoter("");
      if (genesDAO.getSpecies() == null) genesDAO.setSpecies("");
      if (genesDAO.getSymbol() == null) genesDAO.setSymbol("");
      if (genesDAO.getUsername() == null) genesDAO.setUsername("");
    }

    return genesDAO;
  }
  /**
   * Transforms a <code>List&lt;GenesDAO&gt;</code> to a JSON string.
   *
   * @param genesDAOList the list to be transformed
   * @return the transformed JSON string
   */
  public static String toJSON(List<GenesDAO> genesDAOList) {
    JSONArray jsonList = new JSONArray();
    for (GenesDAO gene : genesDAOList) {
      JSONObject jsonGeneDAO = new JSONObject();
      jsonGeneDAO.put("centimorgan", gene.getCentimorgan() == null ? "" : gene.getCentimorgan());
      jsonGeneDAO.put("chromosome", gene.getChromosome() == null ? "" : gene.getChromosome());
      jsonGeneDAO.put("cytoband", gene.getCytoband() == null ? "" : gene.getCytoband());
      jsonGeneDAO.put("ensembl_ref", gene.getEnsembl_ref() == null ? "" : gene.getEnsembl_ref());
      jsonGeneDAO.put(
          "founder_line_number",
          gene.getFounder_line_number() == null ? "" : gene.getFounder_line_number());
      jsonGeneDAO.put("id_gene", Integer.toString(gene.getId_gene()));
      jsonGeneDAO.put("mgi_ref", gene.getMgi_ref() == null ? "" : gene.getMgi_ref());
      jsonGeneDAO.put("name", gene.getName() == null ? "" : gene.getName());
      jsonGeneDAO.put(
          "plasmid_construct",
          gene.getPlasmid_construct() == null ? "" : gene.getPlasmid_construct());
      jsonGeneDAO.put("promoter", gene.getPromoter() == null ? "" : gene.getPromoter());
      jsonGeneDAO.put("species", gene.getSpecies() == null ? "" : gene.getSpecies());
      jsonGeneDAO.put("symbol", gene.getSymbol() == null ? "" : gene.getSymbol());

      if ((gene.getSynonyms() != null) && (gene.getSynonyms().size() > 0)) {
        JSONArray synonyms = new JSONArray();
        Iterator<Syn_GenesDAO> iterator = gene.getSynonyms().iterator();
        while (iterator.hasNext()) {
          Syn_GenesDAO syn_genesDAO = iterator.next();
          JSONObject synonym = new JSONObject();
          synonym.put("id_syn", Integer.toString(syn_genesDAO.getId_syn()));
          synonym.put("name", syn_genesDAO.getName());
          synonym.put("symbol", syn_genesDAO.getSymbol());
          synonyms.add(synonym);
        }
        jsonGeneDAO.put("synonyms", synonyms);
      }

      jsonList.add(jsonGeneDAO);
    }

    // Gson dosn't reserve space for fields with null values!!!!
    ////////        Gson gson = new Gson();
    ////////            String s = gson.toJson(genesDAOList);
    ////////            System.out.println(s);
    ////////        return s;

    return jsonList.toString();
  }