001/*
002 * Copyright 2020 Global Biodiversity Information Facility (GBIF)
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 org.gbif.api.model.common.messaging;
017
018import java.util.Collections;
019import java.util.HashMap;
020import java.util.Map;
021import java.util.Map.Entry;
022
023/**
024 * Class that represents a generic invocation of operation.
025 * This class encapsulates the parameters required to perform the operation by the request consumer.
026 *
027 * @param <T> content type of the request object
028 */
029public class Request<T> {
030
031  private OperationType operationType;
032
033  private Map<String, String> context;
034
035  private T content;
036
037  /**
038   * @return the content
039   */
040  public T getContent() {
041    return content;
042  }
043
044  /**
045   * Context contains additional request parameters.
046   *
047   * @return the context
048   */
049  public Map<String, String> getContext() {
050    return context;
051  }
052
053  /**
054   * @param context the context to set
055   */
056  public void setContext(Map<String, String> context) {
057    this.context = context;
058  }
059
060  /**
061   * Type of the operation to be performed.
062   * This field is optional.
063   *
064   * @return the operationType
065   */
066  protected OperationType getOperationType() {
067    return operationType;
068  }
069
070  /**
071   * @param operationType the operationType to set
072   */
073  protected void setOperationType(OperationType operationType) {
074    this.operationType = operationType;
075  }
076
077  /**
078   * @param content the content to set
079   */
080  public void setParameter(T content) {
081    this.content = content;
082  }
083
084  /**
085   * CRUD operation types.
086   */
087  public enum OperationType {
088
089    CREATE, RETRIEVE, UPDATE, DELETE, SELECT
090
091  }
092
093  /**
094   * Builder class for {@link Request} instances.
095   */
096  public static class Builder<T> {
097
098    private OperationType operationType;
099    private Map<String, String> context;
100    private T content;
101
102    public Request<T> build() {
103      Request<T> request = new Request<T>();
104      request.operationType = operationType;
105      request.content = content;
106      request.context = Collections.unmodifiableMap(context);
107      return request;
108    }
109
110    public Builder<T> content(T content) {
111      this.content = content;
112      context = new HashMap<>();
113      return this;
114    }
115
116    public Builder<T> context(Map<String, String> context) {
117      this.context = context;
118      return this;
119    }
120
121    public Builder<T> contextParam(Entry<String, String> entry) {
122      context.entrySet().add(entry);
123      return this;
124    }
125
126    public Builder<T> contextParam(String key, String value) {
127      context.put(key, value);
128      return this;
129    }
130
131    public Builder<T> operationType(OperationType operationType) {
132      this.operationType = operationType;
133      return this;
134    }
135
136  }
137
138}