@@ -6,9 +6,7 @@ import app.softnetwork.persistence.jdbc.db.SlickDatabase
66import app .softnetwork .persistence .model .{StateWrapper , Timestamped }
77import app .softnetwork .persistence .query .JsonProvider
88import app .softnetwork .serialization .{serialization , updateCaseClass }
9- import com .typesafe .scalalogging .Logger
109import org .json4s .Formats
11- import org .slf4j .LoggerFactory
1210import slick .jdbc .JdbcProfile
1311
1412import java .time .Instant
@@ -22,14 +20,22 @@ trait JdbcStateProvider[T <: Timestamped]
2220 with Completion {
2321 _ : ManifestWrapper [T ] with JdbcProfile =>
2422
25- protected lazy val alogger : Logger =
26- Logger (LoggerFactory .getLogger(getClass.getName))
27-
28- val dataset : Option [String ] = None
23+ def dataset : Option [String ] = {
24+ if (config.hasPath(" jdbc-external-processor.dataset" )) {
25+ val d = config.getString(" jdbc-external-processor.dataset" )
26+ if (d.nonEmpty) {
27+ Some (d)
28+ } else {
29+ None
30+ }
31+ } else {
32+ None
33+ }
34+ }
2935
3036 lazy val table : String = getType[T ](manifestWrapper.wrapped)
3137
32- lazy val tableFullName : String = dataset match {
38+ private [ this ] lazy val tableFullName : String = dataset match {
3339 case Some (s) => s " $s. $table"
3440 case _ => table
3541 }
@@ -198,7 +204,7 @@ trait JdbcStateProvider[T <: Timestamped]
198204 (if (! to_update) {
199205 insert(document.uuid, document.lastUpdated, document.deleted, state)
200206 } else {
201- alogger .debug(s " Updating document ${document.uuid} with data $state" )
207+ log .debug(s " Updating document ${document.uuid} with data $state" )
202208 update(document.uuid, document.lastUpdated, document.deleted, state)
203209 }) && writeToFile(
204210 document,
@@ -229,10 +235,10 @@ trait JdbcStateProvider[T <: Timestamped]
229235 (states += (uuid, lastUpdated, deleted, state)).map(_ > 0 )
230236 db.run(action) complete () match {
231237 case Success (value) =>
232- alogger .debug(s " Insert to $tableFullName with $uuid -> $value" )
238+ log .debug(s " Insert to $tableFullName with $uuid -> $value" )
233239 value
234240 case Failure (f) =>
235- alogger .error(f.getMessage, f)
241+ log .error(f.getMessage, f)
236242 false
237243 }
238244 }
@@ -265,13 +271,13 @@ trait JdbcStateProvider[T <: Timestamped]
265271 db.run(action) complete () match {
266272 case Success (value) =>
267273 if (deleted) {
268- alogger .debug(s " Delete from $tableFullName with $uuid -> $value" )
274+ log .debug(s " Delete from $tableFullName with $uuid -> $value" )
269275 } else {
270- alogger .debug(s " Update to $tableFullName with $uuid -> $value" )
276+ log .debug(s " Update to $tableFullName with $uuid -> $value" )
271277 }
272278 value
273279 case Failure (f) =>
274- alogger .error(f.getMessage, f)
280+ log .error(f.getMessage, f)
275281 false
276282 }
277283 }
@@ -287,10 +293,10 @@ trait JdbcStateProvider[T <: Timestamped]
287293 val action = states.filter(_.uuid === uuid).delete.map(_ > 0 )
288294 db.run(action) complete () match {
289295 case Success (value) =>
290- alogger .debug(s " Delete from $tableFullName with $uuid -> $value" )
296+ log .debug(s " Delete from $tableFullName with $uuid -> $value" )
291297 value
292298 case Failure (f) =>
293- alogger .error(f.getMessage, f)
299+ log .error(f.getMessage, f)
294300 false
295301 }
296302 }
@@ -312,18 +318,18 @@ trait JdbcStateProvider[T <: Timestamped]
312318 // logger.info(s"$document")
313319 document match {
314320 case (_, _, deleted, _) if deleted =>
315- alogger .debug(s " Load $tableFullName with $uuid -> None " )
321+ log .debug(s " Load $tableFullName with $uuid -> None " )
316322 None
317323 case (_, _, _, state) =>
318- alogger .debug(s " Load $tableFullName with $uuid -> $value" )
319- Option (readState(state))
324+ log .debug(s " Load $tableFullName with $uuid -> $value" )
325+ Option (readState(state)(manifest) )
320326 }
321327 case _ =>
322- alogger .debug(s " Load $tableFullName with $uuid -> None " )
328+ log .debug(s " Load $tableFullName with $uuid -> None " )
323329 None
324330 }
325331 case Failure (f) =>
326- alogger .error(f.getMessage, f)
332+ log .error(f.getMessage, f)
327333 None
328334 }
329335 }
@@ -347,15 +353,15 @@ trait JdbcStateProvider[T <: Timestamped]
347353 }
348354 db.run(action) complete () match {
349355 case Success (value) =>
350- alogger .debug(s " Search $tableFullName with $query -> $value" )
356+ log .debug(s " Search $tableFullName with $query -> $value" )
351357 value.map(readState)
352358 case Failure (f) =>
353- alogger .error(f.getMessage, f)
359+ log .error(f.getMessage, f)
354360 Nil
355361 }
356362 }
357363
358- class States (tag : Tag ) extends Table [(String , Instant , Boolean , String )](tag, tableFullName ) {
364+ class States (tag : Tag ) extends Table [(String , Instant , Boolean , String )](tag, dataset, table ) {
359365 def uuid = column[String ](" uuid" , O .PrimaryKey )
360366 def lastUpdated = column[Instant ](" last_updated" )
361367 def deleted = column[Boolean ](" deleted" )
@@ -375,14 +381,36 @@ trait JdbcStateProvider[T <: Timestamped]
375381 }
376382
377383 def initTable (): Unit = {
378- alogger.info(
384+ initDataset()
385+ log.info(
379386 s " Setting up table $tableFullName ${states.schema.createStatements.mkString(" ;\n " )}"
380387 )
381388 db.run(DBIO .seq(states.schema.createIfNotExists)).complete() match {
382389 case Success (_) =>
383- alogger .debug(s " Setup table $tableFullName" )
390+ log .debug(s " Setup table $tableFullName" )
384391 case Failure (f) =>
385- alogger.error(f.getMessage, f)
392+ log.error(f.getMessage, f)
393+ }
394+ }
395+
396+ def initDataset (): Unit = {
397+ dataset match {
398+ case Some (d) =>
399+ log.info(
400+ s " Setting up dataset $d"
401+ )
402+ val ddl = s " CREATE SCHEMA IF NOT EXISTS $d"
403+ withStatement { stmt =>
404+ try {
405+ stmt.executeUpdate(ddl)
406+ log.debug(s " Setup dataset $d" )
407+ } catch {
408+ case t : java.sql.SQLSyntaxErrorException
409+ if t.getMessage contains " ORA-00942" => // suppress known error message in the test
410+ case other : Throwable => log.error(other.getMessage)
411+ }
412+ }
413+ case _ =>
386414 }
387415 }
388416}
0 commit comments