io.personium.client.LinkManager.java Source code

Java tutorial

Introduction

Here is the source code for io.personium.client.LinkManager.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 LinkManager. This class performs CRUD operations on link between two cell control objects.
 */
public class LinkManager {

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

    // /** . */
    /** Link Subject. */
    ILinkageResource context;

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

    // /**
    // * ?.
    // * @param cx ??
    // * @throws DaoException DAO
    // */
    /**
     * This method is used to create a link between two cell control objects.
     * @param cx ILinkageResource
     * @throws DaoException Exception thrown
     */
    @SuppressWarnings("unchecked")
    public void link(ILinkageResource cx) throws DaoException {
        String uri = getLinkUrl(cx);

        String linksUri = null;
        if (this.accessor.isBatchMode()) {
            linksUri = ((BatchLinksEntity) this.context).getCollectionUrl() + cx.getODataLink();
        } else {
            linksUri = cx.getODataLink();
        }

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

        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 generate and return the link URL.
     * @param cx ILinkageResource
     * @return Link URL value
     */
    protected String getLinkUrl(final ILinkageResource cx) {
        StringBuilder sb = new StringBuilder();
        sb.append(this.context.makeUrlForLink());
        sb.append("_" + cx.getClassName());
        return sb.toString();
    }
}