Example usage for org.springframework.data.mongodb.core.query Update addToSet

List of usage examples for org.springframework.data.mongodb.core.query Update addToSet

Introduction

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

Prototype

public Update addToSet(String key, Object value) 

Source Link

Document

Update using the $addToSet update modifier

Usage

From source file:app.data.local.CollectionBindingRepositoryImpl.java

@Override
public void addFavor(long colId, String uid, @Nonnull CollectionBinding.Data data) {
    if (hasExistFavor(colId, data)) {
        return;//from www  . j ava  2s  .  c o m
    }
    data.uid = uid;

    Query query = new Query();
    query.addCriteria(Criteria.where("collectionId").is(colId));

    Update update = new Update();
    update.addToSet("favors", data);

    mMongoTemplate.upsert(query, update, CollectionBinding.class);
}

From source file:org.ow2.play.metadata.service.MetadataServiceImpl.java

@Override
public void setMetadata(Resource resource, Metadata metadata) throws MetadataException {
    if (logger.isLoggable(Level.INFO))
        logger.info(String.format("Setting metdata %s to resource %s", metadata, resource));

    logger.warning("FIXME : Set the metadata value");
    Update update = new Update();
    update.addToSet("metadata", metadata);
    mongoTemplate.updateFirst(// w  w w.  ja v  a 2s .c  o m
            query(where("resource.name").is(resource.getName()).and("resource.url").is(resource.getUrl())),
            update, org.ow2.play.metadata.service.document.MetaResource.class);
}

From source file:org.ow2.play.metadata.service.MetadataServiceImpl.java

@Override
@WebMethod/*from w w  w .ja  v  a2 s.  c o  m*/
public void addMetadata(Resource resource, Metadata metadata) throws MetadataException {

    if (logger.isLoggable(Level.INFO))
        logger.info(String.format("Adding metdata %s to resource %s", metadata, resource));

    if (resource == null || resource.getName() == null || resource.getUrl() == null) {
        throw new MetadataException("Null resource");
    }

    if (metadata == null || metadata.getData() == null || metadata.getName() == null) {
        throw new MetadataException("Null metadata");
    }

    Update update = new Update();
    update.addToSet("metadata", metadata);
    mongoTemplate.updateFirst(
            query(where("resource.name").is(resource.getName()).and("resource.url").is(resource.getUrl())),
            update, org.ow2.play.metadata.service.document.MetaResource.class);

}

From source file:com.traffitruck.service.MongoDAO.java

public void addDevice(String username, String regid) {
    Query findByUsername = new Query().addCriteria(Criteria.where("username").is(username));
    Update update = new Update();
    update.addToSet("registrationIds", regid);
    mongoTemplate.upsert(findByUsername, update, LoadsUser.class);
}

From source file:com.gongpingjia.carplay.service.impl.ActivityServiceImpl.java

@Override
public ResponseDo sendAppointment(String activityId, String userId, Appointment appointment)
        throws ApiException {
    Activity activity = activityDao.findOne(Query.query(Criteria.where("activityId").is(activityId)));
    if (activity == null) {
        LOG.warn("No activity exist : {}", activityId);
        throw new ApiException("");
    }/*from w w  w  .  j av a 2  s.  c  om*/

    List<String> members = activity.getMembers();
    for (String member : members) {
        if (member.equals(userId)) {
            LOG.warn("Already be a member");
            throw new ApiException("????");
        }
    }

    Appointment appointmentData = appointmentDao.findOne(Query.query(Criteria.where("activityId").is(activityId)
            .and("applyUserId").is(userId).and("status").is(Constants.AppointmentStatus.APPLYING)));
    if (appointmentData != null) {
        LOG.warn("already applying for this activity");
        throw new ApiException("??");
    }

    User user = userDao.findById(userId);

    Long current = DateUtil.getTime();
    appointment.setActivityId(activity.getActivityId());
    appointment.setApplyUserId(userId);
    appointment.setInvitedUserId(activity.getUserId());
    appointment.setCreateTime(current);
    appointment.setStatus(Constants.AppointmentStatus.APPLYING);
    appointment.setModifyTime(current);
    appointment.setActivityCategory(Constants.ActivityCatalog.COMMON);
    //appointment destination activity destination  destPoint   estabPoint
    //appointment  estabPoint  applyUserId ? landmark
    appointment.setDestPoint(activity.getEstabPoint());
    appointment.setDestination(activity.getDestination());
    appointment.setEstabPoint(user.getLandmark());
    appointment.setDistance(DistanceUtil.getDistance(appointment.getEstabPoint(), appointment.getDestPoint()));
    appointmentDao.save(appointment);

    //?ID
    Update update = new Update();
    update.addToSet("applyIds", userId);
    activityDao.update(activityId, update);

    //????
    User organizer = userDao.findById(activity.getUserId());
    User applier = userDao.findById(userId);
    String message = MessageFormat.format(
            PropertiesUtil.getProperty("dynamic.format.activity.invite", "{0}{1}"),
            applier.getNickname(), activity.getType());
    chatThirdPartyService.sendUserGroupMessage(chatCommonService.getChatToken(),
            Constants.EmchatAdmin.ACTIVITY_STATE, organizer.getEmchatName(), message);

    return ResponseDo.buildSuccessResponse();
}

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

