package hero.hook;

import hero.interfaces.BnNodeLocal;
import hero.interfaces.Constants;
import hero.interfaces.UserSessionLocal;
import hero.interfaces.UserSessionLocalHome;
import hero.interfaces.UserSessionUtil;
import hero.util.HeroHookException;

public class DeadlineWait implements NodeHookI {

  public String getMetadata() {
    return Constants.Nd.ONDEADLINE;
  }

  public void beforeStart(Object arg0, BnNodeLocal arg1)
      throws HeroHookException {
  }

  public void afterStart(Object arg0, BnNodeLocal arg1)
      throws HeroHookException {
  }

  public void beforeTerminate(Object arg0, BnNodeLocal arg1)
      throws HeroHookException {
  }

  public void afterTerminate(Object arg0, BnNodeLocal arg1)
      throws HeroHookException {
  }

  public void anticipate(Object arg0, BnNodeLocal arg1)
      throws HeroHookException {
  }

  public void onCancel(Object arg0, BnNodeLocal arg1) throws HeroHookException {
  }

  public void onDeadline(Object obj, BnNodeLocal node)
      throws HeroHookException {

    UserSessionLocal userSession = null;
    
    try {
      // Retrieve textual information
      String projectName = node.getBnProject().getName();
      String nodeName = node.getName();
      
      // Initialize User Session
      UserSessionLocalHome userSessionHome = UserSessionUtil.getLocalHome();
      userSession = userSessionHome.create();
      
      // Start and terminate the Activity
      userSession.startActivity(projectName, nodeName);
      userSession.terminateActivity(projectName, nodeName);
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    finally {
      try {
        userSession.remove();
      }
      catch(Exception ignore) {
      }
    }
  }

  public void onReady(Object arg0, BnNodeLocal arg1) throws HeroHookException {
  }
}
