All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ning.api.client.access.Comments Maven / Gradle / Ivy

There is a newer version: 0.5.1
Show newest version
package com.ning.api.client.access;

import com.ning.api.client.NingClientConfig;
import com.ning.api.client.access.impl.DefaultCreator;
import com.ning.api.client.access.impl.DefaultDeleter;
import com.ning.api.client.access.impl.DefaultLister;
import com.ning.api.client.access.impl.PagedListImpl;
import com.ning.api.client.action.Creator;
import com.ning.api.client.action.Deleter;
import com.ning.api.client.action.PagedList;
import com.ning.api.client.http.NingHttpPost;
import com.ning.api.client.http.NingHttpRequest.Param;
import com.ning.api.client.item.*;

public class Comments extends Items
{
    public Comments(NingConnection connection, NingClientConfig config)
    {
        super(connection, config, "Comment", Comment.class, CommentField.class);
    }

    /*
    ///////////////////////////////////////////////////////////////////////
    // Public API: constructing request builders
    ///////////////////////////////////////////////////////////////////////
     */

    public final Deleter deleter(Key id) {
        return new DefaultDeleter(connection, config, endpointForDELETE(), id);
    }

    /**
     * @param attachedTo Item that comments to retrieve are attached to
     */
    public Lister listerForRecent(Key attachedTo, CommentField firstField, CommentField... otherFields) {
        return listerForRecent(attachedTo, new Fields(CommentField.class, firstField, otherFields));
    }

    /**
     * @param attachedTo Item that comments to retrieve are attached to
     */
    public Lister listerForRecent(Key attachedTo, Fields fields) {
        return new Lister(connection, config, endpointForRecent(), fields,
                null, attachedTo);
    }

    public Creator creator(Comment comment) {
        return new CommentCreator(connection, config, endpointForPOST(), comment);
    }
    
    protected static Param attachedToParam(Key attachedTo)
    {
        return new Param("attachedTo", attachedTo.toString());
    }
    
    /*
    ///////////////////////////////////////////////////////////////////////
    // Public API: constructing request builders
    ///////////////////////////////////////////////////////////////////////
     */

    /**
     * Accessor used for fetching sequences of items
     */
    public static class Lister extends DefaultLister
    {
        protected final Key attachedTo;
        
        protected Lister(NingConnection connection, NingClientConfig config, String endpoint,
                Fields fields, Boolean isApproved, Key attachedTo)
        {
            super(connection, config, endpoint, fields, null, null, isApproved);
            this.attachedTo = attachedTo;
        }

        public Lister approved() {
            return new Lister(connection, config, endpoint, fields,
                    Boolean.TRUE, attachedTo);
        }
        
        public Lister unapproved() {
            return new Lister(connection, config, endpoint, fields,
                    Boolean.FALSE, attachedTo);
        }
        
        @Override
        public PagedList list() {
            return new PagedListImpl(connection, config, endpoint,
                    Comment.class, fields, null, null, isApproved,
                    attachedToParam(attachedTo));
        }
    }    

    public static class CommentCreator extends DefaultCreator
    {
        protected final Key> attachedTo;
        protected final String description;
        
        public CommentCreator(NingConnection connection, NingClientConfig config, String endpoint,
                Comment comment)
        {
            super(connection, config, endpoint);
            attachedTo = comment.getAttachedTo();
            description = comment.getDescription();
        }

        @Override
        protected NingHttpPost addCreateParameters(NingHttpPost create)
        {
            if (attachedTo != null) {
                create = create.addFormParameter("attachedTo", attachedTo.toString());
            }
            if (description != null) {
                create = create.addFormParameter("description", description);
            }
            return create;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy