From c5e50481b4f7581ef9d63a9c2093c9a7d6527af6 Mon Sep 17 00:00:00 2001 From: Mike Neilson Date: Thu, 20 May 2021 19:36:48 -0700 Subject: [PATCH 01/13] moved to avoid package conflict in VSCODE. --- .../{ => java}/units/R__unit_conversions.java | 2 +- .../java/db/migration/UnitConversionTest.java | 2 +- housedb-dao/.classpath | 25 ------------------- 3 files changed, 2 insertions(+), 27 deletions(-) rename database/src/main/java/db/migration/{ => java}/units/R__unit_conversions.java (99%) delete mode 100644 housedb-dao/.classpath diff --git a/database/src/main/java/db/migration/units/R__unit_conversions.java b/database/src/main/java/db/migration/java/units/R__unit_conversions.java similarity index 99% rename from database/src/main/java/db/migration/units/R__unit_conversions.java rename to database/src/main/java/db/migration/java/units/R__unit_conversions.java index 46f2838..530adfb 100644 --- a/database/src/main/java/db/migration/units/R__unit_conversions.java +++ b/database/src/main/java/db/migration/java/units/R__unit_conversions.java @@ -1,4 +1,4 @@ -package db.migration.units; +package db.migration.java.units; import java.io.BufferedReader; import java.io.InputStream; diff --git a/database/src/test/java/db/migration/UnitConversionTest.java b/database/src/test/java/db/migration/UnitConversionTest.java index 2eda3fc..9e2e39e 100644 --- a/database/src/test/java/db/migration/UnitConversionTest.java +++ b/database/src/test/java/db/migration/UnitConversionTest.java @@ -2,7 +2,7 @@ import org.junit.jupiter.api.Test; -import db.migration.units.R__unit_conversions; +import db.migration.java.units.R__unit_conversions; import net.hobbyscience.SimpleInfixCalculator; import net.hobbyscience.database.Conversion; import net.hobbyscience.database.Unit; diff --git a/housedb-dao/.classpath b/housedb-dao/.classpath deleted file mode 100644 index 5785087..0000000 --- a/housedb-dao/.classpath +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - From 1b9b1400da1c70d404b1c34c2f480bb2727a49f8 Mon Sep 17 00:00:00 2001 From: Mike Neilson Date: Thu, 20 May 2021 20:29:33 -0700 Subject: [PATCH 02/13] [database] fixed uniques on locations and the search algorithm. --- .../db/migration/Locations/R__a_locations.sql | 15 +++++++++++--- .../V4/V4.5.1__fix_location_uniqueness.sql | 4 ++++ .../db/migration/Views/R__locations.sql | 9 +++++++++ database/src/test/sql/location_test.sql | 20 +++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 database/src/main/resources/db/migration/V4/V4.5.1__fix_location_uniqueness.sql create mode 100644 database/src/main/resources/db/migration/Views/R__locations.sql diff --git a/database/src/main/resources/db/migration/Locations/R__a_locations.sql b/database/src/main/resources/db/migration/Locations/R__a_locations.sql index d492f3f..7ba87ec 100644 --- a/database/src/main/resources/db/migration/Locations/R__a_locations.sql +++ b/database/src/main/resources/db/migration/Locations/R__a_locations.sql @@ -50,10 +50,19 @@ BEGIN foreach cur_level in ARRAY parts loop -- search for existing object at this level - --raise info 'Search for (%,%)', cur_level,the_parent_id; - select into the_id,found_parent_id id,parent_id from locations where name=cur_level and (parent_id = the_parent_id); -- or parent_id is null); + raise info 'Search for (%,%)', cur_level,the_parent_id; + select + into the_id,found_parent_id id,parent_id + from locations + where + name=cur_level + and ( + (parent_id = the_parent_id) + or + (parent_id is null and the_parent_id is null) + ); if the_id is NULL THEN - --raise notice 'insert new value'; + raise notice 'insert new value'; --raise notice '%',found_parent_id ; insert into locations(name,parent_id) values (cur_level,the_parent_id) returning id into the_id; the_parent_id = the_id; diff --git a/database/src/main/resources/db/migration/V4/V4.5.1__fix_location_uniqueness.sql b/database/src/main/resources/db/migration/V4/V4.5.1__fix_location_uniqueness.sql new file mode 100644 index 0000000..22af342 --- /dev/null +++ b/database/src/main/resources/db/migration/V4/V4.5.1__fix_location_uniqueness.sql @@ -0,0 +1,4 @@ +drop index housedb.location_names_lower; + +create unique index location_names_lower_no_parent on housedb.locations( lower(name) ) where parent_id is null; +create unique index location_names_lower_has_parent on housedb.locations( lower(name), parent_id ) where parent_id is not null; \ No newline at end of file diff --git a/database/src/main/resources/db/migration/Views/R__locations.sql b/database/src/main/resources/db/migration/Views/R__locations.sql new file mode 100644 index 0000000..51d9b8c --- /dev/null +++ b/database/src/main/resources/db/migration/Views/R__locations.sql @@ -0,0 +1,9 @@ +create or replace view housedb.view_locations as + select id, + housedb_locations.expand_location_name( + id + ) AS name, + parent_id + from housedb.locations +; + diff --git a/database/src/test/sql/location_test.sql b/database/src/test/sql/location_test.sql index db11e01..ea2de54 100644 --- a/database/src/test/sql/location_test.sql +++ b/database/src/test/sql/location_test.sql @@ -57,6 +57,26 @@ END; $$ LANGUAGE plpgsql; +CREATE OR REPLACE FUNCTION housedb_tests.test_create_with_sub_doesnt_create_duplicate() +RETURNS SETOF TEXT AS $$ +DECLARE + base_name text = 'Test'; + full_loc text = 'Test-Sub Location'; + n_rows int; +BEGIN + perform housedb_security.add_permission('guest', 'CREATE', 'locations','.*'); + + perform housedb_locations.create_location(base_name); + select into n_rows count(*) from housedb.locations; + RETURN NEXT ok( n_rows = 1, 'Should only have 1 row'); + + perform housedb_locations.create_location(full_loc); + select into n_rows count(*) from housedb.locations; + RETURN NEXT ok( n_rows = 2, 'Should only have 2 rows, duplicate row created'); + +END; +$$ LANGUAGE plpgsql; + From e2abaa661c96a6dbb57cb44308489499285e66fb Mon Sep 17 00:00:00 2001 From: Mike Neilson Date: Thu, 20 May 2021 20:52:27 -0700 Subject: [PATCH 03/13] [database] handles location insert on view correctly and changes in capitalization. --- .../db/migration/Locations/R__a_locations.sql | 44 ++++++++++++++++++- .../db/migration/Views/R__locations.sql | 8 +++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/database/src/main/resources/db/migration/Locations/R__a_locations.sql b/database/src/main/resources/db/migration/Locations/R__a_locations.sql index 7ba87ec..f31851b 100644 --- a/database/src/main/resources/db/migration/Locations/R__a_locations.sql +++ b/database/src/main/resources/db/migration/Locations/R__a_locations.sql @@ -55,7 +55,7 @@ BEGIN into the_id,found_parent_id id,parent_id from locations where - name=cur_level + lower(name)=lower(cur_level) and ( (parent_id = the_parent_id) or @@ -74,4 +74,44 @@ BEGIN end loop; RETURN the_id; END; -$$ LANGUAGE plpgsql; \ No newline at end of file +$$ LANGUAGE plpgsql; + + +create or replace function housedb_timeseries.insert_location() +returns trigger +as $$ +declare + loc_info housedb.timeseries%rowtype; + l_loc_id bigint; + l_parent_id bigint; + loc_name text; +begin + set search_path to housedb_timeseries,housedb_units,housedb,public; + if TG_OP = 'DELETE' then + raise notice 'deleting %', OLD; + return OLD; + else + --raise notice 'Inserting or updating value %', NEW; + if NEW.id is not null and NEW.name is not null THEN + raise exception 'Specify only timeseries_id or name, not both' using ERRCODE = 'ZX082'; + end if; + + if TG_OP = 'UPDATE' then + perform 'housedb_security.can_perform(housedb_security.get_session_user(),''UPDATE'',''timeseries'',ts_name)'; + raise exception 'Update not supported at this time'; + else -- INSERT + perform 'housedb_security.can_perform(housedb_security.get_session_user(),''CREATE'',''timeseries'',ts_name)'; + select housedb_locations.create_location(NEW.name) into l_loc_id; + select into l_parent_id parent_id from housedb.view_locations where lower(name) = lower(NEW.name); + NEW.id = l_loc_id; + NEW.parent_id = l_parent_id; + end if ; + + + end if; + + return new; +end; +$$ language plpgsql; + + diff --git a/database/src/main/resources/db/migration/Views/R__locations.sql b/database/src/main/resources/db/migration/Views/R__locations.sql index 51d9b8c..f03562e 100644 --- a/database/src/main/resources/db/migration/Views/R__locations.sql +++ b/database/src/main/resources/db/migration/Views/R__locations.sql @@ -3,7 +3,13 @@ create or replace view housedb.view_locations as housedb_locations.expand_location_name( id ) AS name, - parent_id + parent_id, + housedb_locations.expand_location_name( + parent_id + ) AS parent from housedb.locations ; +drop trigger if exists insert_location_trigger on housedb.view_locations; +create trigger insert_location_trigger instead of insert or update or delete on housedb.view_locations + for each row execute procedure housedb_timeseries.insert_location(); From 4a75aac5396bba9d517748d0173746b7556db1c6 Mon Sep 17 00:00:00 2001 From: Mike Neilson Date: Fri, 21 May 2021 20:18:42 -0700 Subject: [PATCH 04/13] [database] added basic 'geo' location information to location table. --- .../db/migration/V5/V5.0.0__location_coordinates.sql | 5 +++++ .../src/main/resources/db/migration/Views/R__locations.sql | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 database/src/main/resources/db/migration/V5/V5.0.0__location_coordinates.sql diff --git a/database/src/main/resources/db/migration/V5/V5.0.0__location_coordinates.sql b/database/src/main/resources/db/migration/V5/V5.0.0__location_coordinates.sql new file mode 100644 index 0000000..21f8bda --- /dev/null +++ b/database/src/main/resources/db/migration/V5/V5.0.0__location_coordinates.sql @@ -0,0 +1,5 @@ +alter table housedb.locations add column latitude double precision; +alter table housedb.locations add column longitude double precision; +alter table housedb.locations add column horizontal_datum varchar(50); +alter table housedb.locations add column elevation double precision; +alter table housedb.locations add column vertical_datum varchar(50); \ No newline at end of file diff --git a/database/src/main/resources/db/migration/Views/R__locations.sql b/database/src/main/resources/db/migration/Views/R__locations.sql index f03562e..39c8772 100644 --- a/database/src/main/resources/db/migration/Views/R__locations.sql +++ b/database/src/main/resources/db/migration/Views/R__locations.sql @@ -6,7 +6,12 @@ create or replace view housedb.view_locations as parent_id, housedb_locations.expand_location_name( parent_id - ) AS parent + ) AS parent, + latitude, + longitude, + horizontal_datum, + elevation, + vertical_datum from housedb.locations ; From 043edda7071f4059bb7111564b4a7d7d9601b021 Mon Sep 17 00:00:00 2001 From: Mike Neilson Date: Fri, 21 May 2021 20:25:30 -0700 Subject: [PATCH 05/13] [database] filled out locations object with new values. --- .../hobbyscience/housedb/dao/Location.java | 65 +++++++++++++++---- 1 file changed, 52 insertions(+), 13 deletions(-) diff --git a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/Location.java b/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/Location.java index b1f11d4..df4c4dc 100644 --- a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/Location.java +++ b/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/Location.java @@ -4,6 +4,46 @@ public class Location{ private String name; + private String parent; + private Double latitude; + private Double longitude; + private String horizontal_datum; + private Double elevation; + private String vertical_datum; + + public Location(String name, String parent, Double latitude, Double longitude, String horizontal_datum, Double elevation, String vertical_datum) { + this.name = name; + this.parent = parent; + this.latitude = latitude; + this.longitude = longitude; + this.horizontal_datum = horizontal_datum; + this.elevation = elevation; + this.vertical_datum = vertical_datum; + } + + public String getParent() { + return this.parent; + } + + public Double getLatitude() { + return this.latitude; + } + + public Double getLongitude() { + return this.longitude; + } + + public String getHorizontal_datum() { + return this.horizontal_datum; + } + + public Double getElevation() { + return this.elevation; + } + + public String getVertical_datum() { + return this.vertical_datum; + } public Location() { } @@ -14,16 +54,8 @@ public Location(String name) { public String getName() { return this.name; - } + } - public void setName(String name) { - this.name = name; - } - - public Location name(String name) { - setName(name); - return this; - } @Override public boolean equals(Object o) { @@ -33,18 +65,25 @@ public boolean equals(Object o) { return false; } Location location = (Location) o; - return Objects.equals(name, location.name); + return Objects.equals(name, location.name) && Objects.equals(parent, location.parent) && Objects.equals(latitude, location.latitude) && Objects.equals(longitude, location.longitude) && Objects.equals(horizontal_datum, location.horizontal_datum) && Objects.equals(elevation, location.elevation) && Objects.equals(vertical_datum, location.vertical_datum); } @Override - public int hashCode() { - return Objects.hashCode(name); + public int hashCode() { + return Objects.hash(name, parent, latitude, longitude, horizontal_datum, elevation, vertical_datum); } + @Override public String toString() { return "{" + - " name='" + getName() + "'" + + " name='" + name + "'" + + ", parent='" + parent + "'" + + ", latitude='" + latitude + "'" + + ", longitude='" + longitude + "'" + + ", horizontal_datum='" + horizontal_datum + "'" + + ", elevation='" + elevation + "'" + + ", vertical_datum='" + vertical_datum + "'" + "}"; } } \ No newline at end of file From d716262b6059c5222a1a645de189988c982daf5a Mon Sep 17 00:00:00 2001 From: Mike Neilson Date: Fri, 21 May 2021 20:34:04 -0700 Subject: [PATCH 06/13] [dao] reorganized classes to begin better structure. --- .../src/main/java/net/hobbyscience/housedb/dao/HouseDb.java | 4 ++++ .../java/net/hobbyscience/housedb/{dao => dto}/Location.java | 2 +- .../net/hobbyscience/housedb/{dao => dto}/TimeSeries.java | 3 ++- .../net/hobbyscience/housedb/{dao => types}/DataTriple.java | 2 +- .../{dao => types/serializers}/DataTripleDeserializer.java | 4 +++- .../{dao => types/serializers}/DataTripleSerializer.java | 4 +++- .../java/net/hobbyscience/housedb/dao/DataTripleTest.java | 4 ++++ z-api/src/main/java/net/hobbyscience/housedb/api/Entry.java | 3 +++ .../housedb/api/controllers/LocationController.java | 1 + .../housedb/api/controllers/TimeSeriesController.java | 2 ++ 10 files changed, 24 insertions(+), 5 deletions(-) rename housedb-dao/src/main/java/net/hobbyscience/housedb/{dao => dto}/Location.java (98%) rename housedb-dao/src/main/java/net/hobbyscience/housedb/{dao => dto}/TimeSeries.java (95%) rename housedb-dao/src/main/java/net/hobbyscience/housedb/{dao => types}/DataTriple.java (97%) rename housedb-dao/src/main/java/net/hobbyscience/housedb/{dao => types/serializers}/DataTripleDeserializer.java (91%) rename housedb-dao/src/main/java/net/hobbyscience/housedb/{dao => types/serializers}/DataTripleSerializer.java (90%) diff --git a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/HouseDb.java b/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/HouseDb.java index 3fa6bb1..a0dbdd8 100644 --- a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/HouseDb.java +++ b/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/HouseDb.java @@ -9,6 +9,9 @@ import java.util.ArrayList; import static net.hobbyscience.housedb.housedb.Routines.*; + +import net.hobbyscience.housedb.dto.Location; +import net.hobbyscience.housedb.dto.TimeSeries; import net.hobbyscience.housedb.housedb.tables.*; import static net.hobbyscience.housedb.housedb.tables.TimeseriesValues.*; @@ -17,6 +20,7 @@ import static net.hobbyscience.housedb.housedb.tables.Parameters.*; import static net.hobbyscience.housedb.housedb.tables.Intervals.*; import net.hobbyscience.housedb.housedb_security.Routines; +import net.hobbyscience.housedb.types.DataTriple; import static org.jooq.impl.DSL.*; diff --git a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/Location.java b/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/Location.java similarity index 98% rename from housedb-dao/src/main/java/net/hobbyscience/housedb/dao/Location.java rename to housedb-dao/src/main/java/net/hobbyscience/housedb/dto/Location.java index df4c4dc..ca55b92 100644 --- a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/Location.java +++ b/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/Location.java @@ -1,4 +1,4 @@ -package net.hobbyscience.housedb.dao; +package net.hobbyscience.housedb.dto; import java.util.Objects; diff --git a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/TimeSeries.java b/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/TimeSeries.java similarity index 95% rename from housedb-dao/src/main/java/net/hobbyscience/housedb/dao/TimeSeries.java rename to housedb-dao/src/main/java/net/hobbyscience/housedb/dto/TimeSeries.java index 70ffc1d..18e95f3 100644 --- a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/TimeSeries.java +++ b/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/TimeSeries.java @@ -1,4 +1,4 @@ -package net.hobbyscience.housedb.dao; +package net.hobbyscience.housedb.dto; import java.util.List; @@ -6,6 +6,7 @@ import io.swagger.v3.oas.annotations.media.ArraySchema; import io.swagger.v3.oas.annotations.media.Schema; +import net.hobbyscience.housedb.types.DataTriple; /** * */ diff --git a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/DataTriple.java b/housedb-dao/src/main/java/net/hobbyscience/housedb/types/DataTriple.java similarity index 97% rename from housedb-dao/src/main/java/net/hobbyscience/housedb/dao/DataTriple.java rename to housedb-dao/src/main/java/net/hobbyscience/housedb/types/DataTriple.java index c754465..b1d0b6a 100644 --- a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/DataTriple.java +++ b/housedb-dao/src/main/java/net/hobbyscience/housedb/types/DataTriple.java @@ -1,4 +1,4 @@ -package net.hobbyscience.housedb.dao; +package net.hobbyscience.housedb.types; import java.time.OffsetDateTime; diff --git a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/DataTripleDeserializer.java b/housedb-dao/src/main/java/net/hobbyscience/housedb/types/serializers/DataTripleDeserializer.java similarity index 91% rename from housedb-dao/src/main/java/net/hobbyscience/housedb/dao/DataTripleDeserializer.java rename to housedb-dao/src/main/java/net/hobbyscience/housedb/types/serializers/DataTripleDeserializer.java index b27b115..003bf98 100644 --- a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/DataTripleDeserializer.java +++ b/housedb-dao/src/main/java/net/hobbyscience/housedb/types/serializers/DataTripleDeserializer.java @@ -1,4 +1,4 @@ -package net.hobbyscience.housedb.dao; +package net.hobbyscience.housedb.types.serializers; import java.io.IOException; import java.time.OffsetDateTime; @@ -9,6 +9,8 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import net.hobbyscience.housedb.types.DataTriple; + public class DataTripleDeserializer extends StdDeserializer{ public static final Logger logger = Logger.getLogger(DataTripleDeserializer.class.getName()); public DataTripleDeserializer(){ diff --git a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/DataTripleSerializer.java b/housedb-dao/src/main/java/net/hobbyscience/housedb/types/serializers/DataTripleSerializer.java similarity index 90% rename from housedb-dao/src/main/java/net/hobbyscience/housedb/dao/DataTripleSerializer.java rename to housedb-dao/src/main/java/net/hobbyscience/housedb/types/serializers/DataTripleSerializer.java index 52bc1f3..5784031 100644 --- a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/DataTripleSerializer.java +++ b/housedb-dao/src/main/java/net/hobbyscience/housedb/types/serializers/DataTripleSerializer.java @@ -1,4 +1,4 @@ -package net.hobbyscience.housedb.dao; +package net.hobbyscience.housedb.types.serializers; import java.io.IOException; import java.time.format.DateTimeFormatter; @@ -7,6 +7,8 @@ import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.std.StdSerializer; +import net.hobbyscience.housedb.types.DataTriple; + public class DataTripleSerializer extends StdSerializer{ /** diff --git a/housedb-dao/src/test/java/net/hobbyscience/housedb/dao/DataTripleTest.java b/housedb-dao/src/test/java/net/hobbyscience/housedb/dao/DataTripleTest.java index 5b5f266..8e89b57 100644 --- a/housedb-dao/src/test/java/net/hobbyscience/housedb/dao/DataTripleTest.java +++ b/housedb-dao/src/test/java/net/hobbyscience/housedb/dao/DataTripleTest.java @@ -2,6 +2,10 @@ import org.junit.jupiter.api.Test; +import net.hobbyscience.housedb.types.DataTriple; +import net.hobbyscience.housedb.types.serializers.DataTripleDeserializer; +import net.hobbyscience.housedb.types.serializers.DataTripleSerializer; + import static org.junit.jupiter.api.Assertions.*; import java.time.OffsetDateTime; diff --git a/z-api/src/main/java/net/hobbyscience/housedb/api/Entry.java b/z-api/src/main/java/net/hobbyscience/housedb/api/Entry.java index d67274a..bea566a 100644 --- a/z-api/src/main/java/net/hobbyscience/housedb/api/Entry.java +++ b/z-api/src/main/java/net/hobbyscience/housedb/api/Entry.java @@ -10,6 +10,9 @@ import net.hobbyscience.housedb.api.controllers.*; import net.hobbyscience.housedb.dao.*; import net.hobbyscience.housedb.jackson.*; +import net.hobbyscience.housedb.types.DataTriple; +import net.hobbyscience.housedb.types.serializers.DataTripleDeserializer; +import net.hobbyscience.housedb.types.serializers.DataTripleSerializer; import org.apache.tomcat.jdbc.pool.DataSource; import java.util.Base64; diff --git a/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/LocationController.java b/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/LocationController.java index 80f728c..149f956 100644 --- a/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/LocationController.java +++ b/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/LocationController.java @@ -4,6 +4,7 @@ import io.javalin.http.*; import io.javalin.plugin.openapi.annotations.*; import net.hobbyscience.housedb.dao.*; +import net.hobbyscience.housedb.dto.Location; import java.util.logging.Logger; diff --git a/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/TimeSeriesController.java b/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/TimeSeriesController.java index 347387e..06c807e 100644 --- a/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/TimeSeriesController.java +++ b/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/TimeSeriesController.java @@ -3,6 +3,8 @@ import io.javalin.http.*; import io.javalin.plugin.openapi.annotations.*; import net.hobbyscience.housedb.dao.*; +import net.hobbyscience.housedb.dto.TimeSeries; + import javax.sql.DataSource; import org.jooq.exception.*; import java.time.*; From 499b5e3eaf2db923552f986b02c2e6eb248e33d3 Mon Sep 17 00:00:00 2001 From: Mike Neilson Date: Fri, 21 May 2021 21:15:07 -0700 Subject: [PATCH 07/13] [dao] created a locations dao layer. --- .../net/hobbyscience/housedb/dao/Dao.java | 23 ++++++ .../housedb/dao/LocationsDao.java | 79 +++++++++++++++++++ .../hobbyscience/housedb/dto/Location.java | 9 +-- 3 files changed, 103 insertions(+), 8 deletions(-) create mode 100644 housedb-dao/src/main/java/net/hobbyscience/housedb/dao/Dao.java create mode 100644 housedb-dao/src/main/java/net/hobbyscience/housedb/dao/LocationsDao.java diff --git a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/Dao.java b/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/Dao.java new file mode 100644 index 0000000..aea84c4 --- /dev/null +++ b/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/Dao.java @@ -0,0 +1,23 @@ +package net.hobbyscience.housedb.dao; + +import java.util.List; +import java.util.Optional; + +import org.jooq.DSLContext; + +public abstract class Dao { + + @SuppressWarnings("unused") + protected DSLContext dsl = null; + + public Dao(DSLContext dsl){ + this.dsl = dsl; + } + + public abstract List getAll(); + public abstract Optional getByUniqueName(String uniqueName); + + public abstract T update(T modified); + public abstract void save(T newObj); + +} diff --git a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/LocationsDao.java b/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/LocationsDao.java new file mode 100644 index 0000000..a8610b1 --- /dev/null +++ b/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/LocationsDao.java @@ -0,0 +1,79 @@ +package net.hobbyscience.housedb.dao; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +import org.jooq.DSLContext; +import org.jooq.impl.DSL; + +import net.hobbyscience.housedb.dto.Location; + +import static net.hobbyscience.housedb.housedb.tables.ViewLocations.*; + +public class LocationsDao extends Dao{ + + public LocationsDao(DSLContext dsl) { + super(dsl); + } + + @Override + public List getAll() { + var locs = dsl + .select() + .from(VIEW_LOCATIONS).orderBy(VIEW_LOCATIONS.NAME) + .fetch().stream().map( r -> r.into(VIEW_LOCATIONS) ) + .map( r -> { + return new Location ( + r.getName(), + r.getParent(), + r.getLatitude(), + r.getLongitude(), + r.getHorizontalDatum(), + r.getElevation(), + r.getVerticalDatum() + ); + }).collect(Collectors.toList()); + return locs; + } + + @Override + public Optional getByUniqueName(String uniqueName) { + var r = dsl + .select() + .from(VIEW_LOCATIONS) + .where(DSL.upper(VIEW_LOCATIONS.NAME).eq(DSL.upper(uniqueName))) + .orderBy(VIEW_LOCATIONS.NAME) + .fetchOne().into(VIEW_LOCATIONS); + if( r == null ){ + return Optional.ofNullable(null); + } else { + return Optional.of( + new Location ( + r.getName(), + r.getParent(), + r.getLatitude(), + r.getLongitude(), + r.getHorizontalDatum(), + r.getElevation(), + r.getVerticalDatum() + ) + ); + } + + + } + + @Override + public Location update(Location modified) { + // TODO Auto-generated method stub + return null; + } + + @Override + public void save(Location newObj) { + // TODO Auto-generated method stub + + } + +} diff --git a/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/Location.java b/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/Location.java index ca55b92..48ad93a 100644 --- a/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/Location.java +++ b/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/Location.java @@ -43,14 +43,7 @@ public Double getElevation() { public String getVertical_datum() { return this.vertical_datum; - } - - public Location() { - } - - public Location(String name) { - this.name = name; - } + } public String getName() { return this.name; From f6965ede3b116d2f5fdb2bfd4170c4cecedc6d05 Mon Sep 17 00:00:00 2001 From: Mike Neilson Date: Sat, 22 May 2021 23:21:02 -0700 Subject: [PATCH 08/13] [dao] added update support to LocationsDao. --- .../net/hobbyscience/housedb/dao/Dao.java | 2 +- .../housedb/dao/LocationsDao.java | 36 ++++++++++++++++--- .../hobbyscience/housedb/dto/Location.java | 8 ++++- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/Dao.java b/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/Dao.java index aea84c4..21c7b73 100644 --- a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/Dao.java +++ b/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/Dao.java @@ -17,7 +17,7 @@ public Dao(DSLContext dsl){ public abstract List getAll(); public abstract Optional getByUniqueName(String uniqueName); - public abstract T update(T modified); + public abstract void update(T modified); public abstract void save(T newObj); } diff --git a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/LocationsDao.java b/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/LocationsDao.java index a8610b1..2ddcab8 100644 --- a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/LocationsDao.java +++ b/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/LocationsDao.java @@ -10,6 +10,7 @@ import net.hobbyscience.housedb.dto.Location; import static net.hobbyscience.housedb.housedb.tables.ViewLocations.*; +import static net.hobbyscience.housedb.housedb.tables.Locations.*; public class LocationsDao extends Dao{ @@ -25,6 +26,7 @@ public List getAll() { .fetch().stream().map( r -> r.into(VIEW_LOCATIONS) ) .map( r -> { return new Location ( + r.getId(), r.getName(), r.getParent(), r.getLatitude(), @@ -50,6 +52,7 @@ public Optional getByUniqueName(String uniqueName) { } else { return Optional.of( new Location ( + r.getId(), r.getName(), r.getParent(), r.getLatitude(), @@ -65,14 +68,39 @@ public Optional getByUniqueName(String uniqueName) { } @Override - public Location update(Location modified) { - // TODO Auto-generated method stub - return null; + public void update(Location modified) { + dsl.update(VIEW_LOCATIONS) + .set(VIEW_LOCATIONS.NAME,modified.getName()) + + .set(VIEW_LOCATIONS.LATITUDE,modified.getLatitude()) + .set(VIEW_LOCATIONS.LONGITUDE,modified.getLongitude()) + .set(VIEW_LOCATIONS.HORIZONTAL_DATUM,modified.getHorizontal_datum()) + + .set(VIEW_LOCATIONS.LATITUDE,modified.getElevation()) + .set(VIEW_LOCATIONS.VERTICAL_DATUM,modified.getVertical_datum()) + + .where(VIEW_LOCATIONS.ID.eq(modified.getId())) + .execute(); } @Override public void save(Location newObj) { - // TODO Auto-generated method stub + dsl.insertInto(VIEW_LOCATIONS) + .columns( + VIEW_LOCATIONS.NAME, + VIEW_LOCATIONS.LATITUDE, + VIEW_LOCATIONS.LONGITUDE, + VIEW_LOCATIONS.HORIZONTAL_DATUM, + VIEW_LOCATIONS.ELEVATION, + VIEW_LOCATIONS.HORIZONTAL_DATUM + ).values( + newObj.getName(), + newObj.getLatitude(), + newObj.getLongitude(), + newObj.getHorizontal_datum(), + newObj.getElevation(), + newObj.getVertical_datum() + ).execute(); } diff --git a/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/Location.java b/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/Location.java index 48ad93a..17bf76b 100644 --- a/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/Location.java +++ b/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/Location.java @@ -3,6 +3,7 @@ import java.util.Objects; public class Location{ + private long id; private String name; private String parent; private Double latitude; @@ -11,7 +12,8 @@ public class Location{ private Double elevation; private String vertical_datum; - public Location(String name, String parent, Double latitude, Double longitude, String horizontal_datum, Double elevation, String vertical_datum) { + public Location(long id, String name, String parent, Double latitude, Double longitude, String horizontal_datum, Double elevation, String vertical_datum) { + this.id = id; this.name = name; this.parent = parent; this.latitude = latitude; @@ -21,6 +23,10 @@ public Location(String name, String parent, Double latitude, Double longitude, S this.vertical_datum = vertical_datum; } + public long getId(){ + return this.id; + } + public String getParent() { return this.parent; } From 31ff6277c083a824dbeaf9fefb896cfba86111cf Mon Sep 17 00:00:00 2001 From: Mike Neilson Date: Tue, 25 May 2021 19:34:16 -0700 Subject: [PATCH 09/13] [locations] api for sending locations information two and from appears to work. --- build.gradle | 2 +- .../net/hobbyscience/housedb/dao/HouseDb.java | 4 +++ .../housedb/dao/LocationsDao.java | 9 ++--- .../hobbyscience/housedb/dto/Location.java | 16 ++++++++- .../net/hobbyscience/housedb/api/Entry.java | 5 +++ .../housedb/api/NotAuthorized.java | 7 ++++ .../api/controllers/LocationController.java | 34 ++++++++++++++++--- 7 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 z-api/src/main/java/net/hobbyscience/housedb/api/NotAuthorized.java diff --git a/build.gradle b/build.gradle index 36ec6d8..64064f3 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ allprojects { apply plugin: 'java' - version = '0.1.2' + version = '0.1.3' group = 'net.hobbyscience.housedb' repositories { diff --git a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/HouseDb.java b/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/HouseDb.java index a0dbdd8..4c7ec1a 100644 --- a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/HouseDb.java +++ b/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/HouseDb.java @@ -57,6 +57,10 @@ public HouseDb setUsername(String username){ return this; } + public LocationsDao locationDao(){ + return new LocationsDao(dsl); + } + public List getAllLocations() throws Exception{ ArrayList locations = new ArrayList<>(); diff --git a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/LocationsDao.java b/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/LocationsDao.java index 2ddcab8..adc0952 100644 --- a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/LocationsDao.java +++ b/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/LocationsDao.java @@ -41,16 +41,17 @@ public List getAll() { @Override public Optional getByUniqueName(String uniqueName) { - var r = dsl + var res = dsl .select() .from(VIEW_LOCATIONS) .where(DSL.upper(VIEW_LOCATIONS.NAME).eq(DSL.upper(uniqueName))) .orderBy(VIEW_LOCATIONS.NAME) - .fetchOne().into(VIEW_LOCATIONS); - if( r == null ){ + .fetchOne(); + if( res == null ){ return Optional.ofNullable(null); } else { - return Optional.of( + var r = res.into(VIEW_LOCATIONS); + return Optional.of( new Location ( r.getId(), r.getName(), diff --git a/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/Location.java b/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/Location.java index 17bf76b..97e3152 100644 --- a/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/Location.java +++ b/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/Location.java @@ -2,9 +2,14 @@ import java.util.Objects; +import com.fasterxml.jackson.annotation.*; + + public class Location{ + @JsonIgnore private long id; private String name; + @JsonIgnore private String parent; private Double latitude; private Double longitude; @@ -12,7 +17,16 @@ public class Location{ private Double elevation; private String vertical_datum; - public Location(long id, String name, String parent, Double latitude, Double longitude, String horizontal_datum, Double elevation, String vertical_datum) { + @JsonCreator + public Location( + @JsonProperty(value="id", defaultValue="-1",required=false) long id, + @JsonProperty("name") String name, + @JsonProperty(value="parent",defaultValue="",required=false) String parent, + @JsonProperty("latitude") Double latitude, + @JsonProperty("longitude") Double longitude, + @JsonProperty("horizontal-datum") String horizontal_datum, + @JsonProperty("elevation") Double elevation, + @JsonProperty("vertical_datum") String vertical_datum) { this.id = id; this.name = name; this.parent = parent; diff --git a/z-api/src/main/java/net/hobbyscience/housedb/api/Entry.java b/z-api/src/main/java/net/hobbyscience/housedb/api/Entry.java index bea566a..54f0497 100644 --- a/z-api/src/main/java/net/hobbyscience/housedb/api/Entry.java +++ b/z-api/src/main/java/net/hobbyscience/housedb/api/Entry.java @@ -96,6 +96,11 @@ public static void main(String []args) { logger.log(Level.WARNING,"database error",e); } }) + .exception(NotAuthorized.class, (e, ctx ) -> { + logger.warning("Unauthorized access"); + logger.warning(e.getCause().getMessage()); + ctx.status(HttpServletResponse.SC_UNAUTHORIZED).json("Unauthorized Access"); + }) .exception(UnsupportedOperationException.class, (e,ctx) -> { ctx.status(HttpServletResponse.SC_NOT_IMPLEMENTED); logger.log(Level.WARNING,"error",e); diff --git a/z-api/src/main/java/net/hobbyscience/housedb/api/NotAuthorized.java b/z-api/src/main/java/net/hobbyscience/housedb/api/NotAuthorized.java new file mode 100644 index 0000000..93c812c --- /dev/null +++ b/z-api/src/main/java/net/hobbyscience/housedb/api/NotAuthorized.java @@ -0,0 +1,7 @@ +package net.hobbyscience.housedb.api; + +public class NotAuthorized extends RuntimeException{ + public NotAuthorized(String msg, Throwable err ){ + super(msg,err); + } +} diff --git a/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/LocationController.java b/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/LocationController.java index 149f956..5571266 100644 --- a/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/LocationController.java +++ b/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/LocationController.java @@ -3,13 +3,18 @@ import io.javalin.apibuilder.*; import io.javalin.http.*; import io.javalin.plugin.openapi.annotations.*; +import net.hobbyscience.housedb.api.NotAuthorized; import net.hobbyscience.housedb.dao.*; import net.hobbyscience.housedb.dto.Location; +import java.sql.SQLException; import java.util.logging.Logger; +import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; +import org.jooq.exception.DataAccessException; + public class LocationController implements CrudHandler { public static final Logger logger = Logger.getLogger(LocationController.class.getName()); @@ -32,13 +37,27 @@ public void getAll( Context ctx){ } @OpenApi( - tags = {"Locations"}, + pathParams = { + @OpenApiParam(name="location-name",description = "Name of the location you're trying to look up.") + }, responses = { @OpenApiResponse(status="200", content = {@OpenApiContent( from = Location.class)}) - } + }, + tags = {"Locations"} ) public void getOne( Context ctx, String locationName ){ - throw new UnsupportedOperationException("not implemented yet"); + var ds = ctx.appAttribute(DataSource.class); + try( var conn = ds.getConnection(); ){ + var db = new HouseDb(conn,ctx.attribute("username")); + var loc = db.locationDao().getByUniqueName(locationName); + if( loc.isPresent() ){ + ctx.json(loc.get()).contentType("application/json"); + } else { + ctx.result("Location not found").status(HttpServletResponse.SC_NOT_FOUND); + } + } catch( SQLException err ){ + throw new RuntimeException("Database error",err); + } } @OpenApi( @@ -53,8 +72,13 @@ public void create(Context ctx) { var ds = ctx.appAttribute(DataSource.class); try( var conn = ds.getConnection(); ){ var db = new HouseDb(conn,ctx.attribute("username")); - db.saveLocation(loc); - } catch( Exception err ){ + db.locationDao().save(loc); + } catch (DataAccessException err ){ + if( err.getLocalizedMessage().contains("no CREATE")){ + throw new NotAuthorized("Location", err); + } + } + catch( Exception err ){ throw new RuntimeException("Error creating location",err); } } From 6c48da4e52671aabc9e7ebf67c7eb3f9da6d1b2e Mon Sep 17 00:00:00 2001 From: Mike Neilson Date: Tue, 25 May 2021 20:41:54 -0700 Subject: [PATCH 10/13] [locations view] added tests, fixed insert logic. --- .../db/migration/Locations/R__a_locations.sql | 9 ++++++++- database/src/test/sql/location_test.sql | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/database/src/main/resources/db/migration/Locations/R__a_locations.sql b/database/src/main/resources/db/migration/Locations/R__a_locations.sql index f31851b..4eac9c6 100644 --- a/database/src/main/resources/db/migration/Locations/R__a_locations.sql +++ b/database/src/main/resources/db/migration/Locations/R__a_locations.sql @@ -86,7 +86,7 @@ declare l_parent_id bigint; loc_name text; begin - set search_path to housedb_timeseries,housedb_units,housedb,public; + set search_path to housedb_locations,housedb_units,housedb,public; if TG_OP = 'DELETE' then raise notice 'deleting %', OLD; return OLD; @@ -105,6 +105,13 @@ begin select into l_parent_id parent_id from housedb.view_locations where lower(name) = lower(NEW.name); NEW.id = l_loc_id; NEW.parent_id = l_parent_id; + update locations + set latitude = NEW.latitude, + longitude = NEW.longitude, + horizontal_datum = NEW.horizontal_datum, + elevation = NEW.elevation, + vertical_datum = NEW.vertical_datum + where id = NEW.id; end if ; diff --git a/database/src/test/sql/location_test.sql b/database/src/test/sql/location_test.sql index ea2de54..a067ec2 100644 --- a/database/src/test/sql/location_test.sql +++ b/database/src/test/sql/location_test.sql @@ -77,6 +77,23 @@ BEGIN END; $$ LANGUAGE plpgsql; +CREATE OR REPLACE FUNCTION housedb_tests.test_insert_new_locations_into_view() +RETURNS SETOF TEXT AS $$ +DECLARE + n_rows int; +BEGIN + perform housedb_security.add_permission('guest', 'CREATE', 'locations','.*'); + + insert into + housedb.view_locations(name,latitude,longitude,horizontal_datum) + values ('Test',0,3,'RASTER'); + select into n_rows count(*) from housedb.locations; + RETURN NEXT ok( n_rows = 1, 'There should be one row'); + +END; +$$ LANGUAGE plpgsql; + + From 56f8975ddbe7524620ce8cb118f71081ce9bbe6a Mon Sep 17 00:00:00 2001 From: Mike Neilson Date: Tue, 25 May 2021 20:44:45 -0700 Subject: [PATCH 11/13] [locations] base line of adding and listing locations. --- .../housedb/dao/LocationsDao.java | 10 ++-- .../hobbyscience/housedb/dto/Location.java | 56 +++++++++++++------ .../net/hobbyscience/housedb/api/Entry.java | 14 ++++- .../api/controllers/LocationController.java | 5 +- 4 files changed, 59 insertions(+), 26 deletions(-) diff --git a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/LocationsDao.java b/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/LocationsDao.java index adc0952..97ea8e1 100644 --- a/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/LocationsDao.java +++ b/housedb-dao/src/main/java/net/hobbyscience/housedb/dao/LocationsDao.java @@ -75,10 +75,10 @@ public void update(Location modified) { .set(VIEW_LOCATIONS.LATITUDE,modified.getLatitude()) .set(VIEW_LOCATIONS.LONGITUDE,modified.getLongitude()) - .set(VIEW_LOCATIONS.HORIZONTAL_DATUM,modified.getHorizontal_datum()) + .set(VIEW_LOCATIONS.HORIZONTAL_DATUM,modified.getHorizontalDatum()) .set(VIEW_LOCATIONS.LATITUDE,modified.getElevation()) - .set(VIEW_LOCATIONS.VERTICAL_DATUM,modified.getVertical_datum()) + .set(VIEW_LOCATIONS.VERTICAL_DATUM,modified.getVerticalDatum()) .where(VIEW_LOCATIONS.ID.eq(modified.getId())) .execute(); @@ -93,14 +93,14 @@ public void save(Location newObj) { VIEW_LOCATIONS.LONGITUDE, VIEW_LOCATIONS.HORIZONTAL_DATUM, VIEW_LOCATIONS.ELEVATION, - VIEW_LOCATIONS.HORIZONTAL_DATUM + VIEW_LOCATIONS.VERTICAL_DATUM ).values( newObj.getName(), newObj.getLatitude(), newObj.getLongitude(), - newObj.getHorizontal_datum(), + newObj.getHorizontalDatum(), newObj.getElevation(), - newObj.getVertical_datum() + newObj.getVerticalDatum() ).execute(); } diff --git a/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/Location.java b/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/Location.java index 97e3152..a4fe4d1 100644 --- a/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/Location.java +++ b/housedb-dao/src/main/java/net/hobbyscience/housedb/dto/Location.java @@ -13,34 +13,54 @@ public class Location{ private String parent; private Double latitude; private Double longitude; - private String horizontal_datum; + private String horizontalDatum; private Double elevation; - private String vertical_datum; + private String verticalDatum; + @JsonCreator - public Location( - @JsonProperty(value="id", defaultValue="-1",required=false) long id, - @JsonProperty("name") String name, - @JsonProperty(value="parent",defaultValue="",required=false) String parent, + public Location( + @JsonProperty("name") String name, @JsonProperty("latitude") Double latitude, @JsonProperty("longitude") Double longitude, - @JsonProperty("horizontal-datum") String horizontal_datum, + @JsonProperty("horizontalDatum") String horizontalDatum, @JsonProperty("elevation") Double elevation, - @JsonProperty("vertical_datum") String vertical_datum) { + @JsonProperty("verticalDatum") String verticalDatum) { + this.id = -1; + this.name = name; + this.parent = null; + this.latitude = latitude; + this.longitude = longitude; + this.horizontalDatum = horizontalDatum; + this.elevation = elevation; + this.verticalDatum = verticalDatum; + } + + public Location( + long id, + String name, + String parent, + Double latitude, + Double longitude, + String horizontalDatum, + Double elevation, + String verticalDatum) { this.id = id; this.name = name; this.parent = parent; this.latitude = latitude; this.longitude = longitude; - this.horizontal_datum = horizontal_datum; + this.horizontalDatum = horizontalDatum; this.elevation = elevation; - this.vertical_datum = vertical_datum; + this.verticalDatum = verticalDatum; } + @JsonIgnore public long getId(){ return this.id; } + @JsonIgnore public String getParent() { return this.parent; } @@ -53,16 +73,16 @@ public Double getLongitude() { return this.longitude; } - public String getHorizontal_datum() { - return this.horizontal_datum; + public String getHorizontalDatum() { + return this.horizontalDatum; } public Double getElevation() { return this.elevation; } - public String getVertical_datum() { - return this.vertical_datum; + public String getVerticalDatum() { + return this.verticalDatum; } public String getName() { @@ -78,12 +98,12 @@ public boolean equals(Object o) { return false; } Location location = (Location) o; - return Objects.equals(name, location.name) && Objects.equals(parent, location.parent) && Objects.equals(latitude, location.latitude) && Objects.equals(longitude, location.longitude) && Objects.equals(horizontal_datum, location.horizontal_datum) && Objects.equals(elevation, location.elevation) && Objects.equals(vertical_datum, location.vertical_datum); + return Objects.equals(name, location.name) && Objects.equals(parent, location.parent) && Objects.equals(latitude, location.latitude) && Objects.equals(longitude, location.longitude) && Objects.equals(horizontalDatum, location.horizontalDatum) && Objects.equals(elevation, location.elevation) && Objects.equals(verticalDatum, location.verticalDatum); } @Override public int hashCode() { - return Objects.hash(name, parent, latitude, longitude, horizontal_datum, elevation, vertical_datum); + return Objects.hash(name, parent, latitude, longitude, horizontalDatum, elevation, verticalDatum); } @@ -94,9 +114,9 @@ public String toString() { ", parent='" + parent + "'" + ", latitude='" + latitude + "'" + ", longitude='" + longitude + "'" + - ", horizontal_datum='" + horizontal_datum + "'" + + ", horizontal_datum='" + horizontalDatum + "'" + ", elevation='" + elevation + "'" + - ", vertical_datum='" + vertical_datum + "'" + + ", vertical_datum='" + verticalDatum + "'" + "}"; } } \ No newline at end of file diff --git a/z-api/src/main/java/net/hobbyscience/housedb/api/Entry.java b/z-api/src/main/java/net/hobbyscience/housedb/api/Entry.java index 54f0497..5b69eb2 100644 --- a/z-api/src/main/java/net/hobbyscience/housedb/api/Entry.java +++ b/z-api/src/main/java/net/hobbyscience/housedb/api/Entry.java @@ -30,15 +30,24 @@ import io.javalin.plugin.openapi.OpenApiPlugin; import io.javalin.plugin.openapi.ui.ReDocOptions; import io.javalin.plugin.openapi.ui.SwaggerOptions; +import io.swagger.v3.oas.models.Components; +import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.security.SecurityScheme; +import io.swagger.v3.oas.models.security.SecurityScheme.Type; public class Entry { private static final Logger logger = Logger.getLogger(Entry.class.getName()); public static OpenApiPlugin getOpenApiPlugin() { Info info = new Info().version("1.0").description("HouseDB API"); - OpenApiOptions ops = new OpenApiOptions(info) - .path("/swagger-docs") + Components components = new Components().addSecuritySchemes( + "bearerAuth", + new SecurityScheme().type(Type.HTTP).scheme("bearer").bearerFormat("JWT") + ); + + OpenApiOptions ops = new OpenApiOptions( () -> new OpenAPI().components(components).info(info) ); + ops.path("/swagger-docs") //.toJsonMapper(JacksonToJsonMapper.INSTANCE) .swagger(new SwaggerOptions("/swagger-ui")) .reDoc(new ReDocOptions("/redoc")) @@ -46,6 +55,7 @@ public static OpenApiPlugin getOpenApiPlugin() { doc.json("500", ErrorResponse.class); doc.json("503", ErrorResponse.class); }); + return new OpenApiPlugin(ops); } diff --git a/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/LocationController.java b/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/LocationController.java index 5571266..419346a 100644 --- a/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/LocationController.java +++ b/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/LocationController.java @@ -29,7 +29,7 @@ public void getAll( Context ctx){ var ds = ctx.appAttribute(DataSource.class); try( var conn = ds.getConnection(); ){ var db = new HouseDb(conn,ctx.attribute("username")); - var locations = db.getAllLocations(); + var locations = db.locationDao().getAll(); ctx.json(locations); } catch( Exception err ){ throw new RuntimeException("Error retrieving locations",err); @@ -65,6 +65,9 @@ public void getOne( Context ctx, String locationName ){ requestBody = @OpenApiRequestBody(content = {@OpenApiContent(from = Location.class)}), responses = { @OpenApiResponse(status="200", content = {@OpenApiContent( from = Location.class)}) + }, + security = { + @OpenApiSecurity(name = "bearerAuth") } ) public void create(Context ctx) { From fb1c7a9e8bdba032b9ebee666c7ff2c34f0b0da6 Mon Sep 17 00:00:00 2001 From: Mike Neilson Date: Tue, 25 May 2021 20:48:49 -0700 Subject: [PATCH 12/13] [api] fixed logging around bearer token. --- .../main/java/net/hobbyscience/housedb/api/Entry.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/z-api/src/main/java/net/hobbyscience/housedb/api/Entry.java b/z-api/src/main/java/net/hobbyscience/housedb/api/Entry.java index 5b69eb2..a8ffb74 100644 --- a/z-api/src/main/java/net/hobbyscience/housedb/api/Entry.java +++ b/z-api/src/main/java/net/hobbyscience/housedb/api/Entry.java @@ -126,17 +126,14 @@ public static void main(String []args) { var header = ctx.header(Header.AUTHORIZATION); if( header != null ){ // verification will be handled at the gateway - //val jwt = Jwts.parserBuilder().build().parseClaimsJws(ctx.header("Authorization")) - logger.info(header); + //val jwt = Jwts.parserBuilder().build().parseClaimsJws(ctx.header("Authorization")) var parts = header.split("\\\\s+"); - var jwt = parts[parts.length-1].split("\\."); - logger.info(""+jwt.length); + var jwt = parts[parts.length-1].split("\\."); var jwtClaims = Base64.getDecoder().decode(jwt[1]); var jsonClaims = om.readTree(jwtClaims); - //val user = jwt.subject() - logger.fine(jsonClaims.toString()); + //val user = jwt.subject() var user = om.treeToValue(jsonClaims.get("sub"),String.class); - logger.fine(user); + logger.fine(user); ctx.attribute("username",user); } else { ctx.attribute("username","guest"); From 284cdfbb6a68b87dada96e5005187335365cd7a0 Mon Sep 17 00:00:00 2001 From: Mike Neilson Date: Tue, 25 May 2021 21:01:18 -0700 Subject: [PATCH 13/13] [api] added auth mechanism to api docs. --- .../housedb/api/controllers/LocationController.java | 12 ++++++++++++ .../api/controllers/TimeSeriesController.java | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/LocationController.java b/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/LocationController.java index 419346a..8bf92d2 100644 --- a/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/LocationController.java +++ b/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/LocationController.java @@ -22,6 +22,9 @@ public class LocationController implements CrudHandler { tags = {"Locations"}, responses = { @OpenApiResponse(status="200", content = {@OpenApiContent( from = Location[].class,isArray=true)}) + }, + security = { + @OpenApiSecurity(name = "bearerAuth") } ) @Override @@ -43,6 +46,9 @@ public void getAll( Context ctx){ responses = { @OpenApiResponse(status="200", content = {@OpenApiContent( from = Location.class)}) }, + security = { + @OpenApiSecurity(name = "bearerAuth") + }, tags = {"Locations"} ) public void getOne( Context ctx, String locationName ){ @@ -88,11 +94,17 @@ public void create(Context ctx) { @OpenApi( + security = { + @OpenApiSecurity(name = "bearerAuth") + }, tags = {"Locations"} ) public void update(Context ctx, String locationName){ throw new UnsupportedOperationException("not implemented yet"); } @OpenApi( + security = { + @OpenApiSecurity(name = "bearerAuth") + }, tags = {"Locations"} ) public void delete(Context ctx, String locationName){ throw new UnsupportedOperationException("not implemented yet"); } diff --git a/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/TimeSeriesController.java b/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/TimeSeriesController.java index 06c807e..21064cb 100644 --- a/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/TimeSeriesController.java +++ b/z-api/src/main/java/net/hobbyscience/housedb/api/controllers/TimeSeriesController.java @@ -44,6 +44,9 @@ public void getAll(Context ctx){ responses = { @OpenApiResponse(status="200", content = {@OpenApiContent( from = TimeSeries.class)}), @OpenApiResponse(status="404", content = {@OpenApiContent( from = NotFoundResponse.class)}) + }, + security = { + @OpenApiSecurity(name = "bearerAuth") } ) public void getOne(Context ctx, String locationName){ @@ -97,11 +100,17 @@ public void create(Context ctx){ } @OpenApi( + security = { + @OpenApiSecurity(name = "bearerAuth") + }, tags = {"TimeSeries"} ) public void update(Context ctx, String locationName){ throw new UnsupportedOperationException("not implemented yet"); } @OpenApi( + security = { + @OpenApiSecurity(name = "bearerAuth") + }, tags = {"TimeSeries"} ) public void delete(Context ctx , String locationName){ throw new UnsupportedOperationException("not implemented yet"); }