Skip to content

Commit

Permalink
Change SpEL expression parsing for collection names (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpv1989 committed Jul 24, 2018
1 parent 3429a50 commit 176f5da
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 6 deletions.
5 changes: 4 additions & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class DefaultArangoPersistentEntity<T> extends BasicPersistentEntity<T, A
private static final SpelExpressionParser PARSER = new SpelExpressionParser();

private String collection;
private final Expression expression;
private final StandardEvaluationContext context;

private ArangoPersistentProperty arangoIdProperty;
Expand Down Expand Up @@ -108,10 +109,7 @@ public DefaultArangoPersistentEntity(final TypeInformation<T> 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) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.arangodb.springframework.component;

import org.springframework.stereotype.Component;

@Component
public class TenantProvider {

private final ThreadLocal<String> id;

public TenantProvider() {
super();
id = new ThreadLocal<>();
}

public String getId() {
return id.get();
}

public void setId(final String id) {
this.id.set(id);
}

}
Original file line number Diff line number Diff line change
@@ -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));
}

}

0 comments on commit 176f5da

Please sign in to comment.