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

Source Code of com.dodo.blog.ui.page.admin.ArticleEdit

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

import com.dodo.blog.model.Article;
import com.dodo.blog.model.Category;
import com.dodo.blog.model.Role;
import com.dodo.blog.model.Tag;
import com.dodo.blog.request.BasicRequest;
import com.dodo.blog.server.ArticleService;
import com.dodo.blog.server.CategoryService;
import com.dodo.blog.server.TagService;
import com.dodo.blog.ui.ajax.Response;
import com.dodo.blog.ui.ajax.annotation.GetModel;
import com.dodo.blog.ui.ajax.annotation.SetModel;
import com.dodo.blog.ui.authorization.Authorized;
import com.dodo.blog.ui.component.composite.FieldSet;
import com.dodo.blog.ui.component.composite.FormRow;
import com.dodo.blog.ui.component.container.Form;
import com.dodo.blog.ui.component.input.AjaxSubmitButton;
import com.dodo.blog.ui.component.input.Anchor;
import com.dodo.blog.ui.component.input.EntityChoiceRenderer;
import com.dodo.blog.ui.component.input.Select;
import com.dodo.blog.ui.component.input.TextArea;
import com.dodo.blog.ui.component.input.TextBox;
import com.dodo.blog.ui.page.AbstractPage;

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

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

    public static final String ARTICLE_ID = "id";

    private static ArticleService articleService;

    private static CategoryService categoryService;

    private static TagService tagService;

    private static final String ARTICLE_FORM = "article-form";

    @SetModel( name = ARTICLE_FORM )
    public Response setModel( Article article )
    {
        articleService.saveArticle( article );

        return Response.redirect( ArticleList.class );
    }

    @GetModel( name = ARTICLE_FORM )
    public Article getModel()
    {
        Long id = getLongParameter( ARTICLE_ID );

        Article article;
        if ( id != null )
        {
            article = articleService.getArticleById( id );
        }
        else
        {
            article = new Article();
        }

        return article;
    }

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

        Form form = new Form( ARTICLE_FORM, Article.class );
        add( form );

        FieldSet fieldSet = new FieldSet( localize( "title.articleEdit" ) );
        form.add( fieldSet );

        fieldSet.add( new FormRow( localize( "label.name" ), new TextBox( "title" ).setAutoFocus( true ) ) );
        fieldSet.add( new FormRow( localize( "label.content" ), new TextArea( "content" ) ) );
        fieldSet.add( new FormRow( localize( "label.category" ), new Select<Category>( "category", getCategories(), new EntityChoiceRenderer(), true ) ) );

        for ( int i = 1; i <= 5; i++ )
        {
            fieldSet.add( new FormRow( localize( "label.tag" ) + "_" + i, new Select<Tag>( "tag_" + i, getTags(), new EntityChoiceRenderer(), true ) ) );
        }

        form.add( new AjaxSubmitButton( "save", localize( "button.save" ) ) );
        form.add( new Anchor( "cancel", localize( "button.cancel" ), ArticleList.class ) );
    }

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

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

    @Inject
    public void setTagService( TagService tagService )
    {
        ArticleEdit.tagService = tagService;
    }

    private List<Category> getCategories()
    {
        return categoryService.getCategoryList( new BasicRequest() );
    }

    private List<Tag> getTags()
    {
        return tagService.getTagList( new BasicRequest() );
    }
}
TOP

Related Classes of com.dodo.blog.ui.page.admin.ArticleEdit

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.