Index: Dev/trunk/classes/Answer.php
===================================================================
--- Dev/trunk/classes/Answer.php	(revision 185)
+++ Dev/trunk/classes/Answer.php	(revision 186)
@@ -8,5 +8,5 @@
 class Answer extends ResearchToolObject {
     public $question;
-    public $values;
+	public $values;
     
     /**
@@ -24,5 +24,23 @@
       $this->question = $question;
       $this->values = $values;
-    }
+	}
+	
+	/**
+	 * function evaluate()
+	 * Evaluates the references and fills in the objects for them instead.
+	 */
+	public function evaluate()
+	{
+		if(is_string($this->question))
+		{
+			$dbi = new DatabaseInterface();
+			$result = $dbi->get("question", array("code" => $this->question));
+			if(!isset($result[0]))
+				return false;
+			$this->question = $result[0];
+		}
+		return true;
+
+	}
 }
 
Index: Dev/trunk/classes/AnswerConnector.php
===================================================================
--- Dev/trunk/classes/AnswerConnector.php	(revision 185)
+++ Dev/trunk/classes/AnswerConnector.php	(revision 186)
@@ -11,5 +11,4 @@
  */
 class AnswerConnector extends Connector{
-	protected $db;
     
     /**
@@ -51,9 +50,7 @@
 		if(!empty($results))
 		{
-            $this->db = new DatabaseInterface();
 			foreach($results as $result)
 			{
-				$questionResult = $this->db->get("question", array("code" => $result['?question_code']->label));
-				$answers[] = new Answer($result['?uid']->label, $questionResult[0], $this->getValues($result['?uid']->label));
+				$answers[] = new Answer($result['?uid']->label, $result['?question_code']->label, $this->getValues($result['?uid']->label));
 			}
 		}
@@ -84,6 +81,10 @@
     public function set($rToolObject)
     {
-        $this->load();
-        
+		$this->load();
+		//If evaluation fails, some references are incorrect.
+		//We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
+		//TODO: Decide how to fix invalid references graciously.
+		if(!$rToolObject->evaluate()) 
+			return false;
         $resourceAnswer = new Resource(ANSWER.'/'.$rToolObject->uid);        
         //Remove the old value stored with the given id
@@ -112,5 +113,6 @@
 			}
 		}
-        $this->save();
+		$this->save();
+		return true;
     }
 }
Index: Dev/trunk/classes/AnswerSet.php
===================================================================
--- Dev/trunk/classes/AnswerSet.php	(revision 185)
+++ Dev/trunk/classes/AnswerSet.php	(revision 186)
@@ -13,6 +13,7 @@
 	 * Constructor for an AnswerSet object
 	 * @param type $uid
-	 * @param type $question
-	 * @param type $values
+	 * @param type $survey
+	 * @param type $respondent
+	 * @param type $answers
 	 */
 	public function __construct($uid=null, $survey=null, $respondent=null, $answers=null)
@@ -27,4 +28,40 @@
 		$this->answers = $answers;
 	}
+
+	/**
+	 * function evaluate()
+	 * evaluates the references of survey, respondent and answers.
+	 */
+	public function evaluate()
+	{
+		$dbi = new DatabaseInterface();
+		if(is_string($this->survey))
+		{
+			$result = $dbi->get("survey", array("uid" => $this->survey));
+			if(!isset($result[0]))
+				return false;
+			$this->survey = $result[0];
+		}
+		if(is_string($this->respondent))
+		{
+			$result = $dbi->get("respondent", array("uid" => $this->respondent));
+			if(!isset($result[0]))
+				return false;
+			$this->respondent = $result[0];
+		}
+		if(!empty($this->answers) && is_string($this->answers[0]))
+		{
+			$newanswers = array();
+			foreach($this->answers as $answer)
+			{
+				$result = $dbi->get("answer", array("uid" => $answer));
+				if(!isset($result[0]))
+					return false;
+				$newanswers[] = $result[0];
+			}
+			$this->answers = $newanswers;
+		}
+		return true;
+	}
 }
 
