I don't want you anymore

Users may want to delete an event they've created. The classic "are you sure?" message is handled by a macro and therefore very easy to write. Let's have a look at the template web/event/delete.ftl:

<@edit.form model=event actionName="delete">
   <@show.info>
      Are you sure you want to delete this Event?
   </@show.info>
   <@edit.controls submit="delete" />
</@edit.form>

Simple enough.
Now, let us look at the action code:

public class DeleteEvent extends BasicAction {
    private PObject event;
    private PObject user;
    public DeleteEvent(final Repository repository, final Talos talos) {
        super(repository, talos);
    }
    @Override
    public String execute() throws Exception {
        checkConfirmationId();
        if(isConfirmed()) {
            user = event.get("author");
            event.delete();
            getRepository().commit();
            addFlashMessage(getText("message.Event.delete",
            Arrays.asList((String) event.get("title"))));
            return SUCCESS;
        }
        return INPUT;
    }
    @Override
    public boolean authorize(String subject) throws EntityNotFoundException,
      TalosHibernateException {
        return true;
    }
    public boolean isValid() {
        if(isNull(event)){
            addFieldError("event", "cannot be empty");
            return false;
        }
        return true;
    }
    public PObject getUser() {
        return user;
    }
    public PObject getEvent() {
        return event;
    }
    public void setEvent(PObject event) {
        this.event = event;
    }
}

You see, what happens in the action, is that after the event was loaded, it is checked if a confirmationId was set. This is what the macro on the template does: it shows a request, if the user really wants to delete the event and adds a hidden id to that request. So if the id is present in the action, we know that the user really wants to delete the event. Though we do this.

Business as usual

After we delete the event, we redirect the browser to the action that shows the event's author:

<action name="delete" class="org.mumtutorial.action.event.DeleteEvent">
  <result type="redirect">/user/view.action?user=${user.id}</result>
  <result name="input">delete.ftl</result>
</action>

Messages

To finish, write the delete message in config/mum_messages.properties

message.Event.delete=Event \"{0}\" was deleted successfully

Please send us comments, questions, criticism:

Please send us comments, questions, criticism!