Refactor the internal HookSynchronization to a separate external global
class.
Showing
2 changed files
with
88 additions
and
80 deletions
1 | import javax.transaction.Synchronization | ||
2 | import javax.transaction.xa.XAResource | ||
3 | import javax.transaction.xa.Xid | ||
4 | 1 | ||
5 | import groovy.transform.Field | 2 | import groovy.transform.Field |
6 | 3 | ||
... | @@ -15,86 +12,10 @@ import org.moqui.entity.EntityFind | ... | @@ -15,86 +12,10 @@ import org.moqui.entity.EntityFind |
15 | import org.moqui.entity.EntityList | 12 | import org.moqui.entity.EntityList |
16 | import org.moqui.entity.EntityValue | 13 | import org.moqui.entity.EntityValue |
17 | 14 | ||
15 | import org.moqui.keycloak.HookSynchronization | ||
18 | @Field Logger logger = LoggerFactory.getLogger(getClass().getName()) | 16 | @Field Logger logger = LoggerFactory.getLogger(getClass().getName()) |
19 | //'HookServices') | 17 | //'HookServices') |
20 | 18 | ||
21 | class HookSynchronization implements Synchronization { | ||
22 | private final ExecutionContext ec; | ||
23 | |||
24 | private static class EntityToken { | ||
25 | protected final String entityName | ||
26 | protected final String keyName | ||
27 | protected final Object keyValue | ||
28 | protected final Map<String, Object> extraParameters = new HashMap<String, Object>() | ||
29 | |||
30 | protected EntityToken(String entityName, String keyName, Object keyValue) { | ||
31 | this.entityName = entityName | ||
32 | this.keyName = keyName | ||
33 | this.keyValue = keyValue | ||
34 | } | ||
35 | |||
36 | public boolean equals(Object other) { | ||
37 | if (!(other instanceof EntityToken)) { | ||
38 | return false | ||
39 | } | ||
40 | EntityToken that = (EntityToken) other | ||
41 | return entityName.equals(that.entityName) && keyName.equals(that.keyName) && keyValue.equals(that.keyValue) | ||
42 | } | ||
43 | |||
44 | public int hashCode() { | ||
45 | return entityName.hashCode() ^ keyName.hashCode() ^ keyValue.hashCode() | ||
46 | } | ||
47 | } | ||
48 | |||
49 | private Map<EntityToken, Map<String, Object>> updates = new LinkedHashMap<>() | ||
50 | |||
51 | protected HookSynchronization(ExecutionContext ec) { | ||
52 | this.ec = ec | ||
53 | } | ||
54 | |||
55 | protected void add(String entityName, String keyName, Object keyValue, Map<String, Object> extraParameters) { | ||
56 | EntityToken entityToken = new EntityToken(entityName, keyName, keyValue) | ||
57 | // This always move the value to the end of the list | ||
58 | Map<String, Object> existingExtraParameters = updates.remove(entityToken) | ||
59 | Map<String, Object> newExtraParameters = [:] | ||
60 | if (existingExtraParameters) { | ||
61 | newExtraParameters.putAll(existingExtraParameters) | ||
62 | } | ||
63 | if (extraParameters) { | ||
64 | newExtraParameters.putAll(extraParameters) | ||
65 | } | ||
66 | updates.put(entityToken, newExtraParameters) | ||
67 | if (existingExtraParameters == null) { | ||
68 | ec.getLogger().info("Add new synchronization for ${entityName}: [${keyValue}]") | ||
69 | } else { | ||
70 | ec.getLogger().info("Updated synchronization for ${entityName}: [${keyValue}]") | ||
71 | } | ||
72 | } | ||
73 | |||
74 | @Override | ||
75 | public void beforeCompletion() { | ||
76 | List<Map<String, Object>> updates = [] | ||
77 | for (Map.Entry<EntityToken, Map<String, Object>> entry: this.updates.entrySet()) { | ||
78 | EntityToken token = entry.getKey() | ||
79 | Map<String, Object> extraParameters = entry.getValue() | ||
80 | updates.add([ | ||
81 | entityName: token.entityName, | ||
82 | keyName: token.keyName, | ||
83 | keyValue: token.keyValue, | ||
84 | extraParameters: extraParameters, | ||
85 | ]) | ||
86 | } | ||
87 | if (updates) { | ||
88 | ec.getService().async().name("keycloak.HookServices.process#Updates").parameter('updates', updates).call() | ||
89 | } | ||
90 | } | ||
91 | |||
92 | @Override | ||
93 | public void afterCompletion(int status) { | ||
94 | |||
95 | } | ||
96 | } | ||
97 | |||
98 | HookSynchronization getHookSync() { | 19 | HookSynchronization getHookSync() { |
99 | HookSynchronization hookSync = ec.transaction.getActiveSynchronization(getClass().getName()) | 20 | HookSynchronization hookSync = ec.transaction.getActiveSynchronization(getClass().getName()) |
100 | if (hookSync == null) { | 21 | if (hookSync == null) { | ... | ... |
1 | package org.moqui.keycloak | ||
2 | |||
3 | import javax.transaction.Synchronization | ||
4 | |||
5 | import org.slf4j.Logger | ||
6 | import org.slf4j.LoggerFactory | ||
7 | |||
8 | import org.moqui.context.ExecutionContext | ||
9 | |||
10 | public class HookSynchronization implements Synchronization { | ||
11 | private static final Logger logger = LoggerFactory.getLogger(getClass().getName()) | ||
12 | private final ExecutionContext ec; | ||
13 | |||
14 | private static final class EntityToken { | ||
15 | protected final String entityName | ||
16 | protected final String keyName | ||
17 | protected final Object keyValue | ||
18 | protected final Map<String, Object> extraParameters = new HashMap<String, Object>() | ||
19 | |||
20 | protected EntityToken(String entityName, String keyName, Object keyValue) { | ||
21 | this.entityName = entityName | ||
22 | this.keyName = keyName | ||
23 | this.keyValue = keyValue | ||
24 | } | ||
25 | |||
26 | public boolean equals(Object other) { | ||
27 | if (!(other instanceof EntityToken)) { | ||
28 | return false | ||
29 | } | ||
30 | EntityToken that = (EntityToken) other | ||
31 | return entityName.equals(that.entityName) && keyName.equals(that.keyName) && keyValue.equals(that.keyValue) | ||
32 | } | ||
33 | |||
34 | public int hashCode() { | ||
35 | return entityName.hashCode() ^ keyName.hashCode() ^ keyValue.hashCode() | ||
36 | } | ||
37 | } | ||
38 | |||
39 | private final Map<EntityToken, Map<String, Object>> updates = new LinkedHashMap<>() | ||
40 | |||
41 | public HookSynchronization(ExecutionContext ec) { | ||
42 | this.ec = ec | ||
43 | } | ||
44 | |||
45 | protected void add(String entityName, String keyName, Object keyValue, Map<String, Object> extraParameters) { | ||
46 | EntityToken entityToken = new EntityToken(entityName, keyName, keyValue) | ||
47 | // This always move the value to the end of the list | ||
48 | Map<String, Object> existingExtraParameters = updates.remove(entityToken) | ||
49 | Map<String, Object> newExtraParameters = [:] | ||
50 | if (existingExtraParameters) { | ||
51 | newExtraParameters.putAll(existingExtraParameters) | ||
52 | } | ||
53 | if (extraParameters) { | ||
54 | newExtraParameters.putAll(extraParameters) | ||
55 | } | ||
56 | updates.put(entityToken, newExtraParameters) | ||
57 | if (existingExtraParameters == null) { | ||
58 | ec.getLogger().info("Add new synchronization for ${entityName}: [${keyValue}]") | ||
59 | } else { | ||
60 | ec.getLogger().info("Updated synchronization for ${entityName}: [${keyValue}]") | ||
61 | } | ||
62 | } | ||
63 | |||
64 | @Override | ||
65 | public void beforeCompletion() { | ||
66 | List<Map<String, Object>> updates = [] | ||
67 | for (Map.Entry<EntityToken, Map<String, Object>> entry: this.updates.entrySet()) { | ||
68 | EntityToken token = entry.getKey() | ||
69 | Map<String, Object> extraParameters = entry.getValue() | ||
70 | updates.add([ | ||
71 | entityName: token.entityName, | ||
72 | keyName: token.keyName, | ||
73 | keyValue: token.keyValue, | ||
74 | extraParameters: extraParameters, | ||
75 | ]) | ||
76 | } | ||
77 | if (updates) { | ||
78 | ec.getService().async().name("keycloak.HookServices.process#Updates").parameter('updates', updates).call() | ||
79 | } | ||
80 | } | ||
81 | |||
82 | @Override | ||
83 | public void afterCompletion(int status) { | ||
84 | |||
85 | } | ||
86 | } | ||
87 |
-
Please register or sign in to post a comment