Index: Dev/trunk/classes/AnswerSetConnector.php
===================================================================
--- Dev/trunk/classes/AnswerSetConnector.php	(revision 185)
+++ Dev/trunk/classes/AnswerSetConnector.php	(revision 186)
@@ -50,6 +50,6 @@
 			foreach($results as $result)
 			{
-				$survey = $this->db->get("survey", array("uid" => $result['?for_survey']->label));
-				$respondent = $this->db->get("respondent", array("uid" => $result['?by_respondent']->label));
+				$survey = $result['?for_survey']->label;
+				$respondent = $result['?by_respondent']->label;
 				$answers = 	$this->getAnswers($result['?uid']->label);
 				$answerSets[] = new AnswerSet($result['?uid']->label, $survey[0], $respondent[0], $answers);
@@ -71,6 +71,5 @@
 		{
 			$element = $iterator->next();
-			$answersR = $this->db->get("answer", array("uid" => $element->getLabelObject()));
-			$answers[] = $answersR[0];
+			$answers[] = $element->getLabelObject();
 		}
 		return $answers;
@@ -84,4 +83,10 @@
 	{
 		$this->load();
+		//If evaluation fails, some references are incorrect.
+		//We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
+		//TODO: Decide how to fix invalid references graciously.
+		if(!$rToolObject->evaluate()) 
+			return false;
+
 		$resourceAnswerSet = new Resource(ANSWERSET . '/' . $rToolObject->uid);
 		//Remove the old value stored with the given id
@@ -115,4 +120,5 @@
 		}
 		$this->save();
+		return true;
 	}
 }
Index: Dev/trunk/classes/Application.php
===================================================================
--- Dev/trunk/classes/Application.php	(revision 185)
+++ Dev/trunk/classes/Application.php	(revision 186)
@@ -28,6 +28,11 @@
         $this->title = $title;
         $this->description = $description;
-	$this->style = $style;
-    }
+		$this->style = $style;
+	}
+	
+	public function evaluate()
+	{
+		//Do nothing, there are no references in application.
+	}
 
 }
Index: Dev/trunk/classes/Question.php
===================================================================
--- Dev/trunk/classes/Question.php	(revision 185)
+++ Dev/trunk/classes/Question.php	(revision 186)
@@ -39,5 +39,14 @@
         $this->category =  $category;
         $this->answers = $answers;
-    }
+	}
+
+	/**
+	 * function evaluate()
+	 * evaluates all the references of this object.
+	 */
+	public function evaluate()
+	{
+		//Do nothing since Question does not contain any references
+	}
 }
 
Index: Dev/trunk/classes/ResearchToolObject.php
===================================================================
--- Dev/trunk/classes/ResearchToolObject.php	(revision 185)
+++ Dev/trunk/classes/ResearchToolObject.php	(revision 186)
@@ -6,5 +6,7 @@
  */
 abstract class ResearchToolObject {
-    public $uid;
+	public $uid;
+//Uncomment when done with creating all evaluation methods \o/////
+//	public abstract function evaluate();
 }
 
Index: Dev/trunk/classes/Respondent.php
===================================================================
--- Dev/trunk/classes/Respondent.php	(revision 185)
+++ Dev/trunk/classes/Respondent.php	(revision 186)
@@ -26,4 +26,13 @@
       $this->password = $password;
    }
+
+   /*
+	* function evaluate()
+	* evaluates all the references.
+	*/
+   public function evaluate()
+   {
+		//Do nothing since Respondent does not contain any references.
+   }
    
 }
Index: Dev/trunk/classes/Session.php
===================================================================
--- Dev/trunk/classes/Session.php	(revision 185)
+++ Dev/trunk/classes/Session.php	(revision 186)
@@ -37,11 +37,60 @@
 	}
 
