/* (non-Javadoc)
   * @see it.geosolutions.fra2015.services.SurveyService#updateValues(java.lang.String, java.lang.String, java.lang.Integer, java.lang.Integer, java.lang.String)
   */
  @Override
  public Entry updateValues(String iso3, String entryId, Integer row, Integer col, String value)
      throws BadRequestServiceEx, NotFoundServiceEx {

    Entry entry = entryDAO.findByName(entryId);
    if (entry != null) {
      Search searchCriteria = new Search(EntryItem.class);
      searchCriteria.addFilterEqual("rowNumber", row);
      searchCriteria.addFilterEqual("columnNumber", col);
      searchCriteria.addFilterEqual("entry.id", entry.getId());
      List<EntryItem> items = entryItemDAO.search(searchCriteria);
      EntryItem item = null;
      if (items.isEmpty()) {
        // create a new entry item for this entry
        item = new EntryItem();
        item.setType("String"); // TODO
        item.setColumnNumber(col);
        item.setRowNumber(row);
        item.setEntry(entry);

        entry.addEntryItem(item);

        entryItemDAO.persist(item);
        entryDAO.merge(entry);
      } else {
        item = items.get(0);
      }

      // find a country with the given name
      Country country = findCountryByISO3(iso3);
      if (country == null) {
        throw new BadRequestServiceEx("Country with code " + iso3 + " does not exist.");
      }

      // retrieve previous value if it is an update
      ValueDAO valueDAO = daoMap.get(item.getType());
      Value dbValue = valueDAO.read(item.getId(), country);
      if (dbValue == null) {
        // create a new value
        ValueDTO v = new ValueDTO();
        v.setEntryItem(item);
        v.setCountry(country);
        // set value
        v.setContent(value);
        valueDAO.persist(v);
      } else {
        // update values
        dbValue.setContent(value);
        valueDAO.merge(dbValue);
      }

      return entry;
    }
    throw new BadRequestServiceEx("Entry " + entryId + " not found.");
  }
  /* (non-Javadoc)
   * @see it.geosolutions.fra2015.services.SurveyService#removeValues(java.lang.String, java.lang.String, java.lang.Integer, java.lang.Integer, java.lang.String)
   */
  @Override
  public boolean removeValues(String iso3, String entryId, Integer row, Integer col, String value)
      throws BadRequestServiceEx, NotFoundServiceEx {

    Entry entry = entryDAO.findByName(entryId);

    if (entry == null) {
      throw new NotFoundServiceEx("Entry " + entryId + " not found.");
    }

    Search searchCriteria = new Search(EntryItem.class);
    searchCriteria.addFilterEqual("rowNumber", row);
    searchCriteria.addFilterEqual("columnNumber", col);
    searchCriteria.addFilterEqual("entry.id", entry.getId());
    List<EntryItem> items = entryItemDAO.search(searchCriteria);
    EntryItem item = null;
    if (items.isEmpty()) {
      return false;
    } else {
      item = items.get(0);
    }

    // find a country with the given name
    Country country = findCountryByISO3(iso3);
    if (country == null) {
      throw new NotFoundServiceEx("Country with code " + iso3 + " does not exist.");
    }

    // retrieve previous value if it is an update
    ValueDAO valueDAO = daoMap.get(item.getType());
    Value dbValue = valueDAO.read(item.getId(), country);
    if (dbValue == null) {
      return false;
    } else {
      valueDAO.remove(dbValue.getId());
    }

    return true;
  }