Skip to content

Commit

Permalink
Merge pull request #49 from jupiter-tools/fix/value-in-the-document-a…
Browse files Browse the repository at this point in the history
…nnotation

value in the document annotation
  • Loading branch information
antkorwin authored Apr 21, 2019
2 parents 14f311b + 5ad7c84 commit 2b6b53f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.antkorwin</groupId>
<artifactId>spring-test-mongo</artifactId>
<version>0.12</version>
<version>0.13-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-test-mongo</name>
Expand All @@ -25,7 +25,7 @@
</developers>

<properties>
<spring-boot.version>[2.0.3.RELEASE, 2.1.3.RELEASE]</spring-boot.version>
<spring-boot.version>[2.0.3.RELEASE, 2.1.4.RELEASE]</spring-boot.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jupiter.tools.spring.test.mongo.internal.exportdata.scanner;

import org.reflections.Reflections;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.HashMap;
Expand All @@ -9,10 +10,10 @@

/**
* Created on 03.01.2019.
*
* <p>
* Scans selected package on the MongoDB Documents and returns
* a map with all founded collections with their class-types.
*
* <p>
* Finds all classes which annotated by the Document annotation
* and takes collection names from this annotation or takes a class
* name if the collection name doesn't set by the Document annotation.
Expand All @@ -38,24 +39,23 @@ public Map<String, Class<?>> scan() {
for (Class<?> doc : documents) {

Document annotation = doc.getAnnotation(Document.class);
String value = (String) AnnotationUtils.getValue(annotation, "value");

if (notEmpty(annotation.value())) {
result.put(annotation.value(), doc);
continue;
if (empty(value)) {
value = annotation.collection();
}

if (notEmpty(annotation.collection())) {
result.put(annotation.collection(), doc);
continue;
if(empty(value)){
value = doc.getSimpleName().toLowerCase();
}

result.put(doc.getSimpleName().toLowerCase(), doc);
result.put(value, doc);
}

return result;
}

private boolean notEmpty(String value) {
return !("".equals(value));
private boolean empty(String value) {
return value == null || "".equals(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ private class AntkorwinTestDocFirst {

}

@Document(value = "antkorwin-test-doc-second")
// since spring boot 2.1.x
// you can use value instead of the attribute `collection`:
@Document("antkorwin-test-doc-second")
private class AntkorwinTestDocSecond {

}
Expand Down

0 comments on commit 2b6b53f

Please sign in to comment.