package gov.nih.nci.rembrandt.web.ajax;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.genepattern.client.GPServer;
import org.genepattern.webservice.AnalysisWebServiceProxy;
import org.genepattern.webservice.JobInfo;
import org.genepattern.webservice.TaskIntegratorProxy;
import org.genepattern.webservice.WebServiceException;

import uk.ltd.getahead.dwr.ExecutionContext;
import gov.nih.nci.caintegrator.application.cache.CacheFactory;
import gov.nih.nci.caintegrator.application.cache.PresentationTierCache;
import gov.nih.nci.caintegrator.service.task.GPTask;
import gov.nih.nci.caintegrator.enumeration.FindingStatus;
import gov.nih.nci.rembrandt.web.struts.action.GPProcessAction;

import java.io.Serializable;

public class GenePatternHelper {
    private static Logger logger = Logger.getLogger(GenePatternHelper.class);
	public GenePatternHelper() {
    } 

	public String checkGPStatus(String sid) {
		//System.out.println("checkGPStatus called...");
		HttpSession session = ExecutionContext.get().getSession();
		String gpserverURL = System.getProperty("gov.nih.nci.caintegrator.gp.server");
		String gpUserId = (String)session.getAttribute("gpUserId");
		String password = System.getProperty("gov.nih.nci.caintegrator.gp.publicuser.password");
		int jobNumber = Integer.parseInt(sid);
        AnalysisWebServiceProxy analysisProxy = null;
		String message  = null;
        JobInfo info = null;
        FindingStatus findingStatus = FindingStatus.Running;
		try {
			analysisProxy = new AnalysisWebServiceProxy(gpserverURL, gpUserId, password, false);
		
			if(analysisProxy != null){
				analysisProxy.setTimeout(Integer.MAX_VALUE);
				info = analysisProxy.checkStatus(jobNumber);
			}
			
		} catch (WebServiceException e1) {
			message  = "error";
			logger.error(e1.getMessage());
		}
		if (info != null && info.getStatus() != null){
			String status = info.getStatus();
			if (status.equalsIgnoreCase("ERROR")){
				message = "error";
				findingStatus = FindingStatus.Error;
			}
			else if (status.equalsIgnoreCase("Finished")) {
				message = "completed";
				findingStatus = FindingStatus.Completed;
			}
		}
		if (findingStatus != FindingStatus.Running){
			PresentationTierCache ptc = CacheFactory.getPresentationTierCache();							
			if(ptc!=null){
		    	GPTask gpTask = (GPTask)ptc.getNonPersistableObjectFromSessionCache(
		    			session.getId(), "latestGpTask");
		    	gpTask.setStatus(findingStatus);
		    	ptc.addNonPersistableToSessionCache(session.getId(), gpTask.getJobId(),(Serializable) gpTask);
    			ptc.removeObjectFromNonPersistableSessionCache(session.getId(), "latestGpTask");
			}
		}
		return message;
	}
}

@@ -653,16 +653,144 @@
 Status;%0A
+import gov.nih.nci.rembrandt.cache.RembrandtPresentationTierCache;%0Aimport gov.nih.nci.rembrandt.web.factory.ApplicationFactory;%0A
 import g
@@ -2338,16 +2338,18 @@
 ng)%7B%0A%09%09%09
+//
 Presenta
@@ -2412,15 +2412,95 @@
 e();
-%09%09%09%09%09%09%09
+%0A%09%09%09RembrandtPresentationTierCache ptc = ApplicationFactory.getPresentationTierCache();
 %0A%09%09%09
@@ -2640,16 +2640,45 @@
 %0A%09%09    %09
+if (gpTask != null)%7B%0A%09%09    %09%09
 gpTask.s
@@ -2709,16 +2709,17 @@
 %0A%09%09    %09
+%09
 ptc.addN
@@ -2805,23 +2805,24 @@
 pTask);%0A
+%09%09
     %09%09
-%09
 ptc.remo
@@ -2894,16 +2894,25 @@
 Task%22);%0A
+%09%09    %09%7D%0A
 %09%09%09%7D%0A%09%09%7D

Check GPTask for null.