/*
*
* Sample program for use with the Product
* Licensed Materials  - Property of IBM
* 5724-I66
* (c) Copyright IBM Corp.  2006, 2009
*   
*/
package com.ibm.wbit.tel.client.jsf.command;

import java.util.List;

import com.ibm.bpc.clientcore.ClientException;
import com.ibm.bpc.clientcore.Command;
import com.ibm.task.api.HumanTaskManagerService;
import com.ibm.wbit.tel.client.jsf.bean.ToDoInstance;
import com.ibm.wbit.tel.client.jsf.infrastructure.CommandException;
import com.ibm.wbit.tel.client.jsf.infrastructure.ServiceFactory;
import com.ibm.wbit.tel.client.jsf.infrastructure.SharedConstants;

/**
 * The ToDoClaim uses the Human Task Manager API to claim a ToDo provided by list open ToDos.
 * Please refer to the Human Task Manager API for more details.
 * The item provider for the ToDo to claim is ToDoMessageHandler. 
 */
public class ToDoClaim implements Command, SharedConstants{

    
    /**
     * Executes the claim command for the ToDo selected in open ToDos list.
     * Exceptions to be shown in commandbar will be thrown as CommandException.
     * 
     * @param items the list of selected items
     * @return next navigation route as defined in faces-config, may be overwritten by action of commandbar
     * @exception CommandException to be shown in commandbar
     * @see com.ibm.bpc.clientcore.Command#execute(java.util.List)
     */
    @SuppressWarnings("unchecked")
	public String execute(List items) throws ClientException {

        if (items != null) {
            HumanTaskManagerService htm;
            //we have only one selected
            Object obj = items.get(0);
            if (obj instanceof ToDoInstance) {
                ToDoInstance toDoInstance = (ToDoInstance) obj;
                try {
                    htm = ServiceFactory.getInstance().getHumanTaskManagerService();
                    htm.claim(toDoInstance.getID());
                } catch (Exception e) {
                    throw new CommandException(NLS_CATALOG, SERVICE_FAILED_TO_CLAIM_HUMAN_TASK, null, e);
                }
            }
        }
        return null;
    }
    
    /**
     * @see com.ibm.bpc.clientcore.Command#isMultiSelectEnabled()
     */
    public boolean isMultiSelectEnabled() {
        return false;
    }

    /** 
     * @see com.ibm.bpc.clientcore.Command#isApplicable(java.util.List)
     */
    @SuppressWarnings("unchecked")
	public boolean[] isApplicable(List arg0) {
        return (new boolean[0]);
    }

    /**
     * @see com.ibm.bpc.clientcore.Command#setContext(java.lang.Object)
     */
    public void setContext(Object arg0) {
    }

}
