Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. package com.team5.spatial.repository;
  2.  
  3. import com.team5.spatial.Helpers.GeometryParser;
  4. import com.team5.spatial.models.Request;
  5. import com.team5.spatial.repository.contracts.ShapeRepository;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.context.annotation.PropertySource;
  8. import org.springframework.core.env.Environment;
  9. import org.springframework.dao.EmptyResultDataAccessException;
  10. import org.springframework.stereotype.Repository;
  11. import org.springframework.web.client.ResourceAccessException;
  12.  
  13. import java.io.IOException;
  14. import java.sql.*;
  15. import java.text.ParseException;
  16. import java.util.HashMap;
  17.  
  18. @Repository
  19. @PropertySource("classpath:application.properties")
  20. public class ShapeRepositoryImpl implements ShapeRepository {
  21.     private final String NO_RESULT_FOUND = "No data found for this location...";
  22.     private String dbUrl, username, password;
  23.     private GeometryParser geometryParser;
  24.  
  25.     @Autowired
  26.     ShapeRepositoryImpl(Environment env, GeometryParser geometryParser) {
  27.         dbUrl = env.getProperty("database.url");
  28.         username = env.getProperty("database.username");
  29.         password = env.getProperty("database.password");
  30.         this.geometryParser = geometryParser;
  31.     }
  32.  
  33.     @Override
  34.     public HashMap getShape(Request request) throws Exception {
  35.         String sql = "SELECT * FROM " + request.getRequestType() + " WHERE CONTAINS(SHAPE,Point(" + request.getX() + "," + request.getY() + "));";
  36.         try (
  37.                 Connection connection = DriverManager.getConnection(dbUrl, username, password);
  38.                 Statement statement = connection.createStatement()
  39.         ) {
  40.             ResultSet propertiesSet = statement.executeQuery(sql); //Needs closing
  41.  
  42.             if (!propertiesSet.isBeforeFirst()) {
  43.                 System.out.println(NO_RESULT_FOUND);
  44.                 throw new EmptyResultDataAccessException(NO_RESULT_FOUND, 1);
  45.             }
  46.  
  47.             ResultSetMetaData md = propertiesSet.getMetaData();
  48.  
  49.             int columns = md.getColumnCount();
  50.  
  51.             HashMap properties = new HashMap<>();
  52.  
  53.             while (propertiesSet.next()) {
  54.                 for (int i = 1; i <= columns; i++) {
  55.                     if (md.getColumnName(i).equals("SHAPE")) {
  56.                         properties.put("SHAPE", geometryParser.getGeometryFromInputStream(propertiesSet.getBinaryStream("SHAPE")).toString());
  57.                     } else {
  58.                         properties.put(md.getColumnName(i), propertiesSet.getObject(i));
  59.                     }
  60.                 }
  61.             }
  62.  
  63.             return properties;
  64.         } catch (EmptyResultDataAccessException e) {
  65.             throw new EmptyResultDataAccessException(e.getMessage(), e.getActualSize());
  66.         } catch (ParseException | IOException e) {
  67.             throw new IllegalArgumentException("FAILED_TO_PARSE_SHAPE");
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement