/**
  * @param EntryDistribution $entryDistribution
  * @param DistributionProfile $distributionProfile
  * @param int $dc
  * @return bool true if the job could be created
  */
 protected static function prepareDistributionJob(EntryDistribution $entryDistribution, DistributionProfile $distributionProfile, &$dc)
 {
     // prepare ids list of all the assets
     $assetIds = explode(',', implode(',', array($entryDistribution->getThumbAssetIds(), $entryDistribution->getFlavorAssetIds())));
     $assets = assetPeer::retrieveByIds($assetIds);
     $assetObjects = array();
     foreach ($assets as $asset) {
         /* @var $asset asset */
         $assetObjects[$asset->getId()] = array('asset' => $asset, 'downloadUrl' => null);
     }
     // lists all files from all assets
     $c = new Criteria();
     $c->add(FileSyncPeer::OBJECT_TYPE, FileSyncObjectType::ASSET);
     $c->add(FileSyncPeer::OBJECT_SUB_TYPE, asset::FILE_SYNC_ASSET_SUB_TYPE_ASSET);
     $c->add(FileSyncPeer::OBJECT_ID, $assetIds, Criteria::IN);
     $c->add(FileSyncPeer::PARTNER_ID, $entryDistribution->getPartnerId());
     $c->add(FileSyncPeer::STATUS, FileSync::FILE_SYNC_STATUS_READY);
     $fileSyncs = FileSyncPeer::doSelect($c);
     $dcs = array();
     foreach ($fileSyncs as $fileSync) {
         /* @var $fileSync FileSync */
         $assetId = $fileSync->getObjectId();
         if (!isset($assetObjects[$assetId])) {
             // the object is not in the list of assets
             continue;
         }
         $asset = $assetObjects[$assetId]['asset'];
         /* @var $asset asset */
         if ($asset->getVersion() != $fileSync->getVersion()) {
             // the file sync is not of the current asset version
             continue;
         }
         $fileSync = kFileSyncUtils::resolve($fileSync);
         // use the best URL as the source for download in case it will be needed
         if ($fileSync->getFileType() == FileSync::FILE_SYNC_FILE_TYPE_URL) {
             if (!is_null($assetObjects[$assetId]['downloadUrl']) && $fileSync->getDc() != $distributionProfile->getRecommendedStorageProfileForDownload()) {
                 continue;
             }
             $downloadUrl = $fileSync->getExternalUrl($entryDistribution->getEntryId());
             if (!$downloadUrl) {
                 continue;
             }
             $assetObjects[$assetId]['downloadUrl'] = $downloadUrl;
             continue;
         }
         // populates the list of files in each dc
         $fileSyncDc = $fileSync->getDc();
         if (!isset($dcs[$fileSyncDc])) {
             $dcs[$fileSyncDc] = array();
         }
         $dcs[$fileSyncDc][$assetId] = $fileSync->getId();
     }
     if (isset($dcs[$dc]) && count($dcs[$dc]) == count($assets)) {
         KalturaLog::debug("All files exist in the preferred dc [{$dc}]");
         return true;
     }
     // check if all files exist on any of the remote dcs
     $otherDcs = kDataCenterMgr::getAllDcs(true);
     foreach ($otherDcs as $remoteDc) {
         $remoteDcId = $remoteDc['id'];
         if (!isset($dcs[$remoteDcId]) && count($dcs[$remoteDcId]) != count($assets)) {
             continue;
         }
         $dc = $remoteDcId;
         KalturaLog::debug("All files exist in none-preferred dc [{$dc}]");
         return true;
     }
     if ($entryDistribution->getStatus() == EntryDistributionStatus::IMPORT_SUBMITTING || $entryDistribution->getStatus() == EntryDistributionStatus::IMPORT_UPDATING) {
         KalturaLog::debug("Entry distribution already importing");
         return false;
     }
     // create all needed import jobs
     $destinationDc = $distributionProfile->getRecommendedDcForDownload();
     $dcExistingFiles = $dcs[$destinationDc];
     foreach ($assetObjects as $assetId => $assetObject) {
         if (is_null($assetObject['downloadUrl'])) {
             KalturaLog::debug("Download URL not found for asset [{$assetId}]");
             continue;
         }
         $asset = $assetObject['asset'];
         /* @var $asset asset */
         if (isset($dcExistingFiles[$assetId])) {
             continue;
         }
         $jobData = new kImportJobData();
         $jobData->setCacheOnly(true);
         self::addImportJob($destinationDc, $assetObject['downloadUrl'], $asset);
     }
     return false;
 }