Skip to content

Commit 063d8ed

Browse files
Akukaakonczak
authored andcommitted
DATAES-163 - Add support for UUID as primary key
Add support for UUID as primary key Add tests for support of UUID as primary key
1 parent e5b080a commit 063d8ed

File tree

7 files changed

+1041
-211
lines changed

7 files changed

+1041
-211
lines changed
Lines changed: 119 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,119 @@
1-
/*
2-
* Copyright 2013 the original author or authors.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
package org.springframework.data.elasticsearch.repository.support;
17-
18-
import static org.springframework.data.querydsl.QueryDslUtils.*;
19-
20-
import java.io.Serializable;
21-
import java.lang.reflect.Method;
22-
23-
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
24-
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
25-
import org.springframework.data.elasticsearch.repository.query.ElasticsearchPartQuery;
26-
import org.springframework.data.elasticsearch.repository.query.ElasticsearchQueryMethod;
27-
import org.springframework.data.elasticsearch.repository.query.ElasticsearchStringQuery;
28-
import org.springframework.data.projection.ProjectionFactory;
29-
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
30-
import org.springframework.data.repository.core.NamedQueries;
31-
import org.springframework.data.repository.core.RepositoryInformation;
32-
import org.springframework.data.repository.core.RepositoryMetadata;
33-
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
34-
import org.springframework.data.repository.query.QueryLookupStrategy;
35-
import org.springframework.data.repository.query.RepositoryQuery;
36-
import org.springframework.util.Assert;
37-
38-
/**
39-
* Factory to create {@link ElasticsearchRepository}
40-
*
41-
* @author Rizwan Idrees
42-
* @author Mohsin Husen
43-
* @author Ryan Henszey
44-
*/
45-
public class ElasticsearchRepositoryFactory extends RepositoryFactorySupport {
46-
47-
private final ElasticsearchOperations elasticsearchOperations;
48-
private final ElasticsearchEntityInformationCreator entityInformationCreator;
49-
50-
public ElasticsearchRepositoryFactory(ElasticsearchOperations elasticsearchOperations) {
51-
Assert.notNull(elasticsearchOperations);
52-
this.elasticsearchOperations = elasticsearchOperations;
53-
this.entityInformationCreator = new ElasticsearchEntityInformationCreatorImpl(elasticsearchOperations
54-
.getElasticsearchConverter().getMappingContext());
55-
}
56-
57-
@Override
58-
public <T, ID extends Serializable> ElasticsearchEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
59-
return entityInformationCreator.getEntityInformation(domainClass);
60-
}
61-
62-
@Override
63-
@SuppressWarnings({"rawtypes", "unchecked"})
64-
protected Object getTargetRepository(RepositoryInformation metadata) {
65-
return getTargetRepositoryViaReflection(metadata,getEntityInformation(metadata.getDomainType()), elasticsearchOperations);
66-
}
67-
68-
@Override
69-
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
70-
if (isQueryDslRepository(metadata.getRepositoryInterface())) {
71-
throw new IllegalArgumentException("QueryDsl Support has not been implemented yet.");
72-
}
73-
if (Integer.class.isAssignableFrom(metadata.getIdType())
74-
|| Long.class.isAssignableFrom(metadata.getIdType())
75-
|| Double.class.isAssignableFrom(metadata.getIdType())) {
76-
return NumberKeyedRepository.class;
77-
} else if (metadata.getIdType() == String.class) {
78-
return SimpleElasticsearchRepository.class;
79-
} else {
80-
throw new IllegalArgumentException("Unsuppored ID type " + metadata.getIdType());
81-
}
82-
}
83-
84-
private static boolean isQueryDslRepository(Class<?> repositoryInterface) {
85-
return QUERY_DSL_PRESENT && QueryDslPredicateExecutor.class.isAssignableFrom(repositoryInterface);
86-
}
87-
88-
@Override
89-
protected QueryLookupStrategy getQueryLookupStrategy(QueryLookupStrategy.Key key) {
90-
return new ElasticsearchQueryLookupStrategy();
91-
}
92-
93-
private class ElasticsearchQueryLookupStrategy implements QueryLookupStrategy {
94-
95-
/*
96-
* (non-Javadoc)
97-
* @see org.springframework.data.repository.query.QueryLookupStrategy#resolveQuery(java.lang.reflect.Method, org.springframework.data.repository.core.RepositoryMetadata, org.springframework.data.projection.ProjectionFactory, org.springframework.data.repository.core.NamedQueries)
98-
*/
99-
@Override
100-
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
101-
NamedQueries namedQueries) {
102-
103-
ElasticsearchQueryMethod queryMethod = new ElasticsearchQueryMethod(method, metadata, factory);
104-
String namedQueryName = queryMethod.getNamedQueryName();
105-
106-
if (namedQueries.hasQuery(namedQueryName)) {
107-
String namedQuery = namedQueries.getQuery(namedQueryName);
108-
return new ElasticsearchStringQuery(queryMethod, elasticsearchOperations, namedQuery);
109-
} else if (queryMethod.hasAnnotatedQuery()) {
110-
return new ElasticsearchStringQuery(queryMethod, elasticsearchOperations, queryMethod.getAnnotatedQuery());
111-
}
112-
return new ElasticsearchPartQuery(queryMethod, elasticsearchOperations);
113-
}
114-
}
115-
}
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.repository.support;
17+
18+
import static org.springframework.data.querydsl.QueryDslUtils.*;
19+
20+
import java.io.Serializable;
21+
import java.lang.reflect.Method;
22+
import java.util.UUID;
23+
24+
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
25+
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
26+
import org.springframework.data.elasticsearch.repository.query.ElasticsearchPartQuery;
27+
import org.springframework.data.elasticsearch.repository.query.ElasticsearchQueryMethod;
28+
import org.springframework.data.elasticsearch.repository.query.ElasticsearchStringQuery;
29+
import org.springframework.data.projection.ProjectionFactory;
30+
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
31+
import org.springframework.data.repository.core.NamedQueries;
32+
import org.springframework.data.repository.core.RepositoryInformation;
33+
import org.springframework.data.repository.core.RepositoryMetadata;
34+
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
35+
import org.springframework.data.repository.query.QueryLookupStrategy;
36+
import org.springframework.data.repository.query.RepositoryQuery;
37+
import org.springframework.util.Assert;
38+
39+
/**
40+
* Factory to create {@link ElasticsearchRepository}
41+
*
42+
* @author Rizwan Idrees
43+
* @author Mohsin Husen
44+
* @author Ryan Henszey
45+
* @author Gad Akuka
46+
*/
47+
public class ElasticsearchRepositoryFactory extends RepositoryFactorySupport {
48+
49+
private final ElasticsearchOperations elasticsearchOperations;
50+
private final ElasticsearchEntityInformationCreator entityInformationCreator;
51+
52+
public ElasticsearchRepositoryFactory(ElasticsearchOperations elasticsearchOperations) {
53+
Assert.notNull(elasticsearchOperations);
54+
this.elasticsearchOperations = elasticsearchOperations;
55+
this.entityInformationCreator = new ElasticsearchEntityInformationCreatorImpl(elasticsearchOperations
56+
.getElasticsearchConverter().getMappingContext());
57+
}
58+
59+
@Override
60+
public <T, ID extends Serializable> ElasticsearchEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
61+
return entityInformationCreator.getEntityInformation(domainClass);
62+
}
63+
64+
@Override
65+
@SuppressWarnings({"rawtypes", "unchecked"})
66+
protected Object getTargetRepository(RepositoryInformation metadata) {
67+
return getTargetRepositoryViaReflection(metadata,getEntityInformation(metadata.getDomainType()), elasticsearchOperations);
68+
}
69+
70+
@Override
71+
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
72+
if (isQueryDslRepository(metadata.getRepositoryInterface())) {
73+
throw new IllegalArgumentException("QueryDsl Support has not been implemented yet.");
74+
}
75+
if (Integer.class.isAssignableFrom(metadata.getIdType())
76+
|| Long.class.isAssignableFrom(metadata.getIdType())
77+
|| Double.class.isAssignableFrom(metadata.getIdType())) {
78+
return NumberKeyedRepository.class;
79+
} else if (metadata.getIdType() == String.class) {
80+
return SimpleElasticsearchRepository.class;
81+
} else if (metadata.getIdType() == UUID.class) {
82+
return UUIDElasticsearchRepository.class;
83+
} else {
84+
throw new IllegalArgumentException("Unsupported ID type " + metadata.getIdType());
85+
}
86+
}
87+
88+
private static boolean isQueryDslRepository(Class<?> repositoryInterface) {
89+
return QUERY_DSL_PRESENT && QueryDslPredicateExecutor.class.isAssignableFrom(repositoryInterface);
90+
}
91+
92+
@Override
93+
protected QueryLookupStrategy getQueryLookupStrategy(QueryLookupStrategy.Key key) {
94+
return new ElasticsearchQueryLookupStrategy();
95+
}
96+
97+
private class ElasticsearchQueryLookupStrategy implements QueryLookupStrategy {
98+
99+
/*
100+
* (non-Javadoc)
101+
* @see org.springframework.data.repository.query.QueryLookupStrategy#resolveQuery(java.lang.reflect.Method, org.springframework.data.repository.core.RepositoryMetadata, org.springframework.data.projection.ProjectionFactory, org.springframework.data.repository.core.NamedQueries)
102+
*/
103+
@Override
104+
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
105+
NamedQueries namedQueries) {
106+
107+
ElasticsearchQueryMethod queryMethod = new ElasticsearchQueryMethod(method, metadata, factory);
108+
String namedQueryName = queryMethod.getNamedQueryName();
109+
110+
if (namedQueries.hasQuery(namedQueryName)) {
111+
String namedQuery = namedQueries.getQuery(namedQueryName);
112+
return new ElasticsearchStringQuery(queryMethod, elasticsearchOperations, namedQuery);
113+
} else if (queryMethod.hasAnnotatedQuery()) {
114+
return new ElasticsearchStringQuery(queryMethod, elasticsearchOperations, queryMethod.getAnnotatedQuery());
115+
}
116+
return new ElasticsearchPartQuery(queryMethod, elasticsearchOperations);
117+
}
118+
}
119+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.repository.support;
17+
18+
import java.util.UUID;
19+
20+
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
21+
22+
/**
23+
* Elasticsearch specific repository implementation. Likely to be used as target within
24+
* {@link ElasticsearchRepositoryFactory}
25+
*
26+
* @author Gad Akuka
27+
*/
28+
public class UUIDElasticsearchRepository<T> extends AbstractElasticsearchRepository<T, UUID> {
29+
30+
public UUIDElasticsearchRepository() {
31+
super();
32+
}
33+
34+
public UUIDElasticsearchRepository(ElasticsearchEntityInformation<T, UUID> metadata,
35+
ElasticsearchOperations elasticsearchOperations) {
36+
super(metadata, elasticsearchOperations);
37+
}
38+
39+
public UUIDElasticsearchRepository(ElasticsearchOperations elasticsearchOperations) {
40+
super(elasticsearchOperations);
41+
}
42+
43+
@Override
44+
protected String stringIdRepresentation(UUID id) {
45+
return (id != null) ? id.toString() : null;
46+
}
47+
}

0 commit comments

Comments
 (0)