/**
  * Tests the generateSQLQuery() without WHERE clause.
  */
 function test()
 {
     // insert one row
     $query = new InsertQuery();
     $query->setTable("test1");
     $query->setColumns(array("value"));
     $query->addRowOfValues(array("'Spaceboy'"));
     $query->setAutoIncrementColumn("id", "test1_id_seq");
     $result = $this->db->query($query);
     $lastId = $result->getLastAutoIncrementValue();
     // insert it again, the id must have increased by one
     $result = $this->db->query($query);
     $this->assertIdentical($result->getNumberOfRows(), 1);
     $this->assertIdentical($result->getLastAutoIncrementValue(), $lastId + 1);
     // add several rows at the same time
     $query->addRowOfValues(array("'Astrogirl'"));
     $result = $this->db->query($query);
     $this->assertIdentical($result->getLastAutoIncrementValue(), $lastId + 3);
     // now insert in the other test table
     $query = new InsertQuery();
     $query->setTable("test");
     $query->setColumns(array("FK", "value"));
     $query->addRowOfValues(array($lastId, "'Ziggy'"));
     $query->addRowOfValues(array($lastId + 1, "'Lost in the Stars'"));
     $query->addRowOfValues(array($lastId + 2, "'Headstar'"));
     $query->addRowOfValues(array($lastId + 3, "'Stardust'"));
     $query->setAutoIncrementColumn("id", "test1_id_seq");
     $result = $this->db->query($query);
     // join the inserted rows
     $query = new SelectQuery();
     $query->addTable("test1");
     $query->addTable("test", INNER_JOIN, "test.FK = test1.id");
     $query->addColumn("id", "dm86_id", "test");
     $query->addColumn("FK", "dm86_fk", "test");
     $query->addColumn("value", "dm86_value", "test");
     $query->addColumn("id", "dm98_id", "test1");
     $query->addColumn("value", "dm98_value", "test1");
     $query->addWhere("test1.id >= " . $lastId);
     $result = $this->db->query($query);
     $this->assertIdentical($result->getNumberOfRows(), 4);
     $this->assertIdentical((int) $result->field("dm86_fk"), $lastId);
     $this->assertIdentical($result->field("dm86_value"), "Ziggy");
     $this->assertIdentical((int) $result->field("dm98_id"), $lastId);
     $this->assertIdentical($result->field("dm98_value"), "Spaceboy");
     $result->advanceRow();
     $this->assertIdentical((int) $result->field("dm86_fk"), $lastId + 1);
     $this->assertIdentical($result->field("dm86_value"), "Lost in the Stars");
     $this->assertIdentical((int) $result->field("dm98_id"), $lastId + 1);
     $this->assertIdentical($result->field("dm98_value"), "Spaceboy");
     $result->advanceRow();
     $this->assertIdentical((int) $result->field("dm86_fk"), $lastId + 2);
     $this->assertIdentical($result->field("dm86_value"), "Headstar");
     $this->assertIdentical((int) $result->field("dm98_id"), $lastId + 2);
     $this->assertIdentical($result->field("dm98_value"), "Spaceboy");
     $result->advanceRow();
     $this->assertIdentical((int) $result->field("dm86_fk"), $lastId + 3);
     $this->assertIdentical($result->field("dm86_value"), "Stardust");
     $this->assertIdentical((int) $result->field("dm98_id"), $lastId + 3);
     $this->assertIdentical($result->field("dm98_value"), "Astrogirl");
     $result->free();
     $query = new UpdateQuery();
     $query->setTable("test1");
     $query->setColumns(array("value"));
     $query->setValues(array("'I changed you MF!'"));
     $query->addWhere("id = " . $lastId);
     $result = $this->db->query($query);
     $this->assertIdentical($result->getNumberOfRows(), 1);
     $query = new SelectQuery();
     $query->addTable("test1");
     $query->addColumn("value");
     $query->addWhere("test1.id = " . $lastId);
     $result = $this->db->query($query);
     $this->assertIdentical($result->getNumberOfRows(), 1);
     $this->assertIdentical($result->field("value"), "I changed you MF!");
     $result->free();
     $query = new DeleteQuery();
     $query->setTable("test1");
     $query->addWhere("id = " . $lastId);
     $result = $this->db->query($query);
     $this->assertIdentical($result->getNumberOfRows(), 1);
     $query = new SelectQuery();
     $query->addTable("test1");
     $query->addColumn("value");
     $query->addWhere("test1.id = " . $lastId);
     $result = $this->db->query($query);
     $this->assertIdentical($result->getNumberOfRows(), 0);
     $result->free();
 }
 /**
  * Get the key of the type.
  * 
  * @param object Type $type
  * @return integer
  * @access private
  * @since 3/9/05
  */
 function _getTypeKey(Type $type)
 {
     $dbc = Services::getService("DatabaseManager");
     // Check if the type exists and return its key if found.
     $query = new SelectQuery();
     $query->addTable($this->_typeTable);
     $query->addColumn('id');
     $query->addWhere("domain='" . addslashes($type->getDomain()) . "'");
     $query->addWhere("authority='" . addslashes($type->getAuthority()) . "'", _AND);
     $query->addWhere("keyword='" . addslashes($type->getKeyword()) . "'", _AND);
     $result = $dbc->query($query, $this->_dbId);
     if ($result->getNumberOfRows() == 1) {
         return $result->field('id');
     } else {
         $result->free();
         $query = new InsertQuery();
         $query->setTable($this->_typeTable);
         $query->setAutoIncrementColumn("id", $this->_typeTable . "_id_seq");
         $query->setColumns(array('domain', 'authority', 'keyword', 'description'));
         $query->setValues(array("'" . addslashes($type->getDomain()) . "'", "'" . addslashes($type->getAuthority()) . "'", "'" . addslashes($type->getKeyword()) . "'", "'" . addslashes($type->getDescription()) . "'"));
         $result = $dbc->query($query, $this->_dbId);
         return $result->getLastAutoIncrementValue();
     }
 }
 /**
  * Attempts to create the specified node as root node in the database.
  * @access public
  * @param object nodeId The id of the node.
  * @param object type The type of the node.
  * @param string displayName The display name of the node.
  * @param string description The description of the node.
  * @return void
  **/
 function createRootNode(Id $nodeId, Type $type, $displayName, $description)
 {
     // ** parameter validation
     $stringRule = StringValidatorRule::getRule();
     ArgumentValidator::validate($displayName, $stringRule, true);
     ArgumentValidator::validate($description, $stringRule, true);
     // ** end of parameter validation
     // check that the node does not exist in the cache
     $idValue = $nodeId->getIdString();
     if ($this->_isCached($idValue)) {
         // The node has already been cached!
         throw new OperationFailedException("Node, '{$idValue}' is already cached.");
     }
     // attempt to insert the node now
     $dbHandler = Services::getService("DatabaseManager");
     // 1. Insert the type
     $domain = $type->getDomain();
     $authority = $type->getAuthority();
     $keyword = $type->getKeyword();
     $typeDescription = $type->getDescription();
     // check whether the type is already in the DB, if not insert it
     if (isset($this->harmoni_db)) {
         if (!isset($this->createRootNode_selectType_stmt)) {
             $query = $this->harmoni_db->select();
             $query->addTable("az2_node_type");
             $query->addColumn("id");
             $query->addWhereRawEqual("domain", '?');
             $query->addWhereRawEqual("authority", '?');
             $query->addWhereRawEqual("keyword", '?');
             $this->createRootNode_selectType_stmt = $query->prepare();
         }
         $this->createRootNode_selectType_stmt->bindValue(1, $domain);
         $this->createRootNode_selectType_stmt->bindValue(2, $authority);
         $this->createRootNode_selectType_stmt->bindValue(3, $keyword);
         $this->createRootNode_selectType_stmt->execute();
         $queryResult = $this->createRootNode_selectType_stmt->getResult();
     } else {
         $query = new SelectQuery();
         $query->addTable("az2_node_type");
         $query->addColumn("id");
         $query->addWhereEqual("domain", $domain);
         $query->addWhereEqual("authority", $authority);
         $query->addWhereEqual("keyword", $keyword);
         $queryResult = $dbHandler->query($query, $this->_dbIndex);
     }
     if ($queryResult->hasNext()) {
         // if the type is already in the database
         $typeIdValue = $queryResult->field("id");
         // get the id
         $queryResult->free();
     } else {
         // if not, insert it
         $queryResult->free();
         if (isset($this->harmoni_db)) {
             if (!isset($this->createRootNode_insertType_stmt)) {
                 $query = $this->harmoni_db->insert();
                 $query->setTable("az2_node_type");
                 // 					$query->setAutoIncrementColumn("id", "az2_node_type_id_seq");
                 $query->addRawValue("domain", '?');
                 $query->addRawValue("authority", '?');
                 $query->addRawValue("keyword", '?');
                 $query->addRawValue("description", '?');
                 $this->createRootNode_insertType_stmt = $query->prepare();
             }
             $this->createRootNode_insertType_stmt->bindValue(1, $domain);
             $this->createRootNode_insertType_stmt->bindValue(2, $authority);
             $this->createRootNode_insertType_stmt->bindValue(3, $keyword);
             $this->createRootNode_insertType_stmt->bindValue(4, $typeDescription);
             $this->createRootNode_insertType_stmt->execute();
             $queryResult = $this->createRootNode_insertType_stmt->getResult();
         } else {
             $query = new InsertQuery();
             $query->setTable("az2_node_type");
             $query->setAutoIncrementColumn("id", "az2_node_type_id_seq");
             $query->addValue("domain", $domain);
             $query->addValue("authority", $authority);
             $query->addValue("keyword", $keyword);
             $query->addValue("description", $typeDescription);
             $queryResult = $dbHandler->query($query, $this->_dbIndex);
         }
         $typeIdValue = $queryResult->getLastAutoIncrementValue();
     }
     // 2. Now that we know the id of the type, insert the node itself
     if (isset($this->harmoni_db)) {
         if (!isset($this->createRootNode_insertNode_stmt)) {
             $query = $this->harmoni_db->insert();
             $query->setTable("az2_node");
             $query->addRawValue("id", '?');
             $query->addRawValue("display_name", '?');
             $query->addRawValue("description", '?');
             $query->addRawValue("fk_hierarchy", '?');
             $query->addRawValue("fk_type", '?');
             $this->createRootNode_insertNode_stmt = $query->prepare();
         }
         $this->createRootNode_insertNode_stmt->bindValue(1, $idValue);
         $this->createRootNode_insertNode_stmt->bindValue(2, $displayName);
         $this->createRootNode_insertNode_stmt->bindValue(3, $description);
         $this->createRootNode_insertNode_stmt->bindValue(4, $this->_hierarchyId);
         $this->createRootNode_insertNode_stmt->bindValue(5, $typeIdValue);
         $this->createRootNode_insertNode_stmt->execute();
         $queryResult = $this->createRootNode_insertNode_stmt->getResult();
     } else {
         $query = new InsertQuery();
         $query->setTable("az2_node");
         $query->addValue("id", $idValue);
         $query->addValue("display_name", $displayName);
         $query->addValue("description", $description);
         $query->addValue("fk_hierarchy", $this->_hierarchyId);
         $query->addValue("fk_type", $typeIdValue);
         $queryResult = $dbHandler->query($query, $this->_dbIndex);
     }
     if ($queryResult->getNumberOfRows() != 1) {
         //"Could not insert the node (it already exists?)";
         throw new OperationFailedException($queryResult->getNumberOfRows() . " nodes found. Expecting 1. Node already exists?");
     }
     // create the node object to return
     $node = new AuthZ2_Node($nodeId, $type, $displayName, $description, $this);
     // then cache it
     $this->_cache[$idValue][0] = $node;
     $this->_cache[$idValue][1] = -1;
     // fully cached up and down because
     $this->_cache[$idValue][2] = -1;
     // in fact this node does not have any ancestors or descendents
     // update _tree
     $nullValue = NULL;
     // getting rid of PHP warnings by specifying
     // this second argument
     $this->_tree->addNode(new TreeNode($idValue), $nullValue);
     return $node;
 }
 /**
  * A public function for getting a type id (and ensuring that it exists
  * in the database). One might consider implementing a Type manager for 
  * stuff like this that has no proper home.
  * 
  * @param object Type $type
  *
  * @return integer
  *
  * @access public
  *
  * @since 11/18/04
  */
 function getTypeId($type)
 {
     $dbc = Services::getService("DBHandler");
     // Check to see if the type already exists in the DB
     $query = new SelectQuery();
     $query->addColumn("type_id");
     $query->addTable("type");
     $query->addWhere("type_domain='" . addslashes($type->getDomain()) . "'");
     $query->addWhere("type_authority='" . addslashes($type->getAuthority()) . "'", _AND);
     $query->addWhere("type_keyword='" . addslashes($type->getKeyword()) . "'", _AND);
     $result = $dbc->query($query, $this->_dbIndex);
     // If we have a type id already, use that
     if ($result->getNumberOfRows()) {
         $typeId = $result->field("type_id");
         $result->free();
     } else {
         $result->free();
         $query = new InsertQuery();
         $query->setTable("type");
         $query->setAutoIncrementColumn("type_id", "type_type_id_seq");
         $query->setColumns(array("type_domain", "type_authority", "type_keyword", "type_description"));
         $query->setValues(array("'" . addslashes($type->getDomain()) . "'", "'" . addslashes($type->getAuthority()) . "'", "'" . addslashes($type->getKeyword()) . "'", "'" . addslashes($type->getDescription()) . "'"));
         $result = $dbc->query($query, $this->_dbIndex);
         $typeId = $result->getLastAutoIncrementValue();
     }
     return $typeId;
 }
 /**
  * Add a student to the roster and assign the specified Enrollment Status
  * Type.
  * 
  * @param object Id $agentId
  * @param object Type $enrollmentStatusType
  * 
  * @throws object CourseManagementException An exception
  *		   with one of the following messages defined in
  *		   org.osid.coursemanagement.CourseManagementException may be
  *		   thrown:	{@link
  *		   org.osid.coursemanagement.CourseManagementException#OPERATION_FAILED
  *		   OPERATION_FAILED}, {@link
  *		   org.osid.coursemanagement.CourseManagementException#PERMISSION_DENIED
  *		   PERMISSION_DENIED}, {@link
  *		   org.osid.coursemanagement.CourseManagementException#CONFIGURATION_ERROR
  *		   CONFIGURATION_ERROR}, {@link
  *		   org.osid.coursemanagement.CourseManagementException#UNIMPLEMENTED
  *		   UNIMPLEMENTED}, {@link
  *		   org.osid.coursemanagement.CourseManagementException#NULL_ARGUMENT
  *		   NULL_ARGUMENT}, {@link
  *		   org.osid.coursemanagement.CourseManagementException#UNKNOWN_TYPE
  *		   UNKNOWN_TYPE}, {@link
  *		   org.osid.coursemanagement.CourseManagementException#ALREADY_ADDED
  *		   ALREADY_ADDED}
  * 
  * @access public
  */
 function addStudent(Id $agentId, Type $enrollmentStatusType)
 {
     $dbManager = Services::getService("DatabaseManager");
     $query = new SelectQuery();
     $query->addTable('cm_enroll');
     $query->addWhere("fk_cm_section='" . addslashes($this->_id->getIdString()) . "'");
     $query->addWhere("fk_student_id='" . addslashes($agentId->getIdString()) . "'");
     //I don't need Id, but I need to select something for the query to work
     $query->addColumn('id');
     $res = $dbManager->query($query);
     if ($res->getNumberOfRows() == 0) {
         $typeIndex = $this->_typeToIndex('enroll_stat', $enrollmentStatusType);
         $query = new InsertQuery();
         $query->setTable('cm_enroll');
         $values[] = "'" . addslashes($agentId->getIdString()) . "'";
         $values[] = "'" . addslashes($typeIndex) . "'";
         $values[] = "'" . addslashes($this->_id->getIdString()) . "'";
         $query->setColumns(array('fk_student_id', 'fk_cm_enroll_stat_type', 'fk_cm_section'));
         $query->addRowOfValues($values);
         $query->setAutoIncrementColumn('id', 'id_sequence');
         $dbManager->query($query);
     } else {
         print "<b>Warning!</b> Student with id " . $agentId->getIdString() . " is already enrolled in section " . $this->getDisplayName() . ".";
     }
 }
 /**
  * Find the index for our Type of type $type in its table.  If it is not there,
  * put it into the table and return the index.
  *
  * @param string $typename the type of Type that is passed in.
  * @param object Type $type the Type itself
  *
  * @return object Type
  *
  * @access private
  */
 function _typeToIndex($typename, $type)
 {
     //the appropriate table names and fields must be given names according to the pattern indicated below
     //validate the Type
     ArgumentValidator::validate($type, ExtendsValidatorRule::getRule("Type"), true);
     //query to see if it exists
     $dbHandler = Services::getService("DBHandler");
     $query = new SelectQuery();
     $query->addTable('cm_' . $typename . "_type");
     $query->addWhere("domain='" . $type->getDomain() . "'");
     $query->addWhere("authority='" . $type->getAuthority() . "'");
     $query->addWhere("keyword='" . $type->getKeyword() . "'");
     $query->addColumn('id');
     $res = $dbHandler->query($query);
     if ($res->getNumberOfRows() == 0) {
         //if not query to create it
         $query = new InsertQuery();
         $query->setTable('cm_' . $typename . '_type');
         $values[] = "'" . addslashes($type->getDomain()) . "'";
         $values[] = "'" . addslashes($type->getAuthority()) . "'";
         $values[] = "'" . addslashes($type->getKeyword()) . "'";
         if (is_null($type->getDescription())) {
             $query->setColumns(array('domain', 'authority', 'keyword'));
         } else {
             $query->setColumns(array('domain', 'authority', 'keyword', 'description'));
             $values[] = "'" . addslashes($type->getDescription()) . "'";
         }
         $query->addRowOfValues($values);
         $query->setAutoIncrementColumn('id', 'cm_' . $typename . '_type_id_seq');
         $result = $dbHandler->query($query);
         return $result->getLastAutoIncrementValue();
     } elseif ($res->getNumberOfRows() == 1) {
         //if it does exist, create it
         $row = $res->getCurrentRow();
         $the_index = $row['id'];
         return $the_index;
     } else {
         //print a warning if there is more than one such type.  Should never happen.
         print "\n<b>Warning!<\\b> The Type with domain " . $type->getDomain() . ", authority " . $type->getAuthority() . ", and keyword " . $type->getKeyword() . " is not unique--there are " . $res->getNumberOfRows() . " copies.\n";
         //return either one anyway.
         $row = $res->getCurrentRow();
         $the_index = $row['id'];
         return $the_index;
     }
 }
 /**
  * Update the value for this Part.
  * 
  * @param object mixed $value (original type: java.io.Serializable)
  * 
  * @throws object RepositoryException An exception with one of
  *		   the following messages defined in
  *		   org.osid.repository.RepositoryException may be thrown: {@link
  *		   org.osid.repository.RepositoryException#OPERATION_FAILED
  *		   OPERATION_FAILED}, {@link
  *		   org.osid.repository.RepositoryException#PERMISSION_DENIED
  *		   PERMISSION_DENIED}, {@link
  *		   org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  *		   CONFIGURATION_ERROR}, {@link
  *		   org.osid.repository.RepositoryException#UNIMPLEMENTED
  *		   UNIMPLEMENTED}, {@link
  *		   org.osid.repository.RepositoryException#NULL_ARGUMENT
  *		   NULL_ARGUMENT}
  * 
  * @access public
  */
 function updateValue($value)
 {
     if (!is_null($value)) {
         ArgumentValidator::validate($value, NonzeroLengthStringValidatorRule::getRule());
     }
     // Store the name in the object in case its asked for again.
     $this->_type = $value;
     // then write it to the database.
     $dbHandler = Services::getService("DatabaseManager");
     // If we have a key, make sure it exists.
     if ($this->_type && $this->_type != "NULL") {
         // Check to see if the type is in the database
         $query = new SelectQuery();
         $query->addTable("dr_mime_type");
         $query->addColumn("id");
         $query->addWhere("type = '" . $this->_type . "'");
         $result = $dbHandler->query($query, $this->_configuration->getProperty("database_index"));
         // If it doesn't exist, insert it.
         if (!$result->getNumberOfRows()) {
             $query = new InsertQuery();
             $query->setTable("dr_mime_type");
             $query->setAutoIncrementColumn("id", "dr_mime_type_id_seq");
             $query->setColumns(array("type"));
             $query->setValues(array("'" . addslashes($this->_type) . "'"));
             $result2 = $dbHandler->query($query, $this->_configuration->getProperty("database_index"));
             $mimeId = "'" . $result2->getLastAutoIncrementValue() . "'";
         } else {
             $mimeId = "'" . $result->field("id") . "'";
         }
         $result->free();
     } else {
         $mimeId = "NULL";
     }
     // add its id to the file.
     $query = new UpdateQuery();
     $query->setTable("dr_file");
     $query->setColumns(array("fk_mime_type"));
     $query->setValues(array($mimeId));
     $query->addWhere("id = '" . $this->_recordId->getIdString() . "'");
     // run the query
     $dbHandler->query($query, $this->_configuration->getProperty("database_index"));
     $this->_asset->updateModificationDate();
 }
 /**
  * Answer the mime type key
  * 
  * @param string $mimeType
  * @return integer
  * @access public
  * @since 2/13/06
  */
 function getMimeKey()
 {
     // If we have a key, make sure it exists.
     if ($this->_mimeType && $this->_mimeType != "NULL") {
         $dbc = Services::getService('DatabaseManager');
         // Check to see if the type is in the database
         $query = new SelectQuery();
         $query->addTable("dr_mime_type");
         $query->addColumn("id");
         $query->addWhere("type = '" . $this->_mimeType . "'");
         $result = $dbc->query($query, $this->getDBIndex());
         // If it doesn't exist, insert it.
         if (!$result->getNumberOfRows()) {
             $query = new InsertQuery();
             $query->setTable("dr_mime_type");
             $query->setAutoIncrementColumn("id", "dr_mime_type_id_seq");
             $query->setColumns(array("type"));
             $query->setValues(array("'" . addslashes($this->_mimeType) . "'"));
             $result2 = $dbc->query($query, $this->getDBIndex());
             $mimeId = "'" . $result2->getLastAutoIncrementValue() . "'";
         } else {
             $mimeId = "'" . $result->field("id") . "'";
         }
         $result->free();
     } else {
         $mimeId = "NULL";
     }
     return $mimeId;
 }
 /**
  * Add an Agent commitment to this ScheduleItem.
  * 
  * @param object Id $agentId
  * @param object Type $agentStatus
  * 
  * @throws object SchedulingException An exception with one of
  *         the following messages defined in
  *         org.osid.scheduling.SchedulingException may be thrown:   {@link
  *         org.osid.scheduling.SchedulingException#OPERATION_FAILED
  *         OPERATION_FAILED}, {@link
  *         org.osid.scheduling.SchedulingException#PERMISSION_DENIED
  *         PERMISSION_DENIED}, {@link
  *         org.osid.scheduling.SchedulingException#CONFIGURATION_ERROR
  *         CONFIGURATION_ERROR}, {@link
  *         org.osid.scheduling.SchedulingException#UNIMPLEMENTED
  *         UNIMPLEMENTED}, {@link
  *         org.osid.scheduling.SchedulingException#UNKNOWN_ID UNKNOWN_ID},
  *         {@link org.osid.scheduling.SchedulingException#UNKNOWN_TYPE
  *         UNKNOWN_TYPE}, {@link
  *         org.osid.shared.SharedException#ALREADY_ADDED ALREADY_ADDED}
  * 
  * @access public
  */
 function addAgentCommitment(Id $agentId, Type $agentStatus)
 {
     $dbHandler = Services::getService("DBHandler");
     $query = new SelectQuery();
     $query->addTable('sc_commit');
     $query->addWhere("fk_sc_item='" . addslashes($this->_id->getIdString()) . "'");
     $query->addWhere("fk_agent_id='" . addslashes($agentId->getIdString()) . "'");
     $query->addColumn('id');
     //@TODO id is not really needed here--a count should probably be returned.
     $res = $dbHandler->query($query);
     if ($res->getNumberOfRows() == 0) {
         $typeIndex = $this->_typeToIndex('commit_stat', $agentStatus);
         $query = new InsertQuery();
         $query->setTable('sc_commit');
         $values[] = "'" . addslashes($agentId->getIdString()) . "'";
         $values[] = "'" . addslashes($typeIndex) . "'";
         $values[] = "'" . addslashes($this->_id->getIdString()) . "'";
         $query->setColumns(array('fk_agent_id', 'fk_sc_commit_stat_type', 'fk_sc_item'));
         $query->addRowOfValues($values);
         $query->setAutoIncrementColumn('id', 'id_sequence');
         $dbHandler->query($query);
     } else {
         print "<b>Warning!</b> Agent with id " . $agentId->getIdString() . "is already added to ScheduleItem " . $this->getDisplayName() . ".  Use changeAgentCommitment() to change the commitment status.";
     }
 }
 /**
  * Create a new unique identifier.
  *	
  * @return object Id
  * 
  * @throws object IdException An exception with one of the following
  *		   messages defined in org.osid.id.IdException:	 {@link
  *		   org.osid.id.IdException#OPERATION_FAILED OPERATION_FAILED},
  *		   {@link org.osid.id.IdException#PERMISSION_DENIED
  *		   PERMISSION_DENIED}, {@link
  *		   org.osid.id.IdException#CONFIGURATION_ERROR
  *		   CONFIGURATION_ERROR}, {@link
  *		   org.osid.id.IdException#UNIMPLEMENTED UNIMPLEMENTED}
  * 
  * @access public
  */
 function createId()
 {
     if (isset($this->createId_stmt)) {
         $this->createId_stmt->execute();
         $newID = $this->harmoni_db->lastInsertId('id', 'id_value');
         $this->deleteId_stmt->bindValue(1, $newID);
         $this->deleteId_stmt->execute();
     } else {
         debug::output("Attempting to generate new id.", 20, "IdManager");
         $dbHandler = Services::getService("DatabaseManager");
         $query = new InsertQuery();
         $query->setAutoIncrementColumn("id_value", "id_id_value_seq");
         $query->setTable("id");
         $query->addRowOfValues(array());
         $result = $dbHandler->query($query, $this->_dbIndex);
         if ($result->getNumberOfRows() != 1) {
             throwError(new Error(IdException::CONFIGURATION_ERROR(), "IdManager", true));
         }
         $newID = $result->getLastAutoIncrementValue();
         // Clear out any values smaller than our last one to keep the table from
         // exploding size.
         $query = new DeleteQuery();
         $query->setTable("id");
         $query->setWhere("id_value < '" . $newID . "'");
         $result = $dbHandler->query($query, $this->_dbIndex);
     }
     $newID = $this->_prefix . strval($newID);
     debug::output("Successfully created new id '{$newID}'.", DEBUG_SYS5, "IdManager");
     $id = new HarmoniId($newID);
     // cache the id
     //		$this->_ids[$newID] = $id;
     return $id;
 }
 /**
  * Create a new Repository of the specified Type.  The implementation of
  * this method sets the Id for the new object.
  * 
  * @param string $displayName
  * @param string $description
  * @param object Type $repositoryType
  *  
  * @return object Repository
  * 
  * @throws object RepositoryException An exception with one of
  *         the following messages defined in
  *         org.osid.repository.RepositoryException may be thrown: {@link
  *         org.osid.repository.RepositoryException#OPERATION_FAILED
  *         OPERATION_FAILED}, {@link
  *         org.osid.repository.RepositoryException#PERMISSION_DENIED
  *         PERMISSION_DENIED}, {@link
  *         org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  *         CONFIGURATION_ERROR}, {@link
  *         org.osid.repository.RepositoryException#UNIMPLEMENTED
  *         UNIMPLEMENTED}, {@link
  *         org.osid.repository.RepositoryException#NULL_ARGUMENT
  *         NULL_ARGUMENT}, {@link
  *         org.osid.repository.RepositoryException#UNKNOWN_TYPE
  *         UNKNOWN_TYPE}
  * 
  * @access public
  */
 function createRepository($displayName, $description, Type $repositoryType, Id $id = NULL)
 {
     // Argument Validation
     ArgumentValidator::validate($displayName, StringValidatorRule::getRule());
     ArgumentValidator::validate($description, StringValidatorRule::getRule());
     ArgumentValidator::validate($repositoryType, ExtendsValidatorRule::getRule("Type"));
     // Create an Id for the digital Repository Node
     if (!is_object($id)) {
         $IDManager = Services::getService("Id");
         $id = $IDManager->createId();
     }
     // Store the type passed in our own table as we will be using
     // a special type, "repositoryKeyType", as definition of which
     // Nodes in the Hierarchy are Repositories.
     $dbc = Services::getService("DatabaseManager");
     $query = new SelectQuery();
     $query->addColumn("type_id");
     $query->addTable("dr_type");
     $query->addWhere("type_domain = '" . addslashes($repositoryType->getDomain()) . "'");
     $query->addWhere("type_authority = '" . addslashes($repositoryType->getAuthority()) . "'", _AND);
     $query->addWhere("type_keyword = '" . addslashes($repositoryType->getKeyword()) . "'", _AND);
     $result = $dbc->query($query, $this->_dbIndex);
     if ($result->getNumberOfRows()) {
         $typeId = $result->field("type_id");
         $result->free();
     } else {
         $result->free();
         $query = new InsertQuery();
         $query->setTable("dr_type");
         $query->setAutoIncrementColumn("type_id", "dr_type_type_id_seq");
         $query->setColumns(array("type_domain", "type_authority", "type_keyword", "type_description"));
         $query->setValues(array("'" . addslashes($repositoryType->getDomain()) . "'", "'" . addslashes($repositoryType->getAuthority()) . "'", "'" . addslashes($repositoryType->getKeyword()) . "'", "'" . addslashes($repositoryType->getDescription()) . "'"));
         $result = $dbc->query($query, $this->_dbIndex);
         $typeId = $result->getLastAutoIncrementValue();
     }
     $query = new InsertQuery();
     $query->setTable("dr_repository_type");
     $query->setColumns(array("repository_id", "fk_dr_type"));
     $query->setValues(array("'" . addslashes($id->getIdString()) . "'", "'" . addslashes($typeId) . "'"));
     $result = $dbc->query($query, $this->_dbIndex);
     // Add this DR's node to the hierarchy.
     // If we don't have a default parent specified, create
     // it as a root node
     if ($this->_defaultParentId == NULL) {
         $node = $this->_hierarchy->createRootNode($id, $this->repositoryKeyType, $displayName, $description);
     } else {
         $node = $this->_hierarchy->createNode($id, $this->_defaultParentId, $this->repositoryKeyType, $displayName, $description);
     }
     $this->_createdRepositories[$id->getIdString()] = new HarmoniRepository($this, $this->_hierarchy, $id, $this->_configuration);
     return $this->_createdRepositories[$id->getIdString()];
 }
 /**
  * Creates a new Function, insertsi in the DB and caches it.
  * @param ref object functionId is externally defined
  * @param string displayName the name to display for this Function
  * @param string description the description of this Function
  * @param ref object functionType the Type of this Function
  * @param ref object qualifierHierarchyId the Id of the Qualifier Hierarchy associated with this Function
  * @return ref object Function
  */
 function createFunction($functionId, $displayName, $description, $functionType, $qualifierHierarchyId)
 {
     // ** parameter validation
     ArgumentValidator::validate($functionId, ExtendsValidatorRule::getRule("Id"), true);
     ArgumentValidator::validate($displayName, StringValidatorRule::getRule(), true);
     ArgumentValidator::validate($description, StringValidatorRule::getRule(), true);
     ArgumentValidator::validate($functionType, ExtendsValidatorRule::getRule("Type"), true);
     ArgumentValidator::validate($qualifierHierarchyId, ExtendsValidatorRule::getRule("Id"), true);
     // ** end of parameter validation
     // create the Function object
     $idManager = Services::getService("Id");
     $function = new HarmoniFunction($functionId, $displayName, $description, $functionType, $qualifierHierarchyId, $this->_dbIndex);
     // now insert into database
     $dbHandler = Services::getService("DatabaseManager");
     $idValue = $functionId->getIdString();
     // 1. Insert the type
     $domain = $functionType->getDomain();
     $authority = $functionType->getAuthority();
     $keyword = $functionType->getKeyword();
     $functionTypeDescription = $functionType->getDescription();
     // check whether the type is already in the DB, if not insert it
     $query = new SelectQuery();
     $query->addTable("type");
     $query->addColumn("type_id", "id", "type");
     $query->addWhereEqual('type.type_domain', $domain);
     $query->addWhereEqual('type.type_authority', $authority);
     $query->addWhereEqual('type.type_keyword', $keyword);
     $queryResult = $dbHandler->query($query, $this->_dbIndex);
     if ($queryResult->getNumberOfRows() > 0) {
         // if the type is already in the database
         $functionTypeIdValue = $queryResult->field("id");
         // get the id
         $queryResult->free();
     } else {
         // if not, insert it
         $query = new InsertQuery();
         $query->setTable("type");
         $query->setAutoIncrementColumn("type_id", "type_type_id_seq");
         $query->addValue("type_domain", $domain);
         $query->addValue("type_authority", $authority);
         $query->addValue("type_keyword", $keyword);
         $query->addValue("type_description", $functionTypeDescription);
         $queryResult = $dbHandler->query($query, $this->_dbIndex);
         $functionTypeIdValue = $queryResult->getLastAutoIncrementValue();
     }
     // 2. Now that we know the id of the type, insert in the DB
     $query = new InsertQuery();
     $query->setTable("az_function");
     $query->addValue("function_id", $idValue);
     $query->addValue("function_reference_name", $displayName);
     $query->addValue("function_description", $description);
     $query->addValue("fk_qualifier_hierarchy", $qualifierHierarchyId->getIdString());
     $query->addValue("fk_type", $functionTypeIdValue);
     $queryResult = $dbHandler->query($query, $this->_dbIndex);
     if ($queryResult->getNumberOfRows() != 1) {
         $err = "Could not insert into database.";
         throwError(new Error($err, "authorizarion", true));
     }
     $this->_functions[$idValue] = $function;
     return $function;
 }
 /**
  * Answer the database id for the type passed.
  * 
  * @param object Type $type
  * @return string
  * @access public
  * @since 3/1/06
  */
 function _getTypeId(Type $type)
 {
     if (!isset($this->_typeIds)) {
         $this->_typeIds = array();
     }
     if (!isset($this->_typeIds[$type->asString()])) {
         $dbc = Services::getService("DatabaseManager");
         $query = new SelectQuery();
         $query->addColumn("id");
         $query->addTable("log_type");
         $query->addWhere("domain = '" . addslashes($type->getDomain()) . "'");
         $query->addWhere("authority = '" . addslashes($type->getAuthority()) . "'");
         $query->addWhere("keyword = '" . addslashes($type->getKeyword()) . "'");
         $results = $dbc->query($query, $this->_dbIndex);
         if ($results->getNumberOfRows()) {
             $this->_typeIds[$type->asString()] = $results->field("id");
             $results->free();
         } else {
             $results->free();
             $query = new InsertQuery();
             $query->setTable("log_type");
             $query->setAutoIncrementColumn("id", "log_type_id_seq");
             $query->setColumns(array("domain", "authority", "keyword", "description"));
             $query->addRowOfValues(array("'" . addslashes($type->getDomain()) . "'", "'" . addslashes($type->getAuthority()) . "'", "'" . addslashes($type->getKeyword()) . "'", "'" . addslashes($type->getDescription()) . "'"));
             $results = $dbc->query($query, $this->_dbIndex);
             $this->_typeIds[$type->asString()] = $results->getLastAutoIncrementValue();
         }
     }
     return $this->_typeIds[$type->asString()];
 }
 /**
  * Insert [if needed] into the item table and return the database id of this
  * item
  * 
  * @return integer
  * @access public
  * @since 11/6/06
  */
 function getDatabaseId()
 {
     if (!isset($this->_dbId)) {
         $dbc = Services::getService("DatabaseManager");
         $query = new SelectQuery();
         $query->addColumn('db_id');
         $query->addTable('tag_item');
         $query->addWhere("id='" . addslashes($this->getIdString()) . "'");
         $query->addWhere("system='" . addslashes($this->getSystem()) . "'");
         $result = $dbc->query($query, $this->getDatabaseIndex());
         if ($result->getNumberOfRows() && $result->field('db_id')) {
             $this->_dbId = intval($result->field('db_id'));
         } else {
             $query = new InsertQuery();
             $query->setTable('tag_item');
             $query->setAutoIncrementColumn("db_id", "tag_item_db_id_seq");
             $query->setColumns(array('id', 'system'));
             $query->setValues(array("'" . addslashes($this->getIdString()) . "'", "'" . addslashes($this->getSystem()) . "'"));
             $result = $dbc->query($query, $this->getDatabaseIndex());
             $this->_dbId = intval($result->getLastAutoIncrementValue());
         }
     }
     return $this->_dbId;
 }
 /**
  * Creates a new Function, insertsi in the DB and caches it.
  * @param ref object functionId is externally defined
  * @param string displayName the name to display for this Function
  * @param string description the description of this Function
  * @param ref object functionType the Type of this Function
  * @param ref object qualifierHierarchyId the Id of the Qualifier Hierarchy associated with this Function
  * @return ref object Function
  */
 function createFunction($functionId, $displayName, $description, $functionType, $qualifierHierarchyId)
 {
     // ** parameter validation
     ArgumentValidator::validate($functionId, ExtendsValidatorRule::getRule("Id"), true);
     ArgumentValidator::validate($displayName, StringValidatorRule::getRule(), true);
     ArgumentValidator::validate($description, StringValidatorRule::getRule(), true);
     ArgumentValidator::validate($functionType, ExtendsValidatorRule::getRule("Type"), true);
     ArgumentValidator::validate($qualifierHierarchyId, ExtendsValidatorRule::getRule("Id"), true);
     // ** end of parameter validation
     // create the Function object
     $idManager = Services::getService("Id");
     $function = new AuthZ2_Function($functionId, $displayName, $description, $functionType, $qualifierHierarchyId, $this->_dbIndex);
     // now insert into database
     $dbHandler = Services::getService("DatabaseManager");
     $idValue = $functionId->getIdString();
     // 1. Insert the type
     $domain = $functionType->getDomain();
     $authority = $functionType->getAuthority();
     $keyword = $functionType->getKeyword();
     $functionTypeDescription = $functionType->getDescription();
     // check whether the type is already in the DB, if not insert it
     $query = new SelectQuery();
     $query->addTable("az2_function_type");
     $query->addColumn("id");
     $query->addWhereEqual('domain', $domain);
     $query->addWhereEqual('authority', $authority);
     $query->addWhereEqual('keyword', $keyword);
     $queryResult = $dbHandler->query($query, $this->_dbIndex);
     if ($queryResult->getNumberOfRows() > 0) {
         // if the type is already in the database
         $functionTypeIdValue = $queryResult->field("id");
         // get the id
         $queryResult->free();
     } else {
         // if not, insert it
         $query = new InsertQuery();
         $query->setTable("az2_function_type");
         $query->setAutoIncrementColumn("id", "az2_function_type_id_seq");
         $query->addValue("domain", $domain);
         $query->addValue("authority", $authority);
         $query->addValue("keyword", $keyword);
         $query->addValue("description", $functionTypeDescription);
         $queryResult = $dbHandler->query($query, $this->_dbIndex);
         $functionTypeIdValue = $queryResult->getLastAutoIncrementValue();
     }
     // 2. Now that we know the id of the type, insert in the DB
     try {
         $query = new InsertQuery();
         $query->setTable("az2_function");
         $query->addValue("id", $idValue);
         $query->addValue("reference_name", $displayName);
         $query->addValue("description", $description);
         $query->addValue("fk_qualifier_hierarchy", $qualifierHierarchyId->getIdString());
         $query->addValue("fk_type", $functionTypeIdValue);
         $queryResult = $dbHandler->query($query, $this->_dbIndex);
         if ($queryResult->getNumberOfRows() != 1) {
             throw new OperationFailedException("AuthorizationFunction, {$functionId}, could not be inserted. ");
         }
     } catch (DuplicateKeyDatabaseException $e) {
         throw new OperationFailedException("AuthorizationFunction, {$functionId}, already exists.");
     }
     $this->_functions[$idValue] = $function;
     return $function;
 }