1+
12/*
23 * Copyright (c) Mirth Corporation. All rights reserved.
34 *
78 * been included with this distribution in the LICENSE.txt file.
89 */
910
10- package com .mirth .connect .server .userutil ;
11-
12- import java .util .Collection ;
13- import java .util .Collections ;
14- import java .util .HashSet ;
15- import java .util .Map ;
16- import java .util .Set ;
17-
18- import org .mozilla .javascript .Context ;
19-
20- import com .mirth .connect .donkey .server .Constants ;
21- import com .mirth .connect .userutil .ImmutableConnectorMessage ;
22-
23- /**
24- * Utility class used in the preprocessor or source filter/transformer to prevent the message from
25- * being sent to specific destinations.
26- */
27- public class DestinationSet {
28-
29- private Map <String , Integer > destinationIdMap ;
30- private Set <Integer > metaDataIds ;
31-
32- /**
33- * DestinationSet instances should NOT be constructed manually. The instance "destinationSet"
34- * provided in the scope should be used.
35- *
36- * @param connectorMessage
37- * The delegate ImmutableConnectorMessage object.
38- */
39- public DestinationSet (ImmutableConnectorMessage connectorMessage ) {
40- try {
41- if (connectorMessage .getSourceMap ().containsKey (Constants .DESTINATION_SET_KEY )) {
42- this .destinationIdMap = connectorMessage .getDestinationIdMap ();
43- this .metaDataIds = (Set <Integer >) connectorMessage .getSourceMap ().get (Constants .DESTINATION_SET_KEY );
44- }
45- } catch (Exception e ) {
46- }
47- }
48-
49- /**
50- * Stop a destination from being processed for this message.
51- *
52- * @param metaDataIdOrConnectorName
53- * An integer representing the metaDataId of a destination connector, or the actual
54- * destination connector name.
55- * @return A boolean indicating whether at least one destination connector was actually removed
56- * from processing for this message.
57- */
58- public boolean remove (Object metaDataIdOrConnectorName ) {
59- if (metaDataIds != null ) {
60- Integer metaDataId = convertToMetaDataId (metaDataIdOrConnectorName );
61-
62- if (metaDataId != null ) {
63- return metaDataIds .remove (metaDataId );
64- }
65- }
66-
67- return false ;
68- }
69-
70- /**
71- * Stop a destination from being processed for this message.
72- *
73- * @param metaDataIdOrConnectorNames
74- * A collection of integers representing the metaDataId of a destination connectors,
75- * or the actual destination connector names. JavaScript arrays can be used.
76- * @return A boolean indicating whether at least one destination connector was actually removed
77- * from processing for this message.
78- */
79- public boolean remove (Collection <Object > metaDataIdOrConnectorNames ) {
80- boolean removed = false ;
81-
82- for (Object metaDataIdOrConnectorName : metaDataIdOrConnectorNames ) {
83- if (remove (metaDataIdOrConnectorName )) {
84- removed = true ;
85- }
86- }
87-
88- return removed ;
89- }
90-
91- /**
92- * Stop all except one destination from being processed for this message.
93- *
94- * @param metaDataIdOrConnectorName
95- * An integer representing the metaDataId of a destination connector, or the actual
96- * destination connector name.
97- * @return A boolean indicating whether at least one destination connector was actually removed
98- * from processing for this message.
99- */
100- public boolean removeAllExcept (Object metaDataIdOrConnectorName ) {
101- if (metaDataIds != null ) {
102- Integer metaDataId = convertToMetaDataId (metaDataIdOrConnectorName );
103-
104- if (metaDataId != null ) {
105- return metaDataIds .retainAll (Collections .singleton (metaDataId ));
106- }
107- }
108-
109- return false ;
110- }
111-
112- /**
113- * Stop all except one destination from being processed for this message.
114- *
115- * @param metaDataIdOrConnectorNames
116- * A collection of integers representing the metaDataId of a destination connectors,
117- * or the actual destination connector names. JavaScript arrays can be used.
118- * @return A boolean indicating whether at least one destination connector was actually removed
119- * from processing for this message.
120- */
121- public boolean removeAllExcept (Collection <Object > metaDataIdOrConnectorNames ) {
122- if (metaDataIds != null ) {
123- Set <Integer > set = new HashSet <Integer >();
124-
125- for (Object metaDataIdOrConnectorName : metaDataIdOrConnectorNames ) {
126- Integer metaDataId = convertToMetaDataId (metaDataIdOrConnectorName );
127-
128- if (metaDataId != null ) {
129- set .add (metaDataId );
130- }
131- }
132-
133- return metaDataIds .retainAll (set );
134- }
135-
136- return false ;
137- }
138-
139- /**
140- * Stop all destinations from being processed for this message. This does NOT mark the source
141- * message as FILTERED.
142- *
143- * @return A boolean indicating whether at least one destination connector was actually removed
144- * from processing for this message.
145- */
146- public boolean removeAll () {
147- if (metaDataIds != null && metaDataIds .size () > 0 ) {
148- metaDataIds .clear ();
149- return true ;
150- }
151-
152- return false ;
153- }
154-
155- private Integer convertToMetaDataId (Object metaDataIdOrConnectorName ) {
156- if (metaDataIdOrConnectorName != null ) {
157- if (metaDataIdOrConnectorName instanceof Number ) {
158- return ((Number ) metaDataIdOrConnectorName ).intValue ();
159- } else if (metaDataIdOrConnectorName .getClass ().getName ().equals ("org.mozilla.javascript.NativeNumber" )) {
160- return (Integer ) Context .jsToJava (metaDataIdOrConnectorName , int .class );
161- } else if (destinationIdMap != null ) {
162- return destinationIdMap .get (metaDataIdOrConnectorName .toString ());
163- }
164- }
165-
166- return null ;
167- }
168- }
11+ package com .mirth .connect .server .userutil ;
12+
13+ import java .util .Collection ;
14+ import java .util .Collections ;
15+ import java .util .HashSet ;
16+ import java .util .Iterator ;
17+ import java .util .Map ;
18+ import java .util .Optional ;
19+ import java .util .Set ;
20+ import java .util .stream .Collectors ;
21+
22+ import org .mozilla .javascript .Context ;
23+
24+ import com .mirth .connect .donkey .server .Constants ;
25+ import com .mirth .connect .userutil .ImmutableConnectorMessage ;
26+
27+ /**
28+ * Utility class used in the preprocessor or source filter/transformer to prevent the message from
29+ * being sent to specific destinations.
30+ */
31+ public class DestinationSet implements Set <Integer > {
32+
33+ private Map <String , Integer > destinationIdMap = Collections .emptyMap ();
34+ private Set <Integer > metaDataIds ;
35+
36+ /**
37+ * DestinationSet instances should NOT be constructed manually. The instance "destinationSet"
38+ * provided in the scope should be used.
39+ *
40+ * @param connectorMessage
41+ * The delegate ImmutableConnectorMessage object.
42+ */
43+ public DestinationSet (ImmutableConnectorMessage connectorMessage ) {
44+ try {
45+ if (connectorMessage .getSourceMap ().containsKey (Constants .DESTINATION_SET_KEY )) {
46+ this .destinationIdMap = connectorMessage .getDestinationIdMap ();
47+ this .metaDataIds = (Set <Integer >) connectorMessage .getSourceMap ().get (Constants .DESTINATION_SET_KEY );
48+ }
49+ } catch (Exception e ) {
50+ metaDataIds = new HashSet <>();
51+ }
52+ }
53+
54+ /**
55+ * Stop a destination from being processed for this message.
56+ *
57+ * @param metaDataIdOrConnectorName
58+ * An integer representing the metaDataId of a destination connector, or the actual
59+ * destination connector name.
60+ * @return A boolean indicating whether at least one destination connector was actually removed
61+ * from processing for this message.
62+ */
63+ public boolean remove (Object metaDataIdOrConnectorName ) {
64+ return remove (Collections .singleton (metaDataIdOrConnectorName ));
65+ }
66+
67+ /**
68+ * Stop a destination from being processed for this message.
69+ *
70+ * @param metaDataIdOrConnectorNames
71+ * A collection of integers representing the metaDataId of a destination connectors,
72+ * or the actual destination connector names. JavaScript arrays can be used.
73+ * @return A boolean indicating whether at least one destination connector was actually removed
74+ * from processing for this message.
75+ */
76+ public boolean remove (Collection <Object > metaDataIdOrConnectorNames ) {
77+ if (metaDataIdOrConnectorNames == null ) { return false ; }
78+
79+ return metaDataIdOrConnectorNames .stream ()
80+ .map (this ::convertToMetaDataId )
81+ .filter (Optional ::isPresent )
82+ .map (Optional ::get )
83+ .map (metaDataIds ::remove )
84+ .filter (Boolean ::booleanValue )
85+ .count () > 0 ;
86+ }
87+
88+ /**
89+ * Stop all except one destination from being processed for this message.
90+ *
91+ * @param metaDataIdOrConnectorName
92+ * An integer representing the metaDataId of a destination connector, or the actual
93+ * destination connector name.
94+ * @return A boolean indicating whether at least one destination connector was actually removed
95+ * from processing for this message.
96+ */
97+ public boolean removeAllExcept (Object metaDataIdOrConnectorName ) {
98+ return removeAllExcept (Collections .singleton (metaDataIdOrConnectorName ));
99+ }
100+
101+ /**
102+ * Stop all except one destination from being processed for this message.
103+ *
104+ * @param metaDataIdOrConnectorNames
105+ * A collection of integers representing the metaDataId of a destination connectors,
106+ * or the actual destination connector names. JavaScript arrays can be used.
107+ * @return A boolean indicating whether at least one destination connector was actually removed
108+ * from processing for this message.
109+ */
110+ public boolean removeAllExcept (Collection <Object > metaDataIdOrConnectorNames ) {
111+ if (metaDataIdOrConnectorNames == null ) { return false ; }
112+
113+ Set <Integer > set = metaDataIdOrConnectorNames .stream ()
114+ .map (this ::convertToMetaDataId )
115+ .filter (Optional ::isPresent )
116+ .map (Optional ::get )
117+ .collect (Collectors .toSet ());
118+
119+ return metaDataIds .retainAll (set );
120+ }
121+
122+ /**
123+ * Stop all destinations from being processed for this message. This does NOT mark the source
124+ * message as FILTERED.
125+ *
126+ * @return A boolean indicating whether at least one destination connector was actually removed
127+ * from processing for this message.
128+ */
129+ public boolean removeAll () {
130+ int origSize = size ();
131+ clear ();
132+ return origSize > 0 ;
133+ }
134+
135+ private Optional <Integer > convertToMetaDataId (Object metaDataIdOrConnectorName ) {
136+ Integer result = null ;
137+
138+ if (metaDataIdOrConnectorName != null ) {
139+ if (metaDataIdOrConnectorName instanceof Number ) {
140+ result = Integer .valueOf (((Number ) metaDataIdOrConnectorName ).intValue ());
141+ } else if (metaDataIdOrConnectorName .getClass ().getName ().equals ("org.mozilla.javascript.NativeNumber" )) {
142+ result = (Integer ) Context .jsToJava (metaDataIdOrConnectorName , int .class );
143+ } else {
144+ result = destinationIdMap .get (metaDataIdOrConnectorName .toString ());
145+ }
146+ }
147+
148+ return Optional .ofNullable (result );
149+ }
150+
151+ @ Override
152+ public int size () {
153+ return metaDataIds .size ();
154+ }
155+
156+ @ Override
157+ public boolean isEmpty () {
158+ return metaDataIds .isEmpty ();
159+ }
160+
161+ @ Override
162+ public boolean contains (Object metaDataIdOrConnectorName ) {
163+ Optional <Integer > m = convertToMetaDataId (metaDataIdOrConnectorName );
164+
165+ return m .isPresent () && metaDataIds .contains (m .get ());
166+ }
167+
168+ @ Override
169+ public Iterator <Integer > iterator () {
170+ return Collections .unmodifiableSet (metaDataIds ).iterator ();
171+ }
172+
173+ @ Override
174+ public Object [] toArray () {
175+ return metaDataIds .toArray ();
176+ }
177+
178+ @ Override
179+ public <T > T [] toArray (T [] a ) {
180+ return metaDataIds .toArray (a );
181+ }
182+
183+ @ Override
184+ public boolean add (Integer metaDataId ) {
185+ return metaDataId != null && metaDataIds .add (metaDataId );
186+ }
187+
188+ @ Override
189+ public boolean containsAll (Collection <?> metaDataIdOrConnectorNames ) {
190+ if (metaDataIdOrConnectorNames == null ) { return false ; }
191+
192+ return metaDataIdOrConnectorNames .stream ()
193+ .map (this ::contains )
194+ .allMatch (Boolean ::booleanValue );
195+ }
196+
197+ @ Override
198+ public boolean addAll (Collection <? extends Integer > metaDataIdOrConnectorNames ) {
199+ boolean changed = false ;
200+
201+ if (metaDataIdOrConnectorNames != null ) {
202+ for (Object item : metaDataIdOrConnectorNames ) {
203+ Optional <Integer > m = convertToMetaDataId (item );
204+
205+ if (m .isPresent () && metaDataIds .add (m .get ())) {
206+ changed = true ;
207+ }
208+ }
209+ }
210+
211+ return changed ;
212+ }
213+
214+ @ Override
215+ public boolean retainAll (Collection <?> metaDataIdOrConnectorNames ) {
216+ return removeAllExcept ((Collection <Object >)metaDataIdOrConnectorNames );
217+ }
218+
219+ @ Override
220+ public boolean removeAll (Collection <?> metaDataIdOrConnectorNames ) {
221+ return remove ((Collection <Object >)metaDataIdOrConnectorNames );
222+ }
223+
224+ @ Override
225+ public void clear () {
226+ metaDataIds .clear ();
227+ }
228+ }
0 commit comments