Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
V
VIPSLogic
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
VIPS
VIPSLogic
Commits
b40ffad7
Commit
b40ffad7
authored
2 years ago
by
Tor-Einar Skog
Browse files
Options
Downloads
Patches
Plain Diff
Improving documentation for the endpoints in POIService
parent
2bc5c365
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/no/nibio/vips/logic/service/POIService.java
+99
-107
99 additions, 107 deletions
src/main/java/no/nibio/vips/logic/service/POIService.java
with
99 additions
and
107 deletions
src/main/java/no/nibio/vips/logic/service/POIService.java
+
99
−
107
View file @
b40ffad7
/*
* Copyright (c) 202
1
NIBIO <http://www.nibio.no/>.
* Copyright (c) 202
2
NIBIO <http://www.nibio.no/>.
*
* This file is part of VIPSLogic.
* VIPSLogic is free software: you can redistribute it and/or modify
...
...
@@ -40,184 +40,176 @@ import javax.ws.rs.core.Response.Status;
import
org.jboss.resteasy.spi.HttpRequest
;
import
org.locationtech.jts.geom.Coordinate
;
import
org.locationtech.jts.geom.Geometry
;
import
org.locationtech.jts.geom.Point
;
import
com.fasterxml.jackson.core.type.TypeReference
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.webcohesion.enunciate.metadata.Facet
;
import
com.webcohesion.enunciate.metadata.rs.TypeHint
;
import
no.nibio.vips.gis.GISUtil
;
import
no.nibio.vips.logic.entity.Country
;
import
no.nibio.vips.logic.entity.Observation
;
import
no.nibio.vips.logic.entity.ObservationIllustrationPK
;
import
no.nibio.vips.logic.entity.Organization
;
import
no.nibio.vips.logic.entity.PointOfInterest
;
import
no.nibio.vips.logic.entity.PointOfInterestWeatherStation
;
import
no.nibio.vips.logic.entity.PolygonService
;
import
no.nibio.vips.logic.entity.VipsLogicUser
;
import
no.nibio.vips.logic.util.Globals
;
import
no.nibio.vips.logic.controller.session.SessionControllerGetter
;
/**
* @copyright 202
1
<a href="http://www.nibio.no/">NIBIO</a>
* @copyright 202
2
<a href="http://www.nibio.no/">NIBIO</a>
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
@Path
(
"rest/poi"
)
public
class
POIService
{
@Context
@Context
private
HttpRequest
httpRequest
;
@Context
private
HttpServletRequest
httpServletRequest
;
/**
* Get a list of locations (pois) for a given organization
*
* @param organizationId
* @return
* @return
*/
@GET
@Path
(
"organization/{organizationId}"
)
@Produces
(
"application/json;charset=UTF-8"
)
public
Response
getPoisForOrganization
(
@PathParam
(
"organizationId"
)
Integer
organizationId
)
{
@TypeHint
(
PointOfInterestWeatherStation
[].
class
)
public
Response
getPoisForOrganization
(
@PathParam
(
"organizationId"
)
Integer
organizationId
)
{
Organization
organization
=
SessionControllerGetter
.
getUserBean
().
getOrganization
(
organizationId
);
List
<
PointOfInterestWeatherStation
>
retVal
=
SessionControllerGetter
.
getPointOfInterestBean
().
getWeatherstationsForOrganization
(
organization
,
Boolean
.
TRUE
);
return
Response
.
ok
().
entity
(
retVal
).
build
();
}
/**
*
*
* @param pointOfInterestId
* @return a particular POI (Point of interest)
*/
@GET
@Path
(
"{pointOfInterestId}"
)
@Produces
(
"application/json;charset=UTF-8"
)
public
Response
getPoi
(
@PathParam
(
"pointOfInterestId"
)
Integer
pointOfInterestId
)
{
@TypeHint
(
PointOfInterest
.
class
)
public
Response
getPoi
(
@PathParam
(
"pointOfInterestId"
)
Integer
pointOfInterestId
)
{
PointOfInterest
retVal
=
SessionControllerGetter
.
getPointOfInterestBean
().
getPointOfInterest
(
pointOfInterestId
);
return
Response
.
ok
().
entity
(
retVal
).
build
();
}
/**
* Find a POI (Point of interest) by name
*
* @param poiName
* @return
* @return
*/
@GET
@Path
(
"name/{poiName}"
)
@Produces
(
"application/json;charset=UTF-8"
)
public
Response
getPoiByName
(
@PathParam
(
"poiName"
)
String
poiName
)
{
@TypeHint
(
PointOfInterest
.
class
)
public
Response
getPoiByName
(
@PathParam
(
"poiName"
)
String
poiName
)
{
PointOfInterest
retVal
=
SessionControllerGetter
.
getPointOfInterestBean
().
getPointOfInterest
(
poiName
);
return
retVal
!=
null
?
Response
.
ok
().
entity
(
retVal
).
build
()
:
Response
.
noContent
().
build
();
}
/**
* If used outside of VIPSLogic: Requires a valid UUID to be provided in the Authorization header
* @return
* If used outside of VIPSLogic: Requires a valid UUID to be provided in the
* Authorization header
*
* @return
*/
@GET
@Path
(
"user"
)
@Produces
(
"application/json;charset=UTF-8"
)
@Facet
(
"restricted"
)
public
Response
getPoisForCurrentUser
(
)
{
@TypeHint
(
PointOfInterest
[].
class
)
public
Response
getPoisForCurrentUser
()
{
VipsLogicUser
user
=
(
VipsLogicUser
)
httpServletRequest
.
getSession
().
getAttribute
(
"user"
);
// Could be the VIPS obs app or some other client using UUID
if
(
user
==
null
)
{
String
uuidStr
=
httpServletRequest
.
getHeader
(
HttpHeaders
.
AUTHORIZATION
);
UUID
uuid
=
UUID
.
fromString
(
uuidStr
);
user
=
SessionControllerGetter
.
getUserBean
().
findVipsLogicUser
(
uuid
);
if
(
user
==
null
)
{
return
Response
.
status
(
Status
.
UNAUTHORIZED
).
build
();
}
if
(
user
==
null
)
{
String
uuidStr
=
httpServletRequest
.
getHeader
(
HttpHeaders
.
AUTHORIZATION
);
UUID
uuid
=
UUID
.
fromString
(
uuidStr
);
user
=
SessionControllerGetter
.
getUserBean
().
findVipsLogicUser
(
uuid
);
if
(
user
==
null
)
{
return
Response
.
status
(
Status
.
UNAUTHORIZED
).
build
();
}
}
List
<
PointOfInterest
>
retVal
=
SessionControllerGetter
.
getPointOfInterestBean
().
getRelevantPointOfInterestsForUser
(
user
);
return
Response
.
ok
().
entity
(
retVal
).
build
();
}
/**
* TODO Add request example
* @param poiJson
* @return
*/
@POST
@Path
(
"syncpoifromapp"
)
@Consumes
(
"application/json;charset=UTF-8"
)
@Produces
(
"application/json;charset=UTF-8"
)
public
Response
syncPOIFromApp
(
String
poiJson
)
{
VipsLogicUser
user
=
SessionControllerGetter
.
getUserBean
().
getUserFromUUID
(
httpServletRequest
);
if
(
user
==
null
)
{
return
Response
.
status
(
Status
.
UNAUTHORIZED
).
build
();
}
ObjectMapper
oM
=
new
ObjectMapper
();
SimpleDateFormat
df
=
new
SimpleDateFormat
(
Globals
.
defaultTimestampFormat
);
try
{
Map
<
Object
,
Object
>
mapFromApp
=
oM
.
readValue
(
poiJson
,
new
TypeReference
<
HashMap
<
Object
,
Object
>>(){});
// Check if it is marked as deleted or not
if
(
mapFromApp
.
get
(
"deleted"
)
!=
null
&&
((
Boolean
)
mapFromApp
.
get
(
"deleted"
).
equals
(
true
)))
{
if
(
SessionControllerGetter
.
getPointOfInterestBean
().
getPointOfInterest
((
Integer
)
mapFromApp
.
get
(
"pointOfInterestId"
))
!=
null
)
{
SessionControllerGetter
.
getPointOfInterestBean
().
deletePoi
((
Integer
)
mapFromApp
.
get
(
"pointOfInterestId"
));
return
Response
.
ok
().
build
();
}
else
{
return
Response
.
status
(
Status
.
NOT_FOUND
).
build
();
}
}
else
{
PointOfInterest
mergePoi
=
((
Integer
)
mapFromApp
.
get
(
"pointOfInterestId"
))
>
0
?
SessionControllerGetter
.
getPointOfInterestBean
().
getPointOfInterest
((
Integer
)
mapFromApp
.
get
(
"pointOfInterestId"
)):
PointOfInterest
.
getInstance
((
Integer
)
mapFromApp
.
get
(
"pointOfInterestTypeId"
));
// Trying to sync a non-existing observation
if
(
mergePoi
==
null
)
{
return
Response
.
status
(
Status
.
NOT_FOUND
).
build
();
}
mergePoi
.
setName
((
String
)
mapFromApp
.
get
(
"name"
));
mergePoi
.
setTimeZone
(
mapFromApp
.
get
(
"timeZone"
)
!=
null
?
(
String
)
mapFromApp
.
get
(
"timeZone"
)
:
user
.
getOrganizationId
().
getDefaultTimeZone
()
);
mergePoi
.
setLongitude
((
Double
)
mapFromApp
.
get
(
"longitude"
));
mergePoi
.
setLatitude
((
Double
)
mapFromApp
.
get
(
"latitude"
));
try
{
Double
altitude
=
mapFromApp
.
get
(
"altitude"
)
instanceof
Integer
?
((
Integer
)
mapFromApp
.
get
(
"altitude"
)).
doubleValue
()
:(
Double
)
mapFromApp
.
get
(
"altitude"
);
mergePoi
.
setAltitude
(
altitude
);
}
catch
(
NullPointerException
|
ClassCastException
ex
)
{
mergePoi
.
setAltitude
(
0.0
);
}
mergePoi
.
setCountryCode
(
mapFromApp
.
get
(
"countryCode"
)
!=
null
?
new
Country
((
String
)(((
Map
<
Object
,
Object
>)
mapFromApp
.
get
(
"countryCode"
)).
get
(
"countryCode"
)))
:
user
.
getOrganizationId
().
getCountryCode
()
);
mergePoi
.
setUser
(
user
);
mergePoi
.
setLastEditedTime
(
new
Date
());
GISUtil
gisUtil
=
new
GISUtil
();
Coordinate
coordinate
=
new
Coordinate
(
mergePoi
.
getLongitude
(),
mergePoi
.
getLatitude
(),
mergePoi
.
getAltitude
());
Point
p3d
=
gisUtil
.
createPointWGS84
(
coordinate
);
mergePoi
.
setGisGeom
(
p3d
);
mergePoi
=
SessionControllerGetter
.
getPointOfInterestBean
().
getPointOfInterest
(
SessionControllerGetter
.
getPointOfInterestBean
().
storePoi
(
mergePoi
).
getPointOfInterestId
());
return
Response
.
ok
().
entity
(
mergePoi
).
build
();
}
}
catch
(
IOException
e
)
{
return
Response
.
serverError
().
entity
(
e
).
build
();
}
String
poiJson
)
{
VipsLogicUser
user
=
SessionControllerGetter
.
getUserBean
().
getUserFromUUID
(
httpServletRequest
);
if
(
user
==
null
)
{
return
Response
.
status
(
Status
.
UNAUTHORIZED
).
build
();
}
ObjectMapper
oM
=
new
ObjectMapper
();
SimpleDateFormat
df
=
new
SimpleDateFormat
(
Globals
.
defaultTimestampFormat
);
try
{
Map
<
Object
,
Object
>
mapFromApp
=
oM
.
readValue
(
poiJson
,
new
TypeReference
<
HashMap
<
Object
,
Object
>>()
{
});
// Check if it is marked as deleted or not
if
(
mapFromApp
.
get
(
"deleted"
)
!=
null
&&
((
Boolean
)
mapFromApp
.
get
(
"deleted"
).
equals
(
true
)))
{
if
(
SessionControllerGetter
.
getPointOfInterestBean
().
getPointOfInterest
((
Integer
)
mapFromApp
.
get
(
"pointOfInterestId"
))
!=
null
)
{
SessionControllerGetter
.
getPointOfInterestBean
().
deletePoi
((
Integer
)
mapFromApp
.
get
(
"pointOfInterestId"
));
return
Response
.
ok
().
build
();
}
else
{
return
Response
.
status
(
Status
.
NOT_FOUND
).
build
();
}
}
else
{
PointOfInterest
mergePoi
=
((
Integer
)
mapFromApp
.
get
(
"pointOfInterestId"
))
>
0
?
SessionControllerGetter
.
getPointOfInterestBean
().
getPointOfInterest
((
Integer
)
mapFromApp
.
get
(
"pointOfInterestId"
))
:
PointOfInterest
.
getInstance
((
Integer
)
mapFromApp
.
get
(
"pointOfInterestTypeId"
));
// Trying to sync a non-existing observation
if
(
mergePoi
==
null
)
{
return
Response
.
status
(
Status
.
NOT_FOUND
).
build
();
}
mergePoi
.
setName
((
String
)
mapFromApp
.
get
(
"name"
));
mergePoi
.
setTimeZone
(
mapFromApp
.
get
(
"timeZone"
)
!=
null
?
(
String
)
mapFromApp
.
get
(
"timeZone"
)
:
user
.
getOrganizationId
().
getDefaultTimeZone
()
);
mergePoi
.
setLongitude
((
Double
)
mapFromApp
.
get
(
"longitude"
));
mergePoi
.
setLatitude
((
Double
)
mapFromApp
.
get
(
"latitude"
));
try
{
Double
altitude
=
mapFromApp
.
get
(
"altitude"
)
instanceof
Integer
?
((
Integer
)
mapFromApp
.
get
(
"altitude"
)).
doubleValue
()
:
(
Double
)
mapFromApp
.
get
(
"altitude"
);
mergePoi
.
setAltitude
(
altitude
);
}
catch
(
NullPointerException
|
ClassCastException
ex
)
{
mergePoi
.
setAltitude
(
0.0
);
}
mergePoi
.
setCountryCode
(
mapFromApp
.
get
(
"countryCode"
)
!=
null
?
new
Country
((
String
)
(((
Map
<
Object
,
Object
>)
mapFromApp
.
get
(
"countryCode"
)).
get
(
"countryCode"
)))
:
user
.
getOrganizationId
().
getCountryCode
()
);
mergePoi
.
setUser
(
user
);
mergePoi
.
setLastEditedTime
(
new
Date
());
GISUtil
gisUtil
=
new
GISUtil
();
Coordinate
coordinate
=
new
Coordinate
(
mergePoi
.
getLongitude
(),
mergePoi
.
getLatitude
(),
mergePoi
.
getAltitude
());
Point
p3d
=
gisUtil
.
createPointWGS84
(
coordinate
);
mergePoi
.
setGisGeom
(
p3d
);
mergePoi
=
SessionControllerGetter
.
getPointOfInterestBean
().
getPointOfInterest
(
SessionControllerGetter
.
getPointOfInterestBean
().
storePoi
(
mergePoi
).
getPointOfInterestId
());
return
Response
.
ok
().
entity
(
mergePoi
).
build
();
}
}
catch
(
IOException
e
)
{
return
Response
.
serverError
().
entity
(
e
).
build
();
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment