Search in sources :

Example 1 with Collection

use of com.baremaps.model.Collection in project baremaps by baremaps.

the class CollectionsResourceIntegrationTest method test.

@Test
public void test() {
    // Create a new collection
    Collection collection = new Collection().title("test").links(List.of(new Link().href("/link").rel("self")));
    // List the collections
    Collections collections = target().path("/collections").request().get(Collections.class);
    assertEquals(0, collections.getCollections().size());
    // Insert the collection
    Response response = target().path("/collections").request(MediaType.APPLICATION_JSON).post(Entity.entity(collection, MediaType.valueOf("application/json")));
    assertEquals(201, response.getStatus());
    // List the collections
    collections = target().path("/collections").request().get(Collections.class);
    assertEquals(1, collections.getCollections().size());
    // Get the collection
    String id = response.getHeaderString("Location").split("/")[4];
    collection = target().path("/collections/" + id).request().get(Collection.class);
    assertEquals("test", collection.getTitle());
    // Update the collection
    collection.setTitle("test_update");
    response = target().path("/collections/" + id).request().put(Entity.entity(collection, MediaType.valueOf("application/json")));
    assertEquals(204, response.getStatus());
    // Delete the collection
    response = target().path("/collections/" + id).request().delete();
    assertEquals(204, response.getStatus());
    // List the collections
    collections = target().path("/collections").request().get(Collections.class);
    assertEquals(0, collections.getCollections().size());
}
Also used : Response(javax.ws.rs.core.Response) Collection(com.baremaps.model.Collection) Collections(com.baremaps.model.Collections) Link(com.baremaps.model.Link) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 2 with Collection

use of com.baremaps.model.Collection in project baremaps by baremaps.

the class ImportResource method uploadData.

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("studio/import")
public Response uploadData(@FormDataParam("file") InputStream fileInputStream, @FormDataParam("file") FormDataContentDisposition fileMetaData) throws Exception {
    // Read FeatureCollection
    FeatureJSON fjson = new FeatureJSON();
    var fc = fjson.readFeatureCollection(fileInputStream);
    // Setup Collection
    String fileName = fileMetaData.getFileName();
    Collection collection = new Collection().id(UUID.randomUUID()).title(fileName.substring(0, fileName.lastIndexOf(".") - 1)).extent(new Extent().spatial(new ExtentSpatial().bbox(List.of(List.of(fc.getBounds().getMinX(), fc.getBounds().getMinY(), fc.getBounds().getMaxX(), fc.getBounds().getMaxY()))))).count(fc.size()).created(new Date()).geometryType(fc.getSchema().getGeometryDescriptor().getType().getBinding().getSimpleName());
    // Load data
    jdbi.useTransaction(handle -> {
        // Create collection
        handle.createUpdate("insert into collections (id, collection) values (:id, CAST(:collection AS jsonb))").bind("id", collection.getId()).bindByType("collection", collection, COLLECTION).execute();
        // Create table
        handle.execute(String.format("create table \"%s\" (id serial, tags hstore, geom geometry)", collection.getId()));
        // Insert features
        var features = fc.features();
        while (features.hasNext()) {
            SimpleFeature feature = (SimpleFeature) features.next();
            HashMap<String, String> properties = new HashMap<>();
            feature.getProperties().stream().filter(property -> !property.getName().getLocalPart().equals("geometry")).forEach(property -> properties.put(property.getName().getLocalPart(), property.getValue().toString()));
            Object geom = feature.getDefaultGeometryProperty().getValue();
            handle.createUpdate(String.format("insert into \"%s\" (tags, geom) values (:tags, ST_Transform(ST_SetSRID(CAST(:geom as geometry), 4326), 3857))", collection.getId())).bind("tags", properties).bind("geom", geom.toString()).execute();
        }
    });
    return Response.created(URI.create("collections/" + collection.getId())).build();
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) POST(javax.ws.rs.POST) FormDataContentDisposition(org.glassfish.jersey.media.multipart.FormDataContentDisposition) ExtentSpatial(com.baremaps.model.ExtentSpatial) Date(java.util.Date) Path(javax.ws.rs.Path) HashMap(java.util.HashMap) UUID(java.util.UUID) Singleton(javax.inject.Singleton) Collection(com.baremaps.model.Collection) Json(org.jdbi.v3.json.Json) Inject(javax.inject.Inject) List(java.util.List) MediaType(javax.ws.rs.core.MediaType) FormDataParam(org.glassfish.jersey.media.multipart.FormDataParam) Consumes(javax.ws.rs.Consumes) Response(javax.ws.rs.core.Response) SimpleFeature(org.opengis.feature.simple.SimpleFeature) FeatureJSON(org.geotools.geojson.feature.FeatureJSON) Extent(com.baremaps.model.Extent) URI(java.net.URI) QualifiedType(org.jdbi.v3.core.qualifier.QualifiedType) InputStream(java.io.InputStream) FeatureJSON(org.geotools.geojson.feature.FeatureJSON) Extent(com.baremaps.model.Extent) HashMap(java.util.HashMap) ExtentSpatial(com.baremaps.model.ExtentSpatial) Collection(com.baremaps.model.Collection) Date(java.util.Date) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 3 with Collection

use of com.baremaps.model.Collection in project baremaps by baremaps.

the class CollectionsResource method getCollection.

@Override
public Response getCollection(UUID collectionId) {
    Collection collection = jdbi.withHandle(handle -> handle.createQuery("select collection from collections where id = :id").bind("id", collectionId).mapTo(COLLECTION).one());
    collection.getLinks().add(new Link().href(uriInfo.getRequestUri().toString()).rel("self"));
    return Response.ok(collection).build();
}
Also used : Collection(com.baremaps.model.Collection) Link(com.baremaps.model.Link)

Example 4 with Collection

use of com.baremaps.model.Collection in project baremaps by baremaps.

the class CollectionsResource method getCollections.

@Override
public Response getCollections() {
    List<Collection> collectionList = jdbi.withHandle(handle -> handle.createQuery("select collection from collections").mapTo(COLLECTION).list());
    collectionList.forEach(collection -> collection.getLinks().add(new Link().href(uriInfo.getRequestUri().toString() + "/" + collection.getId()).rel("self")));
    Collections collections = new Collections().collections(collectionList);
    collections.getLinks().add(new Link().href(uriInfo.getRequestUri().toString()).rel("self"));
    return Response.ok(collections).build();
}
Also used : Collection(com.baremaps.model.Collection) Collections(com.baremaps.model.Collections) Link(com.baremaps.model.Link)

Aggregations

Collection (com.baremaps.model.Collection)4 Link (com.baremaps.model.Link)3 Collections (com.baremaps.model.Collections)2 Response (javax.ws.rs.core.Response)2 Extent (com.baremaps.model.Extent)1 ExtentSpatial (com.baremaps.model.ExtentSpatial)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 UUID (java.util.UUID)1 Inject (javax.inject.Inject)1 Singleton (javax.inject.Singleton)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 MediaType (javax.ws.rs.core.MediaType)1 FeatureJSON (org.geotools.geojson.feature.FeatureJSON)1 FormDataContentDisposition (org.glassfish.jersey.media.multipart.FormDataContentDisposition)1