View Javadoc
1   package de.dlr.shepard.endpoints;
2   
3   import java.io.IOException;
4   import java.util.ArrayList;
5   import java.util.Set;
6   
7   import de.dlr.shepard.filters.Subscribable;
8   import de.dlr.shepard.influxDB.FillOption;
9   import de.dlr.shepard.influxDB.SingleValuedUnaryFunction;
10  import de.dlr.shepard.neo4Core.io.TimeseriesReferenceIO;
11  import de.dlr.shepard.neo4Core.services.TimeseriesReferenceService;
12  import de.dlr.shepard.util.Constants;
13  import jakarta.ws.rs.Consumes;
14  import jakarta.ws.rs.DELETE;
15  import jakarta.ws.rs.GET;
16  import jakarta.ws.rs.POST;
17  import jakarta.ws.rs.Path;
18  import jakarta.ws.rs.PathParam;
19  import jakarta.ws.rs.Produces;
20  import jakarta.ws.rs.QueryParam;
21  import jakarta.ws.rs.core.Context;
22  import jakarta.ws.rs.core.MediaType;
23  import jakarta.ws.rs.core.Response;
24  import jakarta.ws.rs.core.Response.Status;
25  import jakarta.ws.rs.core.SecurityContext;
26  
27  @Consumes(MediaType.APPLICATION_JSON)
28  @Produces(MediaType.APPLICATION_JSON)
29  @Path(Constants.COLLECTIONS + "/{" + Constants.COLLECTION_ID + "}/" + Constants.DATAOBJECTS + "/{"
30  		+ Constants.DATAOBJECT_ID + "}/" + Constants.TIMESERIES_REFERENCES)
31  public class TimeseriesReferenceRestImpl implements TimeseriesReferenceRest {
32  	private TimeseriesReferenceService timeseriesReferenceService = new TimeseriesReferenceService();
33  
34  	@Context
35  	private SecurityContext securityContext;
36  
37  	@GET
38  	@Override
39  	public Response getAllTimeseriesReferences(@PathParam(Constants.COLLECTION_ID) long collectionId,
40  			@PathParam(Constants.DATAOBJECT_ID) long dataObjectId) {
41  		var references = timeseriesReferenceService.getAllReferencesByDataObjectShepardId(dataObjectId);
42  		var result = new ArrayList<TimeseriesReferenceIO>(references.size());
43  		for (var reference : references) {
44  			result.add(new TimeseriesReferenceIO(reference));
45  		}
46  
47  		return Response.ok(result).build();
48  	}
49  
50  	@GET
51  	@Path("/{" + Constants.TIMESERIES_REFERENCE_ID + "}")
52  	@Override
53  	public Response getTimeseriesReference(@PathParam(Constants.COLLECTION_ID) long collectionId,
54  			@PathParam(Constants.DATAOBJECT_ID) long dataObjectId,
55  			@PathParam(Constants.TIMESERIES_REFERENCE_ID) long timeseriesId) {
56  		var result = timeseriesReferenceService.getReferenceByShepardId(timeseriesId);
57  
58  		return Response.ok(new TimeseriesReferenceIO(result)).build();
59  	}
60  
61  	@POST
62  	@Subscribable
63  	@Override
64  	public Response createTimeseriesReference(@PathParam(Constants.COLLECTION_ID) long collectionId,
65  			@PathParam(Constants.DATAOBJECT_ID) long dataObjectId, TimeseriesReferenceIO timeseriesReference) {
66  		var result = timeseriesReferenceService.createReferenceByShepardId(dataObjectId, timeseriesReference,
67  				securityContext.getUserPrincipal().getName());
68  
69  		return Response.ok(new TimeseriesReferenceIO(result)).status(Status.CREATED).build();
70  	}
71  
72  	@DELETE
73  	@Path("/{" + Constants.TIMESERIES_REFERENCE_ID + "}")
74  	@Subscribable
75  	@Override
76  	public Response deleteTimeseriesReference(@PathParam(Constants.COLLECTION_ID) long collectionId,
77  			@PathParam(Constants.DATAOBJECT_ID) long dataObjectId,
78  			@PathParam(Constants.TIMESERIES_REFERENCE_ID) long timeseriesId) {
79  		var result = timeseriesReferenceService.deleteReferenceByShepardId(timeseriesId,
80  				securityContext.getUserPrincipal().getName());
81  
82  		return result ? Response.status(Status.NO_CONTENT).build()
83  				: Response.status(Status.INTERNAL_SERVER_ERROR).build();
84  	}
85  
86  	@GET
87  	@Path("/{" + Constants.TIMESERIES_REFERENCE_ID + "}/" + Constants.PAYLOAD)
88  	@Override
89  	public Response getTimeseriesPayload(@PathParam(Constants.COLLECTION_ID) long collectionId,
90  			@PathParam(Constants.DATAOBJECT_ID) long dataObjectId,
91  			@PathParam(Constants.TIMESERIES_REFERENCE_ID) long timeseriesReferenceId,
92  			@QueryParam(Constants.FUNCTION) SingleValuedUnaryFunction function,
93  			@QueryParam(Constants.GROUP_BY) Long groupBy, @QueryParam(Constants.FILLOPTION) FillOption fillOption,
94  			@QueryParam(Constants.DEVICE) Set<String> deviceFilterTag,
95  			@QueryParam(Constants.LOCATION) Set<String> locationFilterTag,
96  			@QueryParam(Constants.SYMBOLICNAME) Set<String> symbolicNameFilterTag) {
97  		var payload = timeseriesReferenceService.getTimeseriesPayloadByShepardId(timeseriesReferenceId, function,
98  				groupBy, fillOption, deviceFilterTag, locationFilterTag, symbolicNameFilterTag,
99  				securityContext.getUserPrincipal().getName());
100 		return Response.ok(payload).build();
101 	}
102 
103 	@GET
104 	@Path("/{" + Constants.TIMESERIES_REFERENCE_ID + "}/" + Constants.EXPORT)
105 	@Produces({ MediaType.APPLICATION_OCTET_STREAM, MediaType.APPLICATION_JSON })
106 	@Override
107 	public Response exportTimeseriesPayload(@PathParam(Constants.COLLECTION_ID) long collectionId,
108 			@PathParam(Constants.DATAOBJECT_ID) long dataObjectId,
109 			@PathParam(Constants.TIMESERIES_REFERENCE_ID) long timeseriesReferenceId,
110 			@QueryParam(Constants.FUNCTION) SingleValuedUnaryFunction function,
111 			@QueryParam(Constants.GROUP_BY) Long groupBy, @QueryParam(Constants.FILLOPTION) FillOption fillOption,
112 			@QueryParam(Constants.DEVICE) Set<String> deviceFilterTag,
113 			@QueryParam(Constants.LOCATION) Set<String> locationFilterTag,
114 			@QueryParam(Constants.SYMBOLICNAME) Set<String> symbolicNameFilterTag) throws IOException {
115 		var stream = timeseriesReferenceService.exportTimeseriesPayloadByShepardId(timeseriesReferenceId, function,
116 				groupBy, fillOption, deviceFilterTag, locationFilterTag, symbolicNameFilterTag,
117 				securityContext.getUserPrincipal().getName());
118 		if (stream == null)
119 			return Response.status(Status.NOT_FOUND).build();
120 		return Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM).build();
121 
122 	}
123 
124 }