All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.twilio.taskrouter.WorkflowRule Maven / Gradle / Ivy

package com.twilio.taskrouter;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.base.MoreObjects;

import java.io.IOException;
import java.util.List;

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class WorkflowRule extends TaskRouterResource {

    private final String expression;
    private final String friendlyName;
    private final List targets;

    /**
     * Define a workflow rule.
     * @param b workflow rule builder
     * @throws IllegalArgumentException if expression or targets is empty
     */
    private WorkflowRule(Builder b) {
        this.expression = b.expression;
        this.friendlyName = b.friendlyName;
        this.targets = b.targets;
    }

    /**
     * Get the expression for the workflow rule.
     * @return the expression
     */
    public String getExpression() {
        return expression;
    }

    /**
     * Get the friendly name / label for the workflow rule.
     * @return the friendly name
     */
    public String getFriendlyName() {
        return friendlyName;
    }

    /**
     * Get the list of workflow rule targets for this workflow rule.
     * @return list of workflow rule targets
     */
    @JsonIgnore
    public List getWorkflowRuleTargets() {
        return targets;
    }

    /**
     * Return a string representation of this workflow rule target.
     */
    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
            .add("expression", expression)
            .add("friendlyName", friendlyName)
            .add("targets", targets)
            .toString();
    }

    /**
     * Converts a JSON workflow configuration to a workflow rule object.
     *
     * @param json JSON for workflow rule
     * @return a workflow rule target object
     * @throws IOException if unable to create object
     */
    public static WorkflowRule fromJson(String json) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(json, Builder.class).build();
    }

    public static class Builder {

        private String expression;
        private String friendlyName;
        private List targets;

        @JsonCreator
        private Builder(
            @JsonProperty("expression") String expression,
            @JsonProperty("filter_friendly_name") String filterFriendlyName,
            @JsonProperty("friendly_name") String friendlyName,
            @JsonProperty("targets") List targets
        ) {
            this.expression = expression;
            this.friendlyName = filterFriendlyName;
            this.friendlyName = friendlyName;
            this.targets = targets;
        }

        public Builder(String expression, List targets) {
            this.expression = expression;
            this.targets = targets;
        }

        public Builder friendlyName(String friendlyName) {
            this.friendlyName = friendlyName;
            return this;
        }

        public WorkflowRule build() {
            return new WorkflowRule(this);
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy