/**
  * Save the metadata and store the catalog data for this published
  * monograph.
  */
 function execute($request)
 {
     parent::execute();
     $monograph = $this->getMonograph();
     $monographDao = DAORegistry::getDAO('MonographDAO');
     $publishedMonographDao = DAORegistry::getDAO('PublishedMonographDAO');
     $publishedMonograph = $publishedMonographDao->getById($monograph->getId(), null, false);
     /* @var $publishedMonograph PublishedMonograph */
     $isExistingEntry = $publishedMonograph ? true : false;
     if (!$publishedMonograph) {
         $publishedMonograph = $publishedMonographDao->newDataObject();
         $publishedMonograph->setId($monograph->getId());
     }
     // Populate the published monograph with the cataloging metadata
     $publishedMonograph->setAudience($this->getData('audience'));
     $publishedMonograph->setAudienceRangeQualifier($this->getData('audienceRangeQualifier'));
     $publishedMonograph->setAudienceRangeFrom($this->getData('audienceRangeFrom'));
     $publishedMonograph->setAudienceRangeTo($this->getData('audienceRangeTo'));
     $publishedMonograph->setAudienceRangeExact($this->getData('audienceRangeExact'));
     // If a cover image was uploaded, deal with it.
     if ($temporaryFileId = $this->getData('temporaryFileId')) {
         // Fetch the temporary file storing the uploaded library file
         $temporaryFileDao = DAORegistry::getDAO('TemporaryFileDAO');
         $temporaryFile = $temporaryFileDao->getTemporaryFile($temporaryFileId, $this->_userId);
         $temporaryFilePath = $temporaryFile->getFilePath();
         import('classes.file.SimpleMonographFileManager');
         $simpleMonographFileManager = new SimpleMonographFileManager($monograph->getPressId(), $publishedMonograph->getId());
         $basePath = $simpleMonographFileManager->getBasePath();
         // Delete the old file if it exists
         $oldSetting = $publishedMonograph->getCoverImage();
         if ($oldSetting) {
             $simpleMonographFileManager->deleteFile($basePath . $oldSetting['thumbnailName']);
             $simpleMonographFileManager->deleteFile($basePath . $oldSetting['catalogName']);
             $simpleMonographFileManager->deleteFile($basePath . $oldSetting['name']);
         }
         // The following variables were fetched in validation
         assert($this->_sizeArray && $this->_imageExtension);
         // Load the cover image for surrogate production
         $cover = null;
         // Scrutinizer
         switch ($this->_imageExtension) {
             case '.jpg':
                 $cover = imagecreatefromjpeg($temporaryFilePath);
                 break;
             case '.png':
                 $cover = imagecreatefrompng($temporaryFilePath);
                 break;
             case '.gif':
                 $cover = imagecreatefromgif($temporaryFilePath);
                 break;
         }
         assert(isset($cover));
         // Copy the new file over (involves creating the appropriate subdirectory too)
         $filename = 'cover' . $this->_imageExtension;
         $simpleMonographFileManager->copyFile($temporaryFile->getFilePath(), $basePath . $filename);
         // Generate surrogate images (thumbnail and catalog image)
         $press = $request->getPress();
         $coverThumbnailsMaxWidth = $press->getSetting('coverThumbnailsMaxWidth');
         $coverThumbnailsMaxHeight = $press->getSetting('coverThumbnailsMaxHeight');
         $thumbnailImageInfo = $this->_buildSurrogateImage($cover, $basePath, SUBMISSION_IMAGE_TYPE_THUMBNAIL, $coverThumbnailsMaxWidth, $coverThumbnailsMaxHeight);
         $catalogImageInfo = $this->_buildSurrogateImage($cover, $basePath, SUBMISSION_IMAGE_TYPE_CATALOG);
         // Clean up
         imagedestroy($cover);
         $publishedMonograph->setCoverImage(array('name' => $filename, 'width' => $this->_sizeArray[0], 'height' => $this->_sizeArray[1], 'thumbnailName' => $thumbnailImageInfo['filename'], 'thumbnailWidth' => $thumbnailImageInfo['width'], 'thumbnailHeight' => $thumbnailImageInfo['height'], 'catalogName' => $catalogImageInfo['filename'], 'catalogWidth' => $catalogImageInfo['width'], 'catalogHeight' => $catalogImageInfo['height'], 'uploadName' => $temporaryFile->getOriginalFileName(), 'dateUploaded' => Core::getCurrentDate()));
         // Clean up the temporary file
         import('lib.pkp.classes.file.TemporaryFileManager');
         $temporaryFileManager = new TemporaryFileManager();
         $temporaryFileManager->deleteFile($temporaryFileId, $this->_userId);
     }
     if ($this->getData('attachPermissions')) {
         $monograph->setCopyrightYear($this->getData('copyrightYear'));
         $monograph->setCopyrightHolder($this->getData('copyrightHolder'), null);
         // Localized
         $monograph->setLicenseURL($this->getData('licenseURL'));
     } else {
         $monograph->setCopyrightYear(null);
         $monograph->setCopyrightHolder(null, null);
         $monograph->setLicenseURL(null);
     }
     $monographDao->updateObject($monograph);
     // Update the modified fields or insert new.
     if ($isExistingEntry) {
         $publishedMonographDao->updateObject($publishedMonograph);
     } else {
         $publishedMonographDao->insertObject($publishedMonograph);
     }
     import('classes.publicationFormat.PublicationFormatTombstoneManager');
     $publicationFormatTombstoneMgr = new PublicationFormatTombstoneManager();
     $publicationFormatDao = DAORegistry::getDAO('PublicationFormatDAO');
     $publicationFormatFactory = $publicationFormatDao->getBySubmissionId($monograph->getId());
     $publicationFormats = $publicationFormatFactory->toAssociativeArray();
     $notificationMgr = new NotificationManager();
     if ($this->getData('confirm')) {
         // Update the monograph status.
         $monograph->setStatus(STATUS_PUBLISHED);
         $monographDao->updateObject($monograph);
         $publishedMonograph->setDatePublished(Core::getCurrentDate());
         $publishedMonographDao->updateObject($publishedMonograph);
         $notificationMgr->updateNotification($request, array(NOTIFICATION_TYPE_APPROVE_SUBMISSION), null, ASSOC_TYPE_MONOGRAPH, $publishedMonograph->getId());
         // Remove publication format tombstones.
         $publicationFormatTombstoneMgr->deleteTombstonesByPublicationFormats($publicationFormats);
         // Update the search index for this published monograph.
         import('classes.search.MonographSearchIndex');
         MonographSearchIndex::indexMonographMetadata($monograph);
         // Log the publication event.
         import('lib.pkp.classes.log.SubmissionLog');
         SubmissionLog::logEvent($request, $monograph, SUBMISSION_LOG_METADATA_PUBLISH, 'submission.event.metadataPublished');
     } else {
         if ($isExistingEntry) {
             // Update the monograph status.
             $monograph->setStatus(STATUS_QUEUED);
             $monographDao->updateObject($monograph);
             // Unpublish monograph.
             $publishedMonograph->setDatePublished(null);
             $publishedMonographDao->updateObject($publishedMonograph);
             $notificationMgr->updateNotification($request, array(NOTIFICATION_TYPE_APPROVE_SUBMISSION), null, ASSOC_TYPE_MONOGRAPH, $publishedMonograph->getId());
             // Create tombstones for each publication format.
             $publicationFormatTombstoneMgr->insertTombstonesByPublicationFormats($publicationFormats, $request->getContext());
             // Log the unpublication event.
             import('lib.pkp.classes.log.SubmissionLog');
             SubmissionLog::logEvent($request, $monograph, SUBMISSION_LOG_METADATA_UNPUBLISH, 'submission.event.metadataUnpublished');
         }
     }
 }