Skip to content

Commit f62d7d1

Browse files
authored
Merge pull request #65 from kaiso/feature/#59
Feature/#59
2 parents a5013f4 + 88e7728 commit f62d7d1

13 files changed

Lines changed: 99 additions & 54 deletions

File tree

docs/src/Gemfile.lock

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,55 @@ GEM
44
addressable (2.7.0)
55
public_suffix (>= 2.0.2, < 5.0)
66
colorator (1.1.0)
7-
concurrent-ruby (1.1.5)
8-
em-websocket (0.5.1)
7+
concurrent-ruby (1.1.7)
8+
em-websocket (0.5.2)
99
eventmachine (>= 0.12.9)
1010
http_parser.rb (~> 0.6.0)
1111
eventmachine (1.2.7)
12-
ffi (1.11.3)
12+
ffi (1.13.1)
1313
forwardable-extended (2.6.0)
1414
http_parser.rb (0.6.0)
1515
i18n (0.9.5)
1616
concurrent-ruby (~> 1.0)
17-
jekyll (3.8.6)
17+
jekyll (3.9.0)
1818
addressable (~> 2.4)
1919
colorator (~> 1.0)
2020
em-websocket (~> 0.5)
2121
i18n (~> 0.7)
2222
jekyll-sass-converter (~> 1.0)
2323
jekyll-watch (~> 2.0)
24-
kramdown (~> 1.14)
24+
kramdown (>= 1.17, < 3)
2525
liquid (~> 4.0)
2626
mercenary (~> 0.3.3)
2727
pathutil (~> 0.9)
2828
rouge (>= 1.7, < 4)
2929
safe_yaml (~> 1.0)
3030
jekyll-sass-converter (1.5.2)
3131
sass (~> 3.4)
32-
jekyll-seo-tag (2.6.1)
33-
jekyll (>= 3.3, < 5.0)
32+
jekyll-seo-tag (2.7.1)
33+
jekyll (>= 3.8, < 5.0)
3434
jekyll-sitemap (1.4.0)
3535
jekyll (>= 3.7, < 5.0)
3636
jekyll-theme-architect (0.1.1)
3737
jekyll (~> 3.5)
3838
jekyll-seo-tag (~> 2.0)
3939
jekyll-watch (2.2.1)
4040
listen (~> 3.0)
41-
kramdown (1.17.0)
41+
kramdown (2.3.0)
42+
rexml
4243
liquid (4.0.3)
4344
listen (3.2.1)
4445
rb-fsevent (~> 0.10, >= 0.10.3)
4546
rb-inotify (~> 0.9, >= 0.9.10)
4647
mercenary (0.3.6)
4748
pathutil (0.16.2)
4849
forwardable-extended (~> 2.6)
49-
public_suffix (4.0.1)
50-
rb-fsevent (0.10.3)
51-
rb-inotify (0.10.0)
50+
public_suffix (4.0.6)
51+
rb-fsevent (0.10.4)
52+
rb-inotify (0.10.1)
5253
ffi (~> 1.0)
53-
rouge (3.14.0)
54+
rexml (3.2.4)
55+
rouge (3.24.0)
5456
safe_yaml (1.0.5)
5557
sass (3.7.4)
5658
sass-listen (~> 4.0.0)

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>io.github.kaiso.relmongo</groupId>
77
<artifactId>relmongo</artifactId>
8-
<version>3.4.0-RC1</version>
8+
<version>3.4.1</version>
99
<packaging>jar</packaging>
1010

1111
<name>relmongo</name>

