src/main/scala/bootstrap/liftweb/Boot.scala
author Tomas Zeman <tzeman@volny.cz>
Wed, 30 Nov 2011 16:59:10 +0100
changeset 22 6b1a7f3429ca
parent 2 cf829ec742b3
permissions -rw-r--r--
ca1737768512bd36 Search by dynamic IP address, IP assignment history list

/*
 * Copyright 2011 Tomas Zeman <tzeman@volny.cz>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package bootstrap.liftweb

import net.liftweb.common._
import net.liftweb.http._
import net.liftweb.http.js.JsObj
import net.liftweb.http.js.JE.JsObj
import net.liftweb.mapper._
import net.liftweb.sitemap._
import net.liftweb.sitemap.Loc._
import net.liftweb.widgets.menu.MenuStyle
import net.liftweb.widgets.menu.{MenuWidget => LiftMenuWidget}
import net.liftweb.util._
import net.tz.lift.snippet.ActionLinks
import net.tz.lift.util.YmdDateTimeConverter
import radview.model._
import radview.snippet._
import scala.xml.NodeSeq

class Boot extends Logger {

  def boot = {
    /* DB stuff */
    initDb("radacct", RadAcctConnectionIdentifier)
    initDb("radius", RadiusConnectionIdentifier)
    initDb("syslog", SyslogConnectionIdentifier)
    S.addAround(DB.buildLoanWrapper(List(RadAcctConnectionIdentifier,
      RadiusConnectionIdentifier, SyslogConnectionIdentifier)))

    if (Props.mode == Props.RunModes.Development)
      DB.addLogFunc { (dbLog, l) => dbLog.statementEntries.foreach { e =>
        debug("Query: " + e.statement)
    }}

    /* Date format */
    LiftRules.dateTimeConverter.default.set { () => YmdDateTimeConverter }

    /* Handle end slash and drop it (except for home page) */
    LiftRules.statelessRewrite.append {
      case RewriteRequest(ParsePath(xs,_,_,true),_,_) if (xs.size > 1) &&
        (xs.lastOption == Some("index")) =>
          RewriteResponse(xs dropRight 1)
    }

    /* Snippet dispatch */
    LiftRules.snippetDispatch.append {
      case "Menubar" => new AnyRef with DispatchSnippet {
        def dispatch: DispatchIt = {
          case _ => { xhtml =>
            new MenuWidget(MenuStyle.HORIZONTAL, JsObj(), Nil) render
          }
        }
      }
      case "action-links" => ActionLinks
    }

    /* Sitemap */
    SiteMap.enforceUniqueLinks = false

    LiftRules.setSiteMap(SiteMap(
      Menu.i("Home") / "index" >> Hidden,
      Menu(CellSnippet),
      Menu.i("Cells") / "cell",
      Menu(SessionSnippet),
      Menu(AccountSnippet),
      Menu.i("Accounts") / "account",
      DynIpSnippet.menu
    ))

    /* Menu widget */
    LiftMenuWidget.init()

    /* Http conf */
    LiftRules.logServiceRequestTiming = false
    LiftRules.early.append(_.setCharacterEncoding("UTF-8"))
  }

  def initDb(ident: String, connId: ConnectionIdentifier) = {
    val driver = "com.mysql.jdbc.Driver"
    val vendor = new StandardDBVendor(driver,
      Props.get("db."+ident+".url") openOr ("jdbc:mysql://localhost/"+ident),
      Props.get("db."+ident+".user"), Props.get("db."+ident+".pass"))
    //LiftRules.unloadHooks.append(vendor.closeAllConnections_! _)
    DB.defineConnectionManager(connId, vendor)
    vendor.newConnection(connId)
  }
}

class MenuWidget(style: MenuStyle.Value, jsObj: JsObj, groups : List[String])
  extends LiftMenuWidget(style, jsObj, groups) {

  override def render: NodeSeq = { 
    head ++ <div>{groups match {
      case Nil =>
        <lift:Menu.builder expandAll="true" linkToSelf="true" top:class={style.toString} />
      case xs => groups.flatMap(group =>
        <lift:Menu.builder expandAll="true" linkToSelf="true" top:class={style.toString} group={group} />)
    }}</div>
  }
}


// vim: set ts=2 sw=2 et: