Example usage for org.springframework.data.mongodb.core FindAndModifyOptions FindAndModifyOptions

List of usage examples for org.springframework.data.mongodb.core FindAndModifyOptions FindAndModifyOptions

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core FindAndModifyOptions FindAndModifyOptions.

Prototype

FindAndModifyOptions

Source Link

Usage

From source file:no.nlf.dal.LicenseController.java

public License update(License license) {
    MongoLicense mongoLicense = new MongoLicense(license);
    Query searchQuery = new Query(Criteria.where("id").is(mongoLicense.getId()));

    Update updateLicense = new Update();

    updateLicense.set("melwinId", mongoLicense.getMelwinId());
    updateLicense.set("licenseName", mongoLicense.getLicenseName());
    updateLicense.set("active", mongoLicense.isActive());

    mongoLicense = appContext.mongoOperation().findAndModify(searchQuery, updateLicense,
            new FindAndModifyOptions().returnNew(true), MongoLicense.class);

    if (mongoLicense == null) {
        return new License();
    }//from w  w w  .  j  ava 2  s.c o m

    return mongoLicense.toLicense();
}

From source file:eu.trentorise.game.managers.DBPlayerManager.java

private StatePersistence persist(String gameId, String playerId, List<GenericObjectPersistence> concepts,
        CustomData customData, Map<String, Object> metadata) {
    if (StringUtils.isBlank(gameId) || StringUtils.isBlank(playerId)) {
        throw new IllegalArgumentException("field gameId and playerId of PlayerState MUST be set");
    }//from ww  w  .  j  av  a 2  s. c  o  m

    Criteria criteria = new Criteria();
    criteria = criteria.and("gameId").is(gameId).and("playerId").is(playerId);
    Query query = new Query(criteria);
    Update update = new Update();
    if (concepts != null) {
        update.set("concepts", concepts);
    }
    if (customData != null) {
        update.set("customData", customData);
    }
    if (metadata != null) {
        update.set("metadata", metadata);
    }
    FindAndModifyOptions options = new FindAndModifyOptions();
    options.upsert(true);
    options.returnNew(true);
    return mongoTemplate.findAndModify(query, update, options, StatePersistence.class);
}

From source file:no.nlf.dal.ParachutistController.java

public Parachutist update(Parachutist parachutist) {
    MongoParachutist mongoParachutist = new MongoParachutist(parachutist);

    Update updateMongoParacutist = new Update();

    Query queryMongoParachutists = new Query(Criteria.where("melwinId").is(mongoParachutist.getMelwinId()));

    updateMongoParacutist.set("memberclubs", mongoParachutist.getMemberclubs());
    updateMongoParacutist.set("licenses", mongoParachutist.getLicenses());
    updateMongoParacutist.set("firstname", mongoParachutist.getFirstname());
    updateMongoParacutist.set("lastname", mongoParachutist.getLastname());
    updateMongoParacutist.set("bithdate", mongoParachutist.getBirthdate());
    updateMongoParacutist.set("street", mongoParachutist.getStreet());
    updateMongoParacutist.set("gender", mongoParachutist.getGender());
    updateMongoParacutist.set("phone", mongoParachutist.getPhone());
    updateMongoParacutist.set("mail", mongoParachutist.getMail());
    updateMongoParacutist.set("postnumber", mongoParachutist.getPostnumber());
    updateMongoParacutist.set("postplace", mongoParachutist.getPostplace());

    mongoParachutist = appContext.mongoOperation().findAndModify(queryMongoParachutists, updateMongoParacutist,
            new FindAndModifyOptions().returnNew(true), MongoParachutist.class);

    if (mongoParachutist != null) {
        return mongoParachutist.toParachutist();
    }/*from www.j  a v  a2  s.  com*/

    return new Parachutist();
}

From source file:se.inera.axel.shs.broker.messagestore.internal.MongoMessageLogService.java

