use of org.springframework.data.mongodb.core.query.Update in project books by aidanwhiteley.
the class UserRepositoryImpl method updateUserRoles.
@Override
public long updateUserRoles(ClientRoles clientRoles) {
List<User.Role> roles = new ArrayList<>();
if (clientRoles.isAdmin()) {
roles.add(User.Role.ROLE_ADMIN);
}
if (clientRoles.isEditor()) {
roles.add(User.Role.ROLE_EDITOR);
}
Query query = new Query(Criteria.where("id").is(clientRoles.getId()));
Update update = new Update();
update.set("roles", roles);
UpdateResult result = mongoTemplate.updateFirst(query, update, User.class);
// noinspection ConstantConditions
if (result != null) {
return result.getModifiedCount();
} else {
return 0;
}
}
use of org.springframework.data.mongodb.core.query.Update in project commons-dao by reportportal.
the class ProjectRepositoryCustomImpl method removeUserFromProjects.
@Override
public void removeUserFromProjects(String userId) {
Query query = Query.query(Criteria.where(USER_LOGIN).is(userId));
mongoTemplate.updateMulti(query, new Update().pull("users", new BasicDBObject("login", userId)), Project.class);
}
use of org.springframework.data.mongodb.core.query.Update in project commons-dao by reportportal.
the class TestItemRepositoryCustomImpl method updateItemsIssues.
@Override
public void updateItemsIssues(Map<String, TestItemIssue> forUpdate) {
Query query = query(where(ID).in(forUpdate.keySet()));
Update update = new Update();
mongoTemplate.stream(query, TestItem.class).forEachRemaining(dbo -> {
String currentId = dbo.getId();
TestItemIssue newValue = forUpdate.get(currentId);
update.set(ISSUE_TYPE, newValue.getIssueType());
update.set(ISSUE_DESCRIPTION, newValue.getIssueDescription());
update.set(ISSUE_TICKET, newValue.getExternalSystemIssues());
update.set(ISSUE_ANALYZED, newValue.isAutoAnalyzed());
mongoTemplate.updateFirst(Query.query(Criteria.where(ID).is(currentId)), update, mongoTemplate.getCollectionName(TestItem.class));
});
}
use of org.springframework.data.mongodb.core.query.Update in project commons-dao by reportportal.
the class UserPreferenceRepositoryCustomImpl method deleteUnsharedFilters.
@Override
public void deleteUnsharedFilters(String username, String project, String filterId) {
Query query = Query.query(where("userRef").ne(username)).addCriteria(where("projectRef").is(project)).addCriteria(where("launchTabs.filters").is(filterId));
Update update = new Update().pull("launchTabs.filters", filterId);
mongoTemplate.updateMulti(query, update, UserPreference.class);
}
use of org.springframework.data.mongodb.core.query.Update in project spring-data-mongodb by spring-projects.
the class MongoTemplate method doSaveVersioned.
private <T> T doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity, String collectionName) {
ConvertingPropertyAccessor convertingAccessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(objectToSave), mongoConverter.getConversionService());
MongoPersistentProperty property = entity.getRequiredVersionProperty();
Number number = convertingAccessor.getProperty(property, Number.class);
if (number != null) {
// Bump version number
convertingAccessor.setProperty(property, number.longValue() + 1);
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
assertUpdateableIdIfNotSet(objectToSave);
Document document = new Document();
this.mongoConverter.write(objectToSave, document);
maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, document, collectionName));
Update update = Update.fromDocument(document, ID_FIELD);
// Create query for entity with the id and old version
MongoPersistentProperty idProperty = entity.getRequiredIdProperty();
Object id = entity.getIdentifierAccessor(objectToSave).getRequiredIdentifier();
Query query = new Query(Criteria.where(idProperty.getName()).is(id).and(property.getName()).is(number));
UpdateResult result = doUpdate(collectionName, query, update, objectToSave.getClass(), false, false);
if (result.getModifiedCount() == 0) {
throw new OptimisticLockingFailureException(String.format("Cannot save entity %s with version %s to collection %s. Has it been modified meanwhile?", id, number, collectionName));
}
maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, document, collectionName));
return objectToSave;
}
doInsert(collectionName, objectToSave, this.mongoConverter);
return objectToSave;
}
Aggregations