package org.modmine.web;

/*
 * Copyright (C) 2002-2009 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.tiles.ComponentContext;
import org.apache.struts.tiles.actions.TilesAction;
import org.intermine.model.bio.Experiment;
import org.intermine.model.bio.Project;
import org.intermine.objectstore.ObjectStore;
import org.intermine.objectstore.query.ConstraintOp;
import org.intermine.objectstore.query.ContainsConstraint;
import org.intermine.objectstore.query.Query;
import org.intermine.objectstore.query.QueryClass;
import org.intermine.objectstore.query.QueryCollectionReference;
import org.intermine.objectstore.query.QueryField;
import org.intermine.objectstore.query.Results;
import org.intermine.objectstore.query.ResultsRow;
import org.intermine.web.logic.Constants;

/**
 * Read experiment and submission details into DisplayExperiments, cache results.
 * @author Richard Smith
 *
 */

public class ExperimentController extends TilesAction 
{
    
    private static List<DisplayExperiment> experimentCache = null;
    
    /**
     * {@inheritDoc}
     */
    public ActionForward execute(@SuppressWarnings("unused")  ComponentContext context,
                                 @SuppressWarnings("unused") ActionMapping mapping,
                                 @SuppressWarnings("unused") ActionForm form,
                                 HttpServletRequest request,
                                 @SuppressWarnings("unused") HttpServletResponse response)
        throws Exception {
        
        if (experimentCache == null) {
            readExperiments(request);
        }
        request.setAttribute("experiments", experimentCache);
        return null;
    }
    
    private void readExperiments(HttpServletRequest request) {
        
        try {
            HttpSession session = request.getSession();
            ObjectStore os =
                (ObjectStore) session.getServletContext().getAttribute(Constants.OBJECTSTORE);
            
            Query q = new Query();  
            QueryClass qcProject = new QueryClass(Project.class);
            QueryField qcName = new QueryField(qcProject, "name");
            q.addFrom(qcProject);
            q.addToSelect(qcProject);
            
            QueryClass qcExperiment = new QueryClass(Experiment.class);
            q.addFrom(qcExperiment);
            q.addToSelect(qcExperiment);

            QueryCollectionReference projExperiments = new QueryCollectionReference(qcProject, "experiments");
            ContainsConstraint cc = new ContainsConstraint(projExperiments, ConstraintOp.CONTAINS, qcExperiment);
            
            q.setConstraint(cc);
            q.addToOrderBy(qcName);
            
            
            Results results = os.execute(q);
            
            experimentCache = new ArrayList<DisplayExperiment>();
            
            Iterator i = results.iterator();
            while (i.hasNext()) {
                ResultsRow row = (ResultsRow) i.next();

                Project project = (Project) row.get(0);
                Experiment experiment = (Experiment) row.get(1);

                DisplayExperiment displayExp = new DisplayExperiment(experiment, project);
                experimentCache.add(displayExp);
            }
        } catch (Exception err) {
            err.printStackTrace();
        }
    }
}

@@ -321,63 +321,8 @@
 */%0A%0A
-import java.util.ArrayList;%0Aimport java.util.Iterator;%0A
 impo
@@ -738,667 +738,136 @@
 ine.
-model.bio.Experiment;%0Aimport org.intermine.model.bio.Project;%0Aimport org.intermine.objectstore.ObjectStore;%0Aimport org.intermine.objectstore.query.ConstraintOp;%0Aimport org.intermine.objectstore.query.ContainsConstraint;%0Aimport org.intermine.objectstore.query.Query;%0Aimport org.intermine.objectstore.query.QueryClass;%0Aimport org.intermine.objectstore.query.QueryCollectionReference;%0Aimport org.intermine.objectstore.query.QueryField;%0Aimport org.intermine.objectstore.query.Results;%0Aimport org.intermine.objectstore.query.ResultsRow;%0Aimport org.intermine.web.logic.Constants;%0A%0A/**%0A * Read experiment and submission details into DisplayExperiments, cache results
+objectstore.ObjectStore;%0Aimport org.intermine.web.logic.session.SessionMethods;%0A%0A/**%0A * Set up modENCODE experiments for display
 .%0A *
@@ -957,81 +957,8 @@
 n %0A%7B
-%0A    %0A    private static List%3CDisplayExperiment%3E experimentCache = null;%0A
     
@@ -1436,288 +1436,8 @@
     
-%0A        if (experimentCache == null) %7B%0A            readExperiments(request);%0A        %7D%0A        request.setAttribute(%22experiments%22, experimentCache);%0A        return null;%0A    %7D%0A    %0A    private void readExperiments(HttpServletRequest request) %7B%0A        %0A        try %7B%0A            
 Http
@@ -1484,20 +1484,16 @@
         
-    
 ObjectSt
@@ -1504,1483 +1504,227 @@
 os =
-%0A                (ObjectStore) session.getServletContext().getAttribute(Constants.OBJECTSTORE);%0A            %0A            Query q = new Query();  %0A            QueryClass qcProject = new QueryClass(Project.class);%0A            QueryField qcName = new QueryField(qcProject, %22name%22);%0A            q.addFrom(qcProject);%0A            q.addToSelect(qcProject);%0A            %0A            QueryClass qcExperiment = new QueryClass(Experiment.class);%0A            q.addFrom(qcExperiment);%0A            q.addToSelect(qcExperiment);%0A%0A            QueryCollectionReference projExperiments = new QueryCollectionReference(qcProject, %22experiments%22);%0A            ContainsConstraint cc = new ContainsConstraint(projExperiments, ConstraintOp.CONTAINS, qcExperiment);%0A            %0A            q.setConstraint(cc);%0A            q.addToOrderBy(qcName);%0A            %0A            %0A            Results results = os.execute(q);%0A            %0A            experimentCache = new ArrayList%3CDisplayExperiment%3E();%0A            %0A            Iterator i = results.iterator();%0A            while (i.hasNext()) %7B%0A                ResultsRow row = (ResultsRow) i.next();%0A%0A                Project project = (Project) row.get(0);%0A                Experiment experiment = (Experiment) row.get(1);%0A%0A                DisplayExperiment displayExp = new DisplayExperiment(experiment, project);%0A                experimentCache.add(displayExp);%0A            %7D%0A        %7D catch (Exception err) %7B%0A            err.printStackTrace();%0A        %7D
+ SessionMethods.getObjectStore(session.getServletContext());%0A        List%3CDisplayExperiment%3E experiments = MetadataCache.getExperiments(os);%0A        request.setAttribute(%22experiments%22, experiments);%0A        return null;
 %0A   

Use MetaDataCache for experiments page.