public WriteResult updateFirst(NeutralQuery query, Map<String, Object> update, String collectionName) {
    // Enforcing the tenantId query. The rationale for this is all CRUD
    // Operations should be restricted based on tenant.
    this.addDefaultQueryParams(query, collectionName);

    Query convertedQuery = this.queryConverter.convert(collectionName, query);
    Update convertedUpdate = new Update();

    for (Map.Entry<String, Object> entry : update.entrySet()) {
        String operation = entry.getKey();
        @SuppressWarnings("unchecked")
        Map<String, Object> operands = (Map<String, Object>) entry.getValue();

        if (operation.equals("push")) {
            for (Map.Entry<String, Object> fieldValues : operands.entrySet()) {
                convertedUpdate.push(fieldValues.getKey(), fieldValues.getValue());
            }/*from ww w  . j  a v  a2  s.  com*/
        } else if (operation.equals("pushAll")) {
            for (Map.Entry<String, Object> fieldValues : operands.entrySet()) {
                convertedUpdate.pushAll(fieldValues.getKey(), (Object[]) fieldValues.getValue());
            }
        } else if (operation.equals("addToSet")) {
            for (Map.Entry<String, Object> fieldValues : operands.entrySet()) {
                convertedUpdate.addToSet(fieldValues.getKey(), fieldValues.getValue());
            }
        }
    }

    return updateFirst(convertedQuery, convertedUpdate, collectionName);
}

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

@Override
public WriteResult updateMulti(NeutralQuery query, Map<String, Object> update, String collectionName) {
    // Enforcing the tenantId query. The rationale for this is all CRUD
    // Operations should be restricted based on tenant.
    this.addDefaultQueryParams(query, collectionName);

    Query convertedQuery = this.queryConverter.convert(collectionName, query);
    Update convertedUpdate = new Update();

    for (Map.Entry<String, Object> entry : update.entrySet()) {
        String operation = entry.getKey();
        @SuppressWarnings("unchecked")
        Map<String, Object> operands = (Map<String, Object>) entry.getValue();

        if (operation.equals("push")) {
            for (Map.Entry<String, Object> fieldValues : operands.entrySet()) {
                convertedUpdate.push(fieldValues.getKey(), fieldValues.getValue());
            }/*from  ww w  .j a  v  a  2s . c om*/
        } else if (operation.equals("pushAll")) {
            for (Map.Entry<String, Object> fieldValues : operands.entrySet()) {
                convertedUpdate.pushAll(fieldValues.getKey(), (Object[]) fieldValues.getValue());
            }
        } else if (operation.equals("addToSet")) {
            for (Map.Entry<String, Object> fieldValues : operands.entrySet()) {
                convertedUpdate.addToSet(fieldValues.getKey(), fieldValues.getValue());
            }
        }
    }
    guideIfTenantAgnostic(collectionName);
    return template.updateMulti(convertedQuery, convertedUpdate, collectionName);
}

From source file:piecework.repository.concrete.ProcessInstanceRepositoryCustomImpl.java

private void include(Update update, Map<String, List<Value>> data, String id) {
    if (data != null && !data.isEmpty()) {
        MongoConverter converter = mongoOperations.getConverter();
        MongoTypeMapper typeMapper = converter.getTypeMapper();

        Set<String> keywords = new HashSet<String>();
        for (Map.Entry<String, List<Value>> entry : data.entrySet()) {
            String key = "data." + entry.getKey();
            List<Value> values = entry.getValue();
            List<Object> dbObjects = new ArrayList<Object>();

            for (Value value : values) {
                if (value != null) {
                    Object dbObject = converter.convertToMongoType(value);
                    Class<?> clz = null;
                    if (value instanceof File)
                        clz = File.class;
                    else if (value instanceof User)
                        clz = User.class;
                    else if (value instanceof Secret)
                        clz = Secret.class;
                    else if (value instanceof DateValue)
                        clz = DateValue.class;

                    keywords.addAll(ProcessInstanceUtility.keywords(value));

                    if (clz != null)
                        typeMapper.writeType(clz, DBObject.class.cast(dbObject));

                    dbObjects.add(dbObject);
                }/*from   w w w. java  2s.c  om*/
            }

            update.set(key, dbObjects);
        }
        if (!keywords.isEmpty()) {
            BasicDBList eachList = new BasicDBList();
            for (String keyword : keywords) {
                eachList.add(keyword);
            }
            if (StringUtils.isNotEmpty(id))
                eachList.add(id);
            update.addToSet("keywords", BasicDBObjectBuilder.start("$each", eachList).get());
        }

    }
}