-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathStateTransformUtils.java
More file actions
88 lines (76 loc) · 3.38 KB
/
StateTransformUtils.java
File metadata and controls
88 lines (76 loc) · 3.38 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.commercetools.sync.states.utils;
import static com.commercetools.sync.states.utils.StateReferenceResolutionUtils.mapToStateDrafts;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import com.commercetools.api.client.ProjectApiRoot;
import com.commercetools.api.models.state.State;
import com.commercetools.api.models.state.StateDraft;
import com.commercetools.api.models.state.StateReference;
import com.commercetools.sync.commons.models.GraphQlQueryResource;
import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache;
import com.commercetools.sync.services.impl.BaseTransformServiceImpl;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import javax.annotation.Nonnull;
public final class StateTransformUtils {
/**
* Transforms States by resolving the references and map them to StateDrafts.
*
* <p>This method resolves(fetch key values for the reference id's) non null and unexpanded
* references of the State{@link State} by using cache.
*
* <p>If the reference ids are already cached, key values are pulled from the cache, otherwise it
* executes the query to fetch the key value for the reference id's and store the idToKey value
* pair in the cache for reuse.
*
* <p>Then maps the State to StateDraft by performing reference resolution considering idToKey
* value from the cache.
*
* @param client commercetools client.
* @param referenceIdToKeyCache the instance that manages cache.
* @param states the states to resolve the references.
* @return a new list which contains StateDrafts which have all their references resolved.
*/
@Nonnull
public static CompletableFuture<List<StateDraft>> toStateDrafts(
@Nonnull final ProjectApiRoot client,
@Nonnull final ReferenceIdToKeyCache referenceIdToKeyCache,
@Nonnull final List<State> states) {
final StateTransformServiceImpl stateTransformService =
new StateTransformServiceImpl(client, referenceIdToKeyCache);
return stateTransformService.toStateDrafts(states);
}
private static class StateTransformServiceImpl extends BaseTransformServiceImpl {
StateTransformServiceImpl(
@Nonnull final ProjectApiRoot ctpClient,
@Nonnull final ReferenceIdToKeyCache referenceIdToKeyCache) {
super(ctpClient, referenceIdToKeyCache);
}
@Nonnull
public CompletableFuture<List<StateDraft>> toStateDrafts(@Nonnull final List<State> states) {
return transformTransitionReference(states)
.thenApply(ignore -> mapToStateDrafts(states, referenceIdToKeyCache));
}
@Nonnull
private CompletableFuture<Void> transformTransitionReference(
@Nonnull final List<State> states) {
final Set<String> setOfTransitionStateIds =
states.stream()
.map(State::getTransitions)
.filter(Objects::nonNull)
.map(
transitions ->
transitions.stream()
.filter(Objects::nonNull)
.map(StateReference::getId)
.collect(toList()))
.flatMap(Collection::stream)
.collect(toSet());
return fetchAndFillReferenceIdToKeyCache(
setOfTransitionStateIds, GraphQlQueryResource.STATES);
}
}
}