-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#464] added history API implementation
- Loading branch information
1 parent
da2cece
commit 166a904
Showing
11 changed files
with
726 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
app/src/main/java/org/lfenergy/compas/scl/data/rest/v1/CompasSclHistoryResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
app/src/main/java/org/lfenergy/compas/scl/data/rest/v1/DateUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
app/src/main/java/org/lfenergy/compas/scl/data/rest/v1/HistoryMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.