Source for file HarmoniIdManager.class.php

Documentation is available at HarmoniIdManager.class.php

  1. <?php
  2.  
  3. require_once(OKI2."/osid/id/IdManager.php");
  4. require_once(OKI2."/osid/id/IdException.php");
  5.  
  6. require_once(HARMONI."oki2/shared/HarmoniId.class.php");
  7.  
  8. /**
  9. * IdManager creates and gets Ids. Ids are used in many different contexts
  10. * throughout the OSIDs. As with other Managers, use the OsidLoader to load
  11. * an implementation of this interface.
  12. *
  13. * <p>
  14. * All implementations of OsidManager (manager) provide methods for accessing
  15. * and manipulating the various objects defined in the OSID package. A manager
  16. * defines an implementation of an OSID. All other OSID objects come either
  17. * directly or indirectly from the manager. New instances of the OSID objects
  18. * are created either directly or indirectly by the manager. Because the OSID
  19. * objects are defined using interfaces, create methods must be used instead
  20. * of the new operator to create instances of the OSID objects. Create methods
  21. * are used both to instantiate and persist OSID objects. Using the
  22. * OsidManager class to define an OSID's implementation allows the application
  23. * to change OSID implementations by changing the OsidManager package name
  24. * used to load an implementation. Applications developed using managers
  25. * permit OSID implementation substitution without changing the application
  26. * source code. As with all managers, use the OsidLoader to load an
  27. * implementation of this interface.
  28. * </p>
  29. *
  30. * <p>
  31. * Unlike most Managers, IdManager does not have methods to return Type
  32. * information.
  33. * </p>
  34. *
  35. *
  36. * <p>
  37. * OSID Version: 2.0
  38. * </p>
  39. *
  40. * @package harmoni.osid_v2.id
  41. *
  42. * @copyright Copyright &copy; 2005, Middlebury College
  43. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  44. *
  45. * @version $Id: HarmoniIdManager.class.php,v 1.26 2007/09/13 16:04:20 adamfranco Exp $
  46. */
  47.  
  48. class HarmoniIdManager
  49. extends IdManager
  50. {
  51.  
  52. /**
  53. * The database connection as returned by the DBHandler.
  54. * @var integer _dbIndex
  55. * @access private
  56. */
  57. var $_dbIndex;
  58. /**
  59. * An array of all cached Id objects.
  60. * @var array _ids
  61. * @access private
  62. */
  63. var $_ids;
  64. /**
  65. * Constructor. Set up any database connections needed.
  66. * @param integer dbIndex The database connection as returned by the DBHandler.
  67. */
  68. function HarmoniIdManager() {
  69. // initialize cache
  70. $this->_ids = array();
  71. $this->_prefix = '';
  72. }
  73. /**
  74. * Assign the configuration of this Manager. Valid configuration options are as
  75. * follows:
  76. * database_index integer
  77. * database_name string
  78. *
  79. * @param object Properties $configuration (original type: java.util.Properties)
  80. *
  81. * @throws object OsidException An exception with one of the following
  82. * messages defined in org.osid.OsidException: {@link }
  83. * org.osid.OsidException#OPERATION_FAILED OPERATION_FAILED},
  84. * {@link org.osid.OsidException#PERMISSION_DENIED}
  85. * PERMISSION_DENIED}, {@link }
  86. * org.osid.OsidException#CONFIGURATION_ERROR
  87. * CONFIGURATION_ERROR}, {@link }
  88. * org.osid.OsidException#UNIMPLEMENTED UNIMPLEMENTED}, {@link }
  89. * org.osid.OsidException#NULL_ARGUMENT NULL_ARGUMENT}
  90. *
  91. * @access public
  92. */
  93. function assignConfiguration ( $configuration ) {
  94. $this->_configuration =$configuration;
  95. $dbIndex = $configuration->getProperty('database_index');
  96. $prefix = $configuration->getProperty('id_prefix');
  97. // ** parameter validation
  98. ArgumentValidator::validate($dbIndex, IntegerValidatorRule::getRule(), true);
  99. ArgumentValidator::validate($prefix, OptionalRule::getRule(
  100. StringValidatorRule::getRule(), true));
  101. // ** end of parameter validation
  102. $this->_dbIndex = $dbIndex;
  103. if ($prefix)
  104. $this->_prefix = $prefix;
  105. }
  106.  
  107. /**
  108. * Return context of this OsidManager.
  109. *
  110. * @return object OsidContext
  111. *
  112. * @throws object OsidException
  113. *
  114. * @access public
  115. */
  116. function getOsidContext () {
  117. return $this->_osidContext;
  118. }
  119.  
  120. /**
  121. * Assign the context of this OsidManager.
  122. *
  123. * @param object OsidContext $context
  124. *
  125. * @throws object OsidException An exception with one of the following
  126. * messages defined in org.osid.OsidException: {@link }
  127. * org.osid.OsidException#NULL_ARGUMENT NULL_ARGUMENT}
  128. *
  129. * @access public
  130. */
  131. function assignOsidContext ( $context ) {
  132. $this->_osidContext =$context;
  133. }
  134.  
  135. /**
  136. * Create a new unique identifier.
  137. *
  138. * @return object Id
  139. *
  140. * @throws object IdException An exception with one of the following
  141. * messages defined in org.osid.id.IdException: {@link }
  142. * org.osid.id.IdException#OPERATION_FAILED OPERATION_FAILED},
  143. * {@link org.osid.id.IdException#PERMISSION_DENIED}
  144. * PERMISSION_DENIED}, {@link }
  145. * org.osid.id.IdException#CONFIGURATION_ERROR
  146. * CONFIGURATION_ERROR}, {@link }
  147. * org.osid.id.IdException#UNIMPLEMENTED UNIMPLEMENTED}
  148. *
  149. * @access public
  150. */
  151. function createId () {
  152. debug::output("Attempting to generate new id.", 20, "IdManager");
  153. $dbHandler = Services::getService("DatabaseManager");
  154. $query = new InsertQuery();
  155. $query->setAutoIncrementColumn("id_value", "id_id_value_seq");
  156. $query->setTable("id");
  157. $query->addRowOfValues(array());
  158. $result =$dbHandler->query($query,$this->_dbIndex);
  159. if ($result->getNumberOfRows() != 1) {
  160. throwError( new Error(IdException::CONFIGURATION_ERROR(), "IdManager", true));
  161. }
  162. $newID = $result->getLastAutoIncrementValue();
  163. // Clear out any values smaller than our last one to keep the table from
  164. // exploding size.
  165. $query = new DeleteQuery();
  166. $query->setTable("id");
  167. $query->setWhere("id_value < '".$newID."'");
  168. $result =$dbHandler->query($query,$this->_dbIndex);
  169. $newID = $this->_prefix.strval($newID);
  170. debug::output("Successfully created new id '$newID'.",DEBUG_SYS5,"IdManager");
  171. $id = new HarmoniId($newID);
  172. // cache the id
  173. // $this->_ids[$newID] = $id;
  174. return $id;
  175. }
  176.  
  177. /**
  178. * Get the unique Id with this String representation or create a new unique
  179. * Id with this representation.
  180. *
  181. * @param string $idString
  182. *
  183. * @return object Id
  184. *
  185. * @throws object IdException An exception with one of the following
  186. * messages defined in org.osid.id.IdException: {@link }
  187. * org.osid.id.IdException#OPERATION_FAILED OPERATION_FAILED},
  188. * {@link org.osid.id.IdException#PERMISSION_DENIED}
  189. * PERMISSION_DENIED}, {@link }
  190. * org.osid.id.IdException#CONFIGURATION_ERROR
  191. * CONFIGURATION_ERROR}, {@link }
  192. * org.osid.id.IdException#UNIMPLEMENTED UNIMPLEMENTED}, {@link }
  193. * org.osid.id.IdException#NULL_ARGUMENT NULL_ARGUMENT}
  194. *
  195. * @access public
  196. */
  197. function getId ( $idString ) {
  198. // if (isset($this->_ids[$idString])) {
  199. // print "id:". $idString." and ".$this->_ids[$idString]->getIdString()."<br/>";
  200. // return $this->_ids[$idString];
  201. // }
  202. ArgumentValidator::validate($idString,
  203. OrValidatorRule::getRule(
  204. NonzeroLengthStringValidatorRule::getRule(),
  205. NumericValidatorRule::getRule()));
  206. $id = new HarmoniId($idString);
  207. // cache the id
  208. // $this->_ids[$idString] = $id;
  209. return $id;
  210. }
  211. }

Documentation generated on Wed, 19 Sep 2007 10:24:04 -0400 by phpDocumentor 1.3.0RC3