Skip to content

Commit 6a078cc

Browse files
noubasenoubase
noubase
authored and
noubase
committed
many to many
1 parent 729e361 commit 6a078cc

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

core-crud/src/main/java/com/noubase/core/crud/model/relation/AbstractRelationsConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ abstract class AbstractRelationsConfig<P extends Serializable, S extends Seriali
2121

2222
private Method method;
2323

24-
@Nullable
24+
@NotNull
2525
public Method getMethod() {
2626
return method;
2727
}

core-crud/src/main/java/com/noubase/core/crud/model/relation/RelationsConfig.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.noubase.core.crud.model.relation;
22

33
import org.jetbrains.annotations.NotNull;
4-
import org.jetbrains.annotations.Nullable;
54
import org.springframework.data.domain.Persistable;
65

76
import java.io.Serializable;
@@ -23,7 +22,7 @@ public interface RelationsConfig<P extends Serializable, S extends Serializable>
2322
@NotNull
2423
String getField();
2524

26-
@Nullable
25+
@NotNull
2726
Method getMethod();
2827

2928
void setMethod(@NotNull Method method);

core-crud/src/main/java/com/noubase/core/crud/model/relation/RelationsFetcher.java

+18-21
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.springframework.data.domain.Page;
99
import org.springframework.data.domain.Persistable;
1010

11+
import java.beans.PropertyDescriptor;
1112
import java.io.Serializable;
1213
import java.lang.reflect.InvocationTargetException;
1314
import java.lang.reflect.Method;
@@ -21,35 +22,24 @@ public class RelationsFetcher<T extends Persistable<ID>, ID extends Serializable
2122

2223
private final Set<RelationsConfig<ID, ? extends Serializable>> configs;
2324

24-
private final Class<T> tClass;
25-
2625
public RelationsFetcher(Set<RelationsConfig<ID, ? extends Serializable>> configs, Class<T> tClass) {
2726
this.configs = configs;
28-
this.tClass = tClass;
27+
this.validate(configs, tClass);
2928
}
3029

31-
public T fetchRelations(
32-
final T one,
33-
final ResourceRequest request
34-
) throws InvocationTargetException, IllegalAccessException {
30+
public T fetchRelations(final T one, final ResourceRequest request) throws InvocationTargetException, IllegalAccessException {
3531
if (!configs.isEmpty() && !request.getRelated().isEmpty()) {
3632
Map<RequestRelation, RelationsConfig<ID, ? extends Serializable>> map = getExisted(request, configs);
3733
for (Map.Entry<RequestRelation, RelationsConfig<ID, ? extends Serializable>> entry : map.entrySet()) {
3834
RelationsConfig<ID, ? extends Serializable> config = entry.getValue();
3935
Set<? extends Serializable> ids = config.getIds(one.getId());
40-
if (config.getMethod() == null) {
41-
config.setMethod(getSetter(tClass, config.getField()));
42-
}
4336
config.getMethod().invoke(one, config.getItems(ids, entry.getKey().getFields()));
4437
}
4538
}
4639
return one;
4740
}
4841

49-
public Set<T> fetchRelations(
50-
final Page<T> page,
51-
final ResourceRequest request
52-
) throws InvocationTargetException, IllegalAccessException {
42+
public Set<T> fetchRelations(final Page<T> page, final ResourceRequest request) throws InvocationTargetException, IllegalAccessException {
5343
Set<T> all = new LinkedHashSet<>();
5444
if (!configs.isEmpty() && !request.getRelated().isEmpty()) {
5545
HashSet<ID> ids = new HashSet<>();
@@ -59,9 +49,6 @@ public Set<T> fetchRelations(
5949
Map<RequestRelation, RelationsConfig<ID, ? extends Serializable>> map = getExisted(request, configs);
6050
for (Map.Entry<RequestRelation, RelationsConfig<ID, ? extends Serializable>> entry : map.entrySet()) {
6151
RelationsConfig<ID, ? extends Serializable> config = entry.getValue();
62-
if (config.getMethod() == null) {
63-
config.setMethod(getSetter(tClass, config.getField()));
64-
}
6552
Map<ID, ? extends Collection<? extends Serializable>> idMap = config.getIds(ids);
6653
Set<Serializable> relatedIds = new HashSet<>();
6754
for (Collection<? extends Serializable> collection : idMap.values()) {
@@ -82,11 +69,22 @@ public Set<T> fetchRelations(
8269
return all;
8370
}
8471

72+
private void validate(Set<RelationsConfig<ID, ? extends Serializable>> configs, Class<T> tClass) {
73+
for (RelationsConfig<ID, ? extends Serializable> config : configs) {
74+
config.setMethod(getSetter(tClass, config.getField()));
75+
}
76+
}
77+
8578
@NotNull
86-
private Method getSetter(final Class tClass, String field) {
87-
Method writeMethod = BeanUtils.getPropertyDescriptor(tClass, field).getWriteMethod();
79+
private Method getSetter(final Class tClass, String field) throws RuntimeException {
80+
RuntimeException ex = new RuntimeException("Given class has no proper setter for a defined relationship.");
81+
PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(tClass, field);
82+
if (descriptor == null) {
83+
throw ex;
84+
}
85+
Method writeMethod = descriptor.getWriteMethod();
8886
if (writeMethod == null || writeMethod.getModifiers() != Modifier.PUBLIC) {
89-
throw new RuntimeException("Given class has no proper setter for a relationship.");
87+
throw ex;
9088
}
9189

9290
Class<?>[] types = writeMethod.getParameterTypes();
@@ -133,7 +131,6 @@ public Collection<Persistable<? extends Serializable>> getCollection(Serializabl
133131
set.add(item);
134132
}
135133
}
136-
137134
}
138135
}
139136
return set;

0 commit comments

Comments
 (0)