-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathStateUpdateActionUtils.java
240 lines (206 loc) · 10.2 KB
/
StateUpdateActionUtils.java
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package com.commercetools.sync.states.utils;
import static com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.buildUpdateAction;
import static org.apache.commons.lang3.BooleanUtils.toBoolean;
import com.commercetools.api.models.state.State;
import com.commercetools.api.models.state.StateAddRolesActionBuilder;
import com.commercetools.api.models.state.StateChangeInitialActionBuilder;
import com.commercetools.api.models.state.StateChangeTypeActionBuilder;
import com.commercetools.api.models.state.StateDraft;
import com.commercetools.api.models.state.StateReference;
import com.commercetools.api.models.state.StateRemoveRolesActionBuilder;
import com.commercetools.api.models.state.StateResourceIdentifier;
import com.commercetools.api.models.state.StateResourceIdentifierBuilder;
import com.commercetools.api.models.state.StateRoleEnum;
import com.commercetools.api.models.state.StateSetDescriptionActionBuilder;
import com.commercetools.api.models.state.StateSetNameActionBuilder;
import com.commercetools.api.models.state.StateSetTransitionsActionBuilder;
import com.commercetools.api.models.state.StateUpdateAction;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
public final class StateUpdateActionUtils {
private StateUpdateActionUtils() {}
/**
* Compares the {@code type} values of a {@link State} and a {@link StateDraft} and returns an
* {@link java.util.Optional} of update action, which would contain the {@code "changeType"}
* {@link StateUpdateAction}. If both {@link State} and {@link StateDraft} have the same {@code
* type} values, then no update action is needed and empty optional will be returned.
*
* @param oldState the state that should be updated.
* @param newState the state draft which contains the new type.
* @return optional containing update action or empty optional if types are identical.
*/
@Nonnull
public static Optional<StateUpdateAction> buildChangeTypeAction(
@Nonnull final State oldState, @Nonnull final StateDraft newState) {
return buildUpdateAction(
oldState.getType(),
newState.getType(),
() -> StateChangeTypeActionBuilder.of().type(newState.getType()).build());
}
/**
* Compares the {@code name} values of a {@link State} and a {@link StateDraft} and returns an
* {@link java.util.Optional} of update action, which would contain the {@code "setName"} {@link
* StateUpdateAction}. If both {@link State} and {@link StateDraft} have the same {@code name}
* values, then no update action is needed and empty optional will be returned.
*
* @param oldState the state that should be updated.
* @param newState the state draft which contains the new name.
* @return optional containing update action or empty optional if names are identical.
*/
@Nonnull
public static Optional<StateUpdateAction> buildSetNameAction(
@Nonnull final State oldState, @Nonnull final StateDraft newState) {
return buildUpdateAction(
oldState.getName(),
newState.getName(),
() -> StateSetNameActionBuilder.of().name(newState.getName()).build());
}
/**
* Compares the {@code description} values of a {@link State} and a {@link StateDraft} and returns
* an {@link java.util.Optional} of update action, which would contain the {@code
* "setDescription"} {@link StateUpdateAction}. If both {@link State} and {@link StateDraft} have
* the same {@code description} values, then no update action is needed and empty optional will be
* returned.
*
* @param oldState the state that should be updated.
* @param newState the state draft which contains the new description.
* @return optional containing update action or empty optional if descriptions are identical.
*/
@Nonnull
public static Optional<StateUpdateAction> buildSetDescriptionAction(
@Nonnull final State oldState, @Nonnull final StateDraft newState) {
return buildUpdateAction(
oldState.getDescription(),
newState.getDescription(),
() -> StateSetDescriptionActionBuilder.of().description(newState.getDescription()).build());
}
/**
* Compares the {@code initial} values of a {@link State} and a {@link StateDraft} and returns an
* {@link java.util.Optional} of update action, which would contain the {@code "changeInitial"}
* {@link StateUpdateAction}. If both {@link State} and {@link StateDraft} have the same {@code
* initial} values, then no update action is needed and empty optional will be returned.
*
* @param oldState the state that should be updated.
* @param newState the state draft which contains the new initial.
* @return optional containing update action or empty optional if initial are identical.
*/
@Nonnull
public static Optional<StateUpdateAction> buildChangeInitialAction(
@Nonnull final State oldState, @Nonnull final StateDraft newState) {
final boolean isNewStateInitial = toBoolean(newState.getInitial());
final boolean isOldStateInitial = toBoolean(oldState.getInitial());
return buildUpdateAction(
isOldStateInitial,
isNewStateInitial,
() -> StateChangeInitialActionBuilder.of().initial(isNewStateInitial).build());
}
/**
* Compares the roles of a {@link State} and a {@link StateDraft} and returns a list of {@link
* StateUpdateAction} as a result. If both the {@link State} and the {@link StateDraft} have
* identical roles, then no update action is needed and hence an empty {@link java.util.List} is
* returned.
*
* @param oldState the state which should be updated.
* @param newState the state draft where we get the key.
* @return A list with the update actions or an empty list if the roles are identical.
*/
@Nonnull
public static List<StateUpdateAction> buildRolesUpdateActions(
@Nonnull final State oldState, @Nonnull final StateDraft newState) {
boolean emptyNew = newState.getRoles() == null || newState.getRoles().isEmpty();
boolean emptyOld = oldState.getRoles() == null || oldState.getRoles().isEmpty();
if (emptyNew && emptyOld) {
return List.of();
}
final List<StateRoleEnum> newRoles = emptyNew ? new ArrayList<>() : newState.getRoles();
final List<StateRoleEnum> oldRoles = emptyOld ? new ArrayList<>() : oldState.getRoles();
final List<StateUpdateAction> actions = new ArrayList<>();
final List<StateRoleEnum> remove = diffRoles(oldRoles, newRoles);
if (!remove.isEmpty()) {
actions.add(StateRemoveRolesActionBuilder.of().roles(remove).build());
}
final List<StateRoleEnum> add = diffRoles(newRoles, oldRoles);
if (!add.isEmpty()) {
actions.add(StateAddRolesActionBuilder.of().roles(add).build());
}
return actions;
}
/**
* Compares the {@code transitions} values of a {@link State} and a {@link StateDraft} and returns
* an {@link java.util.Optional} of update action, which would contain the {@code
* "setTransitions"} {@link StateUpdateAction}. If both {@link State} and {@link StateDraft} have
* the same {@code transitions} values, then no update action is needed and empty optional will be
* returned. if not, the transition of the old State gets overwritten with the transitions of the
* statedraft
*
* @param oldState the {@link State} which should be updated.
* @param newState the {@link StateDraft} where we get the new data.
* @return optional containing update action or empty optional if there are no changes detected or
* there was an error.
*/
@Nonnull
public static Optional<StateUpdateAction> buildSetTransitionsAction(
@Nonnull final State oldState, @Nonnull final StateDraft newState) {
final boolean emptyNew =
newState.getTransitions() == null
|| newState.getTransitions().isEmpty()
|| newState.getTransitions().stream().noneMatch(Objects::nonNull);
final boolean emptyOld =
oldState.getTransitions() == null
|| oldState.getTransitions().isEmpty()
|| oldState.getTransitions().stream().noneMatch(Objects::nonNull);
if (emptyNew && emptyOld) {
return Optional.empty();
} else if (newState.getTransitions() == null) {
return Optional.of(
StateSetTransitionsActionBuilder.of()
.transitions((List<StateResourceIdentifier>) null)
.build());
} else if (emptyNew) {
return Optional.of(StateSetTransitionsActionBuilder.of().transitions(List.of()).build());
}
final Set<StateResourceIdentifier> newTransitions =
newState.getTransitions().stream().filter(Objects::nonNull).collect(Collectors.toSet());
final Set<StateReference> oldTransitions =
oldState.getTransitions().stream().filter(Objects::nonNull).collect(Collectors.toSet());
if (hasDifferentTransitions(newTransitions, oldTransitions)) {
final List<StateResourceIdentifier> transitions =
newTransitions.stream()
.map(
transition ->
StateResourceIdentifierBuilder.of()
.id(transition.getId())
.key(transition.getKey())
.build())
.collect(Collectors.toList());
return Optional.of(StateSetTransitionsActionBuilder.of().transitions(transitions).build());
}
return Optional.empty();
}
private static boolean hasDifferentTransitions(
final Set<StateResourceIdentifier> newTransitions, final Set<StateReference> oldTransitions) {
final List<String> newTransitionIds =
newTransitions.stream().map(StateResourceIdentifier::getId).collect(Collectors.toList());
final List<String> oldTransitionIds =
oldTransitions.stream().map(StateReference::getId).collect(Collectors.toList());
return !Objects.equals(newTransitionIds, oldTransitionIds);
}
private static List<StateRoleEnum> diffRoles(
final List<StateRoleEnum> src, final List<StateRoleEnum> dst) {
return src.stream()
.map(
role -> {
if (!dst.contains(role)) {
return role;
}
return null;
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
}