io.personium.client.EventManager.java Source code

Java tutorial

Introduction

Here is the source code for io.personium.client.EventManager.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.IRestAdapter;
import io.personium.client.http.RestAdapter;
import io.personium.client.http.RestAdapterFactory;

///**
// * Event????.
// */
/**
 * It creates a new object of EventManager. This class performs the CRUD operations for Event object.
 */
public class EventManager {
    // /** . */
    /** Reference to Accessor. */
    Accessor accessor;

    // /**
    // * .
    // * @param as 
    // */
    /**
     * This is the parameterized constructor with one argument initializing the accessor.
     * @param as Accessor
     */
    public EventManager(Accessor as) {
        this.accessor = as.clone();
    }

    // /**
    // * .
    // * @param obj Event
    // * @throws DaoException DAO
    // */
    /**
     * This method is used to register the event using Event object.
     * @param obj Event object
     * @throws DaoException Exception thrown
     */
    @SuppressWarnings("unchecked")
    public void post(Event obj) throws DaoException {
        JSONObject body = new JSONObject();
        body.put("level", obj.getLevel());
        body.put("action", obj.getAction());
        body.put("object", obj.getObject());
        body.put("result", obj.getResult());
        this.post(body);
    }

    // /**
    // * ???.
    // * @param body ?JSON
    // * @throws DaoException DAO
    // */
    /**
     * This method is used to register the event using request body.
     * @param body Request Body
     * @throws DaoException Exception thrown
     */
    public void post(JSONObject body) throws DaoException {
        this.post(body, null);
    }

    // /**
    // * ???.
    // * @param body ?JSON
    // * @param dcRequestKey X-Personium-RequestKey?
    // * @throws DaoException DAO
    // */
    /**
     * This method is used to register the event using request body.
     * @param body Request Body
     * @param dcRequestKey X-Personium-RequestKey header
     * @throws DaoException Exception thrown
     */
    public void post(JSONObject body, String dcRequestKey) throws DaoException {
        String url = this.getEventUrl();
        IRestAdapter rest = RestAdapterFactory.create(accessor);
        HashMap<String, String> header = new HashMap<String, String>();
        if (dcRequestKey != null) {
            header.put("X-Personium-RequestKey", dcRequestKey);
        }
        rest.post(url, header, JSONObject.toJSONString(body), RestAdapter.CONTENT_TYPE_JSON);
    }

    // /**
    // * ???.
    // * @param level 
    // * @param action ?
    // * @param object ?
    // * @param result ??
    // * @throws DaoException DAO
    // */
    /**
     * This method is used to register the event using level, action, object and result.
     * @param level Log Output Level
     * @param action Action Events
     * @param object Object Event
     * @param result Result Event
     * @throws DaoException Exception thrown
     */
    public void post(String level, String action, String object, String result) throws DaoException {
        JSONObject body = makeLogBody(level, action, object, result);
        this.post(body, null);
    }

    // /**
    // * ???.
    // * @param level 
    // * @param action ?
    // * @param object ?
    // * @param result ??
    // * @param dcRequestKey X-Personium-RequestKey?
    // * @throws DaoException DAO
    // */
    /**
     * This method is used to register the event using level, action, object, result and dcRequestKey.
     * @param level Log Output Level
     * @param action Action Events
     * @param object Object Event
     * @param result Result Event
     * @param dcRequestKey X-Personium-RequestKey Header
     * @throws DaoException Exception thrown
     */
    public void post(String level, String action, String object, String result, String dcRequestKey)
            throws DaoException {
        JSONObject body = makeLogBody(level, action, object, result);
        this.post(body, dcRequestKey);
    }

    // /**
    // * ?URL????.
    // * @return URL
    // */
    /**
     * This method generates and returns the Event URL.
     * @return URL value
     */
    protected String getEventUrl() {
        StringBuilder sb = new StringBuilder(this.accessor.getCurrentCell().getUrl());
        sb.append("__event/");
        return sb.toString();
    }

    /**
     * This method creates Log Body in the form of JSONObject.
     * @param level Log Output Level
     * @param action Action Events
     * @param object Object Event
     * @param result Result Event
     * @return
     */
    @SuppressWarnings("unchecked")
    private JSONObject makeLogBody(String level, String action, String object, String result) {
        JSONObject body = new JSONObject();
        body.put("level", level);
        body.put("action", action);
        body.put("object", object);
        body.put("result", result);
        return body;
    }

}