def updateCanonical()

in path-manager/app/services/PathStore.scala [211:246]


  def updateCanonical(newPath: String, id: Long): Either[String, Map[String, List[PathRecord]]] = {

    logger.debug(s"Updating $CANONICAL_PATH_TYPE path [$newPath] for [$id]")

    if (PathValidator.isInvalid(newPath)) {
      Left(s"invalid path [$newPath]")
    } else {
      val newPathRecord = Option(Dynamo.pathsTable.getItem("path", newPath)).map(PathRecord(_))
      val canonicalPathsForId = Dynamo.pathsTable.getIndex("id-index").query(new KeyAttribute("identifier", id), RangeKeyMatches.rangeKeyMatches("type", CANONICAL_PATH_TYPE)).asScala
      val canonicalPathForId = canonicalPathsForId.map{ PathRecord(_) }.headOption

      if(newPathRecord.exists(_.identifier != id)) {
        logger.warn(s"Failed to update path [$newPath], already claimed by id [${newPathRecord.map{_.identifier}.get}], submitting id [$id]")
        Left("path already in use")
      } else {
        canonicalPathForId.map { existingRecord: PathRecord =>

          val existingPath = existingRecord.path
          val updatedRecord = if (existingPath != newPath) {
              val newRecord = existingRecord.copy(path = newPath)
              logger.debug(s"Removing old path for item [$id]. old path[$existingPath] new path [$newPath]")
              Dynamo.pathsTable.deleteItem("path", existingPath)
              putPathItemAndAwaitIndexUpdate(newRecord)
              newRecord
            } else {
              existingRecord
            }
          logger.debug(s"updated $CANONICAL_PATH_TYPE path [$newPath] for id [$id] successfully")
          List(updatedRecord).groupBy(_.`type`)
        }.toRight{
          logger.warn(s"Failed to update path [$newPath], no existing path found for id [$id]")
          s"unable to find $CANONICAL_PATH_TYPE record for $id"
        }
      }
    }
  }