001/* 002 * Copyright 2024 Vonage 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.vonage.client.proactiveconnect; 017 018import com.vonage.client.QueryParamsRequest; 019import java.time.Instant; 020import java.time.temporal.ChronoUnit; 021import java.util.LinkedHashMap; 022import java.util.Map; 023import java.util.UUID; 024 025/** 026 * Filter options for finding events using {@link ProactiveConnectClient#listEvents(ListEventsFilter)}. 027 */ 028public class ListEventsFilter implements QueryParamsRequest { 029 private final SortOrder order; 030 private final UUID runId, runItemId, invocationId, actionId, traceId; 031 private final String recipientId, sourceContext; 032 private final SourceType sourceType; 033 private final Instant startDate, endDate; 034 035 ListEventsFilter(Builder builder) { 036 order = builder.order; 037 runId = builder.runId != null ? UUID.fromString(builder.runId) : null; 038 runItemId = builder.runItemId != null ? UUID.fromString(builder.runItemId) : null; 039 invocationId = builder.invocationId != null ? UUID.fromString(builder.invocationId) : null; 040 actionId = builder.actionId != null ? UUID.fromString(builder.actionId) : null; 041 traceId = builder.traceId != null ? UUID.fromString(builder.traceId) : null; 042 recipientId = builder.recipientId; 043 sourceContext = builder.sourceContext; 044 sourceType = builder.sourceType; 045 startDate = builder.startDate; 046 if ((endDate = builder.endDate) != null && startDate != null && startDate.isAfter(endDate)) { 047 throw new IllegalStateException("Start date cannot be later than end date."); 048 } 049 } 050 051 @Override 052 public Map<String, String> makeParams() { 053 Map<String, String> params = new LinkedHashMap<>(); 054 params.put("page", "1"); 055 params.put("page_size", "1000"); 056 if (order != null) { 057 params.put("order", order.toString()); 058 } 059 if (runId != null) { 060 params.put("run_id", runId.toString()); 061 } 062 if (runItemId != null) { 063 params.put("run_item_id", runItemId.toString()); 064 } 065 if (invocationId != null) { 066 params.put("invocation_id", invocationId.toString()); 067 } 068 if (actionId != null) { 069 params.put("action_id", actionId.toString()); 070 } 071 if (traceId != null) { 072 params.put("trace_id", traceId.toString()); 073 } 074 if (recipientId != null) { 075 params.put("recipient_id", recipientId); 076 } 077 if (sourceContext != null) { 078 params.put("src_ctx", sourceContext); 079 } 080 if (sourceType != null) { 081 params.put("src_type", sourceType.toString()); 082 } 083 if (startDate != null) { 084 params.put("date_start", startDate.truncatedTo(ChronoUnit.SECONDS).toString()); 085 } 086 if (endDate != null) { 087 params.put("date_end", endDate.truncatedTo(ChronoUnit.SECONDS).toString()); 088 } 089 return params; 090 } 091 092 /** 093 * Sort in either ascending or descending order. 094 * 095 * @return The sort order as an enum, or {@code null} if unspecified. 096 */ 097 public SortOrder getOrder() { 098 return order; 099 } 100 101 /** 102 * Run ID to filter by. 103 * 104 * @return The run ID, or {@code null} if unspecified. 105 */ 106 public UUID getRunId() { 107 return runId; 108 } 109 110 /** 111 * Run item ID to filter by. 112 * 113 * @return The run item ID, or {@code null} if unspecified. 114 */ 115 public UUID getRunItemId() { 116 return runItemId; 117 } 118 119 /** 120 * Invocation ID to filter by. 121 * 122 * @return The invocation ID, or {@code null} if unspecified. 123 */ 124 public UUID getInvocationId() { 125 return invocationId; 126 } 127 128 /** 129 * Action ID to filter by. 130 * 131 * @return The action ID, or {@code null} if unspecified. 132 */ 133 public UUID getActionId() { 134 return actionId; 135 } 136 137 /** 138 * Trace ID to filter by. 139 * 140 * @return The trace ID, or {@code null} if unspecified. 141 */ 142 public UUID getTraceId() { 143 return traceId; 144 } 145 146 /** 147 * Recipient ID to filter by. 148 * 149 * @return The recipient ID, or {@code null} if unspecified. 150 */ 151 public String getRecipientId() { 152 return recipientId; 153 } 154 155 /** 156 * The name of the segment / matcher the item / event to filter by (exact string). 157 * 158 * @return The source context string, or {@code null} if unspecified. 159 */ 160 public String getSourceContext() { 161 return sourceContext; 162 } 163 164 /** 165 * Source type to filter by. 166 * 167 * @return The source type as an enum, or {@code null} if unspecified. 168 */ 169 public SourceType getSourceType() { 170 return sourceType; 171 } 172 173 /** 174 * ISO-8601 formatted date for when to begin events filter. 175 * 176 * @return The start date for events as an Instant, or {@code null} if unspecified. 177 */ 178 public Instant getStartDate() { 179 return startDate; 180 } 181 182 /** 183 * ISO-8601 formatted date for when to end events filter. 184 * 185 * @return The end date for events as an Instant, or {@code null} if unspecified. 186 */ 187 public Instant getEndDate() { 188 return endDate; 189 } 190 191 /** 192 * Entry point for constructing an instance of this class. 193 * 194 * @return A new Builder. 195 */ 196 public static Builder builder() { 197 return new Builder(); 198 } 199 200 /** 201 * Builder for specifying options to filter events by. All fields are optional. 202 */ 203 public static class Builder { 204 private SortOrder order; 205 private String runId, runItemId, invocationId, actionId, traceId, recipientId, sourceContext; 206 private SourceType sourceType; 207 private Instant startDate, endDate; 208 209 Builder() {} 210 211 /** 212 * Sort in either ascending or descending order. 213 * 214 * @param order The sort order as an enum. 215 * 216 * @return This builder. 217 */ 218 public Builder order(SortOrder order) { 219 this.order = order; 220 return this; 221 } 222 223 /** 224 * Run ID to filter by. 225 * 226 * @param runId The run ID. 227 * 228 * @return This builder. 229 */ 230 public Builder runId(String runId) { 231 this.runId = runId; 232 return this; 233 } 234 235 /** 236 * Run item ID to filter by. 237 * 238 * @param runItemId The run item ID. 239 * 240 * @return This builder. 241 */ 242 public Builder runItemId(String runItemId) { 243 this.runItemId = runItemId; 244 return this; 245 } 246 247 /** 248 * Invocation ID to filter by. 249 * 250 * @param invocationId The invocation ID. 251 * 252 * @return This builder. 253 */ 254 public Builder invocationId(String invocationId) { 255 this.invocationId = invocationId; 256 return this; 257 } 258 259 /** 260 * Action ID to filter by. 261 * 262 * @param actionId The action ID. 263 * 264 * @return This builder. 265 */ 266 public Builder actionId(String actionId) { 267 this.actionId = actionId; 268 return this; 269 } 270 271 /** 272 * Trace ID to filter by. 273 * 274 * @param traceId The trace ID. 275 * 276 * @return This builder. 277 */ 278 public Builder traceId(String traceId) { 279 this.traceId = traceId; 280 return this; 281 } 282 283 /** 284 * Recipient ID to filter by. 285 * 286 * @param recipientId The recipient ID. 287 * 288 * @return This builder. 289 */ 290 public Builder recipientId(String recipientId) { 291 this.recipientId = recipientId; 292 return this; 293 } 294 295 /** 296 * The name of the segment / matcher the item / event to filter by (exact string). 297 * 298 * @param sourceContext The source context string. 299 * 300 * @return This builder. 301 */ 302 public Builder sourceContext(String sourceContext) { 303 this.sourceContext = sourceContext; 304 return this; 305 } 306 307 /** 308 * Source type to filter by. 309 * 310 * @param sourceType The source type as an enum. 311 * 312 * @return This builder. 313 */ 314 public Builder sourceType(SourceType sourceType) { 315 this.sourceType = sourceType; 316 return this; 317 } 318 319 /** 320 * ISO-8601 formatted date for when to begin events filter. 321 * 322 * @param startDate The start date for events as an Instant. 323 * 324 * @return This builder. 325 */ 326 public Builder startDate(Instant startDate) { 327 this.startDate = startDate; 328 return this; 329 } 330 331 /** 332 * ISO-8601 formatted date for when to end events filter. 333 * 334 * @param endDate The end date for events as an Instant. 335 * 336 * @return This builder. 337 */ 338 public Builder endDate(Instant endDate) { 339 this.endDate = endDate; 340 return this; 341 } 342 343 /** 344 * Builds the {@linkplain ListEventsFilter}. 345 * 346 * @return An instance of ListEventsFilter, populated with all fields from this builder. 347 */ 348 public ListEventsFilter build() { 349 return new ListEventsFilter(this); 350 } 351 } 352}