From d59c8344c7ea85b72df4dfd44d1abd95a1785bb1 Mon Sep 17 00:00:00 2001 From: Shrikant Sharat Kandula Date: Tue, 27 Feb 2024 16:48:02 +0530 Subject: [PATCH] chore: Remove mongoTemplate in BaseService (#31283) The `BaseService` keeps references to a `ReactiveMongoTemplate`, a `MongoConverter` and a `Scheduler`. For the first two, they shouldn't be directly used at all in Service classes, that's an abstraction leak. That's what repositories are for. The third one isn't really used anywhere in `BaseService` at all. This PR removes all three of these from `BaseService`. --- .../base/ActionCollectionServiceCEImpl.java | 8 +-- .../base/ActionCollectionServiceImpl.java | 9 --- .../base/ApplicationServiceCEImpl.java | 8 +-- .../base/ApplicationServiceImpl.java | 9 --- .../server/helpers/ce/bridge/Bridge.java | 6 ++ .../jslibs/base/CustomJSLibServiceCEImpl.java | 8 +-- .../jslibs/base/CustomJSLibServiceImpl.java | 15 +---- .../base/NewActionServiceCEImpl.java | 8 +-- .../newactions/base/NewActionServiceImpl.java | 9 --- .../newpages/base/NewPageServiceCEImpl.java | 8 +-- .../newpages/base/NewPageServiceImpl.java | 9 --- .../plugins/base/PluginServiceCEImpl.java | 27 ++++---- .../plugins/base/PluginServiceImpl.java | 9 --- .../ce/BaseAppsmithRepositoryCEImpl.java | 19 +++++- .../ce/params/QueryAllParams.java | 5 ++ .../appsmith/server/services/BaseService.java | 61 +++---------------- .../services/CollectionServiceImpl.java | 13 +--- .../services/PermissionGroupServiceImpl.java | 9 --- .../server/services/TenantServiceImpl.java | 15 +---- .../server/services/UserDataServiceImpl.java | 9 --- .../server/services/UserServiceImpl.java | 9 --- .../server/services/WorkspaceServiceImpl.java | 9 --- .../services/ce/CollectionServiceCEImpl.java | 12 +--- .../ce/PermissionGroupServiceCEImpl.java | 8 +-- .../services/ce/TenantServiceCEImpl.java | 8 +-- .../services/ce/UserDataServiceCEImpl.java | 36 +++-------- .../server/services/ce/UserServiceCEImpl.java | 8 +-- .../services/ce/WorkspaceServiceCEImpl.java | 8 +-- .../ApplicationServiceCECompatibleImpl.java | 9 --- ...ermissionGroupServiceCECompatibleImpl.java | 9 --- .../UserServiceCECompatibleImpl.java | 9 --- .../themes/base/ThemeServiceCEImpl.java | 8 +-- .../server/themes/base/ThemeServiceImpl.java | 9 --- .../ActionCollectionServiceImplTest.java | 25 ++++---- .../services/ce/NewActionServiceUnitTest.java | 3 - .../services/ce/PluginServiceCEImplTest.java | 3 - 36 files changed, 85 insertions(+), 352 deletions(-) diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/actioncollections/base/ActionCollectionServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/actioncollections/base/ActionCollectionServiceCEImpl.java index 07ef888de3..771641ea9d 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/actioncollections/base/ActionCollectionServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/actioncollections/base/ActionCollectionServiceCEImpl.java @@ -31,15 +31,12 @@ import org.apache.commons.lang3.ObjectUtils; import org.bson.types.ObjectId; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; -import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.util.CollectionUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import reactor.core.scheduler.Scheduler; import java.time.Instant; import java.util.ArrayList; @@ -69,10 +66,7 @@ public class ActionCollectionServiceCEImpl extends BaseService defaultResourcesService) { - super(scheduler, validator, mongoConverter, reactiveMongoTemplate, repository, analyticsService); + super(validator, repository, analyticsService); this.newActionService = newActionService; this.policyGenerator = policyGenerator; this.applicationService = applicationService; diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/actioncollections/base/ActionCollectionServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/actioncollections/base/ActionCollectionServiceImpl.java index 86c507058d..d4a2521d12 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/actioncollections/base/ActionCollectionServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/actioncollections/base/ActionCollectionServiceImpl.java @@ -12,20 +12,14 @@ import com.appsmith.server.solutions.ActionPermission; import com.appsmith.server.solutions.ApplicationPermission; import jakarta.validation.Validator; import lombok.extern.slf4j.Slf4j; -import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.stereotype.Service; -import reactor.core.scheduler.Scheduler; @Service @Slf4j public class ActionCollectionServiceImpl extends ActionCollectionServiceCEImpl implements ActionCollectionService { public ActionCollectionServiceImpl( - Scheduler scheduler, Validator validator, - MongoConverter mongoConverter, - ReactiveMongoTemplate reactiveMongoTemplate, ActionCollectionRepository repository, AnalyticsService analyticsService, NewActionService newActionService, @@ -36,10 +30,7 @@ public class ActionCollectionServiceImpl extends ActionCollectionServiceCEImpl i ActionPermission actionPermission, DefaultResourcesService defaultResourcesService) { super( - scheduler, validator, - mongoConverter, - reactiveMongoTemplate, repository, analyticsService, newActionService, diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/applications/base/ApplicationServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/applications/base/ApplicationServiceCEImpl.java index b126bff8a6..2540b8322f 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/applications/base/ApplicationServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/applications/base/ApplicationServiceCEImpl.java @@ -50,15 +50,12 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DuplicateKeyException; -import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.http.codec.multipart.Part; import org.springframework.stereotype.Service; import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import reactor.core.scheduler.Scheduler; import java.time.Instant; import java.util.ArrayList; @@ -99,10 +96,7 @@ public class ApplicationServiceCEImpl extends BaseService value) { + criteriaList.add(Criteria.where(key).in(value)); + return this; + } + public Bridge exists(@NonNull String key) { criteriaList.add(Criteria.where(key).exists(true)); return this; diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/jslibs/base/CustomJSLibServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/jslibs/base/CustomJSLibServiceCEImpl.java index 8b19094c9c..90e276c5db 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/jslibs/base/CustomJSLibServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/jslibs/base/CustomJSLibServiceCEImpl.java @@ -11,11 +11,8 @@ import com.appsmith.server.services.BaseService; import jakarta.validation.Validator; import jakarta.validation.constraints.NotNull; import lombok.extern.slf4j.Slf4j; -import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import reactor.core.scheduler.Scheduler; import java.util.Comparator; import java.util.List; @@ -30,14 +27,11 @@ public class CustomJSLibServiceCEImpl extends BaseService applicationContextBasedJsLibService; public CustomJSLibServiceCEImpl( - Scheduler scheduler, Validator validator, - MongoConverter mongoConverter, - ReactiveMongoTemplate reactiveMongoTemplate, CustomJSLibRepository repository, AnalyticsService analyticsService, ContextBasedJsLibService applicationContextBasedJsLibService) { - super(scheduler, validator, mongoConverter, reactiveMongoTemplate, repository, analyticsService); + super(validator, repository, analyticsService); this.applicationContextBasedJsLibService = applicationContextBasedJsLibService; } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/jslibs/base/CustomJSLibServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/jslibs/base/CustomJSLibServiceImpl.java index 2a6b1d6dc7..17f42f150e 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/jslibs/base/CustomJSLibServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/jslibs/base/CustomJSLibServiceImpl.java @@ -6,29 +6,16 @@ import com.appsmith.server.repositories.CustomJSLibRepository; import com.appsmith.server.services.AnalyticsService; import jakarta.validation.Validator; import lombok.extern.slf4j.Slf4j; -import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.stereotype.Service; -import reactor.core.scheduler.Scheduler; @Service @Slf4j public class CustomJSLibServiceImpl extends CustomJSLibServiceCEImpl implements CustomJSLibService { public CustomJSLibServiceImpl( - Scheduler scheduler, Validator validator, - MongoConverter mongoConverter, - ReactiveMongoTemplate reactiveMongoTemplate, CustomJSLibRepository repository, AnalyticsService analyticsService, ContextBasedJsLibService applicationContextBasedJsLibService) { - super( - scheduler, - validator, - mongoConverter, - reactiveMongoTemplate, - repository, - analyticsService, - applicationContextBasedJsLibService); + super(validator, repository, analyticsService, applicationContextBasedJsLibService); } } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/newactions/base/NewActionServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/newactions/base/NewActionServiceCEImpl.java index c5148e3be5..8c5bdc1f64 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/newactions/base/NewActionServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/newactions/base/NewActionServiceCEImpl.java @@ -62,8 +62,6 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import org.bson.types.ObjectId; import org.springframework.data.domain.Sort; -import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.util.CollectionUtils; import org.springframework.util.LinkedCaseInsensitiveMap; import org.springframework.util.LinkedMultiValueMap; @@ -72,7 +70,6 @@ import org.springframework.util.StringUtils; import reactor.core.observability.micrometer.Micrometer; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import reactor.core.scheduler.Scheduler; import reactor.util.function.Tuple2; import java.time.Instant; @@ -135,10 +132,7 @@ public class NewActionServiceCEImpl extends BaseService dtoDefaultResourcesService; public NewActionServiceCEImpl( - Scheduler scheduler, Validator validator, - MongoConverter mongoConverter, - ReactiveMongoTemplate reactiveMongoTemplate, NewActionRepository repository, AnalyticsService analyticsService, DatasourceService datasourceService, @@ -161,7 +155,7 @@ public class NewActionServiceCEImpl extends BaseService defaultResourcesService, DefaultResourcesService dtoDefaultResourcesService) { - super(scheduler, validator, mongoConverter, reactiveMongoTemplate, repository, analyticsService); + super(validator, repository, analyticsService); this.repository = repository; this.datasourceService = datasourceService; this.pluginService = pluginService; diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/newactions/base/NewActionServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/newactions/base/NewActionServiceImpl.java index feb1afa0c4..337773a644 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/newactions/base/NewActionServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/newactions/base/NewActionServiceImpl.java @@ -24,20 +24,14 @@ import com.appsmith.server.validations.EntityValidationService; import io.micrometer.observation.ObservationRegistry; import jakarta.validation.Validator; import lombok.extern.slf4j.Slf4j; -import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.stereotype.Service; -import reactor.core.scheduler.Scheduler; @Service @Slf4j public class NewActionServiceImpl extends NewActionServiceCEImpl implements NewActionService { public NewActionServiceImpl( - Scheduler scheduler, Validator validator, - MongoConverter mongoConverter, - ReactiveMongoTemplate reactiveMongoTemplate, NewActionRepository repository, AnalyticsService analyticsService, DatasourceService datasourceService, @@ -60,10 +54,7 @@ public class NewActionServiceImpl extends NewActionServiceCEImpl implements NewA DefaultResourcesService defaultResourcesService, DefaultResourcesService dtoDefaultResourcesService) { super( - scheduler, validator, - mongoConverter, - reactiveMongoTemplate, repository, analyticsService, datasourceService, diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/newpages/base/NewPageServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/newpages/base/NewPageServiceCEImpl.java index 37ea59fb3b..8f129be770 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/newpages/base/NewPageServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/newpages/base/NewPageServiceCEImpl.java @@ -30,13 +30,10 @@ import net.minidev.json.parser.JSONParser; import net.minidev.json.parser.ParseException; import org.bson.types.ObjectId; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import reactor.core.scheduler.Scheduler; import java.util.ArrayList; import java.util.Collection; @@ -64,10 +61,7 @@ public class NewPageServiceCEImpl extends BaseService implements PluginServiceCE { @@ -93,10 +91,7 @@ public class PluginServiceCEImpl extends BaseService reactiveTemplate, ChannelTopic topic, ObjectMapper objectMapper) { - super(scheduler, validator, mongoConverter, reactiveMongoTemplate, repository, analyticsService); + super(validator, repository, analyticsService); this.workspaceService = workspaceService; this.pluginManager = pluginManager; this.reactiveTemplate = reactiveTemplate; @@ -136,20 +131,20 @@ public class PluginServiceCEImpl extends BaseService pluginIds = org.getPlugins().stream() .map(WorkspacePlugin::getPluginId) .collect(Collectors.toList()); - Query query = new Query(); - query.addCriteria(Criteria.where(FieldName.ID).in(pluginIds)); + final Bridge criteria = bridge().in(FieldName.ID, pluginIds); - if (params.getFirst(FieldName.TYPE) != null) { + final String typeString = params.getFirst(FieldName.TYPE); + if (typeString != null) { try { - PluginType pluginType = PluginType.valueOf(params.getFirst(FieldName.TYPE)); - query.addCriteria(Criteria.where(FieldName.TYPE).is(pluginType)); + PluginType.valueOf(typeString); // Check if the type is valid + criteria.equal(FieldName.TYPE, typeString); } catch (IllegalArgumentException e) { - log.error("No plugins for type : {}", params.getFirst(FieldName.TYPE)); + log.error("No plugins for type : {}", typeString); return Flux.empty(); } } - return mongoTemplate.find(query, Plugin.class); + return repository.queryBuilder().criteria(criteria).all(); }) .flatMap(plugin -> getTemplates(plugin).doOnSuccess(plugin::setTemplates).thenReturn(plugin)); diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/plugins/base/PluginServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/plugins/base/PluginServiceImpl.java index 619b67cc60..ba4cf06c1c 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/plugins/base/PluginServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/plugins/base/PluginServiceImpl.java @@ -7,22 +7,16 @@ import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.validation.Validator; import lombok.extern.slf4j.Slf4j; import org.pf4j.PluginManager; -import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.data.redis.core.ReactiveRedisTemplate; import org.springframework.data.redis.listener.ChannelTopic; import org.springframework.stereotype.Service; -import reactor.core.scheduler.Scheduler; @Slf4j @Service public class PluginServiceImpl extends PluginServiceCEImpl implements PluginService { public PluginServiceImpl( - Scheduler scheduler, Validator validator, - MongoConverter mongoConverter, - ReactiveMongoTemplate reactiveMongoTemplate, PluginRepository repository, AnalyticsService analyticsService, WorkspaceService workspaceService, @@ -32,10 +26,7 @@ public class PluginServiceImpl extends PluginServiceCEImpl implements PluginServ ObjectMapper objectMapper) { super( - scheduler, validator, - mongoConverter, - reactiveMongoTemplate, repository, analyticsService, workspaceService, diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/BaseAppsmithRepositoryCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/BaseAppsmithRepositoryCEImpl.java index 167b09c9e3..3eda9b1f9c 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/BaseAppsmithRepositoryCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/BaseAppsmithRepositoryCEImpl.java @@ -296,6 +296,23 @@ public abstract class BaseAppsmithRepositoryCEImpl { this.genericDomain))); } + public Mono updateExecute(@NonNull QueryAllParams params, @NonNull T resource) { + final Update update = new Update(); + + // In case the update is not used to update the policies, then set the policies to null to ensure that the + // existing policies are not overwritten. + if (resource.getPolicies().isEmpty()) { + resource.setPolicies(null); + } + + final Map updateMap = getDbObject(resource).toMap(); + for (Map.Entry entry : updateMap.entrySet()) { + update.set(entry.getKey(), entry.getValue()); + } + + return updateExecute(params, update); + } + public Mono updateExecute(@NonNull QueryAllParams params, @NonNull UpdateDefinition update) { Objects.requireNonNull(params.getCriteria()); @@ -316,7 +333,7 @@ public abstract class BaseAppsmithRepositoryCEImpl { return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, "scope")); } })) - .map(updateResult -> Math.toIntExact(updateResult.getModifiedCount())); + .map(updateResult -> Math.toIntExact(updateResult.getMatchedCount())); } public Mono updateExecuteAndFind(@NonNull QueryAllParams params, @NonNull UpdateDefinition update) { diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/params/QueryAllParams.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/params/QueryAllParams.java index 355b9cbf4d..81d55e85e6 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/params/QueryAllParams.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/params/QueryAllParams.java @@ -66,6 +66,11 @@ public class QueryAllParams { return repo.updateExecute(this, update); } + public Mono updateFirst(@NonNull T resource) { + scope = Scope.FIRST; + return repo.updateExecute(this, resource); + } + public Mono updateFirst(@NonNull UpdateDefinition update) { scope = Scope.FIRST; return repo.updateExecute(this, update); diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/BaseService.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/BaseService.java index ef05d73eb3..c28cf640bc 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/BaseService.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/BaseService.java @@ -8,21 +8,15 @@ import com.appsmith.server.exceptions.AppsmithError; import com.appsmith.server.exceptions.AppsmithException; import com.appsmith.server.repositories.AppsmithRepository; import com.appsmith.server.repositories.BaseRepository; -import com.mongodb.BasicDBObject; -import com.mongodb.DBObject; import jakarta.validation.Validator; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; -import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.data.mongodb.core.query.Criteria; -import org.springframework.data.mongodb.core.query.Query; -import org.springframework.data.mongodb.core.query.Update; import org.springframework.util.MultiValueMap; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import reactor.core.scheduler.Scheduler; import java.io.Serializable; import java.time.Instant; @@ -34,42 +28,23 @@ import java.util.function.Function; import java.util.regex.Pattern; import java.util.stream.Collectors; +import static com.appsmith.server.helpers.ce.bridge.Bridge.bridge; import static java.util.stream.Collectors.toSet; @Slf4j +@RequiredArgsConstructor public abstract class BaseService< R extends BaseRepository & AppsmithRepository, T extends BaseDomain, ID extends Serializable> implements CrudService { - final Scheduler scheduler; - - protected final MongoConverter mongoConverter; - - protected final ReactiveMongoTemplate mongoTemplate; + protected final Validator validator; protected final R repository; - protected final Validator validator; - protected final AnalyticsService analyticsService; private static final String ENTITY_FIELDS = "entity_fields"; - public BaseService( - Scheduler scheduler, - Validator validator, - MongoConverter mongoConverter, - ReactiveMongoTemplate reactiveMongoTemplate, - R repository, - AnalyticsService analyticsService) { - this.scheduler = scheduler; - this.validator = validator; - this.mongoConverter = mongoConverter; - this.mongoTemplate = reactiveMongoTemplate; - this.repository = repository; - this.analyticsService = analyticsService; - } - @Override public Mono update(ID id, T resource) { return update(id, resource, "id"); @@ -80,24 +55,14 @@ public abstract class BaseService< return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.ID)); } - Query query = new Query(Criteria.where(key).is(id)); - - // In case the update is not used to update the policies, then set the policies to null to ensure that the - // existing policies are not overwritten. - if (resource.getPolicies().isEmpty()) { - resource.setPolicies(null); - } - resource.setUpdatedAt(Instant.now()); - DBObject update = getDbObject(resource); - - Update updateObj = new Update(); - Map updateMap = update.toMap(); - updateMap.entrySet().stream().forEach(entry -> updateObj.set(entry.getKey(), entry.getValue())); - - return mongoTemplate - .updateFirst(query, updateObj, resource.getClass()) + // TODO(Shri): update happens with `key=id` and find happens with `id=id` criteria. This is incorrect, but is + // too fragile to touch right now. Need to dig in slow and deep to fix this. + return repository + .queryBuilder() + .criteria(bridge().equal(key, (String) id)) + .updateFirst(resource) .flatMap(obj -> repository.findById(id)) .flatMap(savedResource -> analyticsService.sendUpdateEvent(savedResource, getAnalyticsProperties(savedResource))); @@ -150,12 +115,6 @@ public abstract class BaseService< analyticsService.sendCreateEvent(savedResource, getAnalyticsProperties(savedResource))); } - protected DBObject getDbObject(Object o) { - BasicDBObject basicDBObject = new BasicDBObject(); - mongoConverter.write(o, basicDBObject); - return basicDBObject; - } - @Override public Mono archiveById(ID id) { return Mono.error(new AppsmithException(AppsmithError.UNSUPPORTED_OPERATION)); diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/CollectionServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/CollectionServiceImpl.java index f690ce69ab..d9377daed0 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/CollectionServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/CollectionServiceImpl.java @@ -4,23 +4,14 @@ import com.appsmith.server.repositories.CollectionRepository; import com.appsmith.server.services.ce.CollectionServiceCEImpl; import jakarta.validation.Validator; import lombok.extern.slf4j.Slf4j; -import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.stereotype.Service; -import reactor.core.scheduler.Scheduler; @Slf4j @Service public class CollectionServiceImpl extends CollectionServiceCEImpl implements CollectionService { public CollectionServiceImpl( - Scheduler scheduler, - Validator validator, - MongoConverter mongoConverter, - ReactiveMongoTemplate reactiveMongoTemplate, - CollectionRepository repository, - AnalyticsService analyticsService) { - - super(scheduler, validator, mongoConverter, reactiveMongoTemplate, repository, analyticsService); + Validator validator, CollectionRepository repository, AnalyticsService analyticsService) { + super(validator, repository, analyticsService); } } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PermissionGroupServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PermissionGroupServiceImpl.java index 76a4083f31..56d0694f5a 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PermissionGroupServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PermissionGroupServiceImpl.java @@ -7,20 +7,14 @@ import com.appsmith.server.services.ce_compatible.PermissionGroupServiceCECompat import com.appsmith.server.solutions.PermissionGroupPermission; import com.appsmith.server.solutions.PolicySolution; import jakarta.validation.Validator; -import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.stereotype.Service; -import reactor.core.scheduler.Scheduler; @Service public class PermissionGroupServiceImpl extends PermissionGroupServiceCECompatibleImpl implements PermissionGroupService { public PermissionGroupServiceImpl( - Scheduler scheduler, Validator validator, - MongoConverter mongoConverter, - ReactiveMongoTemplate reactiveMongoTemplate, PermissionGroupRepository repository, AnalyticsService analyticsService, SessionUserService sessionUserService, @@ -31,10 +25,7 @@ public class PermissionGroupServiceImpl extends PermissionGroupServiceCECompatib PermissionGroupPermission permissionGroupPermission) { super( - scheduler, validator, - mongoConverter, - reactiveMongoTemplate, repository, analyticsService, sessionUserService, diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/TenantServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/TenantServiceImpl.java index 1251caaf69..ba038be16b 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/TenantServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/TenantServiceImpl.java @@ -6,8 +6,6 @@ import com.appsmith.server.services.ce.TenantServiceCEImpl; import com.appsmith.server.solutions.EnvManager; import jakarta.validation.Validator; import org.springframework.context.annotation.Lazy; -import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.stereotype.Service; import reactor.core.scheduler.Scheduler; @@ -17,22 +15,11 @@ public class TenantServiceImpl extends TenantServiceCEImpl implements TenantServ public TenantServiceImpl( Scheduler scheduler, Validator validator, - MongoConverter mongoConverter, - ReactiveMongoTemplate reactiveMongoTemplate, TenantRepository repository, AnalyticsService analyticsService, ConfigService configService, @Lazy EnvManager envManager, FeatureFlagMigrationHelper featureFlagMigrationHelper) { - super( - scheduler, - validator, - mongoConverter, - reactiveMongoTemplate, - repository, - analyticsService, - configService, - envManager, - featureFlagMigrationHelper); + super(validator, repository, analyticsService, configService, envManager, featureFlagMigrationHelper); } } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserDataServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserDataServiceImpl.java index 7db1d8be84..efcf483b5a 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserDataServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserDataServiceImpl.java @@ -7,19 +7,13 @@ import com.appsmith.server.services.ce.UserDataServiceCEImpl; import com.appsmith.server.solutions.ReleaseNotesService; import com.appsmith.server.solutions.UserChangedHandler; import jakarta.validation.Validator; -import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.stereotype.Service; -import reactor.core.scheduler.Scheduler; @Service public class UserDataServiceImpl extends UserDataServiceCEImpl implements UserDataService { public UserDataServiceImpl( - Scheduler scheduler, Validator validator, - MongoConverter mongoConverter, - ReactiveMongoTemplate reactiveMongoTemplate, UserDataRepository repository, AnalyticsService analyticsService, UserRepository userRepository, @@ -32,10 +26,7 @@ public class UserDataServiceImpl extends UserDataServiceCEImpl implements UserDa TenantService tenantService) { super( - scheduler, validator, - mongoConverter, - reactiveMongoTemplate, repository, analyticsService, userRepository, diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserServiceImpl.java index ffd2131688..9c734817cb 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserServiceImpl.java @@ -16,21 +16,15 @@ import com.appsmith.server.solutions.PolicySolution; import com.appsmith.server.solutions.UserChangedHandler; import jakarta.validation.Validator; import lombok.extern.slf4j.Slf4j; -import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; -import reactor.core.scheduler.Scheduler; @Slf4j @Service public class UserServiceImpl extends UserServiceCECompatibleImpl implements UserService { public UserServiceImpl( - Scheduler scheduler, Validator validator, - MongoConverter mongoConverter, - ReactiveMongoTemplate reactiveMongoTemplate, UserRepository repository, WorkspaceService workspaceService, AnalyticsService analyticsService, @@ -54,10 +48,7 @@ public class UserServiceImpl extends UserServiceCECompatibleImpl implements User PACConfigurationService pacConfigurationService, UserServiceHelper userServiceHelper) { super( - scheduler, validator, - mongoConverter, - reactiveMongoTemplate, repository, workspaceService, analyticsService, diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/WorkspaceServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/WorkspaceServiceImpl.java index b9c3ebc09b..d4621e431a 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/WorkspaceServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/WorkspaceServiceImpl.java @@ -12,20 +12,14 @@ import com.appsmith.server.solutions.WorkspacePermission; import jakarta.validation.Validator; import lombok.extern.slf4j.Slf4j; import org.modelmapper.ModelMapper; -import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.stereotype.Service; -import reactor.core.scheduler.Scheduler; @Slf4j @Service public class WorkspaceServiceImpl extends WorkspaceServiceCEImpl implements WorkspaceService { public WorkspaceServiceImpl( - Scheduler scheduler, Validator validator, - MongoConverter mongoConverter, - ReactiveMongoTemplate reactiveMongoTemplate, WorkspaceRepository repository, AnalyticsService analyticsService, PluginRepository pluginRepository, @@ -41,10 +35,7 @@ public class WorkspaceServiceImpl extends WorkspaceServiceCEImpl implements Work WorkspaceServiceHelper workspaceServiceHelper) { super( - scheduler, validator, - mongoConverter, - reactiveMongoTemplate, repository, analyticsService, pluginRepository, diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/CollectionServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/CollectionServiceCEImpl.java index fe56d49015..07b879a49b 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/CollectionServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/CollectionServiceCEImpl.java @@ -11,10 +11,7 @@ import com.appsmith.server.services.AnalyticsService; import com.appsmith.server.services.BaseService; import jakarta.validation.Validator; import lombok.extern.slf4j.Slf4j; -import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import reactor.core.publisher.Mono; -import reactor.core.scheduler.Scheduler; import java.util.ArrayList; import java.util.List; @@ -25,13 +22,8 @@ public class CollectionServiceCEImpl extends BaseService implements UserDataServiceCE { @@ -73,10 +67,7 @@ public class UserDataServiceCEImpl extends BaseService updateMap = update.toMap(); - updateMap.entrySet().stream().forEach(entry -> updateObj.set(entry.getKey(), entry.getValue())); - - return mongoTemplate - .updateFirst(query, updateObj, resource.getClass()) - .flatMap(updateResult -> - updateResult.getMatchedCount() == 0 ? Mono.empty() : repository.findByUserId(userId)) + return repository + .queryBuilder() + .criteria(bridge().equal(UserData.Fields.userId, userId)) + .updateFirst(resource) + .flatMap(count -> count == 0 ? Mono.empty() : repository.findByUserId(userId)) .flatMap(analyticsService::sendUpdateEvent); } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/UserServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/UserServiceCEImpl.java index cd32a7d19c..09da7fff62 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/UserServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/UserServiceCEImpl.java @@ -44,8 +44,6 @@ import org.apache.http.NameValuePair; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.message.BasicNameValuePair; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.ReactiveSecurityContextHolder; @@ -62,7 +60,6 @@ import org.springframework.web.server.WebFilterChain; import org.springframework.web.server.WebSession; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import reactor.core.scheduler.Scheduler; import reactor.core.scheduler.Schedulers; import java.net.URI; @@ -123,10 +120,7 @@ public class UserServiceCEImpl extends BaseService @Autowired public UserServiceCEImpl( - Scheduler scheduler, Validator validator, - MongoConverter mongoConverter, - ReactiveMongoTemplate reactiveMongoTemplate, UserRepository repository, WorkspaceService workspaceService, AnalyticsService analyticsService, @@ -145,7 +139,7 @@ public class UserServiceCEImpl extends BaseService PACConfigurationService pacConfigurationService, UserServiceHelper userServiceHelper) { - super(scheduler, validator, mongoConverter, reactiveMongoTemplate, repository, analyticsService); + super(validator, repository, analyticsService); this.workspaceService = workspaceService; this.sessionUserService = sessionUserService; this.passwordResetTokenRepository = passwordResetTokenRepository; diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/WorkspaceServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/WorkspaceServiceCEImpl.java index 2129fd0d90..c3c90764ee 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/WorkspaceServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/WorkspaceServiceCEImpl.java @@ -35,15 +35,12 @@ import lombok.extern.slf4j.Slf4j; import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; -import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.http.codec.multipart.Part; import org.springframework.stereotype.Service; import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import reactor.core.scheduler.Scheduler; import java.util.Arrays; import java.util.List; @@ -88,10 +85,7 @@ public class WorkspaceServiceCEImpl extends BaseServiceany()); + Mockito.when(actionCollectionRepository.queryBuilder()).thenReturn(params); Mockito.when(actionCollectionRepository.findById(Mockito.anyString(), Mockito.any())) .thenReturn(Mono.just(actionCollection)); @@ -808,8 +806,9 @@ public class ActionCollectionServiceImplTest { Mockito.when(actionCollectionRepository.findById(Mockito.anyString())).thenReturn(Mono.just(actionCollection)); - Mockito.when(reactiveMongoTemplate.updateFirst(Mockito.any(), Mockito.any(), Mockito.any(Class.class))) - .thenReturn(Mono.just(UpdateResult.acknowledged(1, 1L, new BsonObjectId()))); + final QueryAllParams params = Mockito.spy(new QueryAllParams(null)); + doReturn(Mono.just(1)).when(params).updateFirst(Mockito.any()); + Mockito.when(actionCollectionRepository.queryBuilder()).thenReturn(params); PageDTO oldPageDTO = new PageDTO(); oldPageDTO.setId("oldPageId"); diff --git a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/ce/NewActionServiceUnitTest.java b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/ce/NewActionServiceUnitTest.java index 1d85f7f86d..3bdfadbc66 100644 --- a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/ce/NewActionServiceUnitTest.java +++ b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/ce/NewActionServiceUnitTest.java @@ -127,10 +127,7 @@ public class NewActionServiceUnitTest { @BeforeEach public void setup() { newActionService = new NewActionServiceCEImpl( - scheduler, validator, - mongoConverter, - reactiveMongoTemplate, newActionRepository, analyticsService, datasourceService, diff --git a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/ce/PluginServiceCEImplTest.java b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/ce/PluginServiceCEImplTest.java index 4fdb382981..493e392373 100644 --- a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/ce/PluginServiceCEImplTest.java +++ b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/ce/PluginServiceCEImplTest.java @@ -73,10 +73,7 @@ public class PluginServiceCEImplTest { public void setUp() { objectMapper = new ObjectMapper(); pluginService = new PluginServiceCEImpl( - scheduler, validator, - mongoConverter, - reactiveMongoTemplate, repository, analyticsService, workspaceService,