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.apis.GeoPosicionamientoUsuariosAPI; import edu.utn.frba.grupo5303.serverenviolibre.beans.PosicionBean; import edu.utn.frba.grupo5303.serverenviolibre.model.Posicion; import java.io.IOException; 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; /** * * @author flpitu88 */ @Service public class GeoPosicionamientoUsuariosService implements GeoPosicionamientoUsuariosAPI { private GeoDataManagerConfiguration config; private GeoDataManager geoDataManager; private ObjectMapper mapper; private JsonFactory factory; public GeoPosicionamientoUsuariosService() { setupGeoDataManager(); mapper = new ObjectMapper(); factory = mapper.getJsonFactory(); } private void setupGeoDataManager() { String tableName = "PosicionUsuarios"; 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 timeStamp, Integer idUsuario) { GeoPunto geoPoint = new GeoPunto(p.getLatitud(), p.getLongitud()); AttributeValue rangeKeyAttributeValue = new AttributeValue().withS(UUID.randomUUID().toString()); AttributeValue usuarioAttribute = new AttributeValue().withN(idUsuario.toString()); AttributeValue timeStampAttribute = new AttributeValue().withS(timeStamp); PutPointRequest putPointRequest = new PutPointRequest(geoPoint, rangeKeyAttributeValue); putPointRequest.getPutItemRequest().addItemEntry("usuarioId", usuarioAttribute); putPointRequest.getPutItemRequest().addItemEntry("timeStamp", timeStampAttribute); PutPointResult putPointResult = geoDataManager.putPoint(putPointRequest); p.setRangeKey(putPointRequest.getRangeKeyValue().getS()); } @Override public List<Integer> getPosicionesCercanas(PosicionBean pos, double radioEnMetros) throws IOException { GeoPunto centerPoint = new GeoPunto(pos.getLatitud(), pos.getLongitud()); List<String> attributesToGet = new ArrayList<String>(); attributesToGet.add(config.getRangeKeyAttributeName()); attributesToGet.add(config.getGeoJsonAttributeName()); attributesToGet.add("usuarioId"); attributesToGet.add("timeStamp"); 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 usuarioId = ""; usuarioId = item.get("usuarioId").getN(); idsABuscar.add(Integer.parseInt(usuarioId)); } return idsABuscar; } @Override public void eliminarPosicionUsuario(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); } @Override public void actualizarPosicion(Posicion anterior, Posicion nueva, Integer idUsuario, String timeStamp) { eliminarPosicionUsuario(anterior); guardarPosicion(nueva, timeStamp, idUsuario); } }