/* (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;
  }
  @Override
  public List<Value> getEntryItemsListByFieldValues(
      String field,
      List<String> fieldValues,
      List<String> rowNamesValue,
      String iso3,
      boolean emptyValues)
      throws BadRequestServiceEx {

    Search searchCriteria = new Search(EntryItem.class);

    Country country = findCountryByISO3(iso3);
    if (country == null) {
      throw new BadRequestServiceEx("Country with code " + iso3 + " does not exist.");
    }
    searchCriteria.addFilterIn(field, fieldValues);
    if (rowNamesValue != null && !rowNamesValue.isEmpty()) {
      searchCriteria.addFilterIn("rowName", rowNamesValue);
    }
    List<Value> results = new ArrayList<Value>();
    List<EntryItem> items = entryItemDAO.search(searchCriteria);
    for (EntryItem item : items) {
      String type = item.getType();
      ValueDAO valueDAO = daoMap.get(type);
      if (valueDAO != null) {
        Value value = valueDAO.read(item.getId(), country);
        if (value == null && emptyValues) {

          Value val = new NumberValue();
          val.setEntryItem(item);
          results.add(val);
        } else if (value != null && !emptyValues) {
          results.add(value);
        }
      }
    }
    return results;
  }
  private List<CompactValue> getCompactValues(String iso3, Integer questionNumber)
      throws BadRequestServiceEx {

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

    List<CompactValue> values = new ArrayList<CompactValue>(ENTRIES_IN_QUESTION);

    List<NumberValue> numberValues = bulkLoader.loadAllNumericValues(iso3, questionNumber);
    for (Value value : numberValues) {
      if ("NaN".equalsIgnoreCase(value.getContent())) {
        value.setContent("N/A");
      }
      EntryItem item = value.getEntryItem();
      CompactValue compact =
          new CompactValue(
              item.getEntry().getVariable(),
              item.getRowNumber(),
              item.getColumnNumber(),
              value.getContent());
      values.add(compact);
    }
    List<TextValue> textValues = bulkLoader.loadTextNumericValues(iso3, questionNumber);
    for (Value value : textValues) {
      EntryItem item = value.getEntryItem();
      CompactValue compact =
          new CompactValue(
              item.getEntry().getVariable(),
              item.getRowNumber(),
              item.getColumnNumber(),
              value.getContent());
      values.add(compact);
    }

    /*
         for (Map.Entry<String, ValueDAO> daoEntry : daoMap.entrySet()) {
             String typeName = daoEntry.getKey();
             ValueDAO valueDAO = daoEntry.getValue();
             LOGGER.debug("Loading values for Country:"+iso3 + " question:"+questionNumber + " type:"+typeName);

             List<? extends Value> valuesList = valueDAO.readAll(country, questionNumber);

             for (Value value : valuesList) {
                 if(typeName.equals("Number")){
                     if ("NaN".equalsIgnoreCase(value.getContent())){
                         value.setContent("N/A");
                     }
                 }
                 EntryItem item = value.getEntryItem();
                 CompactValue compact = new CompactValue(
                         item.getEntry().getVariable(),
                         item.getRowNumber(),
                         item.getColumnNumber(),
                         value.getContent());
                 values.add(compact);
             }
         }
    */
    return values;
  }