def process()

in app/model/commands/UpdateAtomCommand.scala [32:108]


  def process(): T = {
    log.info(s"Request to update atom ${atom.id}")

    if (id != atom.id) {
      AtomIdConflict
    }

    val existingAtom = getPreviewAtom(atom.id)

    val diffString = createDiffString(MediaAtom.fromThrift(existingAtom), atom)
    log.info(s"Update atom changes ${atom.id}: $diffString")

    val changeRecord = ChangeRecord.now(user)
    val atomIsPublished = existingAtom.contentChangeDetails.published

    val scheduledLaunchDate: Option[DateTime] = getDateIfNotPublished(atom.contentChangeDetails.scheduledLaunch, atomIsPublished)

    val embargo: Option[DateTime] = getDateIfNotPublished(atom.contentChangeDetails.embargo, atomIsPublished)

    val expiry: Option[DateTime] = atom.expiryDate.map(expiry => new DateTime(expiry))

    def updateIfChanged(newDate: Option[DateTime], changeRecord: Option[ThriftChangeRecord]): Option[ChangeRecord] = {
      if (changeRecord.map(_.date) == newDate.map(_.getMillis)) {
        changeRecord.map(ChangeRecord.fromThrift)
      } else {
        newDate.map(ChangeRecord.build(_, user))
      }
    }

    val existingChangeDetails = existingAtom.contentChangeDetails

    val details = atom.contentChangeDetails.copy(
      revision = atom.contentChangeDetails.revision + 1,
      lastModified = Some(changeRecord),
      scheduledLaunch = updateIfChanged(scheduledLaunchDate, existingChangeDetails.scheduledLaunch),
      embargo = updateIfChanged(embargo, existingChangeDetails.embargo),
      expiry = updateIfChanged(expiry, existingChangeDetails.expiry)
    )

    val newAtom = atom.copy(contentChangeDetails = details)
    val thrift: Atom = newAtom.asThrift
    val newAtomAsJson = AtomSerializer.toJson(thrift)

    log.info(s"Attempting to update atom ${atom.id} in ${awsConfig.dynamoTableName} to new atom: $newAtomAsJson")

    previewDataStore.updateAtom(thrift).fold(
      {
        case err: VersionConflictError =>
          log.warn(s"Unable to update atom due to version conflict with id ${atom.id} in ${awsConfig.dynamoTableName} table to new content: $newAtomAsJson", err)
          AtomUpdateConflictError(err.msg)
        case err =>
          log.error(s"Unable to update atom with id ${atom.id} in ${awsConfig.dynamoTableName} table to new content: $newAtomAsJson", err)
          AtomUpdateFailed(err.msg)
      },
      _ => {
        val event = ContentAtomEvent(thrift, EventType.Update, new Date().getTime)

        previewPublisher.publishAtomEvent(event) match {
          case Success(_) => {

            val existingMediaAtom = MediaAtom.fromThrift(existingAtom)
            val updatedMediaAtom = MediaAtom.fromThrift(thrift)
            processPlutoData(existingMediaAtom, updatedMediaAtom)

            AuditMessage(atom.id, "Update", getUsername(user), Some(diffString)).logMessage()

            log.info(s"atom with id ${atom.id} updated successfully in ${awsConfig.dynamoTableName} table to new content: $newAtomAsJson")

            updatedMediaAtom
          }
          case Failure(err) =>
            log.error(s"Unable to publish updated atom id=${atom.id} new_content=$newAtom", err)
            AtomPublishFailed(s"could not publish: ${err.toString}")
        }
      }
    )
  }