Package com.dodo.blog.ui.page.admin

Source Code of com.dodo.blog.ui.page.admin.ArticleList$ArticleDataSource

package com.dodo.blog.ui.page.admin;

import com.dodo.blog.model.Article;
import com.dodo.blog.model.Role;
import com.dodo.blog.request.ArticlesRequest;
import com.dodo.blog.server.ArticleService;
import com.dodo.blog.server.CategoryService;
import com.dodo.blog.ui.ajax.Response;
import com.dodo.blog.ui.ajax.annotation.AjaxRequest;
import com.dodo.blog.ui.authorization.Authorized;
import com.dodo.blog.ui.component.input.Anchor;
import com.dodo.blog.ui.component.input.Button;
import com.dodo.blog.ui.component.repeater.IDataSource;
import com.dodo.blog.ui.component.repeater.OrderDirection;
import com.dodo.blog.ui.component.repeater.Table;
import com.dodo.blog.ui.component.repeater.TableHeader;
import com.dodo.blog.ui.page.AbstractPage;
import com.dodo.blog.ui.util.FormatUtil;

import javax.inject.Inject;
import java.util.List;

/**
* @author <a href="mailto:pohorelec@comvai.com">Jozef Pohorelec</a>
*/
@Authorized( roles = {Role.ADMIN} )
public class ArticleList
        extends AbstractPage
{
    private static final long serialVersionUID = 1L;

    private static ArticleService articleService;

    private static CategoryService categoryService;

    private static final String DELETE_ARTICLE_METHOD = "deleteArticle";

    private static final String PUBLISH_ARTICLE_METHOD = "publishArticle";

    private static final String HIDE_ARTICLE_METHOD = "hideArticle";

    @AjaxRequest( name = DELETE_ARTICLE_METHOD )
    @SuppressWarnings( value = "unused" )
    public Response deleteArticle()
    {
        articleService.deleteArticle( getLongParameter( ArticleEdit.ARTICLE_ID ) );
        return Response.redirect( ArticleList.class );
    }

    @AjaxRequest( name = PUBLISH_ARTICLE_METHOD )
    @SuppressWarnings( value = "unused" )
    public Response publishArticle()
    {
        articleService.publishArticle( getLongParameter( ArticleEdit.ARTICLE_ID ) );
        return Response.redirect( ArticleList.class );
    }

    @AjaxRequest( name = HIDE_ARTICLE_METHOD )
    @SuppressWarnings( value = "unused" )
    public Response hideArticle()
    {
        articleService.hideArticle( getLongParameter( ArticleEdit.ARTICLE_ID ) );
        return Response.redirect( ArticleList.class );
    }

    @Override
    public void onRender()
    {
        setHeader( localize( "header.articleList" ) );

        Table<Article> table = new Table<Article>( "article-list-table", new ArticleDataSource() )
        {
            private static final long serialVersionUID = 1L;

            @Override
            public TableHeader[] getTableHeaders()
            {
                return new TableHeader[]{
                        new TableHeader( "name", localize( "label.name" ) ),
                        new TableHeader( localize( "label.category" ) ),
                        new TableHeader( "published", localize( "label.published" ) ),
                        new TableHeader( "createdDate", localize( "label.createdDate" ) ),
                        new TableHeader( "updatedDate", localize( "label.updatedDate" ) ),
                        new TableHeader(),
                };
            }

            @Override
            public void populate( Article article )
            {
                Button publish;
                if ( !article.isPublished() )
                {
                    publish = new Button( "publish", article.getId().toString(), localize( "button.publish" ) ).setAjaxMethod( PUBLISH_ARTICLE_METHOD );
                }
                else
                {
                    publish = new Button( "hide", article.getId().toString(), localize( "button.hide" ) ).setAjaxMethod( HIDE_ARTICLE_METHOD );
                }

                addColumn( article.getTitle() );
                addColumn( article.getCategory() != null ? categoryService.getCategoryById( article.getCategory() ).getName() : "" );
                addColumn( article.isPublished().toString() );
                addColumn( article.getCreatedDate(), FormatUtil.DATE_FORMAT_MONTH_YEAR );
                addColumn( article.getUpdatedDate(), FormatUtil.DATE_FORMAT_MONTH_YEAR );
                addColumn(
                        publish,
                        new Anchor( "edit", localize( "button.edit" ), ArticleEdit.class ).addParameter( ArticleEdit.ARTICLE_ID, article.getId() ),
                        new Button( "delete", article.getId().toString(), localize( "button.delete" ) ).setAjaxMethod( DELETE_ARTICLE_METHOD )
                );
            }
        };
        add( table );

        add( new Anchor( "new-article", localize( "button.new" ), ArticleEdit.class ) );
    }

    @Inject
    public void setArticleService( ArticleService articleService )
    {
        ArticleList.articleService = articleService;
    }

    @Inject
    public void setCategoryService( CategoryService categoryService )
    {
        ArticleList.categoryService = categoryService;
    }

    private class ArticleDataSource
            implements IDataSource<Article>
    {
        private static final long serialVersionUID = 1L;

        @Override
        public List<Article> getRows( int firstRow, int maxRows, String orderBy, OrderDirection direction )
        {
            ArticlesRequest request = new ArticlesRequest( firstRow, maxRows );
            request.addSort( orderBy, direction );
            return articleService.getArticles( request );
        }
    }
}
TOP

Related Classes of com.dodo.blog.ui.page.admin.ArticleList$ArticleDataSource

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.