Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package edu.utn.frba.grupo5303.serverenviolibre.services; import amazon.dynamodb.config.GeoDataManager; import amazon.dynamodb.config.GeoDataManagerConfiguration; import amazon.dynamodb.model.DeletePointRequest; import amazon.dynamodb.model.DeletePointResult; import amazon.dynamodb.model.GeoPunto; import amazon.dynamodb.model.PutPointRequest; import amazon.dynamodb.model.PutPointResult; import amazon.dynamodb.model.QueryRadiusRequest; import amazon.dynamodb.model.QueryRadiusResult; import amazon.dynamodb.util.GeoTableUtil; import com.amazonaws.ClientConfiguration; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.regions.Region; import com.amazonaws.regions.Regions; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient; import com.amazonaws.services.dynamodbv2.model.AttributeValue; import com.amazonaws.services.dynamodbv2.model.CreateTableRequest; import edu.utn.frba.grupo5303.serverenviolibre.beans.PosicionBean; import edu.utn.frba.grupo5303.serverenviolibre.model.Posicion; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.UUID; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.map.ObjectMapper; import org.springframework.stereotype.Service; import edu.utn.frba.grupo5303.serverenviolibre.apis.GeoPosicionamientoPublicacionesAPI; /** * * @author flavio */ @Service public class GeoPosicionamientoPublicacionesService implements GeoPosicionamientoPublicacionesAPI { private GeoDataManagerConfiguration config; private GeoDataManager geoDataManager; private ObjectMapper mapper; private JsonFactory factory; public GeoPosicionamientoPublicacionesService() { setupGeoDataManager(); mapper = new ObjectMapper(); factory = mapper.getJsonFactory(); } private void setupGeoDataManager() { String tableName = "Posicion"; String regionName = "us-west-2"; Region region = Region.getRegion(Regions.fromName(regionName)); ClientConfiguration clientConfiguration = new ClientConfiguration().withMaxErrorRetry(20); AmazonDynamoDBClient ddb = new AmazonDynamoDBClient(new ProfileCredentialsProvider(), clientConfiguration); ddb.setRegion(region); config = new GeoDataManagerConfiguration(ddb, tableName); geoDataManager = new GeoDataManager(config); } @Override public void inicializarTabla() { setupGeoDataManager(); GeoDataManagerConfiguration config = geoDataManager.getGeoDataManagerConfiguration(); CreateTableRequest createTableRequest = GeoTableUtil.getCreateTableRequest(config); config.getDynamoDBClient().createTable(createTableRequest); } @Override public void guardarPosicion(Posicion p, String tipoPunto, Integer publicacion) { GeoPunto geoPoint = new GeoPunto(p.getLatitud(), p.getLongitud()); AttributeValue rangeKeyAttributeValue = new AttributeValue().withS(UUID.randomUUID().toString()); AttributeValue publicacionAttribute = new AttributeValue().withN(publicacion.toString()); AttributeValue tipoPuntoAttribute = new AttributeValue().withS(tipoPunto); PutPointRequest putPointRequest = new PutPointRequest(geoPoint, rangeKeyAttributeValue); putPointRequest.getPutItemRequest().addItemEntry("publicacionId", publicacionAttribute); putPointRequest.getPutItemRequest().addItemEntry("tipoPunto", tipoPuntoAttribute); PutPointResult putPointResult = geoDataManager.putPoint(putPointRequest); p.setRangeKey(putPointRequest.getRangeKeyValue().getS()); } @Override public List<Integer> getPosicionesCercanas(PosicionBean pos, double radioEnMetros) { GeoPunto centerPoint = new GeoPunto(pos.getLatitud(), pos.getLongitud()); List<String> attributesToGet = new ArrayList<String>(); attributesToGet.add(config.getRangeKeyAttributeName()); attributesToGet.add(config.getGeoJsonAttributeName()); attributesToGet.add("publicacionId"); attributesToGet.add("tipoPunto"); QueryRadiusRequest queryRadiusRequest = new QueryRadiusRequest(centerPoint, radioEnMetros); queryRadiusRequest.getQueryRequest().setAttributesToGet(attributesToGet); QueryRadiusResult queryRadiusResult = geoDataManager.queryRadius(queryRadiusRequest); List<Integer> idsABuscar = new ArrayList<>(); for (Map<String, AttributeValue> item : queryRadiusResult.getItem()) { String publicacionId = ""; if (item.containsKey("publicacionId") && item.get("tipoPunto").getS().equals("origen")) { publicacionId = item.get("publicacionId").getN(); idsABuscar.add(Integer.parseInt(publicacionId)); } } return idsABuscar; } @Override public void eliminarPuntoGeografico(Posicion posicion) { GeoPunto geoPoint = new GeoPunto(posicion.getLatitud(), posicion.getLongitud()); AttributeValue rangeKeyAttributeValue = new AttributeValue().withS(posicion.getRangeKey()); DeletePointRequest deletePointRequest = new DeletePointRequest(geoPoint, rangeKeyAttributeValue); DeletePointResult deletePointResult = geoDataManager.deletePoint(deletePointRequest); } }