src/main/java/io/github/kaiso/relmongo/events/callback/PersistentPropertyConvertingCallback.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.github.kaiso.relmongo.annotation.OneToOne;
2222
import io.github.kaiso.relmongo.events.processor.MappedByProcessor;
2323
import io.github.kaiso.relmongo.exception.RelMongoConfigurationException;
24+
import io.github.kaiso.relmongo.exception.RelMongoInvalidApiUsageException;
2425
import io.github.kaiso.relmongo.exception.RelMongoProcessingException;
2526
import io.github.kaiso.relmongo.util.AnnotationsUtils;
2627
import io.github.kaiso.relmongo.util.ObjectIdReaderCallback;
@@ -31,8 +32,10 @@
3132
import org.springframework.util.ReflectionUtils.FieldCallback;
3233

3334
import java.lang.reflect.Field;
35+
import java.math.BigInteger;
3436
import java.util.Arrays;
3537
import java.util.Collection;
38+
import java.util.UUID;
3639

3740
/**
3841
*
@@ -92,11 +95,7 @@ private void checkIdentifier(Object obj) {
9295
+ "] must be annotated by @Id (org.springframework.data.annotation.Id)");
9396
}
9497
if (idField.get(obj) == null) {
95-
if (idField.getType().equals(ObjectId.class)) {
96-
ReflectionUtils.setField(idField, obj, ObjectId.get());
97-
} else if (idField.getType().equals(String.class)) {
98-
ReflectionUtils.setField(idField, obj, ObjectId.get().toString());
99-
}
98+
ReflectionUtils.setField(idField, obj, generateId(idField));
10099
}
101100

102101
} catch (IllegalArgumentException | IllegalAccessException e) {
@@ -105,4 +104,22 @@ private void checkIdentifier(Object obj) {
105104

106105
}
107106

107+
private Object generateId(Field idField) {
108+
Object id;
109+
if (idField.getType().equals(BigInteger.class)) {
110+
id = new BigInteger(source.toString(), 16);
111+
} else if (idField.getType().equals(ObjectId.class)) {
112+
id = ObjectId.get();
113+
} else if (idField.getType().equals(String.class)) {
114+
id = ObjectId.get().toString();
115+
} else if (idField.getType().equals(Long.class)) {
116+
id = UUID.randomUUID().getMostSignificantBits() & Long.MAX_VALUE;
117+
} else {
118+
throw new RelMongoInvalidApiUsageException(
119+
String.format("Cannot autogenerate id of type %s for entity of type %s!", idField.getType(),
120+
idField.getDeclaringClass().getName()));
121+
}
122+
return id;
123+
}
124+
108125
}

src/main/java/io/github/kaiso/relmongo/events/callback/PersistentPropertySavingCallback.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
import io.github.kaiso.relmongo.util.ReflectionsUtil;
2828
import io.github.kaiso.relmongo.util.RelMongoConstants;
2929

30-
import org.bson.types.ObjectId;
3130
import org.springframework.data.mongodb.core.MongoOperations;
32-
import org.springframework.data.mongodb.core.mapping.Document;
3331
import org.springframework.util.ReflectionUtils;
3432
import org.springframework.util.ReflectionUtils.FieldCallback;
3533
import org.springframework.util.StringUtils;
@@ -74,7 +72,7 @@ private void saveAssociation(Field field, CascadeType cascadeType, Boolean orpha
7472
String name = AnnotationsUtils.getJoinProperty(field);
7573
Object reference = null;
7674
reference = ((org.bson.Document) source).get(field.getName());
77-
String childCollectionName = getCollectionName(field);
75+
String childCollectionName = AnnotationsUtils.getCollectionName(field);
7876
if (reference instanceof BasicDBList) {
7977
BasicDBList list = new BasicDBList();
8078
list.addAll(((BasicDBList) reference).stream()
@@ -84,7 +82,7 @@ private void saveAssociation(Field field, CascadeType cascadeType, Boolean orpha
8482
((org.bson.Document) source).put(name, list);
8583
if (Boolean.TRUE.equals(orphanRemoval)) {
8684
removeOrphans(((org.bson.Document) source).get("_id"),
87-
list.parallelStream().map(o -> (ObjectId) ((org.bson.Document) o).get("_id")).collect(Collectors.toList()),
85+
list.parallelStream().map(o -> ((org.bson.Document) o).get("_id")).collect(Collectors.toList()),
8886
name,
8987
field);
9088
}
@@ -105,19 +103,12 @@ private org.bson.Document keepOnlyIdentifier(Object obj, String collection, Casc
105103
Object objectId = ((org.bson.Document) obj).get("_id");
106104
if (objectId == null && !Arrays.asList(CascadeType.PERSIST, CascadeType.ALL).contains(cascadeType)) {
107105
throw new RelMongoProcessingException(
108-
"ObjectId must not be null when persisting without cascade ALL or PERSIST ");
106+
"The entity Id must not be null when persisting without cascade ALL or PERSIST ");
109107
}
110108
return new org.bson.Document().append("_id", objectId).append(RelMongoConstants.RELMONGOTARGET_PROPERTY_NAME,
111109
collection);
112110
}
113111

114-
private String getCollectionName(Field field) {
115-
String collection = ReflectionsUtil.getGenericType(field).getAnnotation(Document.class).collection();
116-
if (StringUtils.isEmpty(collection)) {
117-
collection = ReflectionsUtil.getGenericType(field).getSimpleName().toLowerCase();
118-
}
119-
return collection;
120-
}
121112

122113
@SuppressWarnings({ "unchecked" })
123114
private void removeOrphans(Object parentId, List<Object> child, String propertyName, Field field) {
@@ -129,15 +120,15 @@ private void removeOrphans(Object parentId, List<Object> child, String propertyN
129120
org.bson.Document currentDocument = (org.bson.Document) result.get(0);
130121
if (currentDocument != null) {
131122
Object currentChild = currentDocument.get(propertyName);
132-
List<ObjectId> objectsToRemove = null;
123+
List<Object> objectsToRemove = null;
133124
if (currentChild instanceof ArrayList) {
134125
objectsToRemove = new ArrayList<>();
135126
ArrayList<org.bson.Document> childList = (ArrayList<org.bson.Document>) currentChild;
136127
childList.removeIf(o -> child.contains(o.get("_id")));
137-
objectsToRemove.addAll(childList.parallelStream().map(o -> (ObjectId) o.get("_id"))
128+
objectsToRemove.addAll(childList.parallelStream().map(o -> o.get("_id"))
138129
.collect(Collectors.toList()));
139130
} else if (currentChild instanceof org.bson.Document) {
140-
ObjectId currentChildId = (ObjectId) ((org.bson.Document) currentChild).get("_id");
131+
Object currentChildId = ((org.bson.Document) currentChild).get("_id");
141132
if (child.isEmpty() || !currentChildId.equals(child.get(0))) {
142133
objectsToRemove = new ArrayList<>();
143134
objectsToRemove.add(currentChildId);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.github.kaiso.relmongo.exception;
2+
3+
public class RelMongoInvalidApiUsageException extends RuntimeException {
4+
5+
public RelMongoInvalidApiUsageException(String string, Exception e) {
6+
super(string, e);
7+
}
8+
9+
public RelMongoInvalidApiUsageException(String string) {
10+
super(string);
11+
}
12+
13+
/**
14+
*
15+
*/
16+
private static final long serialVersionUID = 1L;
17+
18+
}

