Source for file FileDataPart.class.php

Documentation is available at FileDataPart.class.php

  1. <?php
  2.  
  3. /**
  4. * Each Asset has one of the AssetType supported by the Repository. There are
  5. * also zero or more RecordStructures required by the Repository for each
  6. * AssetType. RecordStructures provide structural information. The values for
  7. * a given Asset's RecordStructure are stored in a Record. RecordStructures
  8. * can contain sub-elements which are referred to as PartStructures. The
  9. * structure defined in the RecordStructure and its PartStructures is used in
  10. * for any Records for the Asset. Records have Parts which parallel
  11. * PartStructures.
  12. *
  13. * <p>
  14. * OSID Version: 2.0
  15. * </p>
  16. *
  17. * @package harmoni.osid_v2.repository
  18. *
  19. * @copyright Copyright &copy;2005, Middlebury College
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
  21. *
  22. * @version $Id: FileDataPart.class.php,v 1.14 2007/09/13 19:08:45 adamfranco Exp $
  23. */
  24. class FileDataPart
  25. extends Part
  26. {
  27.  
  28. var $_recordId;
  29. var $_partStructure;
  30. var $_data;
  31. function FileDataPart( $partStructure, $recordId, $configuration, $asset ) {
  32. $this->_recordId =$recordId;
  33. $this->_partStructure =$partStructure;
  34. $this->_configuration =$configuration;
  35. $this->_asset =$asset;
  36. // Set our data to NULL, so that we can know if it has not been checked
  37. // for yet. If we search for data, but don't have any, or the data is
  38. // an empty string, it will have value "" instead of NULL
  39. $this->_data = NULL;
  40. }
  41. /**
  42. * Get the unique Id for this Part.
  43. *
  44. * @return object Id
  45. *
  46. * @throws object RepositoryException An exception with one of
  47. * the following messages defined in
  48. * org.osid.repository.RepositoryException may be thrown: {@link }
  49. * org.osid.repository.RepositoryException#OPERATION_FAILED
  50. * OPERATION_FAILED}, {@link }
  51. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  52. * PERMISSION_DENIED}, {@link }
  53. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  54. * CONFIGURATION_ERROR}, {@link }
  55. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  56. * UNIMPLEMENTED}
  57. *
  58. * @access public
  59. */
  60. function getId() {
  61. $idManager = Services::getService("Id");
  62. return $idManager->getId($this->_recordId->getIdString()."-FILE_DATA");
  63. }
  64.  
  65. /**
  66. * Create a Part. Records are composed of Parts. Parts can also contain
  67. * other Parts. Each Record is associated with a specific RecordStructure
  68. * and each Part is associated with a specific PartStructure.
  69. *
  70. * @param object Id $partStructureId
  71. * @param object mixed $value (original type: java.io.Serializable)
  72. *
  73. * @return object Part
  74. *
  75. * @throws object RepositoryException An exception with one of
  76. * the following messages defined in
  77. * org.osid.repository.RepositoryException may be thrown: {@link }
  78. * org.osid.repository.RepositoryException#OPERATION_FAILED
  79. * OPERATION_FAILED}, {@link }
  80. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  81. * PERMISSION_DENIED}, {@link }
  82. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  83. * CONFIGURATION_ERROR}, {@link }
  84. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  85. * UNIMPLEMENTED}, {@link }
  86. * org.osid.repository.RepositoryException#NULL_ARGUMENT
  87. * NULL_ARGUMENT}, {@link }
  88. * org.osid.repository.RepositoryException#UNKNOWN_ID UNKNOWN_ID}
  89. *
  90. * @access public
  91. */
  92. function createPart($partStructureId, $value) {
  93. throwError(
  94. new Error(RepositoryException::UNIMPLEMENTED(), "HarmoniPart", true));
  95. }
  96.  
  97. /**
  98. * Delete a Part and all its Parts.
  99. *
  100. * @param object Id $partId
  101. *
  102. * @throws object RepositoryException An exception with one of
  103. * the following messages defined in
  104. * org.osid.repository.RepositoryException may be thrown: {@link }
  105. * org.osid.repository.RepositoryException#OPERATION_FAILED
  106. * OPERATION_FAILED}, {@link }
  107. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  108. * PERMISSION_DENIED}, {@link }
  109. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  110. * CONFIGURATION_ERROR}, {@link }
  111. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  112. * UNIMPLEMENTED}, {@link }
  113. * org.osid.repository.RepositoryException#NULL_ARGUMENT
  114. * NULL_ARGUMENT}, {@link }
  115. * org.osid.repository.RepositoryException#UNKNOWN_ID UNKNOWN_ID}
  116. *
  117. * @access public
  118. */
  119. function deletePart($partId) {
  120. throwError(
  121. new Error(RepositoryException::UNIMPLEMENTED(), "HarmoniPart", true));
  122. }
  123.  
  124. /**
  125. * Get all the Parts in this Part. Iterators return a set, one at a time.
  126. *
  127. * @return object PartIterator
  128. *
  129. * @throws object RepositoryException An exception with one of
  130. * the following messages defined in
  131. * org.osid.repository.RepositoryException may be thrown: {@link }
  132. * org.osid.repository.RepositoryException#OPERATION_FAILED
  133. * OPERATION_FAILED}, {@link }
  134. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  135. * PERMISSION_DENIED}, {@link }
  136. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  137. * CONFIGURATION_ERROR}, {@link }
  138. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  139. * UNIMPLEMENTED}
  140. *
  141. * @access public
  142. */
  143. function getParts() {
  144. throwError(
  145. new Error(RepositoryException::UNIMPLEMENTED(), "HarmoniPart", true));
  146. }
  147.  
  148. /**
  149. * Get the value for this Part.
  150. *
  151. * @return object mixed (original type: java.io.Serializable)
  152. *
  153. * @throws object RepositoryException An exception with one of
  154. * the following messages defined in
  155. * org.osid.repository.RepositoryException may be thrown: {@link }
  156. * org.osid.repository.RepositoryException#OPERATION_FAILED
  157. * OPERATION_FAILED}, {@link }
  158. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  159. * PERMISSION_DENIED}, {@link }
  160. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  161. * CONFIGURATION_ERROR}, {@link }
  162. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  163. * UNIMPLEMENTED}
  164. *
  165. * @access public
  166. */
  167. function getValue() {
  168. // If we don't have the data, load it from the database.
  169. // if ($this->_data === NULL) {
  170. $dbHandler = Services::getService("DatabaseManager");
  171. // Get the data from the database,
  172. $query = new SelectQuery;
  173. $query->addTable("dr_file");
  174. $query->addTable("dr_file_data", LEFT_JOIN, "dr_file.id = dr_file_data.fk_file");
  175. $query->addColumn("data");
  176. $query->addWhere("dr_file.id = '".$this->_recordId->getIdString()."'");
  177. $result =$dbHandler->query($query, $this->_configuration->getProperty("database_index"));
  178. // If no data was found, return an empty string.
  179. if ($result->getNumberOfRows() == 0)
  180. $data = "";
  181. // $this->_data = "";
  182. else
  183. $data = base64_decode($result->field("data"));
  184. // $this->_data = base64_decode($result->field("data"));
  185. $result->free();
  186. // }
  187. return $data; //this->_data;
  188. }
  189. /**
  190. * Update the value for this Part.
  191. *
  192. * @param object mixed $value (original type: java.io.Serializable)
  193. *
  194. * @throws object RepositoryException An exception with one of
  195. * the following messages defined in
  196. * org.osid.repository.RepositoryException may be thrown: {@link }
  197. * org.osid.repository.RepositoryException#OPERATION_FAILED
  198. * OPERATION_FAILED}, {@link }
  199. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  200. * PERMISSION_DENIED}, {@link }
  201. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  202. * CONFIGURATION_ERROR}, {@link }
  203. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  204. * UNIMPLEMENTED}, {@link }
  205. * org.osid.repository.RepositoryException#NULL_ARGUMENT
  206. * NULL_ARGUMENT}
  207. *
  208. * @access public
  209. */
  210. function updateValue($value) {
  211. // ArgumentValidator::validate($value, StringValidatorRule::getRule());
  212. $dbHandler = Services::getService("DatabaseManager");
  213. // Store the data in the object in case its asked for again.
  214. // $this->_data = $value;
  215.  
  216. // Make sure that the dr_file row is inserted.
  217. $query = new InsertQuery;
  218. $query->setTable("dr_file");
  219. $query->addValue("id", $this->_recordId->getIdString());
  220. try {
  221. $dbHandler->query($query, $this->_configuration->getProperty("database_index"));
  222. } catch (QueryDatabaseException $e) {
  223. // If an error is thrown inserting (because the file already exists)
  224. // ignore it.
  225. }
  226. $dbHandler->beginTransaction($this->_configuration->getProperty("database_index"));
  227. // Base64 encode the data to preserve it,
  228. // then write it to the database.
  229. // Check to see if the data is in the database
  230. $query = new SelectQuery;
  231. $query->addTable("dr_file_data");
  232. $query->addColumn("COUNT(*) as count");
  233. $query->addWhere("fk_file = '".$this->_recordId->getIdString()."'");
  234. $result =$dbHandler->query($query, $this->_configuration->getProperty("database_index"));
  235. // If it already exists, use an update query.
  236. if ($result->field("count") > 0) {
  237. $query = new UpdateQuery;
  238. $query->setTable("dr_file_data");
  239. $query->setColumns(array("data"));
  240. $query->setValues(array("'".base64_encode($value)."'"));
  241. $query->addWhere("fk_file = '".$this->_recordId->getIdString()."'");
  242. }
  243. // If it doesn't exist, use an insert query.
  244. else {
  245. $query = new InsertQuery;
  246. $query->setTable("dr_file_data");
  247. $query->setColumns(array("fk_file","data"));
  248. $query->setValues(array("'".$this->_recordId->getIdString()."'",
  249. "'".base64_encode($value)."'"));
  250. }
  251. $result->free();
  252. // printpre($query);
  253. // printpre(MySQL_SQLGenerator::generateSQLQuery($query));
  254. // run the query
  255. $dbHandler->query($query, $this->_configuration->getProperty("database_index"));
  256. // Update the size row.
  257. $query = new UpdateQuery;
  258. $query->setTable("dr_file");
  259. $query->addValue("size", strval(strlen($value)));
  260. $query->addWhereEqual("id", $this->_recordId->getIdString());
  261. $dbHandler->query($query, $this->_configuration->getProperty("database_index"));
  262. $dbHandler->commitTransaction($this->_configuration->getProperty("database_index"));
  263. $this->_asset->updateModificationDate();
  264. }
  265.  
  266. /**
  267. * Get the PartStructure associated with this Part.
  268. *
  269. * @return object PartStructure
  270. *
  271. * @throws object RepositoryException An exception with one of
  272. * the following messages defined in
  273. * org.osid.repository.RepositoryException may be thrown: {@link }
  274. * org.osid.repository.RepositoryException#OPERATION_FAILED
  275. * OPERATION_FAILED}, {@link }
  276. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  277. * PERMISSION_DENIED}, {@link }
  278. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  279. * CONFIGURATION_ERROR}, {@link }
  280. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  281. * UNIMPLEMENTED}
  282. *
  283. * @access public
  284. */
  285. function getPartStructure() {
  286. return $this->_partStructure;
  287. }
  288. }

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