1 package de.dlr.shepard.context.collection.endpoints; 2 3 import de.dlr.shepard.common.filters.Subscribable; 4 import de.dlr.shepard.common.util.Constants; 5 import de.dlr.shepard.common.util.QueryParamHelper; 6 import de.dlr.shepard.context.collection.entities.DataObject; 7 import de.dlr.shepard.context.collection.io.DataObjectIO; 8 import de.dlr.shepard.context.collection.services.DataObjectService; 9 import jakarta.enterprise.context.RequestScoped; 10 import jakarta.inject.Inject; 11 import jakarta.validation.Valid; 12 import jakarta.validation.constraints.NotNull; 13 import jakarta.validation.constraints.Positive; 14 import jakarta.validation.constraints.PositiveOrZero; 15 import jakarta.ws.rs.Consumes; 16 import jakarta.ws.rs.DELETE; 17 import jakarta.ws.rs.GET; 18 import jakarta.ws.rs.POST; 19 import jakarta.ws.rs.PUT; 20 import jakarta.ws.rs.Path; 21 import jakarta.ws.rs.PathParam; 22 import jakarta.ws.rs.Produces; 23 import jakarta.ws.rs.QueryParam; 24 import jakarta.ws.rs.core.Context; 25 import jakarta.ws.rs.core.MediaType; 26 import jakarta.ws.rs.core.Response; 27 import jakarta.ws.rs.core.Response.Status; 28 import jakarta.ws.rs.core.SecurityContext; 29 import java.util.ArrayList; 30 import java.util.UUID; 31 import org.eclipse.microprofile.openapi.annotations.Operation; 32 import org.eclipse.microprofile.openapi.annotations.enums.SchemaType; 33 import org.eclipse.microprofile.openapi.annotations.media.Content; 34 import org.eclipse.microprofile.openapi.annotations.media.Schema; 35 import org.eclipse.microprofile.openapi.annotations.parameters.Parameter; 36 import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody; 37 import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; 38 import org.eclipse.microprofile.openapi.annotations.tags.Tag; 39 40 @Consumes(MediaType.APPLICATION_JSON) 41 @Produces(MediaType.APPLICATION_JSON) 42 @Path(Constants.COLLECTIONS + "/{" + Constants.COLLECTION_ID + "}/" + Constants.DATA_OBJECTS) 43 @RequestScoped 44 public class DataObjectRest { 45 46 @Inject 47 DataObjectService dataObjectService; 48 49 @Context 50 private SecurityContext securityContext; 51 52 @GET 53 @Tag(name = Constants.DATA_OBJECT) 54 @Operation(description = "Get all dataObjects") 55 @APIResponse( 56 description = "ok", 57 responseCode = "200", 58 content = @Content(schema = @Schema(type = SchemaType.ARRAY, implementation = DataObjectIO.class)) 59 ) 60 @APIResponse(responseCode = "400", description = "bad request") 61 @APIResponse(responseCode = "401", description = "not authorized") 62 @APIResponse(responseCode = "404", description = "not found") 63 @Parameter(name = Constants.COLLECTION_ID) 64 @Parameter(name = Constants.QP_NAME) 65 @Parameter(name = Constants.QP_PAGE) 66 @Parameter(name = Constants.QP_SIZE) 67 @Parameter(name = Constants.QP_PARENT_ID) 68 @Parameter(name = Constants.QP_PREDECESSOR_ID) 69 @Parameter(name = Constants.QP_SUCCESSOR_ID) 70 @Parameter(name = Constants.QP_ORDER_BY_ATTRIBUTE) 71 @Parameter(name = Constants.QP_ORDER_DESC) 72 @Parameter(name = Constants.VERSION_UID) 73 public Response getAllDataObjects( 74 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId, 75 @QueryParam(Constants.QP_NAME) String name, 76 @QueryParam(Constants.QP_PAGE) @PositiveOrZero Integer page, 77 @QueryParam(Constants.QP_SIZE) @Positive Integer size, 78 @QueryParam(Constants.QP_PARENT_ID) Long parentId, 79 @QueryParam(Constants.QP_PREDECESSOR_ID) Long predecessorId, 80 @QueryParam(Constants.QP_SUCCESSOR_ID) Long successorId, 81 @QueryParam(Constants.QP_ORDER_BY_ATTRIBUTE) DataObjectAttributes orderBy, 82 @QueryParam(Constants.QP_ORDER_DESC) Boolean orderDesc, 83 @QueryParam(Constants.VERSION_UID) @org.hibernate.validator.constraints.UUID String versionUID 84 ) { 85 UUID versionUUID = null; 86 if (versionUID != null) { 87 versionUUID = UUID.fromString(versionUID); 88 } 89 90 var paramsWithShepardIds = new QueryParamHelper(); 91 if (name != null) paramsWithShepardIds = paramsWithShepardIds.withName(name); 92 if (page != null && size != null) paramsWithShepardIds = paramsWithShepardIds.withPageAndSize(page, size); 93 if (parentId != null) paramsWithShepardIds = paramsWithShepardIds.withParentId(parentId); 94 if (predecessorId != null) paramsWithShepardIds = paramsWithShepardIds.withPredecessorId(predecessorId); 95 if (successorId != null) paramsWithShepardIds = paramsWithShepardIds.withSuccessorId(successorId); 96 if (orderBy != null) paramsWithShepardIds = paramsWithShepardIds.withOrderByAttribute(orderBy, orderDesc); 97 98 var dataObjects = dataObjectService.getAllDataObjectsByShepardIds(collectionId, paramsWithShepardIds, versionUUID); 99 var result = new ArrayList<DataObjectIO>(dataObjects.size()); 100 101 for (var dataObject : dataObjects) { 102 result.add(new DataObjectIO(dataObject)); 103 } 104 return Response.ok(result).build(); 105 } 106 107 @GET 108 @Path("/{" + Constants.DATA_OBJECT_ID + "}") 109 @Tag(name = Constants.DATA_OBJECT) 110 @Operation(description = "Get dataObject") 111 @APIResponse( 112 description = "ok", 113 responseCode = "200", 114 content = @Content(schema = @Schema(implementation = DataObjectIO.class)) 115 ) 116 @APIResponse(responseCode = "400", description = "bad request") 117 @APIResponse(responseCode = "401", description = "not authorized") 118 @APIResponse(responseCode = "403", description = "forbidden") 119 @APIResponse(responseCode = "404", description = "not found") 120 @Parameter(name = Constants.COLLECTION_ID) 121 @Parameter(name = Constants.DATA_OBJECT_ID) 122 @Parameter(name = Constants.VERSION_UID) 123 public Response getDataObject( 124 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId, 125 @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId, 126 @QueryParam(Constants.VERSION_UID) @org.hibernate.validator.constraints.UUID String versionUID 127 ) { 128 UUID versionUUID = null; 129 if (versionUID != null) { 130 versionUUID = UUID.fromString(versionUID); 131 } 132 133 DataObject dataObject = dataObjectService.getDataObject(collectionId, dataObjectId, versionUUID); 134 return Response.ok(new DataObjectIO(dataObject)).build(); 135 } 136 137 @POST 138 @Subscribable 139 @Tag(name = Constants.DATA_OBJECT) 140 @Operation(description = "Create a new dataObject") 141 @APIResponse( 142 description = "created", 143 responseCode = "201", 144 content = @Content(schema = @Schema(implementation = DataObjectIO.class)) 145 ) 146 @APIResponse(responseCode = "400", description = "bad request") 147 @APIResponse(responseCode = "401", description = "not authorized") 148 @APIResponse(responseCode = "403", description = "forbidden") 149 @APIResponse(responseCode = "404", description = "not found") 150 @Parameter(name = Constants.COLLECTION_ID) 151 public Response createDataObject( 152 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId, 153 @RequestBody( 154 required = true, 155 content = @Content(schema = @Schema(implementation = DataObjectIO.class)) 156 ) @Valid DataObjectIO dataObject 157 ) { 158 DataObject newDataObject = dataObjectService.createDataObject(collectionId, dataObject); 159 return Response.ok(new DataObjectIO(newDataObject)).status(Status.CREATED).build(); 160 } 161 162 @PUT 163 @Path("/{" + Constants.DATA_OBJECT_ID + "}") 164 @Subscribable 165 @Tag(name = Constants.DATA_OBJECT) 166 @Operation(description = "Update dataObject") 167 @APIResponse( 168 description = "ok", 169 responseCode = "200", 170 content = @Content(schema = @Schema(implementation = DataObjectIO.class)) 171 ) 172 @APIResponse(responseCode = "400", description = "bad request") 173 @APIResponse(responseCode = "401", description = "not authorized") 174 @APIResponse(responseCode = "403", description = "forbidden") 175 @APIResponse(responseCode = "404", description = "not found") 176 @Parameter(name = Constants.COLLECTION_ID) 177 @Parameter(name = Constants.DATA_OBJECT_ID) 178 public Response updateDataObject( 179 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId, 180 @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId, 181 @RequestBody( 182 required = true, 183 content = @Content(schema = @Schema(implementation = DataObjectIO.class)) 184 ) @Valid DataObjectIO dataObject 185 ) { 186 DataObject updatedDataObject = dataObjectService.updateDataObject(collectionId, dataObjectId, dataObject); 187 return Response.ok(new DataObjectIO(updatedDataObject)).build(); 188 } 189 190 @DELETE 191 @Path("/{" + Constants.DATA_OBJECT_ID + "}") 192 @Subscribable 193 @Tag(name = Constants.DATA_OBJECT) 194 @Operation(description = "Delete dataObject") 195 @APIResponse(description = "deleted", responseCode = "204") 196 @APIResponse(responseCode = "400", description = "bad request") 197 @APIResponse(responseCode = "401", description = "not authorized") 198 @APIResponse(responseCode = "403", description = "forbidden") 199 @APIResponse(responseCode = "404", description = "not found") 200 @Parameter(name = Constants.COLLECTION_ID) 201 @Parameter(name = Constants.DATA_OBJECT_ID) 202 public Response deleteDataObject( 203 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId, 204 @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId 205 ) { 206 dataObjectService.deleteDataObject(collectionId, dataObjectId); 207 return Response.status(Status.NO_CONTENT).build(); 208 } 209 }