Skip to content

Commit

Permalink
[#464] added history API implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
david-monichi-bp committed Dec 14, 2024
1 parent da2cece commit 166a904
Show file tree
Hide file tree
Showing 11 changed files with 726 additions and 8 deletions.
6 changes: 6 additions & 0 deletions app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ SPDX-License-Identifier: Apache-2.0
<artifactId>quarkus-container-image-docker</artifactId>
</dependency>

<dependency>
<groupId>io.quarkiverse.openapi.generator</groupId>
<artifactId>quarkus-openapi-generator-server</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>

<!-- Test Dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.lfenergy.compas.scl.data.rest.v1;

import io.quarkus.security.Authenticated;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.lfenergy.compas.scl.data.model.IHistoryMetaItem;
import org.lfenergy.compas.scl.data.model.Version;
import org.lfenergy.compas.scl.data.service.CompasSclHistoryService;
import org.lfenergy.compas.scl.extensions.model.SclFileType;
import org.lfenergy.compas.scl.rest.api.HistoryResource;
import org.lfenergy.compas.scl.rest.api.beans.DataResourceHistory;
import org.lfenergy.compas.scl.rest.api.beans.DataResourceSearch;
import org.lfenergy.compas.scl.rest.api.beans.DataResourcesResult;

import java.time.OffsetDateTime;
import java.util.List;
import java.util.UUID;

@Authenticated
@RequestScoped
@Path("/api")
public class CompasSclHistoryResource implements HistoryResource {

private final CompasSclHistoryService compasSclHistoryService;

@Inject
public CompasSclHistoryResource(CompasSclHistoryService compasSclHistoryService) {
this.compasSclHistoryService = compasSclHistoryService;
}

@Override
public DataResourcesResult searchForResources(DataResourceSearch searchQuery) {
List<IHistoryMetaItem> historyItems = fetchHistoryItems(searchQuery);
DataResourcesResult result = new DataResourcesResult();
result.setResults(historyItems.stream().map(HistoryMapper::convertToDataResource).toList());
return result;
}

@Override
public DataResourceHistory retrieveDataResourceHistory(String id) {
List<IHistoryMetaItem> historyItems = compasSclHistoryService.listHistoryVersionsForResource(UUID.fromString(id));
DataResourceHistory resourcesHistories = new DataResourceHistory();
resourcesHistories.setVersions(historyItems.stream().map(HistoryMapper::convertToDataResourceVersion).toList());
return resourcesHistories;
}

@Override
public Response retrieveDataResourceByVersion(String id, String version) {
String fetchedData = compasSclHistoryService.findFileByIdAndVersion(UUID.fromString(id), new Version(version));
return Response.status(Response.Status.OK).entity(fetchedData).type(MediaType.APPLICATION_XML).build();
}

private List<IHistoryMetaItem> fetchHistoryItems(DataResourceSearch searchQuery) {
String uuid = searchQuery.getUuid();

if (uuid != null) {
return compasSclHistoryService.listHistoryVersionsForResource(UUID.fromString(uuid));
}

SclFileType type = searchQuery.getType() != null ? SclFileType.valueOf(searchQuery.getType()) : null;
String name = searchQuery.getName();
String author = searchQuery.getAuthor();
OffsetDateTime from = DateUtil.convertToOffsetDateTime(searchQuery.getFrom());
OffsetDateTime to = DateUtil.convertToOffsetDateTime(searchQuery.getTo());

if (type != null || name != null || author != null || from != null || to != null) {
return compasSclHistoryService.searchResourcesHistoryVersions(type, name, author, from, to);
}
return compasSclHistoryService.listHistory();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.lfenergy.compas.scl.data.rest.v1;

import org.apache.commons.lang3.ObjectUtils;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Date;

public final class DateUtil {

private DateUtil() {
}

public static OffsetDateTime convertToOffsetDateTime(Date date) {
return ObjectUtils.isEmpty(date) ? null : date.toInstant().atOffset(ZoneOffset.UTC);
}

public static Date convertToDate(OffsetDateTime date) {
return ObjectUtils.isEmpty(date) ? null : new Date(date.toInstant().toEpochMilli());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.lfenergy.compas.scl.data.rest.v1;

import org.lfenergy.compas.scl.data.model.IHistoryMetaItem;
import org.lfenergy.compas.scl.rest.api.beans.DataResource;
import org.lfenergy.compas.scl.rest.api.beans.DataResourceVersion;

import java.util.UUID;

public final class HistoryMapper {

private HistoryMapper() {
}

public static DataResourceVersion convertToDataResourceVersion(IHistoryMetaItem item) {
DataResourceVersion version = new DataResourceVersion();
version.setVersion(item.getVersion());
version.setAuthor(item.getAuthor());
version.setName(item.getName());
version.setDeleted(item.isDeleted());
version.setUuid(UUID.fromString(item.getId()));
version.setType(item.getType());
version.setChangedAt(DateUtil.convertToDate(item.getChangedAt()));
version.setComment(item.getComment());
return version;
}

public static DataResource convertToDataResource(IHistoryMetaItem item) {
DataResource version = new DataResource();
version.setVersion(item.getVersion());
version.setAuthor(item.getAuthor());
version.setName(item.getName());
version.setDeleted(item.isDeleted());
version.setUuid(UUID.fromString(item.getId()));
version.setType(item.getType());
version.setChangedAt(DateUtil.convertToDate(item.getChangedAt()));
return version;
}

}
Loading

0 comments on commit 166a904

Please sign in to comment.