def expandParents()

in common/src/main/scala/model/ingestion/IngestMetadata.scala [16:49]


  def expandParents(ingestionUri: String, parentUri: Uri): Either[Failure, List[Uri]] = {
    val ingestionParts = ingestionUri.split("/").toList

    ingestionParts match {
      case collection :: _ :: Nil =>
        @tailrec
        def expandParentsRecursively(path: Path, acc: List[String]): List[String] = {
          if(path == null) {
            acc
          } else {
            val part = path.toString
            expandParentsRecursively(path.getParent, acc :+ part)
          }
        }

        val parentPath = Paths.get(parentUri.value)

        val expanded = expandParentsRecursively(parentPath, List.empty)

        val withoutIngestion = expanded.flatMap {
          case uri if uri == collection => None
          case uri if uri == ingestionUri => None
          case uri if uri.startsWith(ingestionUri) => Some(Uri(uri))
          case uri => Some(Uri(ingestionUri + "/" + uri))
        }

        val withIngestion = withoutIngestion :+ Uri(ingestionUri)

        Right(withIngestion)

      case _ =>
        Left(ClientFailure(s"Invalid ingestion uri $ingestionUri"))
    }
  }