View Javadoc
1   package de.dlr.shepard.neo4Core.export;
2   
3   import de.dlr.shepard.exceptions.ShepardException;
4   import de.dlr.shepard.mongoDB.NamedInputStream;
5   import de.dlr.shepard.mongoDB.StructuredDataPayload;
6   import de.dlr.shepard.neo4Core.entities.TimeseriesReference;
7   import de.dlr.shepard.neo4Core.io.BasicReferenceIO;
8   import de.dlr.shepard.neo4Core.io.FileReferenceIO;
9   import de.dlr.shepard.neo4Core.io.StructuredDataReferenceIO;
10  import de.dlr.shepard.neo4Core.io.TimeseriesReferenceIO;
11  import de.dlr.shepard.neo4Core.io.URIReferenceIO;
12  import de.dlr.shepard.neo4Core.services.BasicReferenceService;
13  import de.dlr.shepard.neo4Core.services.CollectionService;
14  import de.dlr.shepard.neo4Core.services.DataObjectService;
15  import de.dlr.shepard.neo4Core.services.FileReferenceService;
16  import de.dlr.shepard.neo4Core.services.StructuredDataReferenceService;
17  import de.dlr.shepard.neo4Core.services.TimeseriesReferenceService;
18  import de.dlr.shepard.neo4Core.services.URIReferenceService;
19  import io.quarkus.logging.Log;
20  import jakarta.enterprise.context.RequestScoped;
21  import jakarta.inject.Inject;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.util.Collections;
25  import java.util.List;
26  
27  @RequestScoped
28  public class ExportService {
29  
30    private CollectionService collectionService;
31    private DataObjectService dataObjectService;
32    private BasicReferenceService basicReferenceService;
33    private TimeseriesReferenceService timeseriesReferenceService;
34    private FileReferenceService fileReferenceService;
35    private StructuredDataReferenceService structuredDataReferenceService;
36    private URIReferenceService uriReferenceService;
37  
38    ExportService() {}
39  
40    @Inject
41    public ExportService(
42      CollectionService collectionService,
43      DataObjectService dataObjectService,
44      BasicReferenceService basicReferenceService,
45      TimeseriesReferenceService timeseriesReferenceService,
46      FileReferenceService fileReferenceService,
47      StructuredDataReferenceService structuredDataReferenceService,
48      URIReferenceService uriReferenceService
49    ) {
50      this.collectionService = collectionService;
51      this.dataObjectService = dataObjectService;
52      this.basicReferenceService = basicReferenceService;
53      this.timeseriesReferenceService = timeseriesReferenceService;
54      this.fileReferenceService = fileReferenceService;
55      this.structuredDataReferenceService = structuredDataReferenceService;
56      this.uriReferenceService = uriReferenceService;
57    }
58  
59    public InputStream exportCollectionByShepardId(long collectionId, String username) throws IOException {
60      var collection = collectionService.getCollectionByShepardId(collectionId);
61  
62      var builder = new ExportBuilder(collection);
63      for (var dataObject : collection.getDataObjects()) {
64        fetchAndWriteDataObject(builder, dataObject.getShepardId(), username);
65      }
66      return builder.build();
67    }
68  
69    private void fetchAndWriteDataObject(ExportBuilder builder, long dataObjectId, String username) throws IOException {
70      var dataObject = dataObjectService.getDataObjectByShepardId(dataObjectId);
71      builder.addDataObject(dataObject);
72  
73      // TODO: Add more types, maybe improve (StrategyPattern?)
74      for (var reference : dataObject.getReferences()) {
75        switch (reference.getType()) {
76          case "TimeseriesReference" -> fetchAndWriteTimeseriesReference(builder, reference.getShepardId(), username);
77          case "FileReference" -> fetchAndWriteFileReference(builder, reference.getShepardId(), username);
78          case "StructuredDataReference" -> fetchAndWriteStructuredDataReference(
79            builder,
80            reference.getShepardId(),
81            username
82          );
83          case "URIReference" -> fetchAndWriteUriReference(builder, reference.getShepardId(), username);
84          default -> fetchAndWriteBasicReference(builder, reference.getShepardId());
85        }
86      }
87    }
88  
89    private void fetchAndWriteTimeseriesReference(ExportBuilder builder, long referenceId, String username)
90      throws IOException {
91      var reference = timeseriesReferenceService.getReferenceByShepardId(referenceId);
92  
93      builder.addReference(new TimeseriesReferenceIO(reference), reference.getCreatedBy());
94  
95      InputStream timeseriesPayload = null;
96      try {
97        timeseriesPayload = timeseriesReferenceService.exportTimeseriesPayloadByShepardId(referenceId, username);
98      } catch (ShepardException e) {
99        Log.warn("Cannot access timeseries payload during export");
100     }
101     if (timeseriesPayload != null) {
102       writeTimeseriesPayload(builder, timeseriesPayload, reference);
103     }
104   }
105 
106   private void fetchAndWriteFileReference(ExportBuilder builder, long referenceId, String username) throws IOException {
107     var reference = fileReferenceService.getReferenceByShepardId(referenceId);
108 
109     builder.addReference(new FileReferenceIO(reference), reference.getCreatedBy());
110 
111     List<NamedInputStream> payloads = Collections.emptyList();
112     try {
113       payloads = fileReferenceService.getAllPayloadsByShepardId(referenceId, username);
114     } catch (ShepardException e) {
115       Log.warn("Cannot access file payload during export");
116     }
117 
118     for (var nis : payloads) {
119       if (nis.getInputStream() != null) writeFilePayload(builder, nis);
120     }
121   }
122 
123   private void fetchAndWriteStructuredDataReference(ExportBuilder builder, long referenceId, String username)
124     throws IOException {
125     var reference = structuredDataReferenceService.getReferenceByShepardId(referenceId);
126 
127     builder.addReference(new StructuredDataReferenceIO(reference), reference.getCreatedBy());
128 
129     List<StructuredDataPayload> payloads = Collections.emptyList();
130     try {
131       payloads = structuredDataReferenceService.getAllPayloadsByShepardId(referenceId, username);
132     } catch (ShepardException e) {
133       Log.warn("Cannot access structured data payload during export");
134     }
135 
136     for (var sdp : payloads) {
137       if (sdp.getPayload() != null) writeStructuredDataPayload(builder, sdp);
138     }
139   }
140 
141   private void fetchAndWriteUriReference(ExportBuilder builder, long referenceId, String username) throws IOException {
142     var reference = uriReferenceService.getReferenceByShepardId(referenceId);
143 
144     builder.addReference(new URIReferenceIO(reference), reference.getCreatedBy());
145   }
146 
147   private void fetchAndWriteBasicReference(ExportBuilder builder, long referenceId) throws IOException {
148     var reference = basicReferenceService.getReferenceByShepardId(referenceId);
149 
150     builder.addReference(new BasicReferenceIO(reference), reference.getCreatedBy());
151   }
152 
153   private void writeFilePayload(ExportBuilder builder, NamedInputStream nis) throws IOException {
154     var nameSplitted = nis.getName().split("\\.", 2);
155     var filename = nameSplitted.length > 1 ? nis.getOid() + "." + nameSplitted[1] : nis.getOid();
156 
157     builder.addPayload(nis.getInputStream().readAllBytes(), filename, nis.getName());
158   }
159 
160   private void writeStructuredDataPayload(ExportBuilder builder, StructuredDataPayload sdp) throws IOException {
161     var filename = sdp.getStructuredData().getOid() + ExportConstants.JSON_FILE_EXTENSION;
162 
163     builder.addPayload(sdp.getPayload().getBytes(), filename, sdp.getStructuredData().getName(), "application/json");
164   }
165 
166   private void writeTimeseriesPayload(ExportBuilder builder, InputStream payload, TimeseriesReference reference)
167     throws IOException {
168     var filename = reference.getUniqueId() + ExportConstants.CSV_FILE_EXTENSION;
169 
170     builder.addPayload(payload.readAllBytes(), filename, reference.getName(), "text/csv");
171   }
172 }