+	/*
+	 * function evaluate(()
+	 * Evaluates all the references of this object.
+	 */
+	public function evaluate()
+	{
+		$dbi = new DatabaseInterface();
+		if(is_string($this->creator))
+		{
+			$result = $dbi->get("user", array("uid" => $this->creator));
+			if(!isset($result[0]))
+				return false;
+			$this->creator = $result[0];
+		}
+		if(!empty($this->pipeline) && is_string($this->pipeline[0]))
+		{
+			$newPipeline = array();
+			foreach($this->pipeline as $element)
+			{
+				//Check if the element's UID can be found in surveys, if not: 
+				//Check applications. If it isn't in either: invalid reference.
+				$result = $dbi->get("survey", array("uid" => $element));
+				if(!isset($result[0]))
+					$result = $dbi->get("application", array("uid" => $element));
+				if(!isset($result[0]))
+					return false;
+				$newPipeline[] = $result[0];
+			}
+			$this->pipeline = $newPipeline;
+		}
+		if(!empty($this->answersets) && is_string($this->answersets[0]))
+		{
+			$newAnswerSets = array();
+			foreach($this->answersets as $element)
+			{
+				$result = $dbi->get("answerset", array("uid" => $element));
+				if(!isset($result[0]))
+					return false;
+				$newAnswerSets[] = $result[0];
+			}
+			$this->answersets = $newAnswerSets;
+		}
+		return true;
+	}
+
 	/**
 	 * function toSPSS
 	 * @param type $location
 	 * Writes an SPSS parseable file at the given location, named '$datetime_$title.txt'
+	 * One big note about toSPSS: If this session contains multiple instances of THE SAME Survey,
+	 * the world burns, people die, etcetera. (or, in a more fortunate case, the latest values
+	 * are written to the file whilst the others get lost.
 	 */
 	public function toSPSS($location)
 	{
+		$this->evaluate();
 		if(!is_dir($location))
 		{
@@ -58,6 +107,8 @@
 			if(get_class($element) == "Survey")
 			{
+				$element->evaluate();
 				foreach($element->questions as $question)
 				{
+					//TODO: This might need fixing after lazy initialization refactoring.
 					$headers[] = $element->title . '_' . $question->uid;
 				}
Index: Dev/trunk/classes/SessionConnector.php
===================================================================
--- Dev/trunk/classes/SessionConnector.php	(revision 185)
+++ Dev/trunk/classes/SessionConnector.php	(revision 186)
@@ -59,8 +59,8 @@
 				$pipeline = $this->getPipeline($result['?uid']->label);
 				$answersets = $this->getAnswerSets($result['?uid']->label);
-				$creator = $this->db->get("User", array("uid" => $result['?creator']->label));
+				$creator = $result['?creator']->label;
 				$datetime = new DateTime();
 				$datetime->setTimestamp(intval($result['?datetime']->label));
-				$sessions[] = new Session($result['?uid']->label, $result['?title']->label, $creator[0], $datetime, $pipeline, $answersets);
+				$sessions[] = new Session($result['?uid']->label, $result['?title']->label, $creator, $datetime, $pipeline, $answersets);
 			}
 		}
@@ -79,14 +79,5 @@
 		{
 			$element = $iterator->next();
-			if(strpos($element->getLabelPredicate(), "has_application") != false)
-			{
-				$applications = $this->db->get("application", array("uid" => $element->getLabelObject()));
-				$pipeline[] = $applications[0];
-			}
-			else
-			{
-				$surveys = $this->db->get("survey", array("uid" => $element->getLabelObject()));
-				$pipeline[] = $surveys[0];
-			}
+			$pipeline[] = $element->getLabelObject();
 		}
 		return $pipeline;
@@ -105,6 +96,5 @@
 		{
 			$element = $iterator->next();
-			$resultsets = $this->db->get("answerset", array("uid" => $element->getLabelObject()));
-			$answersets[] = $resultsets[0];
+			$answersets[] = $element->getLabelObject();
 		}
 		return $answersets;
@@ -118,4 +108,10 @@
 	{
 		$this->load();
+		//If evaluation fails, some references are incorrect.
+		//We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
+		//TODO: Decide how to fix invalid references graciously.
+		if(!$rToolObject->evaluate()) 
+			return false;
+
 		$resourceSession = new Resource(SESSION . '/' . $rToolObject->uid);
 		//Remove the old value stored with the given id
@@ -171,4 +167,5 @@
 
 		$this->save();
+		return true;
 	}
 }
