Class MongoDBUtils

java.lang.Object
dev.getelements.elements.dao.mongo.MongoDBUtils

public class MongoDBUtils extends Object
Some helper methods used in various parts of the MongoDB code. Created by patricktwohig on 6/10/15.
  • Field Details

  • Constructor Details

    • MongoDBUtils

      public MongoDBUtils()
  • Method Details

    • perform

      public <T> T perform(Function<dev.morphia.Datastore,T> operation)
      Performs the supplied operation, catching all MongoCommandException instances and mapping to the appropriate type of exception internally.
      Type Parameters:
      T - the expected return type
      Parameters:
      operation - the operation
      Returns:
      the object returned by the supplied operation
    • perform

      public <T, U> U perform(Function<dev.morphia.Datastore,T> operation, Class<U> uClass)
      Performs the supplied operation, catching all MongoCommandException instances and mapping to the appropriate type of exception internally.
      Type Parameters:
      T - the expected return type from the datastore
      U - the function return type
      Parameters:
      operation - the operation
      uClass - the type which to convert out of the type returned from the Datastore.
      Returns:
      the object returned by the supplied operation
    • performV

      public void performV(Consumer<dev.morphia.Datastore> operation)
      Performs the supplied operation, catching all MongoCommandException instances and mapping to the appropriate type of exception internally.
      Parameters:
      operation - the operation
    • perform

      public <T, ExceptionT extends Throwable> T perform(Function<dev.morphia.Datastore,T> operation, Function<Throwable,ExceptionT> exceptionTSupplier) throws ExceptionT
      Performs the supplied operation, catching all MongoCommandException instances and mapping to the appropraite type of exception internally.
      Type Parameters:
      T - the expected return type
      Parameters:
      operation - the operation
      Returns:
      the object retured by the supplied operation
      Throws:
      ExceptionT
    • parseOrThrowNotFoundException

      @Deprecated public org.bson.types.ObjectId parseOrThrowNotFoundException(String objectId)
      Parses the given ObjectID string using ObjectId. If this fails, this throws the appropriate exception type of NotFoundException.
      Parameters:
      objectId - the object ID to parse
      Returns:
      an ObjectId (never null)
    • parseOrThrow

      public <ExceptionT extends NotFoundException> org.bson.types.ObjectId parseOrThrow(String objectId, Function<String,ExceptionT> exceptionTSupplier) throws ExceptionT
      Parses the given ObjectID string using ObjectId. If this fails, this throws the appropriate exception type of NotFoundException.
      Parameters:
      objectId - the object ID to parse
      Returns:
      an ObjectId (never null)
      Throws:
      ExceptionT
    • parse

      public Optional<org.bson.types.ObjectId> parse(String idString)
      Tries to parse a string into an ObjectId.
      Parameters:
      idString - the id string
      Returns:
      the Optional<org.bson.types.ObjectId>
    • parseOrReturnNull

      public org.bson.types.ObjectId parseOrReturnNull(String objectId)
      Parses the given ObjectID string using ObjectId. If this fails, this throws the appropriate exception type of NotFoundException.
      Parameters:
      objectId - the object ID to parse
      Returns:
      an ObjectId (never null)
    • paginationFromQuery

      public <ModelT, MongoModelT> Pagination<ModelT> paginationFromQuery(dev.morphia.query.Query<MongoModelT> query, int offset, int count, Class<ModelT> modelTClass)
      Transforms the given Query to the resulting Pagination.
      Type Parameters:
      ModelT - the desired model type
      MongoModelT - the mongoDB model type
      Parameters:
      query - the query
      offset - the offset
      count - the count
      modelTClass - the destination Class
      Returns:
      a Pagination instance for the given ModelT
    • paginationFromQuery

      public <ModelT, MongoModelT> Pagination<ModelT> paginationFromQuery(dev.morphia.query.Query<MongoModelT> query, int offset, int count, Function<MongoModelT,ModelT> function)
      Transforms the given Query to the resulting Pagination.
      Type Parameters:
      ModelT - the desired model type
      MongoModelT - the mongoDB model type
      Parameters:
      query - the query
      offset - the offset
      count - the count
      function - the function to transform the values
      Returns:
      a Pagination instance for the given ModelT
    • paginationFromQuery

      public <ModelT, MongoModelT> Pagination<ModelT> paginationFromQuery(dev.morphia.query.Query<MongoModelT> query, int offset, int count, Function<MongoModelT,ModelT> function, dev.morphia.query.FindOptions options)
      Transforms the given Query to the resulting Pagination.
      Type Parameters:
      ModelT - the desired model type
      MongoModelT - the mongoDB model type
      Parameters:
      query - the query
      offset - the offset
      count - the count
      function - the function to transform the values
      options - a FindOptions used to modify the query results
      Returns:
      a Pagination instance for the given ModelT
    • tabulationFromQuery

      public <ModelT, MongoModelT> Tabulation<ModelT> tabulationFromQuery(dev.morphia.query.Query<MongoModelT> query, Function<MongoModelT,ModelT> function)
      Transforms the given Query to the resulting Pagination.
      Type Parameters:
      ModelT - the desired model type
      MongoModelT - the mongoDB model type
      Parameters:
      query - the query
      function - the function to transform the values
      Returns:
      a Pagination instance for the given ModelT
    • tabulationFromQuery

      public <ModelT, MongoModelT> Tabulation<ModelT> tabulationFromQuery(dev.morphia.query.Query<MongoModelT> query, Function<MongoModelT,ModelT> function, dev.morphia.query.FindOptions options)
      Transforms the given Query to the resulting Pagination.
      Type Parameters:
      ModelT - the desired model type
      MongoModelT - the mongoDB model type
      Parameters:
      query - the query
      function - the function to transform the values
      options - a FindOptions used to modify the query results
      Returns:
      a Pagination instance for the given ModelT
    • paginationFromAggregation

      public <MongoModelT> Pagination<MongoModelT> paginationFromAggregation(Supplier<dev.morphia.aggregation.Aggregation<?>> aggregationSupplier, Class<MongoModelT> modelTClass, int offset, int count)
      Transforms an Aggregation to the resulting Pagination. This function must modify and build upon the supplied Aggregation.
      Type Parameters:
      MongoModelT - the mongoDB model type
      Parameters:
      aggregationSupplier - a Supplier<?> for the aggregation
      offset - the offset
      count - the count
      Returns:
      a Pagination instance for the given ModelT
    • paginationFromAggregation

      public <MongoModelT> Pagination<MongoModelT> paginationFromAggregation(Supplier<dev.morphia.aggregation.Aggregation<?>> aggregationSupplier, Class<MongoModelT> modelTClass, int offset, int count, dev.morphia.aggregation.AggregationOptions options)
      Transforms an Aggregation to the resulting Pagination. This function must modify and build upon the supplied Aggregation.
      Type Parameters:
      MongoModelT - the mongoDB model type
      Parameters:
      aggregationSupplier - a Supplier<?> for the aggregation
      offset - the offset
      count - the count
      options - a AggregationOptions used to modify the query results
      Returns:
      a Pagination instance for the given ModelT
    • isScanQuery

      public boolean isScanQuery(dev.morphia.query.Query<?> query)
    • isIndexedQuery

      public boolean isIndexedQuery(dev.morphia.query.Query<?> query)
    • isIndexedPlan

      public boolean isIndexedPlan(org.bson.Document plan)
    • getDatastore

      public dev.morphia.Datastore getDatastore()
    • setDatastore

      @Inject public void setDatastore(dev.morphia.Datastore datastore)
    • getQueryMaxResults

      public int getQueryMaxResults()
    • setQueryMaxResults

      @Inject public void setQueryMaxResults(@Named("dev.getelements.elements.query.max.results") int queryMaxResults)
    • getMapper

      public MapperRegistry getMapper()
    • setMapper

      @Inject public void setMapper(MapperRegistry mapperRegistry)