77 */
88package org .seedstack .mongodb .morphia ;
99
10+ import static com .google .common .base .Preconditions .checkArgument ;
11+
12+ import java .util .Optional ;
13+ import java .util .stream .Stream ;
14+ import javax .inject .Inject ;
1015import org .mongodb .morphia .Datastore ;
1116import org .mongodb .morphia .mapping .Mapper ;
1217import org .mongodb .morphia .query .CountOptions ;
1318import org .mongodb .morphia .query .CriteriaContainer ;
19+ import org .mongodb .morphia .query .FindOptions ;
1420import org .mongodb .morphia .query .Query ;
1521import org .seedstack .business .domain .AggregateExistsException ;
1622import org .seedstack .business .domain .AggregateNotFoundException ;
1723import org .seedstack .business .domain .AggregateRoot ;
1824import org .seedstack .business .domain .BaseRepository ;
25+ import org .seedstack .business .domain .LimitOption ;
26+ import org .seedstack .business .domain .OffsetOption ;
1927import org .seedstack .business .specification .Specification ;
2028import org .seedstack .business .spi .SpecificationTranslator ;
2129import org .seedstack .mongodb .morphia .internal .DatastoreFactory ;
2230import org .seedstack .mongodb .morphia .internal .specification .MorphiaTranslationContext ;
2331
24- import javax .inject .Inject ;
25- import java .util .Optional ;
26- import java .util .stream .Stream ;
27-
2832/**
2933 * This class can serve as a base class for Morphia repositories. It provides methods for common CRUD operations as
3034 * well as access to the data store through the {@link #getDatastore()} ()} protected method.
@@ -66,7 +70,7 @@ public void add(A aggregate) throws AggregateExistsException {
6670
6771 @ Override
6872 public Stream <A > get (Specification <A > specification , Option ... options ) {
69- return buildQuery (specification ).asList ().stream ();
73+ return buildQuery (specification ).asList (buildFindOptions ( options ) ).stream ();
7074 }
7175
7276 @ Override
@@ -134,4 +138,30 @@ private Query<A> buildQuery(Specification<A> specification) {
134138 );
135139 return query ;
136140 }
141+
142+ private FindOptions buildFindOptions (Option ... options ) {
143+ FindOptions findOptions = new FindOptions ();
144+ for (Option option : options ) {
145+ if (option instanceof OffsetOption ) {
146+ applyOffset (findOptions , ((OffsetOption ) option ));
147+ } else if (option instanceof LimitOption ) {
148+ applyLimit (findOptions , ((LimitOption ) option ));
149+ }
150+ }
151+ return findOptions ;
152+ }
153+
154+ private void applyOffset (FindOptions findOptions , OffsetOption offsetOption ) {
155+ long offset = offsetOption .getOffset ();
156+ checkArgument (offset <= Integer .MAX_VALUE ,
157+ "JPA only supports offsetting results up to " + Integer .MAX_VALUE );
158+ findOptions .skip ((int ) offset );
159+ }
160+
161+ private void applyLimit (FindOptions findOptions , LimitOption limitOption ) {
162+ long limit = limitOption .getLimit ();
163+ checkArgument (limit <= Integer .MAX_VALUE ,
164+ "Morphia only supports limiting results up to " + Integer .MAX_VALUE );
165+ findOptions .limit ((int ) limit );
166+ }
137167}
0 commit comments