io.personium.client.MetadataLinkManager.java Source code

Java tutorial

Introduction

Here is the source code for io.personium.client.MetadataLinkManager.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 org.json.simple.JSONObject;

import io.personium.client.http.IRestAdapter;
import io.personium.client.http.RestAdapter;
import io.personium.client.http.RestAdapterFactory;

///**
// * OData???/?????.
// */
/**
 * It creates a new object of MetadataLinkManager. This class performs link/unlink operations on metadata.
 */
public class MetadataLinkManager {

    // /** . */
    /** Reference to Accessor. */
    Accessor accessor;

    // /** . */
    /** Link subject. */
    AbstractODataContext context;

    // /**
    // * .
    // * @param as 
    // * @param cx 
    // */
    /**
     * This is the parameterized constructor with two arguments initializing the class variables.
     * @param as Accessor
     * @param cx AbstractODataContext
     */
    public MetadataLinkManager(Accessor as, AbstractODataContext cx) {
        this.accessor = as;
        this.context = cx;
    }

    // /**
    // * .
    // * @param cx ?
    // * @throws DaoException DAO
    // */
    /**
     * This method is used to remove the link.
     * @param cx Target object to delete link
     * @throws DaoException Exception thrown
     */
    public void unLink(AbstractODataContext cx) throws DaoException {

        String uri = getLinkUrl(cx);

        IRestAdapter rest = RestAdapterFactory.create(accessor);
        rest.del(uri + cx.getKey());
    }

    // /**
    // * ?.
    // * @param cx ??
    // * @throws DaoException DAO
    // */
    /**
     * This method is used to create the link.
     * @param cx Target object to create link
     * @throws DaoException Exception thrown
     */
    @SuppressWarnings("unchecked")
    public void link(AbstractODataContext cx) throws DaoException {
        String uri = getLinkUrl(cx);

        JSONObject body = new JSONObject();
        body.put("uri", cx.getODataLink());

        IRestAdapter rest = RestAdapterFactory.create(accessor);
        rest.post(uri, body.toJSONString(), RestAdapter.CONTENT_TYPE_JSON);
    }

    // /**
    // * $link??url??.
    // * @param cx ?OData
    // * @return ???url
    // */
    /**
     * This method is used to generates a request to the url $ link.
     * @param cx OData target object
     * @return URL value
     */
    private String getLinkUrl(final AbstractODataContext cx) {

        StringBuilder sb = new StringBuilder();
        sb.append(context.getODataLink());
        sb.append("/$links/");
        sb.append("_" + cx.getClassName());
        return sb.toString();
    }

}