io.personium.client.EntitySet.java Source code

Java tutorial

Introduction

Here is the source code for io.personium.client.EntitySet.java

Source

/**
 * Personium
 * Copyright 2014 - 2017 FUJITSU LIMITED
 *
 * 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 io.personium.client;

import java.util.HashMap;

import org.json.simple.JSONObject;

import io.personium.client.http.PersoniumResponse;
import io.personium.client.http.IRestAdapter;
import io.personium.client.http.RestAdapter;
import io.personium.client.http.RestAdapterFactory;
import io.personium.client.utils.Utils;

///**
// * OData???/?????.
// */
/**
 * It creates a new object of EntitySet. This is the abstract class for performing the merge functions.
 */
public class EntitySet extends ODataManager {

    // /**
    // * .
    // * @param as 
    // */
    /**
     * This is the parameterized constructor with one argument and calls its parent constructor internally.
     * @param as Accessor
     */
    public EntitySet(Accessor as) {
        super(as);
    }

    // /**
    // * .
    // * @param as 
    // * @param col DAV
    // */
    /**
     * This is the parameterized constructor with two arguments and calls its parent constructor internally.
     * @param as Accessor
     * @param col DcCollection
     */
    public EntitySet(Accessor as, PersoniumCollection col) {
        super(as, col);
    }

    // /**
    // * .
    // * @param as 
    // * @param col DAV
    // * @param name EntitySet??
    // */
    /**
     * This is the parameterized constructor with three arguments and calls its parent constructor internally.
     * @param as Accessor
     * @param col DcCollection
     * @param name EntitySet Name
     */
    public EntitySet(Accessor as, PersoniumCollection col, String name) {
        super(as, col, name);
    }

    // /**
    // * .
    // * @param id ?Odata?ID
    // * @param body ?
    // * @param etag Etag
    // * @throws DaoException DAO
    // */
    /**
     * This method is used for the partial update of user data.
     * @param id ID value of the data
     * @param body Request body
     * @param etag Etag value
     * @throws DaoException Exception thrown
     */
    private void internalMerge(String id, HashMap<String, Object> body, String etag) throws DaoException {
        String url = this.getUrl() + "('" + id + "')";
        IRestAdapter rest = RestAdapterFactory.create(accessor);
        rest.merge(url, JSONObject.toJSONString(body), etag, RestAdapter.CONTENT_TYPE_JSON);
    }

    // /**
    // * .
    // * @param id ??ID
    // * @param body PUT?
    // * @param etag ETag
    // * @throws DaoException DAO
    // */
    /**
     * This method is used for the partial update of user data.
     * @param id ID value of the data
     * @param body Request body to be PUT
     * @param etag ETag value
     * @throws DaoException Exception thrown
     */
    public void merge(String id, HashMap<String, Object> body, String etag) throws DaoException {
        this.internalMerge(id, body, etag);
    }

    // /**
    // * .
    // * @param id ??ID
    // * @param body PUT?
    // * @param etag ETag
    // * @return ??Entity
    // * @throws DaoException DAO
    // */
    /**
     * This method is used for the partial update of user data.
     * @param id ID value of the data
     * @param body Request body to be PUT
     * @param etag ETag value
     * @return Partially updated Entity object
     * @throws DaoException Exception thrown
     */
    @SuppressWarnings("unchecked")
    public Entity mergeAsEntity(String id, HashMap<String, Object> body, String etag) throws DaoException {
        String url = this.getUrl() + "('" + id + "')";
        IRestAdapter rest = RestAdapterFactory.create(accessor);
        PersoniumResponse res = rest.merge(url, JSONObject.toJSONString(body), etag, RestAdapter.CONTENT_TYPE_JSON);
        JSONObject json = new JSONObject();
        json.putAll(body);
        Entity entity = new Entity(accessor, json);
        entity.setResHeaders(res.getHeaderList());
        return entity;
    }

    // /**
    // * ?.
    // * @param body 
    // * @return ???Entity
    // * @throws DaoException DAO
    // */
    /**
     * This method is used to create the user data.
     * @param body Request body
     * @return Entity object that is created
     * @throws DaoException Exception thrown
     */
    public Entity createAsEntity(HashMap<String, Object> body) throws DaoException {
        String url = this.getUrl();
        IRestAdapter rest = RestAdapterFactory.create(accessor);
        PersoniumResponse res = rest.post(url, JSONObject.toJSONString(body), RestAdapter.CONTENT_TYPE_JSON);
        JSONObject resbody = (JSONObject) ((JSONObject) res.bodyAsJson().get("d")).get("results");
        Entity entity = new Entity(accessor, (JSONObject) resbody);
        entity.setBody(resbody);
        entity.setResHeaders(res.getHeaderList());
        return entity;
    }

    // /**
    // * .
    // * @param key ??id
    // * @param body 
    // * @param etag ???etag
    // * @return ???Entity
    // * @throws DaoException DAO
    // */
    /**
     * This method is used to update the user data.
     * @param key ID value of the user data
     * @param body Request body
     * @param etag Etag value
     * @return Entity object that is updated
     * @throws DaoException Exception thrown
     */
    @SuppressWarnings("unchecked")
    public Entity updateAsEntity(String key, HashMap<String, Object> body, String etag) throws DaoException {
        String escapekey = "'" + Utils.escapeURI(key) + "'";
        String url = this.getUrl() + "(" + escapekey + ")";
        IRestAdapter rest = RestAdapterFactory.create(accessor);
        PersoniumResponse res = rest.put(url, JSONObject.toJSONString(body), etag, RestAdapter.CONTENT_TYPE_JSON);
        JSONObject json = new JSONObject();
        json.putAll(body);
        Entity entity = new Entity(accessor, json);
        entity.setResHeaders(res.getHeaderList());
        return entity;
    }

    // /**
    // * .
    // * @param id ??id
    // * @return ???Entity
    // * @throws DaoException DAO
    // */
    /**
     * This method is used to retrieve the specified user data.
     * @param id ID value of the user data
     * @return Entity object
     * @throws DaoException Exception thrown
     */
    public Entity retrieveAsEntity(String id) throws DaoException {
        PersoniumResponse res = internalRetrieveAsPersoniumResponse(id);
        JSONObject json = (JSONObject) ((JSONObject) res.bodyAsJson().get("d")).get("results");
        Entity entity = new Entity(accessor, json);
        entity.setResHeaders(res.getHeaderList());
        return entity;
    }
}