Skip to content

Commit caac6f4

Browse files
author
cthermolia-grnet
committed
return assessments id to reports
1 parent 05443ef commit caac6f4

4 files changed

Lines changed: 84 additions & 33 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ According to [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) , the `Unr
108108
- [#552](https://github.com/FC4E-CAT/fc4e-cat-api/pull/552) CAT-1018 Prevent auto signout of admin when validated
109109
- [#563](https://github.com/FC4E-CAT/fc4e-cat-api/pull/563) Use pretty-print Zenodo publication state in responses
110110
- [#567](https://github.com/FC4E-CAT/fc4e-cat-api/pull/567) CAT-1065 API: Grant reporter role permissions to view an assessment
111+
- [#568](https://github.com/FC4E-CAT/fc4e-cat-api/pull/568) CAT-1067 Return ids of assessments to report response
111112
- [#569](https://github.com/FC4E-CAT/fc4e-cat-api/pull/569) CAT-1068 Investigate 500 error when retrieving reports
112113

113-
114114
## 2.0.0 - 2025-03-31
115115
---
116116

‎data-transfer-object/src/main/java/org/grnet/cat/dtos/report/ReportResponseDto.java‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.eclipse.microprofile.openapi.annotations.media.Schema;
88

99
import java.util.List;
10+
import java.util.Map;
1011

1112
@JsonInclude(JsonInclude.Include.NON_NULL)
1213
@Getter
@@ -90,12 +91,13 @@ public class ReportResponseDto {
9091

9192
@Schema(
9293
type = SchemaType.ARRAY,
93-
implementation = String.class,
94-
description = "List of column labels (e.g. assessments)",
95-
example = "[\"Assessment-1\", \"Assessment-2\"]"
94+
implementation = Map.class,
95+
description = "Map of column keys to their labels",
96+
example = "{\"name\":\"Assessment-1\", \"id\":\"c203-c367-555f-4672s\"}"
9697
)
9798
@JsonProperty("columns")
98-
public List<String> columns;
99+
public List<Map<String, String>> columns;
100+
99101

100102
@Schema(
101103
type = SchemaType.ARRAY,
@@ -141,10 +143,14 @@ public List<String> getRows() {
141143
return rows;
142144
}
143145

144-
public List<String> getColumns() {
146+
public List<Map<String, String>> getColumns() {
145147
return columns;
146148
}
147149

150+
public void setColumns(List<Map<String, String>> columns) {
151+
this.columns = columns;
152+
}
153+
148154
public List<List<String>> getData() {
149155
return data;
150156
}
@@ -185,9 +191,6 @@ public void setRows(List<String> rows) {
185191
this.rows = rows;
186192
}
187193

188-
public void setColumns(List<String> columns) {
189-
this.columns = columns;
190-
}
191194

192195
public void setData(List<List<String>> data) {
193196
this.data = data;

‎repository/src/main/java/org/grnet/cat/repositories/ReportRepository.java‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public List<Object[]> fetchReportData(List<String> motivations,
3939
var def = findById(reportDefinitionId);
4040

4141
var where = new StringBuilder("WHERE 1=1 ");
42-
42+
var select =new StringBuilder();
4343
// Handle publicationStatus (IDs: 1 = published, 2 = unpublished)
4444
if (!publicationStatus.isEmpty()) {
4545
where.append("AND (");
@@ -62,13 +62,16 @@ public List<Object[]> fetchReportData(List<String> motivations,
6262
where.append(") ");
6363
}
6464

65-
String selectRow;
66-
String selectCol;
67-
65+
String selectRow="";
66+
String selectCol="";
67+
String selectId="";
6868
if (reportDefinitionId == 1) {
6969
// Actors × Assessments
7070
selectRow = "a.assessment_doc->'actor'->>'name'";
7171
selectCol = "a.assessment_doc->>'name'";
72+
selectId = "a.assessment_doc->>'id'";
73+
select.append( "SELECT " + selectRow + " AS rowLabel, "
74+
+ selectCol + " AS colLabel ");
7275

7376
if (!actors.isEmpty()) {
7477
where.append("AND (");
@@ -84,6 +87,9 @@ public List<Object[]> fetchReportData(List<String> motivations,
8487
// Organisations × Assessments
8588
selectRow = "a.assessment_doc->'organisation'->>'name'";
8689
selectCol = "a.assessment_doc->>'name'";
90+
selectId = "a.assessment_doc->>'id'";
91+
select.append("SELECT " + selectRow + " AS rowLabel, "
92+
+ selectCol + " AS colLabel ");
8793

8894
if (!organisations.isEmpty()) {
8995
where.append("AND (");
@@ -100,6 +106,9 @@ public List<Object[]> fetchReportData(List<String> motivations,
100106
selectRow = "a.assessment_doc->'subject'->>'name'";
101107
selectCol = "(a.assessment_doc->'assessment_type'->>'name') || ' / ' || (a.assessment_doc->'actor'->>'name')";
102108

109+
select.append("SELECT " + selectRow + " AS rowLabel, "
110+
+ selectCol + " AS colLabel ");
111+
103112
// Subject filter
104113
if (!subjects.isEmpty()) {
105114
where.append("AND (");
@@ -126,14 +135,14 @@ public List<Object[]> fetchReportData(List<String> motivations,
126135
throw new IllegalArgumentException("Unsupported definition: " + def);
127136
}
128137

129-
var sql = "SELECT " + selectRow + " AS rowLabel, "
130-
+ selectCol + " AS colLabel, "
131-
+ "a.assessment_doc AS assessmentDoc "
132-
+ "FROM MotivationAssessment a "
133-
+ where
134-
+ "ORDER BY 1, 2";
138+
var sql = select.append(",").append("a.assessment_doc AS assessmentDoc ");
139+
if(!selectId.isEmpty()) {
140+
sql.append(",").append(selectId).append(" AS colId ");
141+
}
142+
sql.append( " FROM MotivationAssessment a ")
143+
.append(where).append(" ORDER BY 1, 2");
135144

136-
var q = em.createNativeQuery(sql);
145+
var q = em.createNativeQuery(sql.toString());
137146

138147
// Bind publicationStatus using enum
139148
for (int i = 0; i < publicationStatus.size(); i++) {

‎service/src/main/java/org/grnet/cat/services/report/ReportService.java‎

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.fasterxml.jackson.databind.JsonNode;
44
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonObject;
7+
import com.google.gson.JsonParser;
58
import jakarta.enterprise.context.ApplicationScoped;
69
import jakarta.inject.Inject;
710
import jakarta.transaction.Transactional;
@@ -136,7 +139,7 @@ public ReportResponseDto run(Long id, ReportFilterDto request, String userId) {
136139

137140
// 5. Build matrix
138141
var matrix = new LinkedHashMap<String, Map<String, String>>();
139-
var colSet = new LinkedHashSet<String>();
142+
var colSet = new LinkedHashSet<Map<String,String>>();
140143
buildMatrix(raw, matrix, colSet);
141144

142145
// 6. Return response
@@ -158,18 +161,27 @@ private boolean validateListType(List<String> values, String expectedType) {
158161

159162
private void buildMatrix(List<Object[]> raw,
160163
Map<String, Map<String, String>> matrix,
161-
Set<String> colSet) {
164+
Set<Map<String,String>> colSet) {
162165
for (Object[] row : raw) {
163166
var rowLabel = safeLabel((String) row[0]);
164167
var colLabel = safeLabel((String) row[1]);
165168
var assessmentDoc = (String) row[2];
169+
var compliance = extractCompliance(assessmentDoc);
170+
var columnInfo = new JsonObject();
171+
columnInfo.addProperty("name", colLabel);
172+
var colMap=new HashMap<String,String>();
173+
colMap.put("name",colLabel);
174+
if(row.length>3){
175+
var colId = safeLabel((String) row[3]);
176+
columnInfo.addProperty("id", colId);
177+
colMap.put("id",colId);
166178

167-
var value = extractCompliance(assessmentDoc);
168-
179+
}
180+
colSet.add(colMap);
169181
matrix.computeIfAbsent(rowLabel, k -> new LinkedHashMap<>())
170-
.put(colLabel, value);
182+
.put(colLabel, compliance);
183+
171184

172-
colSet.add(colLabel);
173185
}
174186
}
175187

@@ -199,7 +211,7 @@ private ReportResponseDto buildResponse(ReportDefinitionDto def,
199211
ReportFilterDto request,
200212
String userId,
201213
LinkedHashMap<String, Map<String, String>> matrix,
202-
LinkedHashSet<String> colSet) {
214+
LinkedHashSet<Map<String,String>> colSet) {
203215

204216
var table = matrixToDto(matrix, colSet);
205217

@@ -234,9 +246,34 @@ public String exportToCsv(ReportResponseDto report) {
234246

235247
// Header: first cell = report label (no separate title row)
236248
sb.append(escapeCsv(report.label != null ? report.label : ""));
237-
for (String col : report.columns) {
238-
sb.append(",").append(escapeCsv(col));
249+
String nameValue = null;
250+
String idValue = null;
251+
252+
for (Map<String, String> col : report.columns) {
253+
for (Map.Entry<String, String> entry : col.entrySet()) {
254+
String key = entry.getKey();
255+
String value = entry.getValue();
256+
257+
if ("name".equalsIgnoreCase(key)) {
258+
nameValue = value;
259+
} else if ("id".equalsIgnoreCase(key)) {
260+
idValue = value;
261+
} else {
262+
sb.append(",").append(escapeCsv(value));
263+
}
264+
}
265+
266+
// After loop, handle name/id concatenation if they exist
267+
var cellValue=new StringBuilder();
268+
if (nameValue != null ) {
269+
cellValue.append(nameValue);
239270
}
271+
if(idValue != null) {
272+
cellValue.append("/").append(idValue);
273+
}
274+
sb.append(",").append(escapeCsv(cellValue.toString()));
275+
}
276+
240277
sb.append("\n");
241278

242279
// Data rows
@@ -263,23 +300,25 @@ private String escapeCsv(String value) {
263300
/**
264301
* Builds the response DTO from definition, userId, and table data.
265302
*/
266-
ReportResponseDto matrixToDto(Map<String, Map<String, String>> matrix, Set<String> assessments) {
303+
ReportResponseDto matrixToDto(Map<String, Map<String, String>> matrix, Set<Map<String, String>> assessments) {
267304
var result = new ReportResponseDto();
268305
result.rows = new ArrayList<>(matrix.keySet());
269306
result.columns = new ArrayList<>(assessments);
270307

271308
List<List<String>> tableData = new ArrayList<>();
272309
for (String org : result.rows) {
273310
List<String> rowData = new ArrayList<>();
274-
for (String assess : result.columns) {
311+
for (Map<String, String> assess : result.columns) {
275312
String val = "N/A";
276-
if (matrix.containsKey(org) && matrix.get(org).containsKey(assess)) {
277-
val = matrix.get(org).get(assess);
313+
if (assess.containsKey("name") && matrix.containsKey(org) && matrix.get(org).containsKey(assess.get("name"))) {
314+
val = matrix.get(org).get(assess.get("name"));
278315
}
279316
rowData.add(val);
280317
}
318+
281319
tableData.add(rowData);
282320
}
321+
283322
result.data = tableData;
284323
return result;
285324
}

0 commit comments

Comments
 (0)