src/main/scala/bootstrap/liftweb/Boot.scala
changeset 22 94ed9cfdb123
parent 16 7be37d58997c
child 35 d77d8194ee59
equal deleted inserted replaced
21:641c7190c566 22:94ed9cfdb123
     1 /*
     1 /*
     2  * Copyright 2011 Tomas Zeman <tzeman@volny.cz>
     2  * Copyright 2011-2012 Tomas Zeman <tzeman@volny.cz>
     3  *
     3  *
     4  * Licensed under the Apache License, Version 2.0 (the "License");
     4  * Licensed under the Apache License, Version 2.0 (the "License");
     5  * you may not use this file except in compliance with the License.
     5  * you may not use this file except in compliance with the License.
     6  * You may obtain a copy of the License at
     6  * You may obtain a copy of the License at
     7  *
     7  *
    15  */
    15  */
    16 package bootstrap.liftweb
    16 package bootstrap.liftweb
    17 
    17 
    18 import fis.base.model._
    18 import fis.base.model._
    19 import fis.crm.ui.{ContactSnippet, ContactSnippet2}
    19 import fis.crm.ui.{ContactSnippet, ContactSnippet2}
       
    20 import fis.db.SquerylTxMgr
    20 import net.liftweb.common._
    21 import net.liftweb.common._
    21 import net.liftweb.db.{DB, ConnectionIdentifier}
       
    22 import net.liftweb.http._
    22 import net.liftweb.http._
    23 import net.liftweb.sitemap._
    23 import net.liftweb.sitemap._
    24 import net.liftweb.squerylrecord.SquerylRecord
    24 import net.liftweb.squerylrecord.SquerylRecord
    25 import net.liftweb.util._
    25 import net.liftweb.util._
    26 import net.liftweb.util.Helpers._
    26 import net.liftweb.util.Helpers._
    27 import net.tz.lift.boot.ProtoBoot
    27 import net.tz.lift.boot.ProtoBoot
    28 import net.tz.lift.util.StandardDBVendor
       
    29 
    28 
    30 class Boot extends ProtoBoot {
    29 class Boot extends ProtoBoot {
    31   override def boot = {
    30   override def boot = {
    32 
    31 
    33     /* DB stuff */
    32     /* DB stuff */
    34     val dbVendor = new FisDbVendor
    33     SquerylTxMgr.init()
    35     DB.defineConnectionManager(FisConnectionIdentifier, dbVendor)
       
    36     S.addAround(DB.buildLoanWrapper(List(FisConnectionIdentifier)))
       
    37     SquerylRecord.init(() => new SeqIdPostgreSqlAdapter)
       
    38     SquerylTxMgr.initSqueryl(FisConnectionIdentifier,
       
    39       () => new SeqIdPostgreSqlAdapter)
       
    40     LiftRules.unloadHooks.append(dbVendor.closeAllConnections_! _)
       
    41 
    34 
    42     super.boot
    35     super.boot
    43 
    36 
    44     import Loc._
    37     import Loc._
    45 
    38 
    48 
    41 
    49     LiftRules.setSiteMap(SiteMap(menus:_*))
    42     LiftRules.setSiteMap(SiteMap(menus:_*))
    50   }
    43   }
    51 }
    44 }
    52 
    45 
    53 case object FisConnectionIdentifier extends ConnectionIdentifier {
       
    54   val jndiName = "fis"
       
    55 }
       
    56 
       
    57 import java.sql.Connection
       
    58 
       
    59 class FisDbVendor extends StandardDBVendor(
       
    60   "org.postgresql.Driver", Props.get("db.fis.url", ""),
       
    61   Props.get("db.fis.user"), Props.get("db.fis.pass")) {
       
    62 
       
    63   val testQuery = "SELECT version()"
       
    64   override protected def testConnection(c: Connection) = {
       
    65     c.prepareStatement(testQuery).executeQuery
       
    66   }
       
    67 }
       
    68 
       
    69 import org.squeryl._
       
    70 import org.squeryl.internals.DatabaseAdapter
       
    71 
       
    72 object SquerylTxMgr {
       
    73   def initSqueryl(name: ConnectionIdentifier,
       
    74     mkAdapter: () => DatabaseAdapter) = {
       
    75     SessionFactory.externalTransactionManagementAdapter =
       
    76     Some(new SquerylTxMgr(name, mkAdapter))
       
    77   }
       
    78 }
       
    79 
       
    80 class SquerylTxMgr(name: ConnectionIdentifier, mkAdapter: () => DatabaseAdapter)
       
    81   extends Loggable with Function0[Session] {
       
    82 
       
    83   private object currentSession extends DynoVar[Session]
       
    84 
       
    85 
       
    86   def apply = currentSession.is openOr {
       
    87     DB.use(name) { superConn =>
       
    88       val sess = Session.create(superConn.connection, mkAdapter())
       
    89       sess.setLogger(s => logger.debug(s))
       
    90       currentSession.set(sess)
       
    91       sess
       
    92     }
       
    93   }
       
    94 }
       
    95 
       
    96 // vim: set ts=2 sw=2 et:
    46 // vim: set ts=2 sw=2 et: