public class RembrandtAsynchronousFindingManagerImpl extends FindingsManager {
  public static final String REMBRANDT_TASK_RESULT = "RembrandtTaskResult:";
  private RembrandtPresentationTierCache presentationTierCache =
      ApplicationFactory.getPresentationTierCache();
  /**
   * Submit query looks at the list of strategies it has available and chooses the correct strategy
   * based on the queryDTO type. It then creates a new Task to be handed by to the user, while it
   * called the execute method of the strategy asynchronously.
   */
  public Task submitQuery(HttpSession session, QueryDTO queryDTO) throws FindingsQueryException {
    Task task =
        new Task(
            REMBRANDT_TASK_RESULT + queryDTO.getQueryName(),
            session.getId(),
            FindingStatus.Running,
            queryDTO);
    task.setQueryDTO(queryDTO);
    RembrandtTaskResult taskResult = new RembrandtTaskResult(task);
    // presentationTierCache.addNonPersistableToSessionCache(taskResult.getTask().getCacheId(),
    //		taskResult.getTask().getId(), taskResult);
    RembrandtAsynchronousFindingStrategy strategy =
        new RembrandtAsynchronousFindingStrategy(taskResult, session);
    strategy.executeQuery();
    return task;
  }
  /**
   * Submit query looks at the list of strategies it has available and chooses the correct strategy
   * based on the queryDTO type. It then creates a new Task to be handed by to the user, while it
   * called the execute method of the strategy asynchronously.
   *
   * @throws UnsupportedEncodingException
   */
  public Task retrieveResultsFromFile(
      String sessionID, String reportName, String userName, HttpSession session)
      throws FindingsQueryException {
    Task task =
        new Task(
            RembrandtAsynchronousFindingManagerImpl.REMBRANDT_TASK_RESULT + reportName,
            sessionID,
            FindingStatus.Retrieving,
            null);
    RembrandtTaskResult taskResult = new RembrandtTaskResult(task);
    taskResult.setReportBeanCacheKey(reportName);
    RembrandtAsynchronousFileRetrivalStrategy strategy =
        new RembrandtAsynchronousFileRetrivalStrategy(taskResult, reportName, userName, session);
    strategy.executeStrategy();
    return task;
  }
  /**
   * This method locates the desired Task by calling chooseStrategy in order to use the correct
   * strategy to retrieve the Task and it status.
   *
   * @param task
   * @return Task
   */
  public Task checkStatus(Task task) {
    SessionBasedFindingStrategy strategy = new RembrandtAsynchronousFindingStrategy();
    TaskResult taskResult = strategy.retrieveTaskResult(task);
    if (taskResult != null) {
      task = taskResult.getTask();
    }
    return task;
  }
  /**
   * This method locates the desired TaskResult by calling chooseStrategy in order to use the
   * correct strategy to retrieve the result.
   *
   * @param task
   * @return TaskResult
   */
  public TaskResult getTaskResult(Task task) {
    SessionBasedFindingStrategy strategy = new RembrandtAsynchronousFindingStrategy();
    TaskResult taskResult = strategy.retrieveTaskResult(task);
    return taskResult;
  }
}
/**
 * The DownloadZipFileAction class is used to get the zip file
 *
 * <p>
 *
 * @author mholck
 * @see org.apache.struts.action.Action
 */
public class DownloadReportFileAction extends ActionSupport
    implements SessionAware, ServletRequestAware {
  private static Logger logger = Logger.getLogger(DownloadReportFileAction.class);
  private RembrandtPresentationTierCache presentationTierCache =
      ApplicationFactory.getPresentationTierCache();
  private String dirPath = System.getProperty("gov.nih.nci.rembrandt.data_directory");
  private MailConfig mailConfigInstance =
      MailConfig.getInstance(ApplicationContext.GOV_NIH_NCI_REMBRANDT_PROPERTIES);

  Map<String, Object> sessionMap;
  HttpServletRequest servletRequest;

  /**
   * execute is called when this action is posted to
   *
   * <p>
   *
   * @param mapping The ActionMapping for this action as configured in struts
   * @param form The ActionForm that posted to this action if any
   * @param request The HttpServletRequest for the current post
   * @param response The HttpServletResponse for the current post
   */
  public String execute() throws Exception {
    String reportName = (String) this.servletRequest.getParameter("reportName");

    if (reportName == null) {
      reportName = (String) this.servletRequest.getSession().getAttribute("emailFileName");
    }
    if (reportName != null) {
      reportName = URLDecoder.decode(reportName, "UTF-8");
    }
    // Check if the user is logged in
    String logged = (String) this.sessionMap.get("logged");
    // if already logged in  and file exists than download file
    if ((logged != null && (logged.equals("yes")))) {

      UserCredentials credentials =
          (UserCredentials) this.sessionMap.get(RembrandtConstants.USER_CREDENTIALS);
      RembrandtAsynchronousFindingManagerImpl asynchronousFindingManagerImpl =
          new RembrandtAsynchronousFindingManagerImpl();
      try {
        asynchronousFindingManagerImpl.retrieveResultsFromFile(
            this.servletRequest.getSession().getId(),
            reportName,
            credentials.getUserName(),
            this.servletRequest.getSession());
      } catch (FindingsQueryException e) {
        logger.error(e.getMessage());
      }
      // Set the forward

      logger.debug("redirecting to download");
      return "viewResults";
    } else // if((logged == null  || !logged.equals("yes")))
    {
      // Set the forward
      this.sessionMap.put("emailFileName", reportName);
      logger.debug("redirecting to login");
      return "registration";
    }
  }

  @Override
  public void setServletRequest(HttpServletRequest arg0) {
    this.servletRequest = arg0;
  }

  @Override
  public void setSession(Map<String, Object> arg0) {
    this.sessionMap = arg0;
  }
}