Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
version = 3.9.8
runner.dialect = scala213source3
runner.dialect = scala3
align.preset = more
maxColumn = 120
newlines.topLevelStatementsMinBreaks = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import no.ndla.validation.ResourceType
import scala.util.Properties.*

trait Props extends HasBaseProps with HasDatabaseProps {
val props: ArticleApiProperties
lazy val props: ArticleApiProperties
}

class ArticleApiProperties extends BaseProps with DatabaseProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ class ComponentRegistry(properties: ArticleApiProperties)
with FrontpageApiClient
with ImageApiClient
with V55__SetHideBylineForImagesNotCopyrighted {
override val props: ArticleApiProperties = properties
override val migrator: DBMigrator = DBMigrator(
override lazy val props: ArticleApiProperties = properties
override lazy val migrator: DBMigrator = DBMigrator(
new R__SetArticleLanguageFromTaxonomy(props),
new R__SetArticleTypeFromTaxonomy,
new V8__CopyrightFormatUpdated,
Expand All @@ -81,32 +81,32 @@ class ComponentRegistry(properties: ArticleApiProperties)
new V33__ConvertLanguageUnknown(props),
new V55__SetHideBylineForImagesNotCopyrighted
)
override val DBUtil: DBUtility = new DBUtility
override lazy val DBUtil: DBUtility = new DBUtility

lazy val dataSource: HikariDataSource = DataSource.getHikariDataSource
lazy val internController = new InternController
lazy val articleControllerV2 = new ArticleControllerV2
lazy val healthController: TapirHealthController = new TapirHealthController
override lazy val dataSource: HikariDataSource = DataSource.getHikariDataSource
override lazy val internController = new InternController
override lazy val articleControllerV2 = new ArticleControllerV2
override lazy val healthController: TapirHealthController = new TapirHealthController

lazy val articleRepository = new ArticleRepository
lazy val articleSearchService = new ArticleSearchService
lazy val articleIndexService = new ArticleIndexService
override lazy val articleRepository = new ArticleRepository
override lazy val articleSearchService = new ArticleSearchService
override lazy val articleIndexService = new ArticleIndexService

lazy val converterService = new ConverterService
lazy val contentValidator = new ContentValidator()
override lazy val converterService = new ConverterService
override lazy val contentValidator = new ContentValidator()

lazy val ndlaClient = new NdlaClient
lazy val searchConverterService = new SearchConverterService
lazy val readService = new ReadService
lazy val writeService = new WriteService
override lazy val ndlaClient = new NdlaClient
override lazy val searchConverterService = new SearchConverterService
override lazy val readService = new ReadService
override lazy val writeService = new WriteService

var e4sClient: NdlaE4sClient = Elastic4sClientFactory.getClient(props.SearchServer)
lazy val searchApiClient = new SearchApiClient
lazy val feideApiClient = new FeideApiClient
lazy val myndlaApiClient = new MyNDLAApiClient
lazy val redisClient = new RedisClient(props.RedisHost, props.RedisPort)
lazy val frontpageApiClient = new FrontpageApiClient
lazy val imageApiClient = new ImageApiClient
var e4sClient: NdlaE4sClient = Elastic4sClientFactory.getClient(props.SearchServer)
override lazy val searchApiClient = new SearchApiClient
override lazy val feideApiClient = new FeideApiClient
override lazy val myndlaApiClient = new MyNDLAApiClient
override lazy val redisClient = new RedisClient(props.RedisHost, props.RedisPort)
override lazy val frontpageApiClient = new FrontpageApiClient
override lazy val imageApiClient = new ImageApiClient

lazy val clock = new SystemClock

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Memoize[R](maxCacheAgeMs: Long, f: () => R, autoRefreshCache: Boolean) ext
def isExpired: Boolean = lastUpdated + maxCacheAgeMs <= System.currentTimeMillis()
}

private[this] var cache: Option[CacheValue] = None
private var cache: Option[CacheValue] = None

private def renewCache(): Unit = {
cache = Some(CacheValue(f(), System.currentTimeMillis()))
Expand All @@ -28,7 +28,7 @@ class Memoize[R](maxCacheAgeMs: Long, f: () => R, autoRefreshCache: Boolean) ext
val task = new Runnable {
def run(): Unit = renewCache()
}
ex.scheduleAtFixedRate(task, 20, maxCacheAgeMs, TimeUnit.MILLISECONDS)
ex.scheduleAtFixedRate(task, 20, maxCacheAgeMs, TimeUnit.MILLISECONDS): Unit
}

def apply(): R = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import no.ndla.network.tapir.Parameters.feideHeader
import no.ndla.network.tapir.{DynamicHeaders, TapirController}
import no.ndla.network.tapir.TapirUtil.errorOutputsFor
import sttp.model.{Header, MediaType}
import sttp.tapir.EndpointIO.annotations.{header, jsonbody}
import sttp.tapir.*
import sttp.tapir.generic.auto.*
import sttp.tapir.server.ServerEndpoint
Expand All @@ -36,9 +35,7 @@ import scala.util.{Failure, Success, Try}
trait ArticleControllerV2 {
this: ReadService & WriteService & ArticleSearchService & SearchConverterService & ConverterService &
ContentValidator & Props & ErrorHandling & TapirController =>
val articleControllerV2: ArticleControllerV2

import props.*
lazy val articleControllerV2: ArticleControllerV2

class ArticleControllerV2 extends TapirController {
protected val applicationDescription = "Services for accessing articles from NDLA."
Expand Down Expand Up @@ -79,25 +76,18 @@ trait ArticleControllerV2 {
private val fallback =
query[Boolean]("fallback").description("Fallback to existing language if language is specified.").default(false)
protected val scrollId: EndpointInput.Query[Option[String]] = query[Option[String]]("search-context").description(
s"""A unique string obtained from a search you want to keep scrolling in. To obtain one from a search, provide one of the following values: ${InitialScrollContextKeywords
s"""A unique string obtained from a search you want to keep scrolling in. To obtain one from a search, provide one of the following values: ${props.InitialScrollContextKeywords
.mkString("[", ",", "]")}.
|When scrolling, the parameters from the initial search is used, except in the case of '${this.language.name}' and '${this.fallback.name}'.
|This value may change between scrolls. Always use the one in the latest scroll result (The context, if unused, dies after $ElasticSearchScrollKeepAlive).
|If you are not paginating past $ElasticSearchIndexMaxResultWindow hits, you can ignore this and use '${this.pageNo.name}' and '${this.pageSize.name}' instead.
|This value may change between scrolls. Always use the one in the latest scroll result (The context, if unused, dies after ${props.ElasticSearchScrollKeepAlive}).
|If you are not paginating past ${props.ElasticSearchIndexMaxResultWindow} hits, you can ignore this and use '${this.pageNo.name}' and '${this.pageSize.name}' instead.
|""".stripMargin
)
private val grepCodes = listQuery[String]("grep-codes")
.description(
"A comma separated list of codes from GREP API the resources should be filtered by."
)

private case class SummaryWithHeader(
@jsonbody
body: SearchResultV2DTO,
@header("search-context")
searchContext: Option[String]
)

/** Does a scroll with [[ArticleSearchService]] If no scrollId is specified execute the function @orFunction in the
* second parameter list.
*
Expand All @@ -110,7 +100,7 @@ trait ArticleControllerV2 {
orFunction: => Try[(SearchResultV2DTO, DynamicHeaders)]
): Try[(SearchResultV2DTO, DynamicHeaders)] =
scrollId match {
case Some(scroll) if !InitialScrollContextKeywords.contains(scroll) =>
case Some(scroll) if !props.InitialScrollContextKeywords.contains(scroll) =>
articleSearchService.scroll(scroll, language) match {
case Success(scrollResult) =>
val body = searchConverterService.asApiSearchResultV2(scrollResult)
Expand All @@ -134,8 +124,8 @@ trait ArticleControllerV2 {
.errorOut(errorOutputsFor())
.serverLogicPure { case (query, pageSize, pageNo, language) =>
val queryOrEmpty = query.getOrElse("")
val parsedPageSize = pageSize.getOrElse(DefaultPageSize) match {
case tooSmall if tooSmall < 1 => DefaultPageSize
val parsedPageSize = pageSize.getOrElse(props.DefaultPageSize) match {
case tooSmall if tooSmall < 1 => props.DefaultPageSize
case x => x
}
val parsedPageNo = pageNo.getOrElse(1) match {
Expand Down Expand Up @@ -228,9 +218,9 @@ trait ArticleControllerV2 {
) =>
scrollSearchOr(scrollId, language.code) {
val sort = Sort.valueOf(maybeSort.getOrElse(""))
val pageSize = maybePageSize.getOrElse(DefaultPageSize)
val pageSize = maybePageSize.getOrElse(props.DefaultPageSize)
val page = maybePageNo.getOrElse(1)
val shouldScroll = scrollId.exists(InitialScrollContextKeywords.contains)
val shouldScroll = scrollId.exists(props.InitialScrollContextKeywords.contains)

search(
query,
Expand Down Expand Up @@ -301,12 +291,12 @@ trait ArticleControllerV2 {
val query = searchParams.query
val sort = searchParams.sort
val license = searchParams.license
val pageSize = searchParams.pageSize.getOrElse(DefaultPageSize)
val pageSize = searchParams.pageSize.getOrElse(props.DefaultPageSize)
val page = searchParams.page.getOrElse(1)
val idList = searchParams.ids.getOrElse(List.empty)
val articleTypesFilter = searchParams.articleTypes.getOrElse(List.empty)
val grepCodes = searchParams.grepCodes.getOrElse(Seq.empty)
val shouldScroll = searchParams.scrollId.exists(InitialScrollContextKeywords.contains)
val shouldScroll = searchParams.scrollId.exists(props.InitialScrollContextKeywords.contains)

search(
query,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import scala.util.{Failure, Success}
trait InternController {
this: ReadService & WriteService & ConverterService & ArticleRepository & IndexService & ArticleIndexService &
ContentValidator & Props & DBArticle & TapirController =>
val internController: InternController
lazy val internController: InternController

class InternController extends TapirController with StrictLogging {
override val prefix: EndpointInput[Unit] = "intern"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class V47__RenameAlignAttributeTable extends BaseJavaMigration {

override def migrate(context: Context): Unit = DB(context.getConnection)
.autoClose(false)
.withinTx { session => migrateRows(session) }
.withinTx { session => migrateRows(using session) }

private def migrateRows(implicit session: DBSession): Unit = {
val count = countAllRows.get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class V48__ConvertSizeVariantToHideByline extends BaseJavaMigration {

override def migrate(context: Context): Unit = DB(context.getConnection)
.autoClose(false)
.withinTx { session => migrateRows(session) }
.withinTx { session => migrateRows(using session) }

private def migrateRows(implicit session: DBSession): Unit = {
val count = countAllRows.get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class V49__RemoveConceptListEmbeds extends BaseJavaMigration {

override def migrate(context: Context): Unit = DB(context.getConnection)
.autoClose(false)
.withinTx { session => migrateRows(session) }
.withinTx { session => migrateRows(using session) }

private def migrateRows(implicit session: DBSession): Unit = {
val count = countAllRows.get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class V50__RemoveUnsupportedContactBlockAttributes extends BaseJavaMigration {

override def migrate(context: Context): Unit = DB(context.getConnection)
.autoClose(false)
.withinTx { session => migrateRows(session) }
.withinTx { session => migrateRows(using session) }

private def migrateRows(implicit session: DBSession): Unit = {
val count = countAllRows.get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class V54__RemoveStrongFromTitle extends BaseJavaMigration {

override def migrate(context: Context): Unit = DB(context.getConnection)
.autoClose(false)
.withinTx { session => migrateRows(session) }
.withinTx { session => migrateRows(using session) }

private def migrateRows(implicit session: DBSession): Unit = {
val count = countAllRows.get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class R__SetArticleLanguageFromTaxonomy(properties: ArticleApiProperties)
extends BaseJavaMigration
with Props
with DBArticle {
override val props: ArticleApiProperties = properties
override def getChecksum: Integer = 1 // Change this to something else if you want to repeat migration
override lazy val props: ArticleApiProperties = properties
override def getChecksum: Integer = 1 // Change this to something else if you want to repeat migration

override def migrate(context: Context): Unit = {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ import no.ndla.articleapi.{ArticleApiProperties, Props}
import org.flywaydb.core.api.migration.{BaseJavaMigration, Context}

class V33__ConvertLanguageUnknown(properties: ArticleApiProperties) extends BaseJavaMigration with Props {
override val props: ArticleApiProperties = properties
override def migrate(context: Context): Unit = {}
override lazy val props: ArticleApiProperties = properties
override def migrate(context: Context): Unit = {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import scala.util.Try

trait FrontpageApiClient {
this: NdlaClient & ConverterService & Props =>
val frontpageApiClient: FrontpageApiClient
lazy val frontpageApiClient: FrontpageApiClient

class FrontpageApiClient(FrontpageApiBaseUrl: String = props.FrontpageApiUrl) extends StrictLogging {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import scala.util.Try

trait ImageApiClient {
this: NdlaClient & ConverterService & Props =>
val imageApiClient: ImageApiClient
lazy val imageApiClient: ImageApiClient

class ImageApiClient {
private val Endpoint = s"http://${props.ImageApiHost}/image-api/v3/images"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import scala.util.{Failure, Success, Try}

trait ArticleRepository {
this: DataSource & DBArticle =>
val articleRepository: ArticleRepository
lazy val articleRepository: ArticleRepository

class ArticleRepository extends StrictLogging {

Expand Down Expand Up @@ -130,7 +130,7 @@ trait ArticleRepository {
ORDER BY revision DESC
LIMIT 1
"""
)(session)
)(using session)

def withIdAndRevision(articleId: Long, revision: Int): Option[ArticleRow] = {
articleWhere(
Expand Down Expand Up @@ -188,8 +188,8 @@ trait ArticleRepository {

private def externalIdsFromResultSet(wrappedResultSet: WrappedResultSet): List[String] = {
Option(wrappedResultSet.array("external_id"))
.map(_.getArray.asInstanceOf[Array[String]])
.getOrElse(Array.empty)
.map(x => x.getArray.asInstanceOf[Array[String]])
.getOrElse(Array.empty[String])
.toList
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ import scala.util.{Failure, Success, Try}

trait ConverterService {
this: Clock & ArticleRepository & Props =>
val converterService: ConverterService

import props.*
lazy val converterService: ConverterService

class ConverterService extends StrictLogging {

Expand Down Expand Up @@ -374,7 +372,7 @@ trait ConverterService {

private def toApiArticleMetaImage(metaImage: ArticleMetaImage): api.ArticleMetaImageDTO = {
api.ArticleMetaImageDTO(
s"${externalApiUrls("raw-image")}/${metaImage.imageId}",
s"${props.externalApiUrls("raw-image")}/${metaImage.imageId}",
metaImage.altText,
metaImage.language
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import scala.util.{Failure, Success, Try}
trait ReadService {
this: ArticleRepository & FeideApiClient & ConverterService & ArticleSearchService & SearchConverterService &
MemoizeHelpers & Props & ErrorHandling & FrontpageApiClient =>
val readService: ReadService
lazy val readService: ReadService

class ReadService extends StrictLogging {
def getInternalIdByExternalId(externalId: String): Option[api.ArticleIdV2DTO] =
Expand Down
Loading