src/main/java/io/github/kaiso/relmongo/mongo/DocumentUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import io.github.kaiso.relmongo.util.RelMongoConstants;
2020

2121
import org.bson.Document;
22-
import org.bson.types.ObjectId;
2322

2423
import java.util.Collection;
2524

@@ -65,7 +64,7 @@ public static Object mapIdentifier(Object object) {
6564
if (id == null) {
6665
throw new RelMongoProcessingException("_id must not be null");
6766
}
68-
return id instanceof ObjectId ? (ObjectId) id : (String) id;
67+
return id;
6968
}
7069

7170
}

src/main/java/io/github/kaiso/relmongo/util/AnnotationsUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import io.github.kaiso.relmongo.exception.RelMongoConfigurationException;
2424
import io.github.kaiso.relmongo.model.MappedByMetadata;
2525

26+
import org.springframework.data.mongodb.core.mapping.Document;
2627
import org.springframework.util.ReflectionUtils;
28+
import org.springframework.util.StringUtils;
2729

2830
import java.lang.annotation.Annotation;
2931
import java.lang.reflect.Field;
@@ -38,6 +40,17 @@ public class AnnotationsUtils {
3840
private AnnotationsUtils() {
3941
super();
4042
}
43+
44+
public static String getCollectionName(Field field) {
45+
String collection = ReflectionsUtil.getGenericType(field).getAnnotation(Document.class).collection();
46+
if (StringUtils.isEmpty(collection)) {
47+
collection = ReflectionsUtil.getGenericType(field).getAnnotation(Document.class).value();
48+
}
49+
if (StringUtils.isEmpty(collection)) {
50+
collection = ReflectionsUtil.getGenericType(field).getSimpleName().toLowerCase();
51+
}
52+
return collection;
53+
}
4154

4255
public static String getJoinProperty(Field field) {
4356
try {

src/main/java/io/github/kaiso/relmongo/util/ObjectIdReaderCallback.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import io.github.kaiso.relmongo.exception.RelMongoConfigurationException;
1919

20-
import org.bson.types.ObjectId;
2120
import org.springframework.data.annotation.Id;
2221
import org.springframework.util.ReflectionUtils;
2322
import org.springframework.util.ReflectionUtils.FieldCallback;
@@ -43,17 +42,10 @@ public void doWith(Field field) throws IllegalAccessException {
4342
if (field.isAnnotationPresent(Id.class)) {
4443
ReflectionUtils.makeAccessible(field);
4544
try {
46-
Object value = field.get(source);
47-
if (value instanceof String) {
48-
objectId = value;
49-
} else {
50-
objectId = (ObjectId) value;
51-
}
45+
objectId = field.get(source);
5246
this.idField = field;
5347
} catch (IllegalArgumentException | IllegalAccessException e) {
5448
throw new RelMongoConfigurationException("unable to access the @Id field", e);
55-
} catch (ClassCastException e) {
56-
throw new RelMongoConfigurationException("the @Id field must be of type ObjectId or String", e);
5749
}
5850
}
5951

src/test/java/io/github/kaiso/relmongo/data/model/Address.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import io.github.kaiso.relmongo.annotation.ManyToOne;
77
import io.github.kaiso.relmongo.annotation.OneToOne;
88

9-
import org.bson.types.ObjectId;
109
import org.springframework.data.annotation.Id;
1110
import org.springframework.data.annotation.LastModifiedDate;
1211
import org.springframework.data.mongodb.core.mapping.Document;
@@ -17,7 +16,7 @@
1716
public class Address {
1817

1918
@Id
20-
private ObjectId id;
19+
private Long id;
2120
private String location;
2221

2322
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@@ -31,12 +30,24 @@ public class Address {
3130
private LocalDateTime lastModifiedDate;
3231

3332
private LocalDateTime creationDate = LocalDateTime.now();
33+
34+
35+
36+
37+
public Address() {
38+
super();
39+
}
40+
41+
public Address(Long id) {
42+
super();
43+
this.id = id;
44+
}
3445

35-
public ObjectId getId() {
46+
public Long getId() {
3647
return id;
3748
}
3849

39-
public void setId(ObjectId id) {
50+
public void setId(Long id) {
4051
this.id = id;
4152
}
4253

src/test/java/io/github/kaiso/relmongo/data/model/DrivingLicense.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.springframework.data.annotation.Id;
99
import org.springframework.data.mongodb.core.mapping.Document;
1010

11-
@Document(collection = "drivingLicenses")
11+
@Document("drivingLicenses")
1212
public class DrivingLicense {
1313

1414
@Id

0 commit comments

Comments
 (0)