View Javadoc
1   package de.dlr.shepard.mongoDB;
2   
3   import static com.mongodb.client.model.Filters.eq;
4   
5   import com.mongodb.MongoException;
6   import com.mongodb.client.MongoCollection;
7   import com.mongodb.client.MongoDatabase;
8   import de.dlr.shepard.exceptions.InvalidBodyException;
9   import de.dlr.shepard.util.DateHelper;
10  import io.quarkus.logging.Log;
11  import jakarta.enterprise.context.RequestScoped;
12  import jakarta.inject.Inject;
13  import jakarta.inject.Named;
14  import java.util.UUID;
15  import org.bson.Document;
16  import org.bson.json.JsonParseException;
17  import org.bson.types.ObjectId;
18  
19  @RequestScoped
20  public class StructuredDataService {
21  
22    private static final String ID_ATTR = "_id";
23    private static final String META_OBJECT = "_meta";
24    private DateHelper dateHelper;
25  
26    StructuredDataService() {}
27  
28    @Inject
29    @Named("mongoDatabase")
30    MongoDatabase mongoDatabase;
31  
32    @Inject
33    public StructuredDataService(DateHelper dateHelper) {
34      this.dateHelper = dateHelper;
35    }
36  
37    public String createStructuredDataContainer() {
38      String mongoid = "StructuredDataContainer" + UUID.randomUUID().toString();
39      mongoDatabase.createCollection(mongoid);
40      return mongoid;
41    }
42  
43    public StructuredData createStructuredData(String mongoid, StructuredDataPayload payload) {
44      MongoCollection<Document> collection;
45      try {
46        collection = mongoDatabase.getCollection(mongoid);
47      } catch (IllegalArgumentException e) {
48        Log.errorf("Could not find container with mongoid: %s", mongoid);
49        return null;
50      }
51      Document toInsert;
52      try {
53        toInsert = Document.parse(payload.getPayload());
54      } catch (JsonParseException e) {
55        Log.errorf("Could not parse json: %s", payload.getPayload());
56        throw new InvalidBodyException("The specified payload is not json parsable");
57      }
58  
59      // Remove fields beginning with an underscore (protected)
60      var forbidden = toInsert.keySet().stream().filter(k -> k.startsWith("_")).toList();
61      forbidden.forEach(toInsert::remove);
62  
63      // Add meta data
64      var newName = payload.getStructuredData() != null ? payload.getStructuredData().getName() : null;
65      var structuredData = new StructuredData(newName, dateHelper.getDate());
66      toInsert.append(META_OBJECT, structuredData);
67      try {
68        collection.insertOne(toInsert);
69      } catch (MongoException e) {
70        Log.errorf("Could not write to mongodb: %s", e.toString());
71        return null;
72      }
73      structuredData.setOid(toInsert.getObjectId(ID_ATTR).toHexString());
74      return structuredData;
75    }
76  
77    public boolean deleteStructuredDataContainer(String mongoid) {
78      MongoCollection<Document> toDelete;
79      try {
80        toDelete = mongoDatabase.getCollection(mongoid);
81      } catch (IllegalArgumentException e) {
82        Log.errorf("Could not delete container with mongoid: %s", mongoid);
83        return false;
84      }
85      toDelete.drop();
86      return true;
87    }
88  
89    public StructuredDataPayload getPayload(String mongoid, String oid) {
90      MongoCollection<Document> collection;
91      try {
92        collection = mongoDatabase.getCollection(mongoid);
93      } catch (IllegalArgumentException e) {
94        Log.errorf("Could not find container with mongoid: %s", mongoid);
95        return null;
96      }
97      var payloadDocument = collection.find(eq(ID_ATTR, new ObjectId(oid))).first();
98      if (payloadDocument == null) {
99        Log.errorf("Could not find document with oid: %s", oid);
100       return null;
101     }
102     var structuredDataDocument = payloadDocument.get(META_OBJECT, Document.class);
103     var structuredData = structuredDataDocument != null
104       ? new StructuredData(structuredDataDocument)
105       : new StructuredData();
106     structuredData.setOid(oid);
107     var payload = new StructuredDataPayload(structuredData, payloadDocument.toJson());
108     return payload;
109   }
110 
111   public boolean deletePayload(String mongoid, String oid) {
112     MongoCollection<Document> collection;
113     try {
114       collection = mongoDatabase.getCollection(mongoid);
115     } catch (IllegalArgumentException e) {
116       Log.errorf("Could not find container with mongoid: %s", mongoid);
117       return false;
118     }
119     var doc = collection.findOneAndDelete(eq(ID_ATTR, new ObjectId(oid)));
120     if (doc == null) {
121       Log.warnf("Could not find and delete document with oid: %s", oid);
122       return true;
123     }
124     return true;
125   }
126 }