Source for file HarmoniNode.class.php

Documentation is available at HarmoniNode.class.php

  1. <?php
  2.  
  3. require_once(OKI2."/osid/hierarchy/Node.php");
  4. require_once(HARMONI."oki2/hierarchy/HierarchyCache.class.php");
  5. require_once(HARMONI."oki2/hierarchy/HarmoniNodeIterator.class.php");
  6. require_once(HARMONI."oki2/hierarchy/tree/Tree.class.php");
  7. require_once(HARMONI."oki2/hierarchy/DefaultNodeType.class.php");
  8.  
  9. /**
  10. * Node is a Hierarchy's representation of an external object that is one of a
  11. * number of similar objects to be organized. Nodes must be connected to a
  12. * Hierarchy.
  13. *
  14. * <p>
  15. * OSID Version: 2.0
  16. * </p>
  17. *
  18. * @package harmoni.osid_v2.hierarchy
  19. *
  20. * @copyright Copyright &copy; 2005, Middlebury College
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  22. *
  23. * @version $Id: HarmoniNode.class.php,v 1.20 2007/09/13 16:04:20 adamfranco Exp $
  24. */
  25.  
  26. class HarmoniNode
  27. extends Node
  28. {
  29.  
  30. /**
  31. * The Id of this node.
  32. * @var object _id
  33. * @access private
  34. */
  35. var $_id;
  36. /**
  37. * The type of this node.
  38. * @var object _type
  39. * @access private
  40. */
  41. var $_type;
  42. /**
  43. * The description for this node.
  44. * @var string $_description
  45. */
  46. var $_description;
  47.  
  48. /**
  49. * The display name for this node.
  50. * @var string $_displayName
  51. */
  52. var $_displayName;
  53.  
  54. /**
  55. * This is the HierarchyCache object. Must be the same
  56. * one that all other nodes in the Hierarchy are using.
  57. * @var object _cache
  58. * @access private
  59. */
  60. var $_cache;
  61.  
  62. /**
  63. * Constructor.
  64. *
  65. * @param ref object id The Id of this Node.
  66. * @param ref object type The Type of the new Node.
  67. * @param string displayName The displayName of the Node.
  68. * @param string description The description of the Node.
  69. * @param ref object cache This is the HierarchyCache object. Must be the same
  70. * one that all other nodes in the Hierarchy are using.
  71. */
  72. function HarmoniNode($id, $type, $displayName, $description, $cache) {
  73. // ** parameter validation
  74. ArgumentValidator::validate($id, ExtendsValidatorRule::getRule("Id"), true);
  75. ArgumentValidator::validate($type, ExtendsValidatorRule::getRule("Type"), true);
  76. ArgumentValidator::validate($displayName, StringValidatorRule::getRule(), true);
  77. ArgumentValidator::validate($description, StringValidatorRule::getRule(), true);
  78. ArgumentValidator::validate($cache, ExtendsValidatorRule::getRule("HierarchyCache"), true);
  79. // ** end of parameter validation
  80. // set the private variables
  81. $this->_id =$id;
  82. $this->_type =$type;
  83. $this->_displayName = $displayName;
  84. $this->_description = $description;
  85. $this->_cache =$cache;
  86. }
  87.  
  88. /**
  89. * Get the unique Id for this Node.
  90. *
  91. * @return object Id
  92. *
  93. * @throws object HierarchyException An exception with one of
  94. * the following messages defined in
  95. * org.osid.hierarchy.HierarchyException may be thrown: {@link }
  96. * org.osid.hierarchy.HierarchyException#OPERATION_FAILED
  97. * OPERATION_FAILED}, {@link }
  98. * org.osid.hierarchy.HierarchyException#PERMISSION_DENIED
  99. * PERMISSION_DENIED}, {@link }
  100. * org.osid.hierarchy.HierarchyException#CONFIGURATION_ERROR
  101. * CONFIGURATION_ERROR}, {@link }
  102. * org.osid.hierarchy.HierarchyException#UNIMPLEMENTED
  103. * UNIMPLEMENTED}
  104. *
  105. * @access public
  106. */
  107. function getId () {
  108. return $this->_id;
  109. }
  110.  
  111. /**
  112. * Get the display name for this Node.
  113. *
  114. * @return string
  115. *
  116. * @throws object HierarchyException An exception with one of
  117. * the following messages defined in
  118. * org.osid.hierarchy.HierarchyException may be thrown: {@link }
  119. * org.osid.hierarchy.HierarchyException#OPERATION_FAILED
  120. * OPERATION_FAILED}, {@link }
  121. * org.osid.hierarchy.HierarchyException#PERMISSION_DENIED
  122. * PERMISSION_DENIED}, {@link }
  123. * org.osid.hierarchy.HierarchyException#CONFIGURATION_ERROR
  124. * CONFIGURATION_ERROR}, {@link }
  125. * org.osid.hierarchy.HierarchyException#UNIMPLEMENTED
  126. * UNIMPLEMENTED}
  127. *
  128. * @access public
  129. */
  130. function getDisplayName () {
  131. return $this->_displayName;
  132. }
  133.  
  134. /**
  135. * Get the description for this Node.
  136. *
  137. * @return string
  138. *
  139. * @throws object HierarchyException An exception with one of
  140. * the following messages defined in
  141. * org.osid.hierarchy.HierarchyException may be thrown: {@link }
  142. * org.osid.hierarchy.HierarchyException#OPERATION_FAILED
  143. * OPERATION_FAILED}, {@link }
  144. * org.osid.hierarchy.HierarchyException#PERMISSION_DENIED
  145. * PERMISSION_DENIED}, {@link }
  146. * org.osid.hierarchy.HierarchyException#CONFIGURATION_ERROR
  147. * CONFIGURATION_ERROR}, {@link }
  148. * org.osid.hierarchy.HierarchyException#UNIMPLEMENTED
  149. * UNIMPLEMENTED}
  150. *
  151. * @access public
  152. */
  153. function getDescription () {
  154. return $this->_description;
  155. }
  156.  
  157. /**
  158. * Get the Type for this Node.
  159. *
  160. * @return object Type
  161. *
  162. * @throws object HierarchyException An exception with one of
  163. * the following messages defined in
  164. * org.osid.hierarchy.HierarchyException may be thrown: {@link }
  165. * org.osid.hierarchy.HierarchyException#OPERATION_FAILED
  166. * OPERATION_FAILED}, {@link }
  167. * org.osid.hierarchy.HierarchyException#PERMISSION_DENIED
  168. * PERMISSION_DENIED}, {@link }
  169. * org.osid.hierarchy.HierarchyException#CONFIGURATION_ERROR
  170. * CONFIGURATION_ERROR}, {@link }
  171. * org.osid.hierarchy.HierarchyException#UNIMPLEMENTED
  172. * UNIMPLEMENTED}
  173. *
  174. * @access public
  175. */
  176. function getType () {
  177. return $this->_type;
  178. }
  179.  
  180. /**
  181. * Get the parents of this Node. To get other ancestors use the Hierarchy
  182. * traverse method.
  183. *
  184. * @return object NodeIterator
  185. *
  186. * @throws object HierarchyException An exception with one of
  187. * the following messages defined in
  188. * org.osid.hierarchy.HierarchyException may be thrown: {@link }
  189. * org.osid.hierarchy.HierarchyException#OPERATION_FAILED
  190. * OPERATION_FAILED}, {@link }
  191. * org.osid.hierarchy.HierarchyException#PERMISSION_DENIED
  192. * PERMISSION_DENIED}, {@link }
  193. * org.osid.hierarchy.HierarchyException#CONFIGURATION_ERROR
  194. * CONFIGURATION_ERROR}, {@link }
  195. * org.osid.hierarchy.HierarchyException#UNIMPLEMENTED
  196. * UNIMPLEMENTED}
  197. *
  198. * @access public
  199. */
  200. function getParents () {
  201. $idValue = $this->_id->getIdString();
  202. // get the children (cache them if necessary)
  203. $children =$this->_cache->getParents($this);
  204. $result = new HarmoniNodeIterator($children);
  205.  
  206. return $result;
  207. }
  208.  
  209. /**
  210. * Get the children of this Node. To get other descendants use the
  211. * Hierarchy traverse method.
  212. *
  213. * @return object NodeIterator
  214. *
  215. * @throws object HierarchyException An exception with one of
  216. * the following messages defined in
  217. * org.osid.hierarchy.HierarchyException may be thrown: {@link }
  218. * org.osid.hierarchy.HierarchyException#OPERATION_FAILED
  219. * OPERATION_FAILED}, {@link }
  220. * org.osid.hierarchy.HierarchyException#PERMISSION_DENIED
  221. * PERMISSION_DENIED}, {@link }
  222. * org.osid.hierarchy.HierarchyException#CONFIGURATION_ERROR
  223. * CONFIGURATION_ERROR}, {@link }
  224. * org.osid.hierarchy.HierarchyException#UNIMPLEMENTED
  225. * UNIMPLEMENTED}
  226. *
  227. * @access public
  228. */
  229. function getChildren () {
  230. $idValue = $this->_id->getIdString();
  231. // get the children (cache them if necessary)
  232. $children =$this->_cache->getChildren($this);
  233. $result = new HarmoniNodeIterator($children);
  234.  
  235. return $result;
  236. }
  237.  
  238. /**
  239. * Update the description of this Node.
  240. *
  241. * @param string $description
  242. *
  243. * @throws object HierarchyException An exception with one of
  244. * the following messages defined in
  245. * org.osid.hierarchy.HierarchyException may be thrown: {@link }
  246. * org.osid.hierarchy.HierarchyException#OPERATION_FAILED
  247. * OPERATION_FAILED}, {@link }
  248. * org.osid.hierarchy.HierarchyException#PERMISSION_DENIED
  249. * PERMISSION_DENIED}, {@link }
  250. * org.osid.hierarchy.HierarchyException#CONFIGURATION_ERROR
  251. * CONFIGURATION_ERROR}, {@link }
  252. * org.osid.hierarchy.HierarchyException#UNIMPLEMENTED
  253. * UNIMPLEMENTED}, {@link }
  254. * org.osid.hierarchy.HierarchyException#NULL_ARGUMENT
  255. * NULL_ARGUMENT}
  256. *
  257. * @access public
  258. */
  259. function updateDescription ( $description ) {
  260. // ** parameter validation
  261. $stringRule = StringValidatorRule::getRule();
  262. ArgumentValidator::validate($description, $stringRule, true);
  263. // ** end of parameter validation
  264. if ($this->_description == $description)
  265. return; // nothing to update
  266.  
  267. // update the object
  268. $this->_description = $description;
  269.  
  270. // update the database
  271. $dbHandler = Services::getService("DatabaseManager");
  272. $query = new UpdateQuery();
  273. $query->setTable("node");
  274. $id =$this->getId();
  275. $idValue = $id->getIdString();
  276. $where = "node_id = '".addslashes($idValue)."'";
  277. $query->setWhere($where);
  278. $query->setColumns(array("node_description"));
  279. $query->setValues(array("'".addslashes($description)."'"));
  280. $queryResult =$dbHandler->query($query, $this->_cache->_dbIndex);
  281. if ($queryResult->getNumberOfRows() == 0)
  282. throwError(new Error(HierarchyException::OPERATION_FAILED(),"Hierarchy",true));
  283. if ($queryResult->getNumberOfRows() > 1)
  284. throwError(new Error(HierarchyException::OPERATION_FAILED() ,"Hierarchy",true));
  285. }
  286.  
  287. /**
  288. * Update the name of this Node. Node name changes are permitted since the
  289. * Hierarchy's integrity is based on the Node's unique Id.
  290. *
  291. * @param string $displayName
  292. *
  293. * @throws object HierarchyException An exception with one of
  294. * the following messages defined in
  295. * org.osid.hierarchy.HierarchyException may be thrown: {@link }
  296. * org.osid.hierarchy.HierarchyException#OPERATION_FAILED
  297. * OPERATION_FAILED}, {@link }
  298. * org.osid.hierarchy.HierarchyException#PERMISSION_DENIED
  299. * PERMISSION_DENIED}, {@link }
  300. * org.osid.hierarchy.HierarchyException#CONFIGURATION_ERROR
  301. * CONFIGURATION_ERROR}, {@link }
  302. * org.osid.hierarchy.HierarchyException#UNIMPLEMENTED
  303. * UNIMPLEMENTED}, {@link }
  304. * org.osid.hierarchy.HierarchyException#NULL_ARGUMENT
  305. * NULL_ARGUMENT}
  306. *
  307. * @access public
  308. */
  309. function updateDisplayName ( $displayName ) {
  310. // ** parameter validation
  311. $stringRule = StringValidatorRule::getRule();
  312. ArgumentValidator::validate($displayName, $stringRule, true);
  313. // ** end of parameter validation
  314. if ($this->_displayName == $displayName)
  315. return; // nothing to update
  316. // update the object
  317. $this->_displayName = $displayName;
  318.  
  319. // update the database
  320. $dbHandler = Services::getService("DatabaseManager");
  321. $query = new UpdateQuery();
  322. $query->setTable("node");
  323. $id =$this->getId();
  324. $idValue = $id->getIdString();
  325. $where = "node_id = '".addslashes($idValue)."'";
  326. $query->setWhere($where);
  327. $query->setColumns(array("node_display_name"));
  328. $query->setValues(array("'".addslashes($displayName)."'"));
  329. $queryResult =$dbHandler->query($query, $this->_cache->_dbIndex);
  330. if ($queryResult->getNumberOfRows() == 0)
  331. throwError(new Error(HierarchyException::OPERATION_FAILED(),"Hierarchy",true));
  332. if ($queryResult->getNumberOfRows() > 1)
  333. throwError(new Error(HierarchyException::OPERATION_FAILED() ,"Hierarchy",true));
  334. }
  335.  
  336. /**
  337. * Return true if this Node is a leaf; false otherwise. A Node is a leaf
  338. * if it has no children.
  339. *
  340. * @return boolean
  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}
  353. *
  354. * @access public
  355. */
  356. function isLeaf () {
  357. return $this->_cache->isLeaf($this);
  358. }
  359.  
  360. /**
  361. * Return true if this Node is a root; false otherwise.
  362. *
  363. * @return boolean
  364. *
  365. * @throws object HierarchyException An exception with one of
  366. * the following messages defined in
  367. * org.osid.hierarchy.HierarchyException may be thrown: {@link }
  368. * org.osid.hierarchy.HierarchyException#OPERATION_FAILED
  369. * OPERATION_FAILED}, {@link }
  370. * org.osid.hierarchy.HierarchyException#PERMISSION_DENIED
  371. * PERMISSION_DENIED}, {@link }
  372. * org.osid.hierarchy.HierarchyException#CONFIGURATION_ERROR
  373. * CONFIGURATION_ERROR}, {@link }
  374. * org.osid.hierarchy.HierarchyException#UNIMPLEMENTED
  375. * UNIMPLEMENTED}
  376. *
  377. * @access public
  378. */
  379. function isRoot () {
  380. // leaf-check is done through getChildren(). A leaf would not have any children.
  381. $parents =$this->getParents();
  382. return (!$parents->hasNext());
  383. }
  384.  
  385. /**
  386. * Link a parent to this Node.
  387. *
  388. * @param object Id $nodeId
  389. *
  390. * @throws object HierarchyException An exception with one of
  391. * the following messages defined in
  392. * org.osid.hierarchy.HierarchyException may be thrown: {@link }
  393. * org.osid.hierarchy.HierarchyException#OPERATION_FAILED
  394. * OPERATION_FAILED}, {@link }
  395. * org.osid.hierarchy.HierarchyException#PERMISSION_DENIED
  396. * PERMISSION_DENIED}, {@link }
  397. * org.osid.hierarchy.HierarchyException#CONFIGURATION_ERROR
  398. * CONFIGURATION_ERROR}, {@link }
  399. * org.osid.hierarchy.HierarchyException#UNIMPLEMENTED
  400. * UNIMPLEMENTED}, {@link }
  401. * org.osid.hierarchy.HierarchyException#NULL_ARGUMENT
  402. * NULL_ARGUMENT}, {@link }
  403. * org.osid.hierarchy.HierarchyException#NODE_TYPE_NOT_FOUND
  404. * NODE_TYPE_NOT_FOUND}, {@link }
  405. * org.osid.hierarchy.HierarchyException#SINGLE_PARENT_HIERARCHY
  406. * SINGLE_PARENT_HIERARCHY}, {@link }
  407. * org.osid.hierarchy.HierarchyException#ALREADY_ADDED
  408. * ALREADY_ADDED}, {@link }
  409. * org.osid.hierarchy.HierarchyException#ATTEMPTED_RECURSION
  410. * ATTEMPTED_RECURSION}
  411. *
  412. * @access public
  413. */
  414. function addParent ( $nodeId ) {
  415. // ** parameter validation
  416. ArgumentValidator::validate($nodeId, ExtendsValidatorRule::getRule("Id"), true);
  417. // ** end of parameter validation
  418. IsAuthorizedCache::dirtyNode($this->_id);
  419. $this->_cache->addParent($nodeId->getIdString(), $this->_id->getIdString());
  420. }
  421.  
  422. /**
  423. * Unlink a parent from this Node.
  424. *
  425. * @param object Id $parentId
  426. *
  427. * @throws object HierarchyException An exception with one of
  428. * the following messages defined in
  429. * org.osid.hierarchy.HierarchyException may be thrown: {@link }
  430. * org.osid.hierarchy.HierarchyException#OPERATION_FAILED
  431. * OPERATION_FAILED}, {@link }
  432. * org.osid.hierarchy.HierarchyException#PERMISSION_DENIED
  433. * PERMISSION_DENIED}, {@link }
  434. * org.osid.hierarchy.HierarchyException#CONFIGURATION_ERROR
  435. * CONFIGURATION_ERROR}, {@link }
  436. * org.osid.hierarchy.HierarchyException#UNIMPLEMENTED
  437. * UNIMPLEMENTED}, {@link }
  438. * org.osid.hierarchy.HierarchyException#NULL_ARGUMENT
  439. * NULL_ARGUMENT}, {@link }
  440. * org.osid.hierarchy.HierarchyException#NODE_TYPE_NOT_FOUND
  441. * NODE_TYPE_NOT_FOUND}, {@link }
  442. * org.osid.hierarchy.HierarchyException#SINGLE_PARENT_HIERARCHY
  443. * SINGLE_PARENT_HIERARCHY}, {@link }
  444. * org.osid.hierarchy.HierarchyException#INCONSISTENT_STATE
  445. * INCONSISTENT_STATE}
  446. *
  447. * @access public
  448. */
  449. function removeParent ( $parentId ) {
  450. // ** parameter validation
  451. ArgumentValidator::validate($parentId, ExtendsValidatorRule::getRule("Id"), true);
  452. // ** end of parameter validation
  453. IsAuthorizedCache::dirtyNode($this->_id);
  454. $this->_cache->removeParent($parentId->getIdString(), $this->_id->getIdString());
  455. }
  456.  
  457. /**
  458. * Changes the parent of this Node by adding a new parent and removing the
  459. * old parent.
  460. *
  461. * @param object Id $oldParentId
  462. * @param object Id $newParentId
  463. *
  464. * @throws object HierarchyException An exception with one of
  465. * the following messages defined in
  466. * org.osid.hierarchy.HierarchyException may be thrown: {@link }
  467. * org.osid.hierarchy.HierarchyException#OPERATION_FAILED
  468. * OPERATION_FAILED}, {@link }
  469. * org.osid.hierarchy.HierarchyException#PERMISSION_DENIED
  470. * PERMISSION_DENIED}, {@link }
  471. * org.osid.hierarchy.HierarchyException#CONFIGURATION_ERROR
  472. * CONFIGURATION_ERROR}, {@link }
  473. * org.osid.hierarchy.HierarchyException#UNIMPLEMENTED
  474. * UNIMPLEMENTED}, {@link }
  475. * org.osid.hierarchy.HierarchyException#NULL_ARGUMENT
  476. * NULL_ARGUMENT}, {@link }
  477. * org.osid.hierarchy.HierarchyException#NODE_TYPE_NOT_FOUND
  478. * NODE_TYPE_NOT_FOUND}, {@link }
  479. * org.osid.hierarchy.HierarchyException#ATTEMPTED_RECURSION
  480. * ATTEMPTED_RECURSION}
  481. *
  482. * @access public
  483. */
  484. function changeParent ( $oldParentId, $newParentId ) {
  485. // ** parameter validation
  486. ArgumentValidator::validate($oldParentId, ExtendsValidatorRule::getRule("Id"), true);
  487. ArgumentValidator::validate($newParentId, ExtendsValidatorRule::getRule("Id"), true);
  488. // ** end of parameter validation
  489. if ($oldParentId->isEqual($newParentId))
  490. return;
  491. IsAuthorizedCache::dirtyNode($this->_id);
  492. $this->_cache->removeParent($oldParentId->getIdString(), $this->_id->getIdString());
  493. $this->_cache->addParent($newParentId->getIdString(), $this->_id->getIdString());
  494. }
  495.  
  496. }
  497.  
  498. ?>

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