package com.k_int.ca.action;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.k_int.aggregator.harvest.service.OAIHarvestService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;


public  class SuspendOrHarvestInstructionAction extends ActionSupport {
  private static final long serialVersionUID = 1L;
  private long instructionId;
  private String action = "none";
  private OAIHarvestService service;
 
  private static Log log = LogFactory.getLog(SuspendOrHarvestInstructionAction.class);
 
  public void setOaiService(OAIHarvestService service)
  {
    this.service = service;
  }
  
  
  public String execute() throws Exception {
    log.debug("Suspend or reharvest Harvest Instruction. instructionId: "  + instructionId);  
    this.clearActionErrors();
    
    if ("reharvest".equals(action) && instructionId > 0){
      service.reharvestInstruction(instructionId, false);
    }
    else if ("reharvestAll".equals(action) && instructionId > 0){
      service.reharvestInstruction(instructionId, true);
    }
    else if ("suspend".equals(action) && instructionId > 0){
    	service.suspendInstruction(instructionId);
    }
    
    return Action.SUCCESS;
  }

  
  public void setInstructionId(String instructionId) {
	  if (instructionId!=null && !"".equals(instructionId)) {
		  try
		  {
			  this.instructionId = Long.parseLong(instructionId);
		  }
		  catch (Exception e){
			  log.debug("Exception thrown when setting the instruction Id to suspend or reharvest: " + e.getMessage());
		  }
	  }
  }
  
  
  public void setAction(String action) {
    this.action = action;
  }
  
  
  public String getAction() {
    return action;
  }
 
}
  