Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@

package com.metaformsystems.redline.dao;

public record FileResource(String fileId, String fileName, String contentType, String uploadDateIso) {
import java.util.Map;

public record FileResource(String fileId, String fileName, String contentType, String uploadDateIso,
Map<String, Object> metadata) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2026 Metaform Systems, Inc.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Metaform Systems, Inc. - initial API and implementation
*
*/

package com.metaformsystems.redline.model;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Converter
public class HashMapConverter implements AttributeConverter<Map<String, Object>, String> {

private final ObjectMapper objectMapper = new ObjectMapper();

@Override
public String convertToDatabaseColumn(Map<String, Object> map) {

String json = null;
try {
json = objectMapper.writeValueAsString(map);
} catch (final JsonProcessingException e) {
throw new RuntimeException(e);
}

return json;
}

@Override
public Map<String, Object> convertToEntityAttribute(String json) {

Map<String, Object> map = null;
try {
map = objectMapper.readValue(json,
new TypeReference<HashMap<String, Object>>() {
});
} catch (final IOException e) {
throw new RuntimeException(e);
}

return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,40 @@

package com.metaformsystems.redline.model;

import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;

import java.util.Map;

@Entity
@Table(name = "uploaded_files")
public class UploadedFile extends VersionedEntity {
private String fileId;
private String originalFilename;
private String contentType;
@Convert(converter = HashMapConverter.class)
private Map<String, Object> metadata;

public UploadedFile(String fileId, String originalFilename, String contentType) {
public UploadedFile(String fileId, String originalFilename, String contentType, Map<String, Object> metadata) {
this.fileId = fileId;
this.originalFilename = originalFilename;
this.contentType = contentType;
this.metadata = metadata;
}

public UploadedFile() {

}

public Map<String, Object> getMetadata() {
return metadata;
}

public void setMetadata(Map<String, Object> metadata) {
this.metadata = metadata;
}

public String getFileId() {
return fileId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,14 @@ public void uploadFileForParticipant(Long participantId, Map<String, Object> met
var fileId = response.id();

//2. track uploaded file in DB
participant.getUploadedFiles().add(new UploadedFile(fileId, originalFilename, contentType));
participant.getUploadedFiles().add(new UploadedFile(fileId, originalFilename, contentType, metadata));
}

@Transactional
public List<FileResource> listFilesForParticipant(Long participantId) {
var participant = participantRepository.findById(participantId).orElseThrow(() -> new IllegalArgumentException("Participant not found with id: " + participantId));
return participant.getUploadedFiles().stream()
.map(f -> new FileResource(f.getFileId(), f.getOriginalFilename(), f.getContentType(), f.getCreatedAt().toString()))
.map(f -> new FileResource(f.getFileId(), f.getOriginalFilename(), f.getContentType(), f.getCreatedAt().toString(), f.getMetadata()))
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -467,8 +468,8 @@ void shouldGetAllFiles() throws Exception {
participant.setTenant(tenant);
participant.setParticipantContextId("test-participant-context-id");
participant.setClientCredentials(new ClientCredentials("test-client", "test-secret"));
participant.getUploadedFiles().add(new UploadedFile("test-file-id", "test-file-name", "test-file-content-type"));
participant.getUploadedFiles().add(new UploadedFile("test-file-id2", "test-file-name2", "test-file-content-type2"));
participant.getUploadedFiles().add(new UploadedFile("test-file-id", "test-file-name", "test-file-content-type", Map.of()));
participant.getUploadedFiles().add(new UploadedFile("test-file-id2", "test-file-name2", "test-file-content-type2", Map.of()));
tenant.addParticipant(participant);
participant = participantRepository.save(participant);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.metaformsystems.redline.model.PartnerReference;
import com.metaformsystems.redline.model.ServiceProvider;
import com.metaformsystems.redline.model.Tenant;
import com.metaformsystems.redline.model.UploadedFile;
import com.metaformsystems.redline.repository.DataspaceRepository;
import com.metaformsystems.redline.repository.ParticipantRepository;
import com.metaformsystems.redline.repository.ServiceProviderRepository;
Expand All @@ -33,6 +34,7 @@
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static com.metaformsystems.redline.TestData.PARTICIPANT_PROFILE_RESPONSE;
Expand Down Expand Up @@ -550,6 +552,31 @@ void shouldListContracts() throws InterruptedException {

}


@Test
void shouldListFiles() throws InterruptedException {
var participant = createAndSaveParticipant("ctx-5", "did:web:me");

participant.setUploadedFiles(new ArrayList<>(List.of(
new UploadedFile("file-id-1", "foobar.jpg", "image/jpeg", Map.of("bar", "baz")),
new UploadedFile("file-id-2", "barbaz.pdf", "application/pdf", Map.of("quizz", "qazz"))
)));

participant = participantRepository.save(participant);

var result = tenantService.listFilesForParticipant(participant.getId());

assertThat(result).hasSize(2);
assertThat(result).anyMatch(f -> f.fileId().equals("file-id-1") &&
f.fileName().equals("foobar.jpg") &&
f.contentType().equals("image/jpeg") &&
f.metadata().get("bar").equals("baz"));
assertThat(result).anyMatch(f -> f.fileId().equals("file-id-2") &&
f.fileName().equals("barbaz.pdf") &&
f.contentType().equals("application/pdf") &&
f.metadata().get("quizz").equals("qazz"));
}

private Participant createAndSaveParticipant(String contextId, String identifier) {
var p = new Participant();
p.setParticipantContextId(contextId);
Expand Down
Loading