
com.baidubce.services.moladb.model.PutRequest Maven / Gradle / Ivy
/*
* Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.baidubce.services.moladb.model;
import java.util.Map;
import java.util.HashMap;
import com.baidubce.services.moladb.MolaDbConstants;
import com.baidubce.util.JsonUtils;
/**
* Represents the input to perform a PutItem operation on an item.
*/
public class PutRequest implements WriteRequest {
private Map item;
/**
* Constructs a new PutRequest object.
* Callers should use the setter or fluent setter (with...) methods to
* initialize any additional object members.
*/
public PutRequest() {
}
/**
* Get an item from this request.
*
* @return A map container of attribute name/value pairs.
*/
public Map getItem() {
return item;
}
/**
* Set an item information with a put item request.
*
* @param item An container of attribute name/value pairs. Only the
* primary key attributes are required; you can optionally provide other
* attribute name-value pairs for the item. You must provide all of
* the attributes for the primary key. For example, with a hash type
* primary key, you only need to provide the hash attribute. For a
* hash-and-range type primary key, you must provide both the hash
* attribute and the range attribute.
If you specify any attributes
* that are part of an key, then the data types for those
* attributes must match those of the schema in the table's attribute
* definition.
*/
public void setItem(Map item) {
this.item = item;
}
/**
* Set an item information with a put item request.
*
* @param item An container of attribute name/value pairs. Only the
* primary key attributes are required; you can optionally provide other
* attribute name-value pairs for the item. You must provide all of
* the attributes for the primary key. For example, with a hash type
* primary key, you only need to provide the hash attribute. For a
* hash-and-range type primary key, you must provide both the hash
* attribute and the range attribute.
If you specify any attributes
* that are part of an key, then the data types for those
* attributes must match those of the schema in the table's attribute
* definition.
* @return Returns a reference to this object so that method calls can be chained together.
*/
public PutRequest withItem(Map item) {
this.setItem(item);
return this;
}
/**
* Return a string representation of the object.
*
* @return A string representation of the object.
*/
public String toString() {
return JsonUtils.toJsonString(this.toJsonObj());
}
protected Map toJsonObj() {
Map rootObj = new HashMap();
Map itemJson = this.marshaller(this.item);
rootObj.put(MolaDbConstants.JSON_ITEM, itemJson);
return rootObj;
}
protected Map marshaller(Map item) {
Map obj = new HashMap();
for (Map.Entry entry : item.entrySet()) {
obj.put(entry.getKey(), entry.getValue().toJsonObj());
}
return obj;
}
}