-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityWalkerService.java
More file actions
69 lines (62 loc) · 2.45 KB
/
Copy pathEntityWalkerService.java
File metadata and controls
69 lines (62 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.jug.joker.javadopexample.service;
import com.jug.joker.javadopexample.model.*;
import com.jug.joker.javadopexample.repository.CustomerRepository;
import com.jug.joker.javadopexample.repository.ProductRepository;
import com.jug.joker.javadopexample.service.integration.ProductPropertiesIntegrationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class EntityWalkerService<T> {
private final CustomerRepository customerRepository;
private final ProductRepository productRepository;
private final ProductPropertiesIntegrationService propertiesIntegrationService;
private Stream<T> processEntityTreeToStream(
SecuredEntity entity,
Function<SecuredEntity, T> mapper
) {
var result = Stream.of(mapper.apply(entity));
switch (entity) {
case Purchase(
var ignored,
var products,
var customerRef
) -> result = Stream.concat(
result, Stream.concat(
products.stream()
.flatMap(ref -> productRepository.findById(ref.id()).stream())
.flatMap(p -> processEntityTreeToStream(p, mapper)),
processEntityTreeToStream(
customerRepository.findById(customerRef.getId()).orElseThrow(),
mapper
)
)
);
case Product(
Long id,
var ignored
) -> result = Stream.concat(
result,
propertiesIntegrationService.findAllByProductId(id)
.stream()
.flatMap(pp -> processEntityTreeToStream(pp, mapper))
);
case Customer ignored -> {
}
case ProductProperties ignored -> {
}
}
return result;
}
public List<T> processEntityTree(
SecuredEntity entity,
Function<SecuredEntity, T> mapper
) {
return processEntityTreeToStream(entity, mapper).toList();
}
}