PersistenceHandler.vm 2.9 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
#imports

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

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

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

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

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

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

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

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

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