Skip to content

Commit 6ba7631

Browse files
committed
Review ClassificationPlanController and ClassificationPlanService
1 parent 9ac21d4 commit 6ba7631

3 files changed

Lines changed: 119 additions & 150 deletions

File tree

roda-core/roda-core/src/main/java/org/roda/core/common/ClassificationPlanUtils.java

Lines changed: 0 additions & 140 deletions
This file was deleted.

roda-ui/roda-wui/src/main/java/org/roda/wui/api/v2/controller/ClassificationPlanController.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,13 @@
2222
import io.swagger.v3.oas.annotations.media.Schema;
2323
import io.swagger.v3.oas.annotations.responses.ApiResponse;
2424
import io.swagger.v3.oas.annotations.tags.Tag;
25-
import jakarta.servlet.http.HttpServletRequest;
2625

2726
@RestController
2827
@RequestMapping(path = "/api/v2/classification-plans")
2928
@Tag(name = ClassificationPlanController.SWAGGER_ENDPOINT)
3029
public class ClassificationPlanController {
3130
public static final String SWAGGER_ENDPOINT = "Classification plan";
3231

33-
@Autowired
34-
HttpServletRequest request;
35-
3632
@Autowired
3733
ClassificationPlanService classificationPlanService;
3834

@@ -41,7 +37,7 @@ public class ClassificationPlanController {
4137

4238
@GetMapping(path = "", produces = MediaType.APPLICATION_JSON_VALUE)
4339
@Operation(summary = "Retrieves the classification plan from Catalogue", responses = {
44-
@ApiResponse(responseCode = "200", description = "OK")})
40+
@ApiResponse(responseCode = "200", description = "OK")})
4541
public ResponseEntity<StreamingResponseBody> getClassificationPlan(
4642
@Parameter(description = "Choose file name", schema = @Schema(defaultValue = "plan.json")) @RequestParam(value = "filename", defaultValue = "plan.json", required = false) String filename) {
4743

Lines changed: 118 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,129 @@
11
package org.roda.wui.api.v2.services;
22

3-
import org.roda.core.common.ClassificationPlanUtils;
4-
import org.roda.core.data.exceptions.*;
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.apache.commons.codec.binary.Base64;
9+
import org.apache.commons.io.IOUtils;
10+
import org.roda.core.RodaCoreFactory;
11+
import org.roda.core.common.RodaUtils;
12+
import org.roda.core.data.common.RodaConstants;
13+
import org.roda.core.data.exceptions.AuthorizationDeniedException;
14+
import org.roda.core.data.exceptions.GenericException;
15+
import org.roda.core.data.exceptions.NotFoundException;
16+
import org.roda.core.data.exceptions.RequestNotValidException;
517
import org.roda.core.data.v2.ConsumesOutputStream;
6-
import org.roda.core.data.v2.user.User;
18+
import org.roda.core.data.v2.DefaultConsumesOutputStream;
19+
import org.roda.core.data.v2.index.filter.Filter;
20+
import org.roda.core.data.v2.index.filter.OneOfManyFilterParameter;
21+
import org.roda.core.data.v2.ip.AIP;
22+
import org.roda.core.data.v2.ip.IndexedAIP;
23+
import org.roda.core.data.v2.ip.metadata.DescriptiveMetadata;
24+
import org.roda.core.index.IndexService;
25+
import org.roda.core.index.utils.IterableIndexResult;
26+
import org.roda.core.model.ModelService;
27+
import org.roda.core.storage.Binary;
28+
import org.roda.wui.common.model.RequestContext;
729
import org.springframework.stereotype.Service;
830

31+
import com.fasterxml.jackson.core.JsonFactory;
32+
import com.fasterxml.jackson.databind.ObjectMapper;
33+
import com.fasterxml.jackson.databind.node.ArrayNode;
34+
import com.fasterxml.jackson.databind.node.ObjectNode;
35+
936
@Service
1037
public class ClassificationPlanService {
1138

12-
public ConsumesOutputStream retrieveClassificationPlan(User user, String filename)
39+
public ConsumesOutputStream retrieveClassificationPlan(RequestContext context, String filename)
1340
throws GenericException, RequestNotValidException, NotFoundException, AuthorizationDeniedException {
14-
return ClassificationPlanUtils.retrieveClassificationPlan(user, filename);
41+
42+
try {
43+
JsonFactory factory = new JsonFactory();
44+
ObjectMapper mapper = new ObjectMapper(factory);
45+
ObjectNode root = mapper.createObjectNode();
46+
47+
ArrayNode array = mapper.createArrayNode();
48+
List<String> descriptionsLevels = RodaUtils
49+
.copyList(RodaCoreFactory.getRodaConfiguration().getList(RodaConstants.LEVELS_CLASSIFICATION_PLAN));
50+
51+
Filter allButRepresentationsFilter = new Filter(
52+
new OneOfManyFilterParameter(RodaConstants.AIP_LEVEL, descriptionsLevels));
53+
54+
IndexService index = context.getIndexService();
55+
boolean justActive = true;
56+
try (IterableIndexResult<IndexedAIP> res = index.findAll(IndexedAIP.class, allButRepresentationsFilter,
57+
context.getUser(), justActive, new ArrayList<>())) {
58+
for (IndexedAIP re : res) {
59+
array.add(aipToJSON(context.getModelService(), re));
60+
}
61+
}
62+
63+
root.set("dos", array);
64+
65+
return new DefaultConsumesOutputStream(filename, RodaConstants.MEDIA_TYPE_APPLICATION_JSON, out -> {
66+
try {
67+
mapper.writeValue(out, root);
68+
} catch (IOException e) {
69+
// do nothing
70+
}
71+
});
72+
} catch (IOException e) {
73+
throw new GenericException(e);
74+
}
75+
}
76+
77+
private static ObjectNode aipToJSON(ModelService model, IndexedAIP indexedAIP)
78+
throws IOException, RequestNotValidException, NotFoundException, GenericException, AuthorizationDeniedException {
79+
JsonFactory factory = new JsonFactory();
80+
ObjectMapper mapper = new ObjectMapper(factory);
81+
82+
ObjectNode node = mapper.createObjectNode();
83+
if (indexedAIP.getTitle() != null) {
84+
node = node.put("title", indexedAIP.getTitle());
85+
}
86+
if (indexedAIP.getId() != null) {
87+
node = node.put("id", indexedAIP.getId());
88+
}
89+
if (indexedAIP.getParentID() != null) {
90+
node = node.put("parentId", indexedAIP.getParentID());
91+
}
92+
if (indexedAIP.getLevel() != null) {
93+
node = node.put("descriptionlevel", indexedAIP.getLevel());
94+
}
95+
AIP modelAIP = model.retrieveAIP(indexedAIP.getId());
96+
97+
if (modelAIP.getType() != null) {
98+
ObjectNode contentTypeNode = mapper.createObjectNode();
99+
contentTypeNode = contentTypeNode.put("packageType", RodaConstants.AIP_DEFAULT_PACKAGE_TYPE);
100+
contentTypeNode = contentTypeNode.put("value", modelAIP.getType());
101+
node.set("contentType", contentTypeNode);
102+
}
103+
List<DescriptiveMetadata> descriptiveMetadata = modelAIP.getDescriptiveMetadata();
104+
if (descriptiveMetadata != null && !descriptiveMetadata.isEmpty()) {
105+
ArrayNode metadata = mapper.createArrayNode();
106+
for (DescriptiveMetadata dm : descriptiveMetadata) {
107+
ObjectNode dmNode = mapper.createObjectNode();
108+
if (dm.getId() != null) {
109+
dmNode = dmNode.put("id", dm.getId());
110+
}
111+
if (dm.getType() != null) {
112+
dmNode = dmNode.put("metadataType", dm.getType());
113+
}
114+
if (dm.getVersion() != null) {
115+
dmNode = dmNode.put("metadataVersion", dm.getVersion());
116+
}
117+
Binary b = model.retrieveDescriptiveMetadataBinary(modelAIP.getId(), dm.getId());
118+
InputStream is = b.getContent().createInputStream();
119+
dmNode = dmNode.put("content", new String(Base64.encodeBase64(IOUtils.toByteArray(is))));
120+
IOUtils.closeQuietly(is);
121+
dmNode = dmNode.put("contentEncoding", "Base64");
122+
123+
metadata = metadata.add(dmNode);
124+
}
125+
node.set("metadata", metadata);
126+
}
127+
return node;
15128
}
16129
}

0 commit comments

Comments
 (0)