Index: Dev/trunk/classes/Survey.php
===================================================================
--- Dev/trunk/classes/Survey.php	(revision 185)
+++ Dev/trunk/classes/Survey.php	(revision 186)
@@ -22,5 +22,35 @@
 		$this->creator = $creator;
         $this->questions = $questions;
-    }
+	}
+
+	/**
+	 * function evaluate()
+	 * Evaluates the references
+	 */
+	public function evaluate()
+	{
+		$dbi = new DatabaseInterface();
+		if(is_string($this->creator))
+		{
+			$result = $dbi->get("user", array("uid" => $this->creator));	
+			if(!isset($result[0]))
+				return false;
+			$this->creator = $result[0];
+		}
+
+		if(!empty($this->questions) && is_string($this->questions[0]))
+		{
+			$newQuestions = array();
+			foreach($this->questions as $question)
+			{
+				$result = $dbi->get("question", array("uid" => $question));
+				if(!isset($result[0]))
+					return false;
+				$newQuestions[] = $result[0];			
+			}
+			$this->questions = newQuestions;
+		}
+		return true;
+	}
 }
 
Index: Dev/trunk/classes/SurveyConnector.php
===================================================================
--- Dev/trunk/classes/SurveyConnector.php	(revision 185)
+++ Dev/trunk/classes/SurveyConnector.php	(revision 186)
@@ -57,5 +57,5 @@
             {
 				$questions = $this->getQuestions($result['?uid']->label);
-				$creator = $this->db->get("user", array("uid" => $result['?creator']->label));
+				$creator = $result['?creator']->label;
                 $surveys[] = new Survey($result['?uid']->label, $result['?title']->label, $result['?description']->label, $creator[0], $questions);
             }
@@ -76,6 +76,5 @@
 		{
 			$element = $iterator->next();
-			$result = $this->db->get("question", array("code" => $element->getLabelObject()));
-			$questions[] = $result[0];
+			$questions[] = $element->getLabelObject();
 		}
 		return $questions;
@@ -90,4 +89,10 @@
         $this->load();
         
+		//If evaluation fails, some references are incorrect.
+		//We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
+		//TODO: Decide how to fix invalid references graciously.
+		if(!$rToolObject->evaluate()) 
+			return false;
+
         $resourceSurvey = new Resource(SURVEY.'/'.$rToolObject->uid);
         //Remove the old value stored with the given id
@@ -122,6 +127,7 @@
                 $this->model->add(new Statement($resourceSurvey,$predicateQuid,$questionUid));
             }
-        }
-        $this->save();
+		}
+		$this->save();
+		return true;
     }
 }
Index: Dev/trunk/classes/User.php
===================================================================
--- Dev/trunk/classes/User.php	(revision 185)
+++ Dev/trunk/classes/User.php	(revision 186)
@@ -24,5 +24,15 @@
         $this->name = $name;
         $this->password = $password;
-    }
+	}
+
+	/**
+	 * function evaluate()
+	 * Evaluates all the references of this object.
+	 */
+	public function evaluate()
+	{
+		//Do nothing.
+	}
+
 }
 
Index: Dev/trunk/classes/master.php
===================================================================
--- Dev/trunk/classes/master.php	(revision 185)
+++ Dev/trunk/classes/master.php	(revision 186)
@@ -15,6 +15,4 @@
 session_start();  // Start the session
 
-
-
 function __autoload($class_name) {
     require 'classes/' . $class_name . '.php';
@@ -31,2 +29,5 @@
 	exit();
 }
+
+
+?>
