# HG changeset patch # User Jaroslav Tulach # Date 1266279756 -3600 # Node ID 3ea04696a115f9222a659a591f91f669f45dcfbb # Parent c8332e8197f7b55a991a78bc6fdf0f0a320536b3 Preparing for more testable 'business' logic - splitting the for cycles and REST queries. diff -r c8332e8197f7 -r 3ea04696a115 emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala --- a/emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala Mon Feb 15 23:34:53 2010 +0100 +++ b/emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala Tue Feb 16 01:22:36 2010 +0100 @@ -44,30 +44,78 @@ val api = new URL(args(0)); - val id = login(api, args(1), args(2)) - - val ret = allPlayers( - api, - 3600 * 1000 * 24 - ) + val loginID = login(api, args(1), args(2)) + val okDelay = 3600 * 1000 * 24 val props = java.lang.System.getProperties props.put("mail.smtp.host", args(3)); val session = javax.mail.Session.getDefaultInstance(props, null) + val notify = new Notify() { + protected def allPlayers(): Map[String,List[Node]] = { + val u = new URL(api, "games"); + val conn = u.openConnection + conn.setRequestProperty("Accept", "text/xml") - for (val address <- emails(api, id, ret.keySet)) { - Console.println("Sending message to " + address) + val ret = new HashMap[String,List[Node]] + val before = System.currentTimeMillis - okDelay + val games = XML.load(conn.getInputStream) + for (val g : Node <- games \\ "gameId") { + val modified = (g \\ "@modified").text.toLong + if (modified < before) { + val status = (g \\ "@status").text; + val name = + if (status == "blackMove") { + (g \\ "@black").text + } else if (status == "whiteMove") { + (g \\ "@white").text + } else { + null + } + if (name != null) { + val prev = ret.getOrElse(name, Nil) + val next = g :: prev + ret.put(name, next); + } + } + } + return ret; + } + + protected def emails(ids : Collection[String]): List[String] = { + var ret : List[String] = Nil; + for (val p <- ids) { + val u = new URL(api, "users/" + p + "?loginID=" + loginID) + val conn = u.openConnection + conn.setRequestProperty("Accept", "text/xml") + val xml = XML.load(conn.getInputStream) + val res = (xml \\ "property").filter({ + n => n.attribute("name") match { + case Some(attr) => attr.text == "email" + case _ => false + } + }) + if (!res.isEmpty) { + ret = res(0).text :: ret; + } + } + return ret + } + + protected def sendEmail(address : String, subject : String, text : String) : Unit = { val message = new MimeMessage(session) message.setFrom(new InternetAddress("quoridor@xelfi.cz")) message.addRecipient(Message.RecipientType.TO, new InternetAddress(address)) - message.setSubject("Play Quoridor!") - message.setText("Visit http://quoridor.xelfi.cz") + message.setSubject(subject) + message.setText(text) Transport.send(message) + } } + + notify.process() } def login(url : URL, name : String, password : String) : String = { @@ -79,55 +127,4 @@ val reply = Source.fromInputStream(conn.getInputStream) return reply.mkString("") } - - def allPlayers(url : URL, okDelay : Long): Map[String,List[Node]] = { - val u = new URL(url, "games"); - val conn = u.openConnection - conn.setRequestProperty("Accept", "text/xml") - - val ret = new HashMap[String,List[Node]] - - val before = System.currentTimeMillis - okDelay - val games = XML.load(conn.getInputStream) - for (val g : Node <- games \\ "gameId") { - val modified = (g \\ "@modified").text.toLong - if (modified < before) { - val status = (g \\ "@status").text; - val name = - if (status == "blackMove") { - (g \\ "@black").text - } else if (status == "whiteMove") { - (g \\ "@white").text - } else { - null - } - if (name != null) { - val prev = ret.getOrElse(name, Nil) - val next = g :: prev - ret.put(name, next); - } - } - } - return ret; - } - - def emails(url : URL, loginID : String, ids : Collection[String]): List[String] = { - var ret : List[String] = Nil; - for (val p <- ids) { - val u = new URL(url, "users/" + p + "?loginID=" + loginID) - val conn = u.openConnection - conn.setRequestProperty("Accept", "text/xml") - val xml = XML.load(conn.getInputStream) - val res = (xml \\ "property").filter({ - n => n.attribute("name") match { - case Some(attr) => attr.text == "email" - case _ => false - } - }) - if (!res.isEmpty) { - ret = res(0).text :: ret; - } - } - return ret - } } diff -r c8332e8197f7 -r 3ea04696a115 emailer/src/main/scala/cz/xelfi/quoridor/emailer/Notify.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/emailer/src/main/scala/cz/xelfi/quoridor/emailer/Notify.scala Tue Feb 16 01:22:36 2010 +0100 @@ -0,0 +1,56 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2009 Jaroslav Tulach + */ + +package cz.xelfi.quoridor.emailer + +import scala.xml._ +import java.net.URL +import scala.io._ +import scala.collection.Map +import scala.collection.mutable.HashMap +import javax.mail.internet._ +import javax.mail.Message +import javax.mail.Transport + +abstract class Notify { + protected def allPlayers(): Map[String,List[Node]] + protected def emails(ids : Collection[String]): List[String] + protected def sendEmail(address : String, subject : String, text : String) : Unit + + def process() { + val ret = allPlayers() + + for (val address <- emails(ret.keySet)) { + Console.println("Sending message to " + address) + + sendEmail( + "quoridor@xelfi.cz", + "Play Quoridor!", + "Visit http://quoridor.xelfi.cz" + ) + } + } +}