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