public boolean updateGameLog(String pbfId, String oldUsername, String newUsername) {
   List<GameLog> gameLogs =
       gameLogCollection.find(DBQuery.is("pbfId", pbfId).is("username", oldUsername)).toArray();
   for (GameLog gl : gameLogs) {
     gl.setUsername(newUsername);
     gameLogCollection.updateById(gl.getId(), gl);
   }
   return !gameLogs.isEmpty();
 }
 @Override
 public List<Spoke> getSpokes() {
   JacksonDBCollection<Spoke, String> col =
       JacksonDBCollection.wrap(mongoDB.getCollection("spokes"), Spoke.class, String.class);
   List<Spoke> res = new ArrayList<Spoke>();
   for (Spoke s : col.find()) {
     res.add(s);
   }
   return res;
 }
 @Override
 public List<Hub> getHubs() {
   JacksonDBCollection<Hub, String> col =
       JacksonDBCollection.wrap(mongoDB.getCollection("hubs"), Hub.class, String.class);
   List<Hub> res = new ArrayList<Hub>();
   for (Hub s : col.find()) {
     res.add(s);
   }
   return res;
 }
 @Override
 public List<TruckType> getTruckTypes() {
   JacksonDBCollection<TruckType, String> col =
       JacksonDBCollection.wrap(
           mongoDB.getCollection("truckTypes"), TruckType.class, String.class);
   List<TruckType> res = new ArrayList<TruckType>();
   for (TruckType s : col.find()) {
     res.add(s);
   }
   return res;
 }
  private <T extends AbstractEdmEntityImpl, G extends MongoTermList<T>> boolean migratePhaseTwo(
      JacksonDBCollection<MongoTerm, String> termColl_source,
      JacksonDBCollection<MongoTerm, String> termColl_target,
      ContextualCategory contextualCategory,
      int skip)
      throws UnknownHostException {
    int counter = 0;
    int interval = 0;
    while (true) {
      try {

        DBCursor<MongoTerm> termCurs = termColl_source.find().skip(skip);
        termCurs.addOption(com.mongodb.Bytes.QUERYOPTION_NOTIMEOUT);
        while (termCurs.hasNext()) {
          MongoTerm term = termCurs.next();
          counter++;
          interval++;
          if (interval == 1000) {
            System.out.println(
                String.format("Phase 2 (%s): %d ", contextualCategory.getEntityClass(), counter));
            interval = 0;
          }

          term.setId(null);
          String codeUri = lookupOriginalCodeUri(term.getCodeUri());

          if (codeUri != null) {
            term.setCodeUri(codeUri);
            try {
              termColl_target.insert(term);

            } catch (DuplicateKeyException me) {
              // do nothing
              // System.out.println("Duplicate:" +
              // term.toString());
              continue;
            }
          }
        }
        termCurs.close();
        break;
      } catch (Exception e) {
        e.printStackTrace();
        initialiseConnections();
        counter--;
        skip = counter;
        continue;
      }
    }
    return true;
  }
  public void migrateAllPhase() throws UnknownHostException {

    mongoSequence = sequenceColl_target.findOne();
    if (mongoSequence == null) {
      mongoSequence = new MongoSequence();
    }

    /*
    migratePhaseOne(conceptTermListColl_source, conceptTermColl_source, conceptTermListColl_target,
    		conceptTermColl_target, ContextualCategory.CONCEPT, "concept");
    migratePhaseOne(timespanTermListColl_source, timespanTermColl_source, timespanTermListColl_target,
    		timespanTermColl_target, ContextualCategory.TIMESPAN, "period");
    migratePhaseOne(agentTermListColl_source, agentTermColl_source, agentTermListColl_target, agentTermColl_target,
    		ContextualCategory.AGENT, "people");*/
    migratePhaseOne(
        placeTermListColl_source,
        placeTermColl_source,
        placeTermListColl_target,
        placeTermColl_target,
        ContextualCategory.PLACE,
        "place");

    // loadCache();

    /*
    migratePhaseTwo(conceptTermColl_source, conceptTermColl_target, ContextualCategory.CONCEPT, 0);
    migratePhaseTwo(timespanTermColl_source, timespanTermColl_target, ContextualCategory.TIMESPAN, 0);
    migratePhaseTwo(agentTermColl_source, agentTermColl_target, ContextualCategory.AGENT, 0);
    */
    migratePhaseTwo(placeTermColl_source, placeTermColl_target, ContextualCategory.PLACE, 0);

    /*migratePhaseThree(conceptTermListColl_source, conceptTermListColl_target,
    		new ConceptRepresentationMigration(lookupCodeUri, lookupOriginalCodeUri), ContextualCategory.CONCEPT,
    		0);
    migratePhaseThree(timespanTermListColl_source, timespanTermListColl_target,
    		new TimespanRepresentationMigration(lookupCodeUri, lookupOriginalCodeUri), ContextualCategory.TIMESPAN,
    		0);
    migratePhaseThree(agentTermListColl_source, agentTermListColl_target,
    		new AgentRepresentationMigration(lookupCodeUri, lookupOriginalCodeUri), ContextualCategory.AGENT, 0);*/
    migratePhaseThree(
        placeTermListColl_source,
        placeTermListColl_target,
        new PlaceRepresentationMigration(lookupCodeUri, lookupOriginalCodeUri),
        ContextualCategory.PLACE,
        0);

    sequenceColl_target.remove(new BasicDBObject());
    mongoSequence.setId(null);
    sequenceColl_target.insert(mongoSequence);
  }
  private <T extends AbstractEdmEntityImpl, G extends MongoTermList<T>> boolean migratePhaseOne(
      JacksonDBCollection<G, String> termListColl_source,
      JacksonDBCollection<MongoTerm, String> termColl_source,
      JacksonDBCollection<G, String> termListColl_target,
      JacksonDBCollection<MongoTerm, String> termColl_target,
      ContextualCategory contextualCategory,
      String termCollection)
      throws UnknownHostException {
    int skip = 0;
    int counter = 0;
    while (true) {
      try {
        DBCursor<G> curs =
            termListColl_source
                .find(new BasicDBObject("entityType", contextualCategory.getEntityClass()))
                .skip(skip);
        curs.addOption(com.mongodb.Bytes.QUERYOPTION_NOTIMEOUT);

        while (curs.hasNext()) {
          long nextSequence = nextSequence(contextualCategory);
          counter++;

          G termList = curs.next();
          String newCodeUri =
              String.format(
                  "http://data.europeana.eu/%s/base" + "/%d",
                  contextualCategory.getLabel(), nextSequence);
          String oldCodeUri = termList.getCodeUri();

          MongoCodeLookup lookup = new MongoCodeLookup();
          lookup.setCodeUri(newCodeUri);
          lookup.setOriginalCodeUri(oldCodeUri);
          lookupColl_target.insert(lookup);

          lookupCodeUri.put(newCodeUri, oldCodeUri);
          lookupOriginalCodeUri.put(oldCodeUri, newCodeUri);
        }
        curs.close();
        break;
      } catch (Exception e) {
        e.printStackTrace();
        initialiseConnections();

        skip = counter - 1;
        continue;
      }
    }
    return true;
  }
  @Override
  public Output create(Output request) throws ValidationException {
    final OutputImpl outputImpl = implOrFail(request);
    final WriteResult<OutputImpl, String> writeResult = coll.save(outputImpl);

    return writeResult.getSavedObject();
  }
 @Override
 public List<Shipment> getShipments() {
   List<Shipment> res = new ArrayList<Shipment>();
   for (Shipment s : shipments.find()) {
     res.add(s);
   }
   return res;
 }
  @Override
  public Output update(String id, Map<String, Object> deltas) {
    DBUpdate.Builder update = new DBUpdate.Builder();
    for (Map.Entry<String, Object> fields : deltas.entrySet())
      update = update.set(fields.getKey(), fields.getValue());

    return coll.findAndModify(DBQuery.is("_id", id), update);
  }
 protected void loadCache() {
   DBCursor<MongoCodeLookup> lookupCurs = lookupColl_target.find();
   lookupCurs.addOption(com.mongodb.Bytes.QUERYOPTION_NOTIMEOUT);
   while (lookupCurs.hasNext()) {
     MongoCodeLookup lookup = lookupCurs.next();
     addInCache(lookup);
   }
 }
  public MailContext getMailContext(String asociatedWorflow) {

    MailContext mail = null;
    try {
      this.db = getConnection();
      this.dbCollectionMail =
          db.getCollection(DatabaseConnectionConfig.getInstance().getMailCollection());
      // this.dbCollectionMail = db.getCollection("alarms");
      JacksonDBCollection<MailContext, String> coll;
      coll = JacksonDBCollection.wrap(this.dbCollectionMail, MailContext.class, String.class);
      mail = coll.findOneById(asociatedWorflow);

    } catch (UnknownHostException ex) {
      Logger.getLogger(DaoMailContext.class.getName()).log(Level.SEVERE, null, ex);
    }

    return mail;
  }
 @Inject
 public MongoMarketReadModelBuilder(DB mongoDb) {
   super(
       mongoDb,
       JacksonDBCollection.wrap(
           mongoDb.getCollection(MongoCollections.MARKET_READ_COLLECTION),
           MarketReadModel.class,
           String.class));
 }
