88import java .util .HashMap ;
99import java .util .List ;
1010import java .util .Map ;
11+ import java .util .stream .Collectors ;
1112
1213public class Client {
1314 private final Registry registry ;
@@ -42,7 +43,7 @@ public String StartWorkflow(
4243 final String wfType = workflowClass .getSimpleName ();
4344 final StateDef stateDef = registry .getWorkflowState (wfType , startStateId );
4445 if (stateDef == null || !stateDef .getCanStartWorkflow ()) {
45- throw new RuntimeException ("invalid start stateId " + startStateId );
46+ throw new IllegalArgumentException ("invalid start stateId " + startStateId );
4647 }
4748
4849 WorkflowStartResponse workflowStartResponse = defaultApi .apiV1WorkflowStartPost (new WorkflowStartRequest ()
@@ -64,11 +65,11 @@ public String StartWorkflow(
6465 * @return
6566 * @param <T> type of the output
6667 */
67- public <T > T GetSimpleWorkflowResultWithLongWait (
68+ public <T > T GetSimpleWorkflowResultWithWait (
6869 Class <T > valueClass ,
6970 final String workflowId ,
7071 final String workflowRunId ) {
71- WorkflowGetResponse workflowGetResponse = defaultApi .apiV1WorkflowGetWithLongWaitPost (
72+ WorkflowGetResponse workflowGetResponse = defaultApi .apiV1WorkflowGetWithWaitPost (
7273 new WorkflowGetRequest ()
7374 .needsResults (true )
7475 .workflowId (workflowId )
@@ -79,17 +80,15 @@ public <T> T GetSimpleWorkflowResultWithLongWait(
7980 Preconditions .checkNotNull (workflowGetResponse .getResults (), checkErrorMessage );
8081 Preconditions .checkArgument (workflowGetResponse .getResults ().size () == 1 , checkErrorMessage );
8182 Preconditions .checkNotNull (workflowGetResponse .getResults ().get (0 ).getCompletedStateOutput (), checkErrorMessage );
82-
83- //TODO validate encoding type
84-
83+
8584 final StateCompletionOutput output = workflowGetResponse .getResults ().get (0 );
8685 return objectEncoder .decode (output .getCompletedStateOutput (), valueClass );
8786 }
8887
89- public <T > T GetSimpleWorkflowResultWithLongWait (
88+ public <T > T GetSimpleWorkflowResultWithWait (
9089 Class <T > valueClass ,
9190 final String workflowId ) {
92- return GetSimpleWorkflowResultWithLongWait (valueClass , workflowId , "" );
91+ return GetSimpleWorkflowResultWithWait (valueClass , workflowId , "" );
9392 }
9493
9594 /**
@@ -98,9 +97,9 @@ public <T> T GetSimpleWorkflowResultWithLongWait(
9897 * @param workflowRunId
9998 * @return a list of the state output for completion states. User code will figure how to use ObjectEncoder to decode the output
10099 */
101- public List <StateCompletionOutput > GetComplexWorkflowResultWithLongWait (
100+ public List <StateCompletionOutput > GetComplexWorkflowResultWithWait (
102101 final String workflowId , final String workflowRunId ) {
103- WorkflowGetResponse workflowGetResponse = defaultApi .apiV1WorkflowGetWithLongWaitPost (
102+ WorkflowGetResponse workflowGetResponse = defaultApi .apiV1WorkflowGetWithWaitPost (
104103 new WorkflowGetRequest ()
105104 .needsResults (true )
106105 .workflowId (workflowId )
@@ -110,8 +109,8 @@ public List<StateCompletionOutput> GetComplexWorkflowResultWithLongWait(
110109 return workflowGetResponse .getResults ();
111110 }
112111
113- public List <StateCompletionOutput > GetComplexWorkflowResultWithLongWait (final String workflowId ) {
114- return GetComplexWorkflowResultWithLongWait (workflowId , "" );
112+ public List <StateCompletionOutput > GetComplexWorkflowResultWithWait (final String workflowId ) {
113+ return GetComplexWorkflowResultWithWait (workflowId , "" );
115114 }
116115 public void SignalWorkflow (
117116 final Class <? extends Workflow > workflowClass ,
@@ -120,17 +119,20 @@ public void SignalWorkflow(
120119 final String signalChannelName ,
121120 final Object signalValue ) {
122121 final String wfType = workflowClass .getSimpleName ();
123- if (registry .getSignalChannelNameToSignalTypeMap (wfType ) == null ) {
124- throw new RuntimeException (String .format ("Workflow %s doesn't have any registered signal channels" , wfType ));
125- }
126122
127123 Map <String , Class <?>> nameToTypeMap = registry .getSignalChannelNameToSignalTypeMap (wfType );
124+ if (nameToTypeMap == null ) {
125+ throw new IllegalArgumentException (
126+ String .format ("Workflow %s is not registered" , wfType )
127+ );
128+ }
129+
128130 if (!nameToTypeMap .containsKey (signalChannelName )) {
129- throw new RuntimeException (String .format ("Workflow %s doesn't have signal %s" , wfType , signalChannelName ));
131+ throw new IllegalArgumentException (String .format ("Workflow %s doesn't have signal %s" , wfType , signalChannelName ));
130132 }
131133 Class <?> signalType = nameToTypeMap .get (signalChannelName );
132134 if (!signalType .isInstance (signalValue )) {
133- throw new RuntimeException (String .format ("Signal value is not of type %s" , signalType .getName ()));
135+ throw new IllegalArgumentException (String .format ("Signal value is not of type %s" , signalType .getName ()));
134136 }
135137
136138 defaultApi .apiV1WorkflowSignalPost (new WorkflowSignalRequest ()
@@ -178,31 +180,38 @@ public String ResetWorkflow(
178180 return resp .getWorkflowRunId ();
179181 }
180182
181- public Map <String , Object > queryWorkflow (
183+ public Map <String , Object > getWorkflowQueryAttributes (
184+ final Class <? extends Workflow > workflowClass ,
185+ final String workflowId ,
186+ final String workflowRunId ,
187+ List <String > attributeKeys ) {
188+ if (attributeKeys == null || attributeKeys .isEmpty ()) {
189+ throw new IllegalArgumentException ("attributeKeys must contain at least one entry, or use getAllQueryAttributes API to get all" );
190+ }
191+ return doGetWorkflowQueryAttributes (workflowClass , workflowId , workflowRunId , attributeKeys );
192+ }
193+
194+ private Map <String , Object > doGetWorkflowQueryAttributes (
182195 final Class <? extends Workflow > workflowClass ,
183196 final String workflowId ,
184197 final String workflowRunId ,
185198 List <String > attributeKeys ) {
186199 final String wfType = workflowClass .getSimpleName ();
187- if (registry .getWorkflow (wfType ) == null ) {
188- throw new RuntimeException (
200+
201+ Map <String , Class <?>> queryAttributeKeyToTypeMap = registry .getQueryAttributeKeyToTypeMap (wfType );
202+ if (queryAttributeKeyToTypeMap == null ) {
203+ throw new IllegalArgumentException (
189204 String .format ("Workflow %s is not registered" , wfType )
190205 );
191206 }
192- if (registry .getQueryAttributeKeyToTypeMap (wfType ) == null ) {
193- throw new RuntimeException (
194- String .format ("Workflow %s doesn't have any registered query attribute" , wfType )
195- );
196- }
197207
198- Map <String , Class <?>> queryAttributeKeyToTypeMap = registry .getQueryAttributeKeyToTypeMap (wfType );
199208 // if attribute keys is null or empty, iwf server will return all query attributes
200209 if (attributeKeys != null && !attributeKeys .isEmpty ()) {
201210 List <String > nonExistingQueryAttributeList = attributeKeys .stream ()
202211 .filter (s -> !queryAttributeKeyToTypeMap .containsKey (s ))
203- .toList ();
212+ .collect ( Collectors . toList () );
204213 if (!nonExistingQueryAttributeList .isEmpty ()) {
205- throw new RuntimeException (
214+ throw new IllegalArgumentException (
206215 String .format (
207216 "Query attributes not registered: %s" ,
208217 String .join (", " , nonExistingQueryAttributeList )
@@ -211,15 +220,15 @@ public Map<String, Object> queryWorkflow(
211220 }
212221 }
213222
214- WorkflowQueryResponse response = defaultApi .apiV1WorkflowQueryPost (
215- new WorkflowQueryRequest ()
223+ WorkflowGetQueryAttributesResponse response = defaultApi .apiV1WorkflowQueryattributesGetPost (
224+ new WorkflowGetQueryAttributesRequest ()
216225 .workflowId (workflowId )
217226 .workflowRunId (workflowRunId )
218227 .attributeKeys (attributeKeys )
219228 );
220229
221230 if (response .getQueryAttributes () == null ) {
222- throw new RuntimeException ("query attributes not returned" );
231+ throw new InternalServiceException ("query attributes not returned" );
223232 }
224233 Map <String , Object > result = new HashMap <>();
225234 for (KeyValue keyValue : response .getQueryAttributes ()) {
@@ -233,10 +242,10 @@ public Map<String, Object> queryWorkflow(
233242 return result ;
234243 }
235244
236- public Map <String , Object > queryAllQueryAttributes (
245+ public Map <String , Object > getAllQueryAttributes (
237246 final Class <? extends Workflow > workflowClass ,
238247 final String workflowId ,
239248 final String workflowRunId ) {
240- return queryWorkflow (workflowClass , workflowId , workflowRunId , null );
249+ return doGetWorkflowQueryAttributes (workflowClass , workflowId , workflowRunId , null );
241250 }
242251}
0 commit comments