This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
PersistenceHandler.vm
122 lines (107 loc) · 2.99 KB
/
PersistenceHandler.vm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
## Declarations
#set($interface = "IPersistenceHandler")
##
#if($package)
package $package;
#end
import java.util.Map;
import java.util.UUID;
import org.bson.Document;
import org.eclipse.ice.dev.annotations.IDataElement;
import org.eclipse.ice.dev.annotations.IPersistenceHandler;
import org.eclipse.ice.dev.annotations.PersistenceFilters;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class $class implements $interface<$elementInterface> {
/**
* The name of the collection backing this PersistenceHandler.
*/
private final static String COLLECTION = "$collection";
/**
* The collection in which implementations of $elementInterface will be stored.
*/
private MongoCollection<Document> collection;
/**
* Convert values from Document to $implementation using mapper.
*/
private ObjectMapper mapper = new ObjectMapper();
public $class(MongoDatabase db) {
this.collection = db.getCollection(COLLECTION);
}
/**
* Save the $elementInterface.
* @param <T> Object extending IDataElement
* @param element
* @throws Exception
*/
@Override
public void save($elementInterface element) throws Exception {
this.collection.insertOne(Document.parse(element.toJSON()));
}
/**
* Find and retrieve all $elementInterface from the collection.
* @param <T>
* @return an iterable of the retrieved elements.
* @throws Exception
*/
@Override
public Iterable<$elementInterface> findAll() throws Exception {
return this.collection.find()
.map(doc -> new ${implementation}().fromJSON(doc));
}
/**
* Clear all $elementInterface from the collection.
* @return
* @throws Exception
*/
@Override
public long clear() throws Exception {
long count = this.collection.count();
this.collection.drop();
return count;
}
#macro(findmethod $var $type $docName)
#if(${var.Getter} && ${var.Search})
#if(${var.Unique})
/**
* Find $elementInterface by ${var.Name}.
* @param ${var.Name}
* @return found element or null
*/
#if(${var.DefaultField})
@Override
#end
public $elementInterface findBy${var.NameForMethod}($type ${var.Name}) throws Exception {
Document doc = this.collection
.find(PersistenceFilters.eq("${docName}", ${var.Name}))
.first();
if (doc == null) {
return null;
}
return new ${implementation}().fromJSON(doc);
}
#else
/**
* Find $elementInterface by ${var.Name}.
* @param ${var.Name}
* @return Iterator of results
*/
#if(${var.DefaultField})
@Override
#end
public Iterable<$elementInterface> findBy${var.NameForMethod}(${var.Type} ${var.Name}) throws Exception {
return this.collection.find(PersistenceFilters.eq("${var.Name}", ${var.Name}))
.map(doc -> new ${implementation}().fromJSON(doc));
}
#end
#end
#end
#foreach($field in $fields)
#findmethod($field ${field.Type} ${field.Name})
#foreach($alias in $field.Aliases)
#findmethod($alias ${field.Type} ${field.Name})
#end
#end
}