|
1 | 1 | package org.roda.wui.api.v2.services; |
2 | 2 |
|
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; |
5 | 17 | 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; |
7 | 29 | import org.springframework.stereotype.Service; |
8 | 30 |
|
| 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 | + |
9 | 36 | @Service |
10 | 37 | public class ClassificationPlanService { |
11 | 38 |
|
12 | | - public ConsumesOutputStream retrieveClassificationPlan(User user, String filename) |
| 39 | + public ConsumesOutputStream retrieveClassificationPlan(RequestContext context, String filename) |
13 | 40 | 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; |
15 | 128 | } |
16 | 129 | } |
0 commit comments