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