src/main/scala/fis/notif/Notification.scala
author Tomas Zeman <tzeman@volny.cz>
Tue, 23 Apr 2013 10:36:04 +0200
changeset 108 ef4e3e0ef83f
permissions -rw-r--r--
84a94fa29a67504b Task/Project notifications
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
108
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     1
/*
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     2
 * Copyright 2012 Tomas Zeman <tzeman@volny.cz>
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     3
 *
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     4
 * Licensed under the Apache License, Version 2.0 (the "License");
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     5
 * you may not use this file except in compliance with the License.
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     6
 * You may obtain a copy of the License at
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     7
 *
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     8
 *     http://www.apache.org/licenses/LICENSE-2.0
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     9
 *
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    10
 * Unless required by applicable law or agreed to in writing, software
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    11
 * distributed under the License is distributed on an "AS IS" BASIS,
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    13
 * See the License for the specific language governing permissions and
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    14
 * limitations under the License.
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    15
 */
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    16
package fis.notif
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    17
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    18
import fis.aaa.model.{User, UserContacts}
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    19
import javax.mail.internet.MimeUtility
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    20
import net.liftweb.common._
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    21
import net.liftweb.http._
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    22
import net.liftweb.util._
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    23
import net.liftweb.util.Helpers._
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    24
import net.tz.lift.snippet._
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    25
import scala.xml.NodeSeq
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    26
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    27
import Mailer._
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    28
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    29
case class Notification(_path: String, _title: CssTr,
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    30
  _content: CssTr, _note: CssTr, _from: From, _subject: String,
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    31
  _mailTypes: Seq[MailTypes]) {
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    32
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    33
  def title(t: CssTr) = this.copy(_title = t)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    34
  def title(t: NodeSeq) = this.copy(_title = lift(t))
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    35
  def content(c: CssTr) = this.copy(_content = c)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    36
  def note(n: CssTr) = this.copy(_note = n)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    37
  def note(n: String) = this.copy(_note = "*" #> n)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    38
  def from(f: From) = this.copy(_from = f)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    39
  def subject(s: String) = this.copy(_subject = s)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    40
  def >> (mt: MailTypes) = this.copy(_mailTypes = _mailTypes :+ mt)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    41
  def >> (u: User) = this.copy(_mailTypes = _mailTypes ++
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    42
    (UserContacts(u) map { c => To(c.workMail.get, Box !! c.linkName)}))
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    43
  def to(u: Box[User]): Notification = u.dmap(this)(>> _)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    44
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    45
  private def lift(ns: NodeSeq): CssTr = { _ => ns }
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    46
}
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    47
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    48
object Notification {
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    49
  def make: Notification = Notification("entity/notif", ClearNodes,
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    50
    ClearNodes, ClearNodes, From(Props.get("sender.email") openOr "",
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    51
    Props.get("sender.name")), "", Nil)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    52
}
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    53
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    54
class NotificationEngine extends Loggable {
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    55
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    56
  type Tr = Notification => Notification
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    57
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    58
  import java.util.concurrent.atomic.AtomicReference
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    59
  import scala.collection.mutable.ArrayBuffer
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    60
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    61
  private lazy val _rules = new ArrayBuffer[Rule]()
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    62
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    63
  class RuleEntrance(val name: String) {
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    64
    def when(cond: => Boolean) = new {
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    65
      def then(f: Tr*) = {
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    66
        val r = new Rule(name, cond, f.toSeq)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    67
        _rules += r
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    68
        logger.debug("Defined new rule: %s, rules count: %d".format(r.name,
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    69
          _rules.size))
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    70
        r
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    71
      }
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    72
    }
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    73
  }
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    74
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    75
  implicit def str2ruleEntrance(name: String) = new RuleEntrance(name)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    76
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    77
  class Rule(val name: String, cond: => Boolean, tr: Seq[Tr]) {
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    78
    lazy val n = Notification.make
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    79
    def active: Boolean = cond
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    80
    def apply() = tr.foldLeft(n)((_n, f) => f(_n))
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    81
  }
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    82
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    83
  def subject(s: String): Tr = _.subject(s)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    84
  def title(t: CssTr): Tr = _.title(t)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    85
  def content(c: CssTr): Tr = _.content(c)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    86
  def to(u: Box[User]): Tr = _.to(u)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    87
  def note(n: CssTr): Tr = _.note(n)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    88
  def note(n: String): Tr = _.note(n)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    89
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    90
  private def enc(s: String) = MimeUtility.encodeText(s, "utf-8", null)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    91
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    92
  def execute(f: => Rule) = {
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    93
    withBaseUrl.run(Props.get("url.base") openOr "") {
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    94
      f
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    95
      for {
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    96
        r <- _rules if r.active
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    97
        (out, n) <- {
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    98
          val n = r()
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    99
          S.runTemplate(n._path split "/" toList,
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   100
            "title" -> n._title,
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   101
            "panel" -> n._content,
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   102
            "note" -> n._note) map(_ -> n)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   103
        }
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   104
      } yield {
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   105
        sendMail(n._from, Subject(enc(n._subject)), (n._mailTypes :+ XHTMLMailBodyType(out)):_*)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   106
        (n, n._subject, n._mailTypes flatMap {
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   107
          case To(to, _) => Full(to)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   108
          case CC(cc, _) => Full(cc)
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   109
          case _ => Empty
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   110
        })
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   111
      }
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   112
    }
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   113
  }
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   114
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   115
}
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   116
ef4e3e0ef83f 84a94fa29a67504b Task/Project notifications
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   117
// vim: set ts=2 sw=2 et: