package com.google.sampling.experiential.server;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.sampling.experiential.model.Experiment;

public class ExperimentRetriever {

  public static boolean isWhoAllowedToPostToExperiment(Experiment experiment, String who) {
    who = who.toLowerCase();
    return experiment.getAdmins().contains(who) || 
      (experiment.getPublished() && (experiment.getPublishedUsers().isEmpty() || experiment.getPublishedUsers().contains(who)));
  }

  public static Experiment getExperiment(String experimentId) {
    PersistenceManager pm = null;
    try {
      if (experimentId != null) {
        pm = PMF.get().getPersistenceManager();
        javax.jdo.Query q = pm.newQuery(Experiment.class);
        q.setFilter("id == idParam");
        q.declareParameters("Long idParam");
        List<Experiment> experiments = (List<Experiment>) q.execute(Long.valueOf(experimentId));
        if (experiments.size() > 0) {
          Experiment experiment = experiments.get(0);
          // load related piecs before we close the Persistence Manager.
          // TODO eager load the experiment's object graph
          experiment.getFeedback();
          experiment.getInputs();
          experiment.getSchedule();
          return experiment;
        }
      }
    } finally {
      if (pm != null) {
        pm.close();
      }
    }
    return null;
  }

}

@@ -65,16 +65,49 @@
 il.List;
+%0Aimport java.util.logging.Logger;
 %0A%0Aimport
@@ -224,44 +224,309 @@
 nt;%0A
-%0Apublic class ExperimentRetriever %7B%0A
+import com.google.sampling.experiential.model.Feedback;%0Aimport com.google.sampling.experiential.model.Input;%0Aimport com.google.sampling.experiential.model.SignalSchedule;%0A%0Apublic class ExperimentRetriever %7B%0A%0A  private static final Logger log = Logger.getLogger(ExperimentRetriever.class.getName());%0A  
 %0A  p
@@ -1293,11 +1293,12 @@
 e() 
-%3E 0
+== 1
 ) %7B%0A
@@ -1497,131 +1497,891 @@
     
-experiment.getFeedback();%0A          experiment.getInputs();%0A          experiment.getSchedule();%0A          return experiment
+// we now need to actually access related objects for them to get loaded.%0A          // Also, defaultFetchGroup was causing errors. TODO: Revisit this in the future.%0A          List%3CFeedback%3E feedback = experiment.getFeedback();%0A          feedback.get(0);%0A          List%3CInput%3E inputs = experiment.getInputs();%0A          inputs.get(0);%0A          SignalSchedule schedule = experiment.getSchedule();%0A          schedule.getId();%0A          return experiment;%0A        %7D else if (experiments.size() %3E 1) %7B%0A          String message = %22There are multiple experiments for this id: %22 + experimentId;%0A          log.info(message);%0A          throw new IllegalArgumentException(message);%0A        %7D else if (experiments.size() %3C 1) %7B%0A          String message = %22There are no experiments for this id: %22 + experimentId;%0A          log.info(message);%0A          throw new IllegalArgumentException(message)
 ;%0A  

deal with bug of lazy loading now requiring actual request to list objects to ensure they are loaded before closing the PM.