package sample.org.itscool.tutorial2;

import java.util.LinkedList;
import java.util.List;
import org.itscool.weber.action.Action;
import org.itscool.weber.action.ActionBucket;

public class DeleteAction extends Action{
    
    public String doExecute(ActionBucket bucket) throws Exception{
        LinkedList messages = (LinkedList)bucket.getAttributeFromSession("messages");
        if(messages == null){
            return "success";
        }
        DeleteData form = (DeleteData)bucket.getForm();
        List idList = form.getId();
        for(int i=0; i<idList.size(); i++){
            String idStr = (String)idList.get(i);
            int id = Integer.parseInt(idStr);
            deleteMessage(messages, id);
        }
        return "success";
    }
    
    public void deleteMessage(LinkedList messages, int id){
        for(int i=0; i<messages.size(); i++){
            AskData ask = (AskData)messages.get(i);
            if( ask.getId() == id ){
                messages.remove(i);
                break;
            }
        }
    }

}
