/package/app/app/plugins/metadata/services/MetadataBatchService.php

https://github.com/richhl/kalturaCE · PHP · 214 lines · 91 code · 37 blank · 86 comment · 7 complexity · 6e3159afd332f3bdb3482572f17c782a MD5 · raw file

  1. <?php
  2. /**
  3. * @service metadataBatch
  4. * @package plugins.metadata
  5. * @subpackage api.services
  6. */
  7. class MetadataBatchService extends BatchService
  8. {
  9. // --------------------------------- ImportMetadataJob functions --------------------------------- //
  10. /**
  11. * batch getExclusiveImportMetadataJob action allows to get a BatchJob of type METADATA_IMPORT
  12. *
  13. * @action getExclusiveImportMetadataJobs
  14. * @param KalturaExclusiveLockKey $lockKey The unique lock key from the batch-process. Is used for the locking mechanism
  15. * @param int $maxExecutionTime The maximum time in seconds the job reguarly take. Is used for the locking mechanism when determining an unexpected termination of a batch-process.
  16. * @param int $numberOfJobs The maximum number of jobs to return.
  17. * @param KalturaBatchJobFilter $filter Set of rules to fetch only rartial list of jobs
  18. * @return KalturaBatchJobArray
  19. */
  20. function getExclusiveImportMetadataJobsAction(KalturaExclusiveLockKey $lockKey, $maxExecutionTime, $numberOfJobs, KalturaBatchJobFilter $filter = null)
  21. {
  22. return $this->getExclusiveJobsAction($lockKey, $maxExecutionTime, $numberOfJobs, $filter, BatchJobType::METADATA_IMPORT );
  23. }
  24. /**
  25. * batch updateExclusiveImportMetadataJob action updates a BatchJob of type METADATA_IMPORT that was claimed using the getExclusiveImportMetadataJobs
  26. *
  27. * @action updateExclusiveImportMetadataJob
  28. * @param int $id The id of the job to free
  29. * @param KalturaExclusiveLockKey $lockKey The unique lock key from the batch-process. Is used for the locking mechanism
  30. * @param KalturaBatchJob $job
  31. * @return KalturaBatchJob
  32. */
  33. function updateExclusiveImportMetadataJobAction($id ,KalturaExclusiveLockKey $lockKey, KalturaBatchJob $job)
  34. {
  35. $dbBatchJob = BatchJobPeer::retrieveByPK($id);
  36. // verifies that the job is of the right type
  37. if($dbBatchJob->getJobType() != KalturaBatchJobType::METADATA_IMPORT)
  38. throw new KalturaAPIException(APIErrors::UPDATE_EXCLUSIVE_JOB_WRONG_TYPE, $id, serialize($lockKey), serialize($job));
  39. $dbBatchJob = kBatchManager::updateExclusiveBatchJob($id, $lockKey->toObject(), $job->toObject($dbBatchJob));
  40. $batchJob = new KalturaBatchJob(); // start from blank
  41. return $batchJob->fromObject($dbBatchJob);
  42. }
  43. /**
  44. * batch freeExclusiveImportMetadataJob action frees a BatchJob of type ImportMetadata that was claimed using the getExclusiveImportMetadataJobs
  45. *
  46. * @action freeExclusiveImportMetadataJob
  47. * @param int $id The id of the job to free
  48. * @param KalturaExclusiveLockKey $lockKey The unique lock key from the batch-process. Is used for the locking mechanism
  49. * @param bool $resetExecutionAttempts Resets the job execution attampts to zero
  50. * @return KalturaFreeJobResponse
  51. */
  52. function freeExclusiveImportMetadataJobAction($id ,KalturaExclusiveLockKey $lockKey, $resetExecutionAttempts = false)
  53. {
  54. return $this->freeExclusiveJobAction($id ,$lockKey, KalturaBatchJobType::METADATA_IMPORT, $resetExecutionAttempts);
  55. }
  56. // --------------------------------- ImportMetadataJob functions --------------------------------- //
  57. // --------------------------------- TransformMetadataJob functions --------------------------------- //
  58. /**
  59. * batch getExclusiveTransformMetadataJob action allows to get a BatchJob of type METADATA_TRANSFORM
  60. *
  61. * @action getExclusiveTransformMetadataJobs
  62. * @param KalturaExclusiveLockKey $lockKey The unique lock key from the batch-process. Is used for the locking mechanism
  63. * @param int $maxExecutionTime The maximum time in seconds the job reguarly take. Is used for the locking mechanism when determining an unexpected termination of a batch-process.
  64. * @param int $numberOfJobs The maximum number of jobs to return.
  65. * @param KalturaBatchJobFilter $filter Set of rules to fetch only rartial list of jobs
  66. * @return KalturaBatchJobArray
  67. */
  68. function getExclusiveTransformMetadataJobsAction(KalturaExclusiveLockKey $lockKey, $maxExecutionTime, $numberOfJobs, KalturaBatchJobFilter $filter = null)
  69. {
  70. $jobs = $this->getExclusiveJobs($lockKey, $maxExecutionTime, $numberOfJobs, $filter, BatchJobType::METADATA_TRANSFORM);
  71. if($jobs)
  72. {
  73. foreach ($jobs as &$job)
  74. {
  75. $data = $job->getData();
  76. $metadataProfileId = $data->getMetadataProfileId();
  77. $metadataProfile = MetadataProfilePeer::retrieveByPK($metadataProfileId);
  78. if(!$metadataProfile)
  79. continue;
  80. $key = $metadataProfile->getSyncKey(MetadataProfile::FILE_SYNC_METADATA_DEFINITION);
  81. $xsdPath = kFileSyncUtils::getLocalFilePathForKey($key);
  82. $data->setDestXsdPath($xsdPath);
  83. $job->setData($data);
  84. }
  85. }
  86. return KalturaBatchJobArray::fromBatchJobArray($jobs);
  87. }
  88. /**
  89. * batch updateExclusiveTransformMetadataJob action updates a BatchJob of type METADATA_TRANSFORM that was claimed using the getExclusiveTransformMetadataJobs
  90. *
  91. * @action updateExclusiveTransformMetadataJob
  92. * @param int $id The id of the job to free
  93. * @param KalturaExclusiveLockKey $lockKey The unique lock key from the batch-process. Is used for the locking mechanism
  94. * @param KalturaBatchJob $job
  95. * @return KalturaBatchJob
  96. */
  97. function updateExclusiveTransformMetadataJobAction($id ,KalturaExclusiveLockKey $lockKey, KalturaBatchJob $job)
  98. {
  99. $dbBatchJob = BatchJobPeer::retrieveByPK($id);
  100. // verifies that the job is of the right type
  101. if($dbBatchJob->getJobType() != KalturaBatchJobType::METADATA_TRANSFORM)
  102. throw new KalturaAPIException(APIErrors::UPDATE_EXCLUSIVE_JOB_WRONG_TYPE, $id, serialize($lockKey), serialize($job));
  103. $dbBatchJob = kBatchManager::updateExclusiveBatchJob($id, $lockKey->toObject(), $job->toObject($dbBatchJob));
  104. $batchJob = new KalturaBatchJob(); // start from blank
  105. return $batchJob->fromObject($dbBatchJob);
  106. }
  107. /**
  108. * batch freeExclusiveTransformMetadataJob action frees a BatchJob of type TransformMetadata that was claimed using the getExclusiveTransformMetadataJobs
  109. *
  110. * @action freeExclusiveTransformMetadataJob
  111. * @param int $id The id of the job to free
  112. * @param KalturaExclusiveLockKey $lockKey The unique lock key from the batch-process. Is used for the locking mechanism
  113. * @param bool $resetExecutionAttempts Resets the job execution attampts to zero
  114. * @return KalturaFreeJobResponse
  115. */
  116. function freeExclusiveTransformMetadataJobAction($id ,KalturaExclusiveLockKey $lockKey, $resetExecutionAttempts = false)
  117. {
  118. return $this->freeExclusiveJobAction($id ,$lockKey, KalturaBatchJobType::METADATA_TRANSFORM, $resetExecutionAttempts);
  119. }
  120. /**
  121. * batch getTransformMetadataObjects action retrieve all metadata objects that requires upgrade and the total count
  122. *
  123. * @action getTransformMetadataObjects
  124. * @param int $metadataProfileId The id of the metadata profile
  125. * @param int $srcVersion The old metadata profile version
  126. * @param int $destVersion The new metadata profile version
  127. * @param KalturaFilterPager $pager
  128. * @return KalturaTransformMetadataResponse
  129. */
  130. function getTransformMetadataObjectsAction($metadataProfileId, $srcVersion, $destVersion, KalturaFilterPager $pager = null)
  131. {
  132. $response = new KalturaTransformMetadataResponse();
  133. $c = new Criteria();
  134. $c->add(MetadataPeer::METADATA_PROFILE_ID, $metadataProfileId);
  135. $c->add(MetadataPeer::METADATA_PROFILE_VERSION, $srcVersion, Criteria::LESS_THAN);
  136. $c->add(MetadataPeer::STATUS, KalturaMetadataStatus::VALID);
  137. $response->lowerVersionCount = MetadataPeer::doCount($c);
  138. $c = new Criteria();
  139. $c->add(MetadataPeer::METADATA_PROFILE_ID, $metadataProfileId);
  140. $c->add(MetadataPeer::METADATA_PROFILE_VERSION, $srcVersion);
  141. $c->add(MetadataPeer::STATUS, KalturaMetadataStatus::VALID);
  142. $response->totalCount = MetadataPeer::doCount($c);
  143. if ($pager)
  144. $pager->attachToCriteria($c);
  145. $list = MetadataPeer::doSelect($c);
  146. $response->objects = KalturaMetadataArray::fromDbArray($list);
  147. return $response;
  148. }
  149. /**
  150. * batch getTransformMetadataObjects action retrieve all metadata objects that requires upgrade and the total count
  151. *
  152. * @action upgradeMetadataObjects
  153. * @param int $metadataProfileId The id of the metadata profile
  154. * @param int $srcVersion The old metadata profile version
  155. * @param int $destVersion The new metadata profile version
  156. * @return KalturaUpgradeMetadataResponse
  157. */
  158. function upgradeMetadataObjectsAction($metadataProfileId, $srcVersion, $destVersion, KalturaFilterPager $pager = null)
  159. {
  160. $response = new KalturaUpgradeMetadataResponse();
  161. $c = new Criteria();
  162. $c->add(MetadataPeer::METADATA_PROFILE_ID, $metadataProfileId);
  163. $c->add(MetadataPeer::METADATA_PROFILE_VERSION, $srcVersion, Criteria::LESS_THAN);
  164. $c->add(MetadataPeer::STATUS, KalturaMetadataStatus::VALID);
  165. $response->lowerVersionCount = MetadataPeer::doCount($c);
  166. $c = new Criteria();
  167. $c->add(MetadataPeer::METADATA_PROFILE_ID, $metadataProfileId);
  168. $c->add(MetadataPeer::METADATA_PROFILE_VERSION, $srcVersion);
  169. $c->add(MetadataPeer::STATUS, KalturaMetadataStatus::VALID);
  170. $update = new Criteria();
  171. $update->add(MetadataPeer::METADATA_PROFILE_VERSION, $destVersion);
  172. $con = Propel::getConnection(MetadataPeer::DATABASE_NAME, Propel::CONNECTION_READ);
  173. $response->totalCount = BasePeer::doUpdate($c, $update, $con);
  174. return $response;
  175. }
  176. // --------------------------------- TransformMetadataJob functions --------------------------------- //
  177. }