|
| 1 | +package org.lfenergy.compas.scl.data.rest.v1; |
| 2 | + |
| 3 | +import io.quarkus.security.Authenticated; |
| 4 | +import jakarta.enterprise.context.RequestScoped; |
| 5 | +import jakarta.inject.Inject; |
| 6 | +import jakarta.ws.rs.Path; |
| 7 | +import jakarta.ws.rs.core.MediaType; |
| 8 | +import jakarta.ws.rs.core.Response; |
| 9 | +import org.lfenergy.compas.scl.data.model.IHistoryMetaItem; |
| 10 | +import org.lfenergy.compas.scl.data.model.Version; |
| 11 | +import org.lfenergy.compas.scl.data.service.CompasSclHistoryService; |
| 12 | +import org.lfenergy.compas.scl.extensions.model.SclFileType; |
| 13 | +import org.lfenergy.compas.scl.rest.api.HistoryResource; |
| 14 | +import org.lfenergy.compas.scl.rest.api.beans.DataResourceHistory; |
| 15 | +import org.lfenergy.compas.scl.rest.api.beans.DataResourceSearch; |
| 16 | +import org.lfenergy.compas.scl.rest.api.beans.DataResourcesResult; |
| 17 | + |
| 18 | +import java.time.OffsetDateTime; |
| 19 | +import java.util.List; |
| 20 | +import java.util.UUID; |
| 21 | + |
| 22 | +@Authenticated |
| 23 | +@RequestScoped |
| 24 | +@Path("/api") |
| 25 | +public class CompasSclHistoryResource implements HistoryResource { |
| 26 | + |
| 27 | + private final CompasSclHistoryService compasSclHistoryService; |
| 28 | + |
| 29 | + @Inject |
| 30 | + public CompasSclHistoryResource(CompasSclHistoryService compasSclHistoryService) { |
| 31 | + this.compasSclHistoryService = compasSclHistoryService; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public DataResourcesResult searchForResources(DataResourceSearch searchQuery) { |
| 36 | + List<IHistoryMetaItem> historyItems = fetchHistoryItems(searchQuery); |
| 37 | + DataResourcesResult result = new DataResourcesResult(); |
| 38 | + result.setResults(historyItems.stream().map(HistoryMapper::convertToDataResource).toList()); |
| 39 | + return result; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public DataResourceHistory retrieveDataResourceHistory(String id) { |
| 44 | + List<IHistoryMetaItem> historyItems = compasSclHistoryService.listHistoryVersionsForResource(UUID.fromString(id)); |
| 45 | + DataResourceHistory resourcesHistories = new DataResourceHistory(); |
| 46 | + resourcesHistories.setVersions(historyItems.stream().map(HistoryMapper::convertToDataResourceVersion).toList()); |
| 47 | + return resourcesHistories; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public Response retrieveDataResourceByVersion(String id, String version) { |
| 52 | + String fetchedData = compasSclHistoryService.findFileByIdAndVersion(UUID.fromString(id), new Version(version)); |
| 53 | + return Response.status(Response.Status.OK).entity(fetchedData).type(MediaType.APPLICATION_XML).build(); |
| 54 | + } |
| 55 | + |
| 56 | + private List<IHistoryMetaItem> fetchHistoryItems(DataResourceSearch searchQuery) { |
| 57 | + String uuid = searchQuery.getUuid(); |
| 58 | + |
| 59 | + if (uuid != null) { |
| 60 | + return compasSclHistoryService.listHistoryVersionsForResource(UUID.fromString(uuid)); |
| 61 | + } |
| 62 | + |
| 63 | + SclFileType type = searchQuery.getType() != null ? SclFileType.valueOf(searchQuery.getType()) : null; |
| 64 | + String name = searchQuery.getName(); |
| 65 | + String author = searchQuery.getAuthor(); |
| 66 | + OffsetDateTime from = DateUtil.convertToOffsetDateTime(searchQuery.getFrom()); |
| 67 | + OffsetDateTime to = DateUtil.convertToOffsetDateTime(searchQuery.getTo()); |
| 68 | + |
| 69 | + if (type != null || name != null || author != null || from != null || to != null) { |
| 70 | + return compasSclHistoryService.searchResourcesHistoryVersions(type, name, author, from, to); |
| 71 | + } |
| 72 | + return compasSclHistoryService.listHistory(); |
| 73 | + } |
| 74 | +} |
0 commit comments