Wiki

Imprimir Propiedades
Blog_with_users_extend_CRUD

Extending CRUD


Classes related to Post CRUD are used to manage the administrative console. But also, these classes are extended in order to customize the view and implement business logic required to non registered users can only read posts and add comments.

Firstly, we are going to extend PostListable class to create UsersPostListable class, in which a new method is included to add comments for each post.

@ViewClass(entity = Post.class)
public class UsersPostListable extends PostListable {

    public UsersPostListable(Post iEntity) {
        super(iEntity);
    }

    @ViewAction(layout = "form:/main/bottom", render = ViewConstants.RENDER_BUTTON)
    public void addComment() {

        Comment comment = new Comment();
        CommentInstance commentInstance = new CommentInstance();
        commentInstance.setEntity(comment);
        commentInstance.setPost(getEntity());
        commentInstance.setBackObject(new UsersPostMain());
        ObjectContext.getInstance().show(commentInstance);
    }
}

In CommentInstance class, save method is overrided to save comments in the related post. A new attribute to manage post and its accessor methods is also required.

@Override
public void save() {
    getPost().getComments().add(getEntity());
    ObjectContext.getInstance().getComponent(PersistenceAspect.class).updateObject(getPost());
    super.save();
}

PostMain class is also extended to create UsersPostMain class, in which search function is executed in the constructor to show posts instances at the beginning and UsersPostListable class is used to display them.

public class UsersPostMain extends PostMain {

    protected List<UsersPostListable> usersResult;

    public UsersPostMain() {
        this(null);
        search();
    }

    public UsersPostMain(Object iBackObject) {
        super(iBackObject, UsersPostListable.class);
        usersResult = new ArrayList<UsersPostListable>();
    }
   
    ...
}

In this case, PostMain requires a new constructor to accept the class that will be used as listable. But it is posible to replace the class to be used for creation, read and update.

 

public PostMain(Object iBackObject, Class<? extends ComposedEntity<?>> listClass) {
    super(listClass, PostInstance.class, PostInstance.class, PostInstance.class);
   
    ...
}

To make UsersPostMain class the main page we have to indicate it in CustomApplicationConfiguration class:

public void startUserSession() {
    ObjectContext.getInstance().show(new UsersPostMain());
}

1060 Accesos, 0 Ficheros adjuntos 0 Ficheros adjuntos

  • Comentarios