/**
  * Allows you to add a new KalturaDropFolderFile object
  * 
  * @action add
  * @param KalturaDropFolderFile $dropFolderFile
  * @return KalturaDropFolderFile
  * 
  * @throws KalturaErrors::PROPERTY_VALIDATION_CANNOT_BE_NULL
  * @throws KalturaDropFolderErrors::DROP_FOLDER_NOT_FOUND
  * @throws KalturaDropFolderErrors::DROP_FOLDER_FILE_ALREADY_EXISTS
  */
 public function addAction(KalturaDropFolderFile $dropFolderFile)
 {
     // check for required parameters
     $dropFolderFile->validatePropertyNotNull('dropFolderId');
     $dropFolderFile->validatePropertyNotNull('fileName');
     $dropFolderFile->validatePropertyMinValue('fileSize', 0);
     // check that drop folder id exists in the system
     $dropFolder = DropFolderPeer::retrieveByPK($dropFolderFile->dropFolderId);
     if (!$dropFolder) {
         throw new KalturaAPIException(KalturaDropFolderErrors::DROP_FOLDER_NOT_FOUND, $dropFolderFile->dropFolderId);
     }
     // check that the file doesn't already exist in the drop folder
     if (DropFolderFilePeer::retrieveByDropFolderIdAndFileName($dropFolderFile->dropFolderId, $dropFolderFile->fileName)) {
         throw new KalturaAPIException(KalturaDropFolderErrors::DROP_FOLDER_FILE_ALREADY_EXISTS, $dropFolderFile->dropFolderId, $dropFolderFile->fileName);
     }
     // save in database
     $dbDropFolderFile = $dropFolderFile->toInsertableObject();
     /* @var $dbDropFolderFile DropFolderFile */
     $dbDropFolderFile->setPartnerId($dropFolder->getPartnerId());
     $dbDropFolderFile->setStatus(DropFolderFileStatus::UPLOADING);
     $dbDropFolderFile->save();
     // return the saved object
     $dropFolderFile = new KalturaDropFolderFile();
     $dropFolderFile->fromObject($dbDropFolderFile);
     return $dropFolderFile;
 }
 private function newFileAddedOrDetected(KalturaDropFolderFile $dropFolderFile, $fileStatus)
 {
     // check for required parameters
     $dropFolderFile->validatePropertyNotNull('dropFolderId');
     $dropFolderFile->validatePropertyNotNull('fileName');
     $dropFolderFile->validatePropertyMinValue('fileSize', 0);
     // check that drop folder id exists in the system
     $dropFolder = DropFolderPeer::retrieveByPK($dropFolderFile->dropFolderId);
     if (!$dropFolder) {
         throw new KalturaAPIException(KalturaDropFolderErrors::DROP_FOLDER_NOT_FOUND, $dropFolderFile->dropFolderId);
     }
     // save in database
     $dropFolderFile->status = null;
     $dbDropFolderFile = $dropFolderFile->toInsertableObject();
     /* @var $dbDropFolderFile DropFolderFile  */
     $dbDropFolderFile->setPartnerId($dropFolder->getPartnerId());
     $dbDropFolderFile->setStatus($fileStatus);
     $dbDropFolderFile->setType($dropFolder->getType());
     try {
         $dbDropFolderFile->save();
     } catch (PropelException $e) {
         if ($e->getCause() && $e->getCause()->getCode() == self::MYSQL_CODE_DUPLICATE_KEY) {
             $existingDropFolderFile = DropFolderFilePeer::retrieveByDropFolderIdAndFileName($dropFolderFile->dropFolderId, $dropFolderFile->fileName);
             switch ($existingDropFolderFile->getStatus()) {
                 case DropFolderFileStatus::PARSED:
                     KalturaLog::info('Exisiting file status is PARSED, updating status to [' . $fileStatus . ']');
                     $existingDropFolderFile = $dropFolderFile->toUpdatableObject($existingDropFolderFile);
                     $existingDropFolderFile->setStatus($fileStatus);
                     $existingDropFolderFile->save();
                     $dbDropFolderFile = $existingDropFolderFile;
                     break;
                 case DropFolderFileStatus::DETECTED:
                     KalturaLog::info('Exisiting file status is DETECTED, updating status to [' . $fileStatus . ']');
                     $existingDropFolderFile = $dropFolderFile->toUpdatableObject($existingDropFolderFile);
                     if ($existingDropFolderFile->getStatus() != $fileStatus) {
                         $existingDropFolderFile->setStatus($fileStatus);
                     }
                     $existingDropFolderFile->save();
                     $dbDropFolderFile = $existingDropFolderFile;
                     break;
                 case DropFolderFileStatus::UPLOADING:
                     if ($fileStatus == DropFolderFileStatus::UPLOADING) {
                         KalturaLog::log('Exisiting file status is UPLOADING, updating properties');
                         $existingDropFolderFile = $dropFolderFile->toUpdatableObject($existingDropFolderFile);
                         $existingDropFolderFile->save();
                         $dbDropFolderFile = $existingDropFolderFile;
                         break;
                     }
                 default:
                     KalturaLog::log('Setting current file to PURGED [' . $existingDropFolderFile->getId() . ']');
                     $existingDropFolderFile->setStatus(DropFolderFileStatus::PURGED);
                     $existingDropFolderFile->save();
                     $newDropFolderFile = $dbDropFolderFile->copy();
                     if ($existingDropFolderFile->getLeadDropFolderFileId() && $existingDropFolderFile->getLeadDropFolderFileId() != $existingDropFolderFile->getId()) {
                         KalturaLog::info('Updating lead id [' . $existingDropFolderFile->getLeadDropFolderFileId() . ']');
                         $newDropFolderFile->setLeadDropFolderFileId($existingDropFolderFile->getLeadDropFolderFileId());
                     }
                     $newDropFolderFile->save();
                     $dbDropFolderFile = $newDropFolderFile;
             }
         } else {
             throw $e;
         }
     }
     // return the saved object
     $dropFolderFile = KalturaDropFolderFile::getInstanceByType($dbDropFolderFile->getType());
     $dropFolderFile->fromObject($dbDropFolderFile, $this->getResponseProfile());
     return $dropFolderFile;
 }
 /**
  * 1. add resource in status PARSED
  * 2. if already exist, but is not processed yet update lead drop folder file id
  * 3. if already processed, mark processed file as purged and create new row in status PARSED
  * @param string $fileName
  * @param DropFolderFile $leadFile
  * @param DropFolder $folder
  * @throws Exception
  */
 private function addParsedContentResourceFile($fileName, DropFolderFile $leadFile, DropFolder $folder)
 {
     KalturaLog::debug('Trying to add content resource in status PARSED [' . $fileName . ']');
     try {
         $newFile = new DropFolderFile();
         $newFile->setDropFolderId($folder->getId());
         $newFile->setFileName($fileName);
         $newFile->setFileSize(0);
         $newFile->setStatus(DropFolderFileStatus::PARSED);
         $newFile->setLeadDropFolderFileId($leadFile->getId());
         $newFile->setPartnerId($folder->getPartnerId());
         $newFile->save();
     } catch (PropelException $e) {
         if ($e->getCause() && $e->getCause()->getCode() == kDropFolderXmlEventsConsumer::MYSQL_CODE_DUPLICATE_KEY) {
             $existingFile = DropFolderFilePeer::retrieveByDropFolderIdAndFileName($folder->getId(), $fileName);
             if ($existingFile) {
                 $unprocessedStatuses = array(DropFolderFileStatus::WAITING, DropFolderFileStatus::DETECTED, DropFolderFileStatus::UPLOADING, DropFolderFileStatus::PENDING);
                 if (in_array($existingFile->getStatus(), $unprocessedStatuses)) {
                     KalturaLog::debug('Updating drop folder file [' . $existingFile->getId() . '] with lead id [' . $leadFile->getId() . ']');
                     $existingFile->setLeadDropFolderFileId($leadFile->getId());
                     $existingFile->save();
                 } else {
                     KalturaLog::debug('Deleting drop folder file [' . $existingFile->getId() . ']');
                     $existingFile->setStatus(DropFolderFileStatus::PURGED);
                     $existingFile->save();
                     KalturaLog::debug('Adding new drop folder file [' . $newFile->getFileName() . '] with status PARSED');
                     $newFileCopy = $newFile->copy();
                     $newFileCopy->save();
                 }
             }
         } else {
             KalturaLog::err('Failed to add content resource for Xml file [' . $leadFile->getId() . '] - ' . $e->getMessage());
             throw new Exception(DropFolderXmlBulkUploadPlugin::ERROR_ADD_CONTENT_RESOURCE_MESSAGE, DropFolderXmlBulkUploadPlugin::getErrorCodeCoreValue(DropFolderXmlBulkUploadErrorCode::ERROR_ADD_CONTENT_RESOURCE));
         }
     }
 }