@Override
public ShsMessageEntry loadEntryAndLockForFetching(String shsTo, String txId) {

    Query query = new Query(Criteria.where("label.txId").is(txId).and("state").is(MessageState.RECEIVED)
            .and("archived").in(false, null).and("label.to.value").is(shsTo));

    Update update = new Update();
    update.set("stateTimeStamp", new Date());
    update.set("state", MessageState.FETCHING_IN_PROGRESS);

    // Enforces that the found object is returned by findAndModify(), i.e. not the original input object
    // It returns null if no document could be updated
    FindAndModifyOptions options = new FindAndModifyOptions().returnNew(true);

    ShsMessageEntry entry = mongoTemplate.findAndModify(query, update, options, ShsMessageEntry.class);

    if (entry == null) {
        throw new MessageNotFoundException("Message entry not found in message log: " + txId);
    }//from  w ww . ja  v  a 2 s. c om

    return entry;
}

From source file:se.inera.axel.shs.broker.messagestore.internal.MongoMessageLogService.java

@Override
public int releaseStaleFetchingInProgress() {
    // Anything older than one hour
    Date dateTime = new Date(System.currentTimeMillis() - 3600 * 1000);

    // List all FETCHING_IN_PROGRESS messages
    Query queryList = Query.query(
            Criteria.where("state").is(MessageState.FETCHING_IN_PROGRESS).and("stateTimeStamp").lt(dateTime));
    List<ShsMessageEntry> list = mongoTemplate.find(queryList, ShsMessageEntry.class);

    for (ShsMessageEntry item : list) {

        // Double check that it is still FETCHING_IN_PROGRESS
        Query queryItem = new Query(Criteria.where("label.txId").is(item.getLabel().getTxId()).and("state")
                .is(MessageState.FETCHING_IN_PROGRESS).and("stateTimeStamp").lt(dateTime));

        Update update = new Update();
        update.set("stateTimeStamp", new Date());
        update.set("state", MessageState.RECEIVED);

        // Enforces that the found object is returned by findAndModify(), i.e. not the original input object
        // It returns null if no document could be updated
        FindAndModifyOptions options = new FindAndModifyOptions().returnNew(true);

        ShsMessageEntry entry = mongoTemplate.findAndModify(queryItem, update, options, ShsMessageEntry.class);

        if (entry != null) {
            log.info("ShsMessageEntry with state FETCHING_IN_PROGRESS moved back to RECEIVED [txId: "
                    + entry.getLabel().getTxId() + "]");
        }/* w w w. java  2s .co  m*/
    }

    return list.size();
}

From source file:org.slc.sli.dal.repository.MongoEntityRepository.java

@Override
@MigrateEntity//from w  w  w.j  a v a2s.c  o m
public Entity findAndUpdate(String collectionName, NeutralQuery neutralQuery, Update update) {
    Query query = this.getQueryConverter().convert(collectionName, neutralQuery);
    FindAndModifyOptions options = new FindAndModifyOptions();
    Entity result = template.findAndModify(query, update, options, getRecordClass(), collectionName);

    if (result != null && collectionName.equals(EntityNames.EDUCATION_ORGANIZATION)) {
        updateAllSchoolLineage();
    }

    return result;
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

public <T> T findAndModify(Query query, Update update, Class<T> entityClass) {
    return findAndModify(query, update, new FindAndModifyOptions(), entityClass,
            determineCollectionName(entityClass));
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

public <T> T findAndModify(Query query, Update update, Class<T> entityClass, String collectionName) {
    return findAndModify(query, update, new FindAndModifyOptions(), entityClass, collectionName);
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

protected <T> T doFindAndModify(String collectionName, DBObject query, DBObject fields, DBObject sort,
        Class<T> entityClass, Update update, FindAndModifyOptions options) {

    EntityReader<? super T, DBObject> readerToUse = this.mongoConverter;

    if (options == null) {
        options = new FindAndModifyOptions();
    }//from w w  w  .  ja  va 2s.c om

    MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);

    DBObject updateObj = update.getUpdateObject();
    for (String key : updateObj.keySet()) {
        updateObj.put(key, mongoConverter.convertToMongoType(updateObj.get(key)));
    }

    DBObject mappedQuery = mapper.getMappedObject(query, entity);

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("findAndModify using query: " + mappedQuery + " fields: " + fields + " sort: " + sort
                + " for class: " + entityClass + " and update: " + updateObj + " in collection: "
                + collectionName);
    }

    return executeFindOneInternal(new FindAndModifyCallback(mappedQuery, fields, sort, updateObj, options),
            new ReadDbObjectCallback<T>(readerToUse, entityClass), collectionName);
}