Esempio n. 14
0
  public Account load(ObjectId id) {

    try {
      return repository.findOneById(id);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }
  @GET
  public List<MongoDocument> fetch(@PathParam("collection") String collection) {
    final JacksonDBCollection<MongoDocument, String> coll =
        JacksonDBCollection.wrap(
            mongoDB.getCollection(collection), MongoDocument.class, String.class);
    final DBCursor<MongoDocument> cursor = coll.find();
    final List<MongoDocument> l = new ArrayList<>();

    try {
      while (cursor.hasNext()) {
        l.add(cursor.next());
      }
    } finally {
      cursor.close();
    }

    return l;
  }
  @Override
  public Output load(String streamOutputId) throws NotFoundException {
    final Output output = coll.findOneById(streamOutputId);
    if (output == null) {
      throw new NotFoundException("Couldn't find output with id " + streamOutputId);
    }

    return output;
  }
  public int deleteMailContext(String workflowId) {
    try {
      this.db = getConnection();
      this.dbCollectionMail = this.db.getCollection(this.config.getMailCollection());
      JacksonDBCollection<MailContext, String> coll;
      coll = JacksonDBCollection.wrap(this.dbCollectionMail, MailContext.class, String.class);

      if (coll.getCount(DBQuery.is("_id", workflowId)) != 0) {
        coll.removeById(workflowId);
      } else {
        return NOT_FOUND_IN_DATABASE;
      }
    } catch (UnknownHostException ex) {
      Logger.getLogger(DaoMailContext.class.getName()).log(Level.SEVERE, null, ex);
      return ERROR_DELETING_DATA;
    }
    return DATA_SUCCESSFULLY_DELETED;
  }
  /*=================================================================
  | Function storeMailContext
  | Propósito: Store the mail settings for a particular worlflow
  | Return values: ERROR / SUCCES code (int)
  ===================================================================*/
  public int storeMailContext(MailContext mailContext) {
    try {
      this.db = getConnection();
      this.dbCollectionMail = this.db.getCollection(this.config.getMailCollection());
      JacksonDBCollection<MailContext, String> coll;
      coll = JacksonDBCollection.wrap(this.dbCollectionMail, MailContext.class, String.class);

      // if exist update
      if (coll.getCount(DBQuery.is("_id", mailContext.getAsociatedWorflow())) != 0) {
        coll.updateById(mailContext.getAsociatedWorflow(), mailContext);
      } else {
        coll.insert(mailContext);
      }
    } catch (UnknownHostException ex) {
      Logger.getLogger(DaoMailContext.class.getName()).log(Level.SEVERE, null, ex);
      return ERROR_STORING_DATA;
    }
    return DATA_SUCCESSFULLY_STORED;
  }
  public void createSequence(
      long nextConceptSequence,
      long nextAgentSequence,
      long nextPlaceSequence,
      long nextTimespanSequence) {

    mongoSequence = new MongoSequence();
    mongoSequence.setNextConceptSequence(nextConceptSequence);
    mongoSequence.setNextAgentSequence(nextAgentSequence);
    mongoSequence.setNextPlaceSequence(nextPlaceSequence);
    mongoSequence.setNextTimespanSequence(nextTimespanSequence);
    sequenceColl_target.insert(mongoSequence);
  }
 @Inject
 public OutputServiceImpl(
     MongoConnection mongoConnection,
     MongoJackObjectMapperProvider mapperProvider,
     StreamService streamService,
     OutputRegistry outputRegistry) {
   this.streamService = streamService;
   final String collectionName = OutputImpl.class.getAnnotation(CollectionName.class).value();
   this.dbCollection = mongoConnection.getDatabase().getCollection(collectionName);
   this.coll =
       JacksonDBCollection.wrap(
           dbCollection, OutputImpl.class, String.class, mapperProvider.get());
   this.outputRegistry = outputRegistry;
 }
  public ConceptTermList findOneHavingTerms() {
    DBCursor<ConceptTermList> curs =
        conceptTermListColl_source.find(
            new BasicDBObject("entityType", ContextualCategory.CONCEPT.getEntityClass()));
    curs.addOption(com.mongodb.Bytes.QUERYOPTION_NOTIMEOUT);
    ConceptTermList conceptTermList = null;
    while (curs.hasNext()) {

      ConceptTermList termList = curs.next();
      if (termList.getTerms() != null && !termList.getTerms().isEmpty()) {
        conceptTermList = termList;
        break;
      }
    }
    curs.close();
    return conceptTermList;
  }
  @PostConstruct
  public void setUp() {

    // look for the DOcloud service configuration
    JsonNode docloud = Environment.getInstance().getService("docloud");
    if (docloud == null) {
      LOG.severe("DOcloud config not found");
    } else {
      JsonNode credential = docloud.get("credentials");
      String url = credential.get("url").asText();
      String client_id = credential.get("client_id").asText();

      LOG.log(Level.INFO, "Using  DOcloud at {0}", url);

      // creates the DOcloud client
      jobclient = JobClientFactory.createDefault(url, client_id);
    }

    // get the OPL model file
    modFile = TruckingManagerBean.class.getResource("truck.mod");
    if (modFile == null) {
      LOG.severe("OPL modfile not found");
    }

    // creates an object mapper use to read/write JSON
    mapper = new ObjectMapper();

    // creates the executor that will be sued by the DOcloud client
    executor = JobExecutorFactory.custom().service(jobService).build();

    // if the MongoDB datastore does not contain data, populate it
    // automatically
    if (!mongoDB.collectionExists("hubs")) {
      LOG.info("Initializing database");
      initialize();
    }

    // start the monitoring service to process asynchronous requests
    monitorService.scheduleAtFixedRate(monitorJob, 0, 2, TimeUnit.SECONDS);

    // prepare the shipment collection for easy access
    shipments =
        JacksonDBCollection.wrap(mongoDB.getCollection("shipments"), Shipment.class, String.class);
  }
 @Override
 public void destroy(Output model) throws NotFoundException {
   coll.removeById(model.getId());
   outputRegistry.removeOutput(model);
   streamService.removeOutputFromAllStreams(model);
 }
  private <
          T extends AbstractEdmEntityImpl,
          G extends MongoTermList<T>,
          H extends AbstractRepresentationMigration<T>>
      boolean migratePhaseThree(
          JacksonDBCollection<G, String> termListColl_source,
          JacksonDBCollection<G, String> termListColl_target,
          H representationMagrition,
          ContextualCategory contextualCategory,
          int skip)
          throws UnknownHostException {
    int counter = 0;
    int interval = 0;
    while (true) {
      try {

        DBCursor<G> curs =
            termListColl_source
                .find(new BasicDBObject("entityType", contextualCategory.getEntityClass()))
                .skip(skip);
        curs.addOption(com.mongodb.Bytes.QUERYOPTION_NOTIMEOUT);

        while (curs.hasNext()) {
          G termList = curs.next();
          counter++;
          interval++;
          if (interval == 1000) {
            System.out.println(
                String.format("Phase 3 (%s): %d ", contextualCategory.getEntityClass(), counter));
            interval = 0;
          }

          String originalUri = termList.getCodeUri();
          String codeUri = lookupOriginalCodeUri(termList.getCodeUri());
          termList.setCodeUri(codeUri);
          termList.setId(null);

          String parent = termList.getParent();

          if (StringUtils.isNotBlank(parent)) {

            String parentCodeUri = lookupOriginalCodeUri(parent);
            if (parentCodeUri == null) {
              termList.setParent(null);
            } else {
              termList.setParent(parentCodeUri);
            }
          }

          T representation = termList.getRepresentation();
          representationMagrition.migrateRepresentation(codeUri, originalUri, representation);
          termList.setOwlSameAs(((PlaceImpl) representation).getOwlSameAs());
          try {
            termListColl_target.insert(termList);

          } catch (DuplicateKeyException me) {
            continue;
          }
        }
        curs.close();
        break;
      } catch (Exception e) {
        e.printStackTrace();
        initialiseConnections();
        counter--;
        skip = counter;
        continue;
      }
    }
    return true;
  }
Esempio n. 25
0
 @POST
 @Timed
 public Response createNewBlog(@Valid Blog blog) {
   collection.insert(blog);
   return Response.noContent().build();
 }
Esempio n. 26
0
  public List<Account> find(DBQuery.Query query) {

    return repository.find(query).toArray();
  }
Esempio n. 27
0
 public void save(Account account) {
   repository.save(account);
 }
  public void initialiseConnections() throws UnknownHostException {
    MongoClientOptions options =
        MongoClientOptions.builder().connectTimeout(15000).socketKeepAlive(true).build();

    Mongo mongo = new MongoClient(new ServerAddress(sourceHost, sourcePort), options);

    sourceDB = mongo.getDB(sourceDBName);

    // conceptTermListColl_source = JacksonDBCollection.wrap(sourceDB.getCollection("TermList"),
    // ConceptTermList.class,
    //		String.class);

    placeTermListColl_source =
        JacksonDBCollection.wrap(
            sourceDB.getCollection("TermList"), PlaceTermList.class, String.class);

    // timespanTermListColl_source = JacksonDBCollection.wrap(sourceDB.getCollection("TermList"),
    //		TimespanTermList.class, String.class);

    // agentTermListColl_source = JacksonDBCollection.wrap(sourceDB.getCollection("TermList"),
    // AgentTermList.class,
    //		String.class);

    // conceptTermColl_source = JacksonDBCollection.wrap(sourceDB.getCollection("concept"),
    // MongoTerm.class,
    //		String.class);

    placeTermColl_source =
        JacksonDBCollection.wrap(sourceDB.getCollection("place"), MongoTerm.class, String.class);

    // timespanTermColl_source = JacksonDBCollection.wrap(sourceDB.getCollection("period"),
    // MongoTerm.class,
    //		String.class);

    // agentTermColl_source = JacksonDBCollection.wrap(sourceDB.getCollection("people"),
    // MongoTerm.class,
    //		String.class);

    if (!sourceHost.equals(targetHost) || sourcePort != targetPort) {
      mongo = new MongoClient(new ServerAddress(targetHost, targetPort), options);
    }

    targetDB = mongo.getDB(targetDBName);

    /*
    		conceptTermListColl_target = JacksonDBCollection.wrap(targetDB.getCollection("TermList"), ConceptTermList.class,
    				String.class);
    		conceptTermListColl_target.createIndex(new BasicDBObject("codeUri", 1), new BasicDBObject("unique", true));
    */
    placeTermListColl_target =
        JacksonDBCollection.wrap(
            targetDB.getCollection("TermList"), PlaceTermList.class, String.class);
    placeTermListColl_target.createIndex(
        new BasicDBObject("codeUri", 1), new BasicDBObject("unique", true));
    /*
    		timespanTermListColl_target = JacksonDBCollection.wrap(targetDB.getCollection("TermList"),
    				TimespanTermList.class, String.class);
    		timespanTermListColl_target.createIndex(new BasicDBObject("codeUri", 1), new BasicDBObject("unique", true));

    		agentTermListColl_target = JacksonDBCollection.wrap(targetDB.getCollection("TermList"), AgentTermList.class,
    				String.class);
    		agentTermListColl_target.createIndex(new BasicDBObject("codeUri", 1), new BasicDBObject("unique", true));

    		conceptTermColl_target = JacksonDBCollection.wrap(targetDB.getCollection("concept"), MongoTerm.class,
    				String.class);
    		conceptTermColl_target.createIndex(new BasicDBObject("label", 1).append("lang", 1).append("codeUri", 1),
    				new BasicDBObject("unique", true));
    		conceptTermColl_target.createIndex(new BasicDBObject("codeUri", 1));
    */
    placeTermColl_target =
        JacksonDBCollection.wrap(targetDB.getCollection("place"), MongoTerm.class, String.class);
    placeTermColl_target.createIndex(
        new BasicDBObject("label", 1).append("lang", 1).append("codeUri", 1),
        new BasicDBObject("unique", true));
    placeTermColl_target.createIndex(new BasicDBObject("codeUri", 1));
    /*
    		timespanTermColl_target = JacksonDBCollection.wrap(targetDB.getCollection("period"), MongoTerm.class,
    				String.class);
    		timespanTermColl_target.createIndex(new BasicDBObject("label", 1).append("lang", 1).append("codeUri", 1),
    				new BasicDBObject("unique", true));
    		timespanTermColl_target.createIndex(new BasicDBObject("codeUri", 1));

    		agentTermColl_target = JacksonDBCollection.wrap(targetDB.getCollection("people"), MongoTerm.class,
    				String.class);
    		agentTermColl_target.createIndex(new BasicDBObject("label", 1).append("lang", 1).append("codeUri", 1),
    				new BasicDBObject("unique", true));
    		agentTermColl_target.createIndex(new BasicDBObject("codeUri", 1));
    */
    lookupColl_target =
        JacksonDBCollection.wrap(
            targetDB.getCollection("lookup"), MongoCodeLookup.class, String.class);
    lookupColl_target.createIndex(
        new BasicDBObject("codeUri", 1).append("originalCodeUri", 1),
        new BasicDBObject("unique", true));
    lookupColl_target.createIndex(new BasicDBObject("originalCodeUri", 1)); // for
    // reverse
    // lookup

    sequenceColl_target =
        JacksonDBCollection.wrap(
            targetDB.getCollection("sequence"), MongoSequence.class, String.class);
  }
 @Override
 public Set<Output> loadAll() {
   return toAbstractSetType(coll.find().toArray());
 }
 @Override
 public long count() {
   return coll.count();
 }