PersistenceHandler.vm 2.87 KB
Newer Older
1
2
3
4
5
#if($package)
package $package;
#end

import org.bson.Document;
6
import org.eclipse.ice.data.IPersistenceHandler;
7
import org.eclipse.ice.dev.annotations.PersistenceFilters;
8
9
10
11
12

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

13
14
15
16
17
/**
 * This is an implementation of $interface<$elementInterface>
 * that satisfies the dependencies of the {@code @Persisted} Annotation and was
 * auto-generated by the ICE Framework.
 */
18
public class $class implements $interface<$elementInterface> {
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

	/**
	 * 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);
	}

39
40
41
42
43
	/**
	 * Save the $elementInterface.
	 * @param element
	 * @throws Exception
	 */
44
	@Override
45
	public void save($elementInterface element) throws Exception {
46
		this.collection.insertOne(Document.parse(element.toJson()));
47
	}
48
49

	/**
Daniel Bluhm's avatar
Daniel Bluhm committed
50
	 * Find and retrieve all $elementInterface from the collection.
51
52
53
	 * @return an iterable of the retrieved elements.
	 * @throws Exception
	 */
54
	@Override
Daniel Bluhm's avatar
Daniel Bluhm committed
55
	public Iterable<$elementInterface> findAll() throws Exception {
56
		return this.collection.find()
57
			.map(doc -> mapper.convertValue(doc, ${implementation}.class));
58
59
	}

60
61
62
63
64
	/**
	 * Clear all $elementInterface from the collection.
	 * @return
	 * @throws Exception
	 */
65
	@Override
66
	public long clear() throws Exception {
Daniel Bluhm's avatar
Daniel Bluhm committed
67
		long count = this.collection.countDocuments();
68
69
70
71
		this.collection.drop();
		return count;
	}

72
73
74
	#foreach($field in $fields)
	#if(${field.Getter} && ${field.Searchable})
	#if(${field.Unique})
75
76

	/**
77
78
	 * Find $elementInterface by ${field.Name}.
	 * @param ${field.VarName}
79
80
	 * @return found element or null
	 */
81
	#if(${field.DefaultField})
82
83
	@Override
	#end
84
	public $elementInterface findBy${field.NameForMethod}(#fieldtype ${field.VarName}) throws Exception {
85
		Document doc = this.collection
86
			.find(PersistenceFilters.eq("${field.VarName}", ${field.VarName}))
87
88
89
90
91
			.first();
		if (doc == null) {
			return null;
		}

92
		return mapper.convertValue(doc, ${implementation}.class);
93
	}
94
	#else
95

96
	/**
97
98
	 * Find $elementInterface by ${field.Name}.
	 * @param ${field.VarName}
99
100
	 * @return Iterator of results
	 */
101
	#if(${field.DefaultField})
102
	@Override
103
	#end
104
105
	public Iterable<$elementInterface> findBy${field.NameForMethod}(#fieldtype ${field.VarName}) throws Exception {
		return this.collection.find(PersistenceFilters.eq("${field.VarName}", ${field.VarName}))
106
			.map(doc -> mapper.convertValue(doc, ${implementation}.class));
107
	}
Daniel Bluhm's avatar
Daniel Bluhm committed
108
109
	#end## if unique
	#end## if getter and search
110
	#end## foreach
111
}