Guest User

Untitled

a guest
Jul 13th, 2016
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. @Singleton
  2. public class ExecutionDao {
  3.  
  4.     @Inject MongoClient mongoClient;
  5.     @Inject Mapper mapper;
  6.  
  7.     MongoCollection mongoCollection;
  8.  
  9.     @PostConstruct
  10.     public void init() {
  11.         Jongo jongo = new Jongo(mongoClient.getDB("mongo_platform"), mapper);
  12.         mongoCollection = jongo.getCollection("executions");
  13.         mongoCollection.ensureIndex("{creationDate:1}");
  14.     }
  15.  
  16.     public Optional<Execution> findLastSuccessful(String executionId) {
  17.         Iterable<Execution> lastSuccessfulExecutions = mongoCollection.find(
  18.                 "{$and: [ {executionId : #}  ," +
  19.                         " { $where : \"function() { " +
  20.                         "                   return this.executions.every( function(operation) {" +
  21.                         "                           return (operation.read.executionStatus === 'SUCCESS' " +
  22.                         "                                   && operation.process.executionStatus === 'SUCCESS' " +
  23.                         "                                   && operation.write.executionStatus === 'SUCCESS') " +
  24.                         "                           }) " +
  25.                         "               }\"" +
  26.                         " } " +
  27.                         "] " +
  28.                         "}", executionId)
  29.                 .sort("{'creationDate' : -1}").limit(1).as(Execution.class);
  30.  
  31.         return FluentIterable.from(lastSuccessfulExecutions).first();
  32.     }
  33. }
  34.  
  35. public class MongoClientFactory implements Factory<MongoClient> {
  36.  
  37.     @Override
  38.     public MongoClient provide() {
  39.         MongoClientOptions mongoClientOptions = new MongoClientOptions.Builder()
  40.                 .cursorFinalizerEnabled(false) //
  41.                 .connectionsPerHost(250).build(); //
  42.         return new MongoClient(new ServerAddress("localhost", 27017), mongoClientOptions);
  43.     }
  44.  
  45.     @Override
  46.     public void dispose(MongoClient mongoClient) {
  47.  
  48.     }
  49. }
  50.  
  51. public class JongoMapperFactory implements Factory<Mapper> {
  52.  
  53.     @Override
  54.     public Mapper provide() {
  55.         return new JacksonMapper.Builder()
  56.                 .registerModule(new JodaModule()
  57.                         .addSerializer(LocalDateTime.class, new CustomLocalDateTimeSerializer())
  58.                         .addDeserializer(LocalDateTime.class, new CustomLocalDateTimeDeserializer())
  59.                 ).disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
  60.                 .build();
  61.     }
  62.  
  63.     @Override
  64.     public void dispose(Mapper mapper) {
  65.  
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment