diff --git a/ChangeLog.md b/ChangeLog.md index 717051e63..2c20f1658 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -19,9 +19,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a - added support for non-String `@Id`s (issue #79) - added convenience method `AbstractArangoConfiguration#customConverters()` to add custom converters -### Changes +### Changed - save `@Id` fields as `_key` instead of `_id` (issue #78) +- changed SpEL expression parsing for collection names + + SpEL expressions in `@Document#value`/`@Edge#value` are now parsed whenever the domain entity is accessed. This allows Multi-tenancy on collection level. ### Fixed diff --git a/src/main/java/com/arangodb/springframework/core/mapping/DefaultArangoPersistentEntity.java b/src/main/java/com/arangodb/springframework/core/mapping/DefaultArangoPersistentEntity.java index f390572d4..223daf575 100644 --- a/src/main/java/com/arangodb/springframework/core/mapping/DefaultArangoPersistentEntity.java +++ b/src/main/java/com/arangodb/springframework/core/mapping/DefaultArangoPersistentEntity.java @@ -73,6 +73,7 @@ public class DefaultArangoPersistentEntity extends BasicPersistentEntity information) { } else { collectionOptions = new CollectionCreateOptions().type(CollectionType.DOCUMENT); } - final Expression expression = PARSER.parseExpression(collection, ParserContext.TEMPLATE_EXPRESSION); - if (expression != null) { - collection = expression.getValue(context, String.class); - } + expression = PARSER.parseExpression(collection, ParserContext.TEMPLATE_EXPRESSION); } private static CollectionCreateOptions createCollectionOptions(final Document annotation) { @@ -175,7 +173,7 @@ private static CollectionCreateOptions createCollectionOptions(final Edge annota @Override public String getCollection() { - return collection; + return expression != null ? expression.getValue(context, String.class) : collection; } @Override diff --git a/src/test/java/com/arangodb/springframework/ArangoTestConfiguration.java b/src/test/java/com/arangodb/springframework/ArangoTestConfiguration.java index acd7d8cf9..9d5492f51 100644 --- a/src/test/java/com/arangodb/springframework/ArangoTestConfiguration.java +++ b/src/test/java/com/arangodb/springframework/ArangoTestConfiguration.java @@ -23,6 +23,7 @@ import java.util.ArrayList; import java.util.Collection; +import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.converter.Converter; @@ -37,6 +38,7 @@ * @author Christian Lechner */ @Configuration +@ComponentScan("com.arangodb.springframework.component") @EnableArangoRepositories(basePackages = { "com.arangodb.springframework.repository" }, namedQueriesLocation = "classpath*:arango-named-queries-test.properties") public class ArangoTestConfiguration extends AbstractArangoConfiguration { diff --git a/src/test/java/com/arangodb/springframework/component/TenantProvider.java b/src/test/java/com/arangodb/springframework/component/TenantProvider.java new file mode 100644 index 000000000..f7fe9cc12 --- /dev/null +++ b/src/test/java/com/arangodb/springframework/component/TenantProvider.java @@ -0,0 +1,23 @@ +package com.arangodb.springframework.component; + +import org.springframework.stereotype.Component; + +@Component +public class TenantProvider { + + private final ThreadLocal id; + + public TenantProvider() { + super(); + id = new ThreadLocal<>(); + } + + public String getId() { + return id.get(); + } + + public void setId(final String id) { + this.id.set(id); + } + +} \ No newline at end of file diff --git a/src/test/java/com/arangodb/springframework/core/mapping/MultiTenancyMappingTest.java b/src/test/java/com/arangodb/springframework/core/mapping/MultiTenancyMappingTest.java new file mode 100644 index 000000000..0b75c0f04 --- /dev/null +++ b/src/test/java/com/arangodb/springframework/core/mapping/MultiTenancyMappingTest.java @@ -0,0 +1,70 @@ +/* + * DISCLAIMER + * + * Copyright 2018 ArangoDB GmbH, Cologne, Germany + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright holder is ArangoDB GmbH, Cologne, Germany + */ + +package com.arangodb.springframework.core.mapping; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import com.arangodb.springframework.AbstractArangoTest; +import com.arangodb.springframework.ArangoTestConfiguration; +import com.arangodb.springframework.annotation.Document; +import com.arangodb.springframework.component.TenantProvider; + +/** + * @author Mark Vollmary + * + */ +public class MultiTenancyMappingTest extends AbstractArangoTest { + + @Document("#{tenantProvider.getId()}_collection") + static class MultiTenancyTestEntity { + + } + + @Autowired + TenantProvider tenantProvider; + + @Test + public void collectionLevel() { + { + tenantProvider.setId("tenant00"); + template.insert(new MultiTenancyTestEntity()); + assertThat(template.driver().db(ArangoTestConfiguration.DB).collection("tenant00_collection").exists(), + is(true)); + } + { + tenantProvider.setId("tenant01"); + template.insert(new MultiTenancyTestEntity()); + assertThat(template.driver().db(ArangoTestConfiguration.DB).collection("tenant01_collection").exists(), + is(true)); + } + assertThat( + template.driver().db(ArangoTestConfiguration.DB).collection("tenant00_collection").count().getCount(), + is(1L)); + assertThat( + template.driver().db(ArangoTestConfiguration.DB).collection("tenant01_collection").count().getCount(), + is(1L)); + } + +}