Exemple #1
0
  public List<Address> getBy(String field, String value) {
    Validate.isTrue(
        FIELDS.contains(field), "Only " + FIELDS + " may be used to get address by field value!");

    ResultSet resultSet = null;
    try {
      value = cleanEscapeQuote(value);
      resultSet =
          select(
              "SELECT id, st_x(point::geometry) as lon, st_y(point::geometry) as lat, country, postcode, a1, a2, a3, street, house FROM address WHERE "
                  + field
                  + (value == null ? " IS NULL" : " = " + value));
      List<Address> addresses = new ArrayList<Address>();
      while (resultSet.next()) {
        Address address = new Address(resultSet.getString("id"));
        address.setCountry(resultSet.getString("country"));
        address.setLat(resultSet.getDouble("lat"));
        address.setLon(resultSet.getDouble("lon"));
        address.setPostcode(resultSet.getString("postcode"));
        address.setA1(resultSet.getString("a1"));
        address.setA2(resultSet.getString("a2"));
        address.setA3(resultSet.getString("a3"));
        address.setStreet(resultSet.getString("street"));
        address.setHouse(resultSet.getString("house"));
        addresses.add(address);
      }
      return addresses;
    } catch (SQLException e) {
      throw new RuntimeException(
          "Could not select addresses with field "
              + field
              + " and value "
              + value
              + " from repository!",
          e);
    } finally {
      try {
        resultSet.close();
      } catch (Exception e) {
        LOG.warning(
            "Exception while closing result set: " + e.getMessage() + " ... never mind, ignoring.");
      }
    }
  }
Exemple #2
0
 public void put(Iterable<Address> addresses) {
   try {
     List<String> addressValues = new ArrayList<String>(BATCH_SIZE);
     for (Address address : addresses) {
       addressValues.add(
           "("
               + cleanEscapeQuote(address.getId())
               + ", 'POINT ("
               + address.getLon()
               + " "
               + address.getLat()
               + ")', "
               + cleanEscapeQuote(address.getCountry())
               + ","
               + cleanEscapeQuote(address.getPostcode())
               + ","
               + cleanEscapeQuote(address.getA1())
               + ","
               + cleanEscapeQuote(address.getA2())
               + ","
               + cleanEscapeQuote(address.getA3())
               + ","
               + cleanEscapeQuote(address.getStreet())
               + ","
               + cleanEscapeQuote(address.getHouse())
               + ")");
       if (addressValues.size() >= BATCH_SIZE) {
         execute(
             "INSERT INTO address (id, point, country, postcode, a1, a2, a3, street, house) VALUES "
                 + StringUtils.join(addressValues, ", "));
         addressValues.clear();
       }
     }
     if (!addressValues.isEmpty()) {
       execute(
           "INSERT INTO address (id, point, country, postcode, a1, a2, a3, street, house) VALUES "
               + StringUtils.join(addressValues, ", "));
     }
   } catch (SQLException e) {
     throw new RuntimeException("Could not insert address into repository!", e);
   }
 }