Source for file HarmoniHierarchyManager.class.php

Documentation is available at HarmoniHierarchyManager.class.php

  1. <?php
  2.  
  3. require_once(OKI2."/osid/hierarchy/HierarchyManager.php");
  4. require_once(OKI2."/osid/hierarchy/HierarchyException.php");
  5.  
  6. require_once(HARMONI."oki2/hierarchy/HarmoniHierarchy.class.php");
  7. require_once(HARMONI."oki2/hierarchy/HarmoniHierarchyIterator.class.php");
  8. require_once(HARMONI."oki2/hierarchy/HarmoniNodeIterator.class.php");
  9. require_once(HARMONI."oki2/hierarchy/HarmoniTraversalInfoIterator.class.php");
  10.  
  11. require_once(HARMONI.'/oki2/id/HarmoniIdManager.class.php');
  12.  
  13. /**
  14. * <p>
  15. * HierarchyManager handles creating, deleting, and getting Hierarchies.
  16. * </p>
  17. *
  18. * <p>
  19. * All implementations of OsidManager (manager) provide methods for accessing
  20. * and manipulating the various objects defined in the OSID package. A manager
  21. * defines an implementation of an OSID. All other OSID objects come either
  22. * directly or indirectly from the manager. New instances of the OSID objects
  23. * are created either directly or indirectly by the manager. Because the OSID
  24. * objects are defined using interfaces, create methods must be used instead
  25. * of the new operator to create instances of the OSID objects. Create methods
  26. * are used both to instantiate and persist OSID objects. Using the
  27. * OsidManager class to define an OSID's implementation allows the application
  28. * to change OSID implementations by changing the OsidManager package name
  29. * used to load an implementation. Applications developed using managers
  30. * permit OSID implementation substitution without changing the application
  31. * source code. As with all managers, use the OsidLoader to load an
  32. * implementation of this interface.
  33. * </p>
  34. *
  35. * <p></p>
  36. *
  37. * <p>
  38. * OSID Version: 2.0
  39. * </p>
  40. *
  41. *
  42. * @package harmoni.osid_v2.hierarchy
  43. *
  44. * @copyright Copyright &copy; 2005, Middlebury College
  45. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  46. *
  47. * @version $Id: HarmoniHierarchyManager.class.php,v 1.28 2007/09/13 16:04:20 adamfranco Exp $
  48. */
  49. class HarmoniHierarchyManager
  50. extends HierarchyManager {
  51.  
  52.  
  53. /**
  54. * The database connection as returned by the DBHandler.
  55. * @var integer _dbIndex
  56. * @access protected
  57. */
  58. var $_dbIndex;
  59. /**
  60. * An array that will store all hierarchies and fulfil the function of a
  61. * cache.
  62. * @var array _hierarchies
  63. * @access private
  64. */
  65. var $_hierarchies;
  66. /**
  67. * TRUE if all hiearchies are cached.
  68. * @var array _hierarchies
  69. * @access private
  70. */
  71. var $_allHierarchiesCached;
  72. /**
  73. * Constructor
  74. * @param integer dbIndex The database connection as returned by the DBHandler.
  75. * @param string hyDB The name of the hierarchy database.
  76. * manager.
  77. * @access public
  78. */
  79. function HarmoniHierarchyManager () {
  80. $this->_hierarchies = array();
  81. $this->_allHierarchiesCached = FALSE;
  82. }
  83. /**
  84. * Assign the configuration of this Manager. Valid configuration options are as
  85. * follows:
  86. * database_index integer
  87. * database_name string
  88. *
  89. * @param object Properties $configuration (original type: java.util.Properties)
  90. *
  91. * @throws object OsidException An exception with one of the following
  92. * messages defined in org.osid.OsidException: {@link }
  93. * org.osid.OsidException#OPERATION_FAILED OPERATION_FAILED},
  94. * {@link org.osid.OsidException#PERMISSION_DENIED}
  95. * PERMISSION_DENIED}, {@link }
  96. * org.osid.OsidException#CONFIGURATION_ERROR
  97. * CONFIGURATION_ERROR}, {@link }
  98. * org.osid.OsidException#UNIMPLEMENTED UNIMPLEMENTED}, {@link }
  99. * org.osid.OsidException#NULL_ARGUMENT NULL_ARGUMENT}
  100. *
  101. * @access public
  102. */
  103. function assignConfiguration ( $configuration ) {
  104. $this->_configuration =$configuration;
  105. $dbIndex =$configuration->getProperty('database_index');
  106. $dbName =$configuration->getProperty('database_name');
  107. // ** parameter validation
  108. ArgumentValidator::validate($dbIndex, IntegerValidatorRule::getRule(), true);
  109. ArgumentValidator::validate($dbName, StringValidatorRule::getRule(), true);
  110. // ** end of parameter validation
  111. $this->_dbIndex = $dbIndex;
  112. }
  113.  
  114. /**
  115. * Return context of this OsidManager.
  116. *
  117. * @return object OsidContext
  118. *
  119. * @throws object OsidException
  120. *
  121. * @access public
  122. */
  123. function getOsidContext () {
  124. return $this->_osidContext;
  125. }
  126.  
  127. /**
  128. * Assign the context of this OsidManager.
  129. *
  130. * @param object OsidContext $context
  131. *
  132. * @throws object OsidException An exception with one of the following
  133. * messages defined in org.osid.OsidException: {@link }
  134. * org.osid.OsidException#NULL_ARGUMENT NULL_ARGUMENT}
  135. *
  136. * @access public
  137. */
  138. function assignOsidContext ( $context ) {
  139. $this->_osidContext =$context;
  140. }
  141.  
  142. /**
  143. * Create a Hierarchy.
  144. *
  145. * @param string $displayName
  146. * @param object Type[] $nodeTypes
  147. * @param string $description
  148. * @param boolean $allowsMultipleParents
  149. * @param boolean $allowsRecursion
  150. * @param optional object Id $id WARNING: NOT IN OSID
  151. *
  152. * @return object Hierarchy
  153. *
  154. * @throws object HierarchyException An exception with one of
  155. * the following messages defined in
  156. * org.osid.hierarchy.HierarchyException may be thrown: {@link }
  157. * org.osid.hierarchy.HierarchyException#OPERATION_FAILED
  158. * OPERATION_FAILED}, {@link }
  159. * org.osid.hierarchy.HierarchyException#PERMISSION_DENIED
  160. * PERMISSION_DENIED}, {@link }
  161. * org.osid.hierarchy.HierarchyException#CONFIGURATION_ERROR
  162. * CONFIGURATION_ERROR}, {@link }
  163. * org.osid.hierarchy.HierarchyException#UNIMPLEMENTED
  164. * UNIMPLEMENTED}, {@link }
  165. * org.osid.hierarchy.HierarchyException#NULL_ARGUMENT
  166. * NULL_ARGUMENT}, {@link }
  167. * org.osid.hierarchy.HierarchyException#UNSUPPORTED_CREATION
  168. * UNSUPPORTED_CREATION}
  169. *
  170. * @access public
  171. */
  172. function createHierarchy ( $displayName, $nodeTypes, $description, $allowsMultipleParents, $allowsRecursion, $id = NULL ) {
  173. // ** parameter validation
  174. ArgumentValidator::validate($description, StringValidatorRule::getRule(), true);
  175. ArgumentValidator::validate($displayName, StringValidatorRule::getRule(), true);
  176. ArgumentValidator::validate($allowsMultipleParents, BooleanValidatorRule::getRule(), true);
  177. ArgumentValidator::validate($allowsRecursion, BooleanValidatorRule::getRule(), true);
  178. ArgumentValidator::validate($id, OptionalRule::getRule(
  179. ExtendsValidatorRule::getRule("Id")), true);
  180. // ** end of parameter validation
  181.  
  182. // check for supported hierarchies
  183. if ($allowsRecursion)
  184. throwError(new Error(HierarchyException::UNSUPPORTED_CREATION(), "HierarchyManager", 1));
  185. $dbHandler = Services::getService("DatabaseManager");
  186.  
  187. // Create an Id for the Hierarchy
  188. if (!is_object($id)) {
  189. $idManager = Services::getService("Id");
  190. $id =$idManager->createId();
  191. }
  192. $idValue = $id->getIdString();
  193.  
  194. $query = new InsertQuery();
  195. $query->setTable("hierarchy");
  196. $columns = array();
  197. $columns[] = "hierarchy_id";
  198. $columns[] = "hierarchy_display_name";
  199. $columns[] = "hierarchy_description";
  200. $columns[] = "hierarchy_multiparent";
  201. $query->setColumns($columns);
  202. $values = array();
  203. $values[] = "'".addslashes($idValue)."'";
  204. $values[] = "'".addslashes($displayName)."'";
  205. $values[] = "'".addslashes($description)."'";
  206. $multiparent = ($allowsMultipleParents) ? '1' : '0';
  207. $values[] = "'".$multiparent."'";
  208. $query->setValues($values);
  209.  
  210. $queryResult =$dbHandler->query($query, $this->_dbIndex);
  211. // Create a new hierarchy and insert it into the database
  212. $hierarchy = new HarmoniHierarchy($id, $displayName, $description,
  213. new HierarchyCache($idValue, $allowsMultipleParents, $this->_dbIndex,
  214. DateAndTime::now()));
  215. // then cache it
  216. $this->_hierarchies[$idValue] =$hierarchy;
  217. return $hierarchy;
  218. }
  219.  
  220. /**
  221. * Get a Hierarchy by unique Id.
  222. *
  223. * @param object Id $hierarchyId
  224. *
  225. * @return object Hierarchy
  226. *
  227. * @throws object HierarchyException An exception with one of
  228. * the following messages defined in
  229. * org.osid.hierarchy.HierarchyException may be thrown: {@link }
  230. * org.osid.hierarchy.HierarchyException#OPERATION_FAILED
  231. * OPERATION_FAILED}, {@link }
  232. * org.osid.hierarchy.HierarchyException#PERMISSION_DENIED
  233. * PERMISSION_DENIED}, {@link }
  234. * org.osid.hierarchy.HierarchyException#CONFIGURATION_ERROR
  235. * CONFIGURATION_ERROR}, {@link }
  236. * org.osid.hierarchy.HierarchyException#UNIMPLEMENTED
  237. * UNIMPLEMENTED}, {@link }
  238. * org.osid.hierarchy.HierarchyException#NULL_ARGUMENT
  239. * NULL_ARGUMENT}, {@link }
  240. * org.osid.hierarchy.HierarchyException#NODE_TYPE_NOT_FOUND
  241. * NODE_TYPE_NOT_FOUND}
  242. *
  243. * @access public
  244. */
  245. function getHierarchy ( $hierarchyId ) {
  246. // ** parameter validation
  247. ArgumentValidator::validate($hierarchyId, ExtendsValidatorRule::getRule("Id"), true);
  248. // ** end of parameter validation
  249.  
  250. $idValue = $hierarchyId->getIdString();
  251. // check the cache
  252. if (isset($this->_hierarchies[$idValue]))
  253. return $this->_hierarchies[$idValue];
  254.  
  255. $dbHandler = Services::getService("DatabaseManager");
  256. $query = new SelectQuery();
  257. $query->addColumn("hierarchy_id", "id", "hierarchy");
  258. $query->addColumn("hierarchy_display_name", "display_name", "hierarchy");
  259. $query->addColumn("hierarchy_description", "description", "hierarchy");
  260. $query->addColumn("hierarchy_multiparent", "multiparent", "hierarchy");
  261. $query->addTable("hierarchy");
  262. $query->addWhere("hierarchy.hierarchy_id = '{$idValue}'");
  263.  
  264. $queryResult =$dbHandler->query($query, $this->_dbIndex);
  265. if ($queryResult->getNumberOfRows() != 1) {
  266. $queryResult->free();
  267. throwError(new Error(HierarchyException::UNKNOWN_ID(), "Hierarchy", 1));
  268. }
  269. $row = $queryResult->getCurrentrow();
  270. $queryResult->free();
  271.  
  272. $idValue =$row['id'];
  273. $idManager = Services::getService("Id");
  274. $id = $idManager->getId($idValue);
  275. $allowsMultipleParents = ($row['multiparent'] == '1');
  276. $cache = new HierarchyCache($idValue, $allowsMultipleParents, $this->_dbIndex);
  277. $hierarchy = new HarmoniHierarchy($id, $row['display_name'], $row['description'], $cache);
  278.  
  279. // cache it
  280. $this->_hierarchies[$idValue] =$hierarchy;
  281.  
  282. return $hierarchy;
  283. }
  284.  
  285. /**
  286. * Get all Hierarchies.
  287. *
  288. * @return object HierarchyIterator
  289. *
  290. * @throws object HierarchyException An exception with one of
  291. * the following messages defined in
  292. * org.osid.hierarchy.HierarchyException may be thrown: {@link }
  293. * org.osid.hierarchy.HierarchyException#OPERATION_FAILED
  294. * OPERATION_FAILED}, {@link }
  295. * org.osid.hierarchy.HierarchyException#PERMISSION_DENIED
  296. * PERMISSION_DENIED}, {@link }
  297. * org.osid.hierarchy.HierarchyException#CONFIGURATION_ERROR
  298. * CONFIGURATION_ERROR}, {@link }
  299. * org.osid.hierarchy.HierarchyException#UNIMPLEMENTED
  300. * UNIMPLEMENTED}
  301. *
  302. * @access public
  303. */
  304. function getHierarchies () {
  305. if(!$this->_allHierarchiesCached) {
  306. $dbHandler = Services::getService("DatabaseManager");
  307. $query = new SelectQuery();
  308. $query->addColumn("hierarchy_id", "id", "hierarchy");
  309. $query->addColumn("hierarchy_display_name", "display_name", "hierarchy");
  310. $query->addColumn("hierarchy_description", "description", "hierarchy");
  311. $query->addColumn("hierarchy_multiparent", "multiparent", "hierarchy");
  312. $query->addTable("hierarchy");
  313. $queryResult =$dbHandler->query($query, $this->_dbIndex);
  314. $hierarchies = array();
  315. $idManager = Services::getService("Id");
  316. while ($queryResult->hasMoreRows()) {
  317. $row = $queryResult->getCurrentrow();
  318. $idValue =$row['id'];
  319. // check the cache
  320. if (!isset($this->_hierarchies[$idValue])) {
  321. $id =$idManager->getId($idValue);
  322. $allowsMultipleParents = ($row['multiparent'] == '1');
  323. $cache = new HierarchyCache($idValue, $allowsMultipleParents, $this->_dbIndex);
  324. $hierarchy = new HarmoniHierarchy($id, $row['display_name'], $row['description'], $cache);
  325. $this->_hierarchies[$idValue] =$hierarchy;
  326. }
  327. $queryResult->advanceRow();
  328. }
  329. $queryResult->free();
  330. $this->_allHierarchiesCached = true;
  331. }
  332. $obj = new HarmoniHierarchyIterator($this->_hierarchies);
  333. return $obj;
  334. }
  335.  
  336. /**
  337. * Delete a Hierarchy by unique Id. All Nodes must be removed from the
  338. * Hierarchy before this method is called.
  339. *
  340. * @param object Id $hierarchyId
  341. *
  342. * @throws object HierarchyException An exception with one of
  343. * the following messages defined in
  344. * org.osid.hierarchy.HierarchyException may be thrown: {@link }
  345. * org.osid.hierarchy.HierarchyException#OPERATION_FAILED
  346. * OPERATION_FAILED}, {@link }
  347. * org.osid.hierarchy.HierarchyException#PERMISSION_DENIED
  348. * PERMISSION_DENIED}, {@link }
  349. * org.osid.hierarchy.HierarchyException#CONFIGURATION_ERROR
  350. * CONFIGURATION_ERROR}, {@link }
  351. * org.osid.hierarchy.HierarchyException#UNIMPLEMENTED
  352. * UNIMPLEMENTED}, {@link }
  353. * org.osid.hierarchy.HierarchyException#NULL_ARGUMENT
  354. * NULL_ARGUMENT}, {@link }
  355. * org.osid.hierarchy.HierarchyException#NODE_TYPE_NOT_FOUND
  356. * NODE_TYPE_NOT_FOUND}, {@link }
  357. * org.osid.hierarchy.HierarchyException#HIERARCHY_NOT_EMPTY
  358. * HIERARCHY_NOT_EMPTY}
  359. *
  360. * @access public
  361. */
  362. function deleteHierarchy ( $hierarchyId ) {
  363. // ** parameter validation
  364. ArgumentValidator::validate($hierarchyId, ExtendsValidatorRule::getRule("Id"), true);
  365. // ** end of parameter validation
  366. $dbHandler = Services::getService("DatabaseManager");
  367. $idValue = $hierarchyId->getIdString();
  368. // see if there are any nodes remaining that have to removed
  369. $query = new SelectQuery();
  370. $query->addTable("node");
  371. $query->addColumn("COUNT({$db}node.node_id)", "num");
  372. $query->addWhere("{$db}node.fk_hierarchy = '{$idValue}'");
  373.  
  374. $queryResult =$dbHandler->query($query, $this->_dbIndex);
  375. $row = $queryResult->getCurrentRow();
  376. $queryResult->free();
  377.  
  378. // if the hierarchy contains any nodes, cannot delete
  379. if ($row['num'] > 0) {
  380. throwError(new Error(HierarchyException::HIERARCHY_NOT_EMPTY(), "Hierarchy", true));
  381. return;
  382. }
  383. // now delete it
  384. $query = new DeleteQuery();
  385. $query->setTable("hierarchy");
  386. $query->addWhere("{$db}hierarchy.hierarchy_id = '{$idValue}'");
  387. $queryResult =$dbHandler->query($query, $this->_dbIndex);
  388. if ($queryResult->getNumberOfRows() != 1) {
  389. throwError(new Error(HierarchyException::OPERATION_FAILED(), "Hierarchy", true));
  390. }
  391. // update the cache
  392. $this->_hierarchies[$idValue] = null;
  393. unset($this->_hierarchies[$idValue]);
  394. }
  395. /**
  396. * This method indicates whether this implementation supports
  397. * HierarchyManager methods: createHierarchy, deleteHierarchy, updateName,
  398. * updateDescription, createRootNode, createNode, deleteNode, addNodeType,
  399. * removeNodeType. Note methods: nodeUpdateDescription,
  400. * noteUpdateDisplayName, addParent, removeParent, changeParent.
  401. *
  402. * @return boolean
  403. *
  404. * @throws object HierarchyException An exception with one of
  405. * the following messages defined in
  406. * org.osid.hierarchy.HierarchyException may be thrown: {@link }
  407. * org.osid.hierarchy.HierarchyException#OPERATION_FAILED
  408. * OPERATION_FAILED}, {@link }
  409. * org.osid.hierarchy.HierarchyException#PERMISSION_DENIED
  410. * PERMISSION_DENIED}, {@link }
  411. * org.osid.hierarchy.HierarchyException#CONFIGURATION_ERROR
  412. * CONFIGURATION_ERROR}, {@link }
  413. * org.osid.hierarchy.HierarchyException#UNIMPLEMENTED
  414. * UNIMPLEMENTED}
  415. *
  416. * @access public
  417. */
  418. function supportsMaintenance () {
  419. return TRUE;
  420. }
  421. /**
  422. * Returns the hierarchy Node with the specified Id.
  423. *
  424. * WARNING: NOT IN OSID - As of Version 2.0, this method has been removed
  425. * from the OSID.
  426. *
  427. * @access public
  428. * @param ref object id The Id object.
  429. * @return ref object The Node with the given Id.
  430. ***/
  431. function getNode($id) {
  432. // ** parameter validation
  433. ArgumentValidator::validate($id, ExtendsValidatorRule::getRule("Id"), true);
  434. // ** end of parameter validation
  435.  
  436. $idValue = $id->getIdString();
  437. $dbHandler = Services::getService("DatabaseManager");
  438.  
  439. // find the hierarchy id for this node
  440. $query = new SelectQuery();
  441. $query->addColumn("fk_hierarchy", "hierarchy_id", "node");
  442. $query->addTable("node");
  443. $joinc = "node.fk_hierarchy = "."hierarchy.hierarchy_id";
  444. $query->addTable("hierarchy", INNER_JOIN, $joinc);
  445. $where = "node.node_id = '".addslashes($idValue)."'";
  446. $query->addWhere($where);
  447. $nodeQueryResult =$dbHandler->query($query, $this->_dbIndex);
  448. if ($nodeQueryResult->getNumberOfRows() != 1) {
  449. $nodeQueryResult->free();
  450. throwError(new Error(HierarchyException::OPERATION_FAILED()." Could not find node of id, '".$id->getIdString()."'.",
  451. "Hierarchy", true));
  452. }
  453. $nodeRow = $nodeQueryResult->getCurrentRow();
  454. $nodeQueryResult->free();
  455.  
  456. $idManager = Services::getService("Id");
  457.  
  458. $hierarchyId = $nodeRow['hierarchy_id'];
  459.  
  460. // get the hierarchy
  461. $hierarchy =$this->getHierarchy($idManager->getId($hierarchyId));
  462. $node =$hierarchy->getNode($id);
  463.  
  464. return $node;
  465. }
  466. /**
  467. * Answer TRUE if the a node exists with the given Id
  468. *
  469. * WARNING: NOT in OSID
  470. *
  471. * @param object Id $nodeId
  472. *
  473. * @return boolean
  474. *
  475. * @access public
  476. */
  477. function nodeExists ( $nodeId ) {
  478. // ** parameter validation
  479. ArgumentValidator::validate($nodeId, ExtendsValidatorRule::getRule("Id"), true);
  480. // ** end of parameter validation
  481. $hierarchies =$this->getHierarchies();
  482. while ($hierarchies->hasNext()) {
  483. $hierarchy =$hierarchies->next();
  484. if ($hierarchy->nodeExists($nodeId))
  485. return true;
  486. }
  487. return false;
  488. }
  489. /**
  490. * Returns the hierarchy to which the given Node belongs.
  491. *
  492. * WARNING: NOT IN OSID - This method is not part of the OKI interface as
  493. * of 07/06/04 but has been scheduled for addition.
  494. *
  495. * Note: As of version 2.0, the getNode() method has been removed from the
  496. * OSID, removing the need for this method.
  497. *
  498. * @access public
  499. * @return ref object The Hierarchy to which the Node belongs.
  500. ***/
  501. function getHierarchyForNode($node) {
  502. $idManager = Services::getService("Id");
  503. $hierarchyId =$idManager->getId($node->_cache->_hierarchyId);
  504. $hierarchy =$this->getHierarchy($hierarchyId);
  505. return $hierarchy;
  506. }
  507. }
  508.  
  509. ?>

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