public void convertStatesAndMerge(StateTraverser tasks) throws NoCountryFoundException {
    Integer numberOfCountriesWithoutStates = 0;
    for (Map.Entry<Place, List<Place>> entry :
        tasks.getWorkflowHelperWrapper().getGeoPlanetStates().entrySet()) {
      Place country = entry.getKey();
      List<Place> states = entry.getValue();

      List<Country> results = countryDtoService.query("name = '" + country.getName() + "'");

      if (results.isEmpty()) {
        throw new NoCountryFoundException(country);
      }

      Country countryDto = results.get(0);
      if ((states.isEmpty() && countryDto.getHasStates())
          || (!states.isEmpty() && !countryDto.getHasStates())) {
        countryDto.setHasStates(!states.isEmpty());
        countryDto = countryDtoService.merge(countryDto);
      }

      if (states.isEmpty()) {
        numberOfCountriesWithoutStates++;
      }

      for (Place state : states) {
        State stateDto = new State();

        List<State> queryCheck = query("name = '" + state.getName().replaceAll("'", "''") + "'");

        if (queryCheck.isEmpty()) {
          stateDto.setName(state.getName());
          stateDto.setCountry(countryDto);

          add(stateDto);
          tasks.getWorkflowHelperWrapper().getAllEntities().add(stateDto);
          continue;
        }

        stateDto = queryCheck.get(0);

        stateDto.setCountry(countryDto);
        stateDto.setDateLastModified(new Date());
        stateDto.setIsDeleted(false);

        stateDto = merge(stateDto);

        tasks.getWorkflowHelperWrapper().getAllEntities().add(stateDto);
      }
    }

    log.info("numberOfCountriesWithoutStates: " + numberOfCountriesWithoutStates);
  }
  public void convertCitiesAndMerge(CityTraverser tasks) throws NoStateFoundException {
    Integer numberOfStatesWithoutCities = 0;
    for (Map.Entry<Place, List<Place>> entry :
        tasks.getWorkflowHelperWrapper().getGeoPlanetCities().entrySet()) {
      Place state = entry.getKey();
      List<Place> cities = entry.getValue();

      List<State> results =
          stateDtoService.query("name = '" + state.getName().replaceAll("'", "''") + "'");

      if (results.isEmpty()) {
        throw new NoStateFoundException(state);
      }

      State stateDto = results.get(0);

      if (cities.isEmpty()) {
        numberOfStatesWithoutCities++;
      }

      for (Place city : cities) {
        City cityDto = new City();

        List<City> queryCheck = query("name = '" + city.getName().replaceAll("'", "''") + "'");

        if (queryCheck.isEmpty()) {
          cityDto.setName(city.getName());
          cityDto.setState(stateDto);
          cityDto.setCountry(stateDto.getCountry());

          add(cityDto);
          tasks.getWorkflowHelperWrapper().getAllEntities().add(cityDto);
          continue;
        }

        cityDto = queryCheck.get(0);

        cityDto.setCountry(stateDto.getCountry());
        cityDto.setState(stateDto);
        cityDto.setDateLastModified(new Date());
        cityDto.setIsDeleted(false);

        cityDto = merge(cityDto);

        tasks.getWorkflowHelperWrapper().getAllEntities().add(cityDto);
      }
    }

    log.info("numberOfStatesWithoutCities: " + numberOfStatesWithoutCities);
  }
  private List<Place> getSafely(Place place, String name) {
    try {
      List<Place> places = place.getChildren().type(name).get();

      return places;
    } catch (GeoPlanetException e) {
      return Collections.<Place>emptyList();
    }
  }
 public NoStateFoundException(Place state) {
   super();
   log.error(
       "The following state was found via Yahoo! API but did not exist in the cloud database: "
           + state.getName());
 }