Source for file HarmoniPropertyManager.class.php

Documentation is available at HarmoniPropertyManager.class.php

  1. <?php
  2.  
  3. /**
  4. * <p>
  5. * Property manager handles the process of storing and retrieving agent
  6. * properties in the database. It also contains functionality to get database
  7. * info on property types. It was designed to work with HarmoniEditableAgent
  8. * which implements functionality to modify, in the context of the program
  9. * properties and other agent attributes. This manager allows the program to
  10. * store those changes.
  11. * </p>
  12. *
  13. * <p>
  14. * THIS MANAGER IS NOT IN THE OSID 2.0!
  15. * However it does follow the outline of a normal OSID manager.
  16. *
  17. * All implementations of OsidManager (manager) provide methods for accessing
  18. * and manipulating the various objects defined in the OSID package. A manager
  19. * defines an implementation of an OSID. All other OSID objects come either
  20. * directly or indirectly from the manager. New instances of the OSID objects
  21. * are created either directly or indirectly by the manager. Because the OSID
  22. * objects are defined using interfaces, create methods must be used instead
  23. * of the new operator to create instances of the OSID objects. Create methods
  24. * are used both to instantiate and persist OSID objects. Using the
  25. * OsidManager class to define an OSID's implementation allows the application
  26. * to change OSID implementations by changing the OsidManager package name
  27. * used to load an implementation. Applications developed using managers
  28. * permit OSID implementation substitution without changing the application
  29. * source code. As with all managers, use the OsidLoader to load an
  30. * implementation of this interface.
  31. * </p>
  32. *
  33. * @package harmoni.osid_v2.agent
  34. *
  35. * @copyright Copyright &copy; 2005, Middlebury College
  36. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  37. *
  38. * @version $Id: HarmoniPropertyManager.class.php,v 1.8 2007/09/13 16:04:17 adamfranco Exp $
  39. *
  40. * @author Ben Gore
  41. */
  42.  
  43. class HarmoniPropertyManager
  44. {
  45. var $_dbIndex;
  46. var $_dbName;
  47. function HarmoniPropertiesManager(){
  48. }
  49. /**
  50. * Assigns the configuration of databases etc.
  51. *
  52. * @param object $configuration
  53. *
  54. * @access public
  55. *
  56. * @return void
  57. */
  58. function assignConfiguration($configuration){
  59. $this->_configuration =$configuration;
  60. $dbIndex =$configuration->getProperty('database_index');
  61. // ** parameter validation
  62. ArgumentValidator::validate($dbIndex, IntegerValidatorRule::getRule(), true);
  63. // ** end of parameter validation
  64. $this->_dbIndex = $dbIndex;
  65. }
  66. /**
  67. * Return context of this OsidManager.
  68. *
  69. * @return object OsidContext
  70. *
  71. * @throws object OsidException
  72. *
  73. * @access public
  74. */
  75. function getOsidContext () {
  76. return $this->_osidContext;
  77. }
  78.  
  79. /**
  80. * Assign the context of this OsidManager.
  81. *
  82. * @param object OsidContext $context
  83. *
  84. * @throws object OsidException An exception with one of the following
  85. * messages defined in org.osid.OsidException: {@link }
  86. * org.osid.OsidException#NULL_ARGUMENT NULL_ARGUMENT}
  87. *
  88. * @access public
  89. */
  90. function assignOsidContext ( $context ) {
  91. $this->_osidContext =$context;
  92. }
  93. /**
  94. * A public function that retrieves properties from the database associated
  95. * with the object represented by object_id_string. Each individual type
  96. * has a separate object, which is placed in the array
  97. *
  98. * @param int $object_id_string
  99. *
  100. * @return array
  101. *
  102. * @access public
  103. */
  104.  
  105. function retrieveProperties($object_id_string){
  106. $current_property_type = null;//for distinguishing whether the type retrieved is new or not. Obviously, the first one is
  107. $propertiesArray = array();//for storing each property object of type
  108. $dbHandler = Services::getService("DBHandler");
  109. //select the properties associated with this object (usually an agent)
  110. $query = new SelectQuery();
  111. $query->addTable("agent_properties");
  112. $query->addTable("type", LEFT_JOIN, "agent_properties.fk_type_id=type.type_id");
  113. $query->addColumn("*");
  114. $query->addWhere("fk_object_id='".addslashes($object_id_string)."'");
  115. $query->addOrderBy("fk_type_id");
  116. $result =$dbHandler->query($query, $this->_dbIndex);
  117. //loop through the results
  118. while($result->hasMoreRows()){
  119. $row = $result->getCurrentRow();//grab a new row
  120. if($row['fk_type_id']!=$current_property_type){//if this is a new type (the results are sorted by type), we add a new property object to the array for that type
  121. $current_property_type = $row['fk_type_id'];
  122. $type = new HarmoniType($row['type_domain'], $row['type_authority'], $row['type_keyword'], $row['type_description']);
  123. $propertiesArray[$current_property_type] = new HarmoniProperties($type);//the property
  124. }
  125. $propertiesArray[$current_property_type]->addProperty($row['property_key'], $row['property_value']);//we can now add the values to the property object
  126. $result->advanceRow();//the next row
  127. }
  128. $result->free();
  129. return $propertiesArray;//return the properties
  130. }
  131. /**
  132. * A public function that stores all of the properties of the property
  133. * object sent to it.
  134. * It deletes the old entries no matter what to avoid arduous checking to
  135. * if each needed to be deleted. The properties are then rewritten to the
  136. * database in the form the agent has them.
  137. *
  138. * @param int $object_id
  139. * @param object Property $properties
  140. *
  141. * @return boolean
  142. *
  143. * @access public
  144. */
  145. function storeProperties($object_id, $properties){
  146. $dbHandler = Services::getService("DBHandler");
  147. //ArgumentValidator::validate($object_id, new StringValidatorRule("Type"), true);
  148.  
  149. if($properties===NULL){
  150. return false;
  151. }
  152. $type =$properties->getType();//so we know the type
  153. $typeIdString = $this->getTypeId($type);//get the database id for type
  154. //If we don't delete them all every time we store then if we've deleted one off of the object it will remain in the DB
  155. $query = new DeleteQuery();
  156. $query->setTable("agent_properties");
  157. $query->addWhere("fk_object_id='$object_id' AND fk_type_id='$typeIdString'");
  158. $dbHandler->query($query, $this->_dbIndex);
  159. $keys =$properties->getKeys();//all the keys for the various properties
  160. while($keys->hasNextObject()){//loop through all the properties
  161. $key =$keys->nextObject();//get the next key
  162. $propertyValue =$properties->getProperty($key);//get the value of the property
  163. $query = new InsertQuery();
  164. $query->setTable("agent_properties");
  165. $query->setColumns(array("fk_object_id","fk_type_id", "property_key", "property_value"));
  166. $query->addRowOfValues(array("'".addslashes($object_id)."'", $typeIdString, "'".addslashes($key)."'", "'".addslashes($propertyValue)."'"));
  167. // }
  168. $result =$dbHandler->query($query, $this->_dbIndex);
  169. if(!$result){//at the first failure we'll stop and return false
  170. return false;
  171. }
  172. }
  173. return true;
  174. }
  175. /**
  176. * A public function for getting a type id (and ensuring that it exists
  177. * in the database). One might consider implementing a Type manager for
  178. * stuff like this that has no proper home.
  179. *
  180. * @param object Type $type
  181. *
  182. * @return integer
  183. *
  184. * @access public
  185. *
  186. * @since 11/18/04
  187. */
  188. function getTypeId ( $type ) {
  189. $dbc = Services::getService("DBHandler");
  190. // Check to see if the type already exists in the DB
  191. $query = new SelectQuery;
  192. $query->addColumn("type_id");
  193. $query->addTable("type");
  194. $query->addWhere("type_domain='".addslashes($type->getDomain())."'");
  195. $query->addWhere("type_authority='".addslashes($type->getAuthority())."'", _AND);
  196. $query->addWhere("type_keyword='".addslashes($type->getKeyword())."'", _AND);
  197. $result =$dbc->query($query, $this->_dbIndex);
  198. // If we have a type id already, use that
  199. if ($result->getNumberOfRows()) {
  200. $typeId = $result->field("type_id");
  201. $result->free();
  202. }
  203. // otherwise insert a new one.
  204. else {
  205. $result->free();
  206. $query = new InsertQuery;
  207. $query->setTable("type");
  208. $query->setAutoIncrementColumn("type_id", "type_type_id_seq");
  209. $query->setColumns(array(
  210. "type_domain",
  211. "type_authority",
  212. "type_keyword",
  213. "type_description"));
  214. $query->setValues(array(
  215. "'".addslashes($type->getDomain())."'",
  216. "'".addslashes($type->getAuthority())."'",
  217. "'".addslashes($type->getKeyword())."'",
  218. "'".addslashes($type->getDescription())."'"));
  219. $result =$dbc->query($query, $this->_dbIndex);
  220. $typeId = $result->getLastAutoIncrementValue();
  221. }
  222. return $typeId;
  223. }
  224. /**
  225. * Delete all properties associated with an object
  226. * This is here partially to preserve the option of using non-editable agents
  227. * If that ceases to be an issue, this more properly belongs in
  228. * HarmoniEditableAgent.class
  229. *
  230. * @param string $object_id_string
  231. * @return boolean
  232. * @access public
  233. */
  234. function deleteAllProperties($object_id_string){
  235. $dbHandler = Services::getService("DBHandler");
  236. //create a query to remove all properties associated with $object_id_string
  237. $query= new DeleteQuery();
  238. $query->setTable("agent_properties");
  239. $query->addWhere("fk_object_id='$object_id_string'");
  240. $result =$dbHandler->query($query, $this->_dbIndex);
  241. return $result ? true : false;
  242. }
  243. /**
  244. * A public function to take a key/value array of properties as might be
  245. * gathered from a web form and turns it into a useable object
  246. *
  247. * @param array $propertiesArray
  248. * @param object Type $type
  249. *
  250. * @return object Property
  251. *
  252. * @acess public
  253. */
  254. function convertArrayToObject($propertiesArray, $type){
  255. $property = new HarmoniProperties($type);
  256. foreach($propertiesArray as $key => $propertyValue){
  257. $property->addProperty($key, $propertyValue);
  258. }
  259. return $property;
  260. }
  261. /**
  262. * Answer an array of all of the property keys inexistance
  263. *
  264. * @return array
  265. * @access public
  266. * @since 11/1/05
  267. */
  268. function getAllPropertyKeys(){
  269. $propertyKeys = array();
  270. $dbHandler = Services::getService("DBHandler");
  271. //select the propertykeys
  272. $query = new SelectQuery();
  273. $query->addTable("agent_properties");
  274. $query->addColumn("DISTINCT property_key");
  275. // $query->addTable("type", LEFT_JOIN, "agent_properties.fk_type_id=type.type_id");
  276. // $query->addColumn("property_key");
  277. // $query->addColumn("type_domain");
  278. // $query->addColumn("type_authority");
  279. // $query->addColumn("type_keyword");
  280. // $query->addColumn("type_description");
  281. $query->addOrderBy("property_key");
  282. $result =$dbHandler->query($query, $this->_dbIndex);
  283. while($result->hasMoreRows()){
  284. $propertyKeys[] = $result->field('property_key');
  285. // $propertyKeys[$result->field('property_key')] = new Type(
  286. // $result->field('type_domain'),
  287. // $result->field('type_authority'),
  288. // $result->field('type_keyword'),
  289. // $result->field('type_description'));
  290. $result->advanceRow();
  291. }
  292. $result->free();
  293. return $propertyKeys;//return the properties
  294. }
  295. }
  296.  
  297. ?>

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