Skip to content

Commit 176f5da

Browse files
authored
Change SpEL expression parsing for collection names (#102)
1 parent 3429a50 commit 176f5da

File tree

5 files changed

+102
-6
lines changed

5 files changed

+102
-6
lines changed

ChangeLog.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
1919
- added support for non-String `@Id`s (issue #79)
2020
- added convenience method `AbstractArangoConfiguration#customConverters()` to add custom converters
2121

22-
### Changes
22+
### Changed
2323

2424
- save `@Id` fields as `_key` instead of `_id` (issue #78)
25+
- changed SpEL expression parsing for collection names
26+
27+
SpEL expressions in `@Document#value`/`@Edge#value` are now parsed whenever the domain entity is accessed. This allows Multi-tenancy on collection level.
2528

2629
### Fixed
2730

src/main/java/com/arangodb/springframework/core/mapping/DefaultArangoPersistentEntity.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public class DefaultArangoPersistentEntity<T> extends BasicPersistentEntity<T, A
7373
private static final SpelExpressionParser PARSER = new SpelExpressionParser();
7474

7575
private String collection;
76+
private final Expression expression;
7677
private final StandardEvaluationContext context;
7778

7879
private ArangoPersistentProperty arangoIdProperty;
@@ -108,10 +109,7 @@ public DefaultArangoPersistentEntity(final TypeInformation<T> information) {
108109
} else {
109110
collectionOptions = new CollectionCreateOptions().type(CollectionType.DOCUMENT);
110111
}
111-
final Expression expression = PARSER.parseExpression(collection, ParserContext.TEMPLATE_EXPRESSION);
112-
if (expression != null) {
113-
collection = expression.getValue(context, String.class);
114-
}
112+
expression = PARSER.parseExpression(collection, ParserContext.TEMPLATE_EXPRESSION);
115113
}
116114

117115
private static CollectionCreateOptions createCollectionOptions(final Document annotation) {
@@ -175,7 +173,7 @@ private static CollectionCreateOptions createCollectionOptions(final Edge annota
175173

176174
@Override
177175
public String getCollection() {
178-
return collection;
176+
return expression != null ? expression.getValue(context, String.class) : collection;
179177
}
180178

181179
@Override

src/test/java/com/arangodb/springframework/ArangoTestConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.ArrayList;
2424
import java.util.Collection;
2525

26+
import org.springframework.context.annotation.ComponentScan;
2627
import org.springframework.context.annotation.Configuration;
2728
import org.springframework.core.convert.converter.Converter;
2829

@@ -37,6 +38,7 @@
3738
* @author Christian Lechner
3839
*/
3940
@Configuration
41+
@ComponentScan("com.arangodb.springframework.component")
4042
@EnableArangoRepositories(basePackages = {
4143
"com.arangodb.springframework.repository" }, namedQueriesLocation = "classpath*:arango-named-queries-test.properties")
4244
public class ArangoTestConfiguration extends AbstractArangoConfiguration {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.arangodb.springframework.component;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class TenantProvider {
7+
8+
private final ThreadLocal<String> id;
9+
10+
public TenantProvider() {
11+
super();
12+
id = new ThreadLocal<>();
13+
}
14+
15+
public String getId() {
16+
return id.get();
17+
}
18+
19+
public void setId(final String id) {
20+
this.id.set(id);
21+
}
22+
23+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.springframework.core.mapping;
22+
23+
import static org.hamcrest.Matchers.is;
24+
import static org.junit.Assert.assertThat;
25+
26+
import org.junit.Test;
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
29+
import com.arangodb.springframework.AbstractArangoTest;
30+
import com.arangodb.springframework.ArangoTestConfiguration;
31+
import com.arangodb.springframework.annotation.Document;
32+
import com.arangodb.springframework.component.TenantProvider;
33+
34+
/**
35+
* @author Mark Vollmary
36+
*
37+
*/
38+
public class MultiTenancyMappingTest extends AbstractArangoTest {
39+
40+
@Document("#{tenantProvider.getId()}_collection")
41+
static class MultiTenancyTestEntity {
42+
43+
}
44+
45+
@Autowired
46+
TenantProvider tenantProvider;
47+
48+
@Test
49+
public void collectionLevel() {
50+
{
51+
tenantProvider.setId("tenant00");
52+
template.insert(new MultiTenancyTestEntity());
53+
assertThat(template.driver().db(ArangoTestConfiguration.DB).collection("tenant00_collection").exists(),
54+
is(true));
55+
}
56+
{
57+
tenantProvider.setId("tenant01");
58+
template.insert(new MultiTenancyTestEntity());
59+
assertThat(template.driver().db(ArangoTestConfiguration.DB).collection("tenant01_collection").exists(),
60+
is(true));
61+
}
62+
assertThat(
63+
template.driver().db(ArangoTestConfiguration.DB).collection("tenant00_collection").count().getCount(),
64+
is(1L));
65+
assertThat(
66+
template.driver().db(ArangoTestConfiguration.DB).collection("tenant01_collection").count().getCount(),
67+
is(1L));
68+
}
69+
70+
}

0 commit comments

Comments
 (0)