-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds CommandHandler trait and ModelCommand base class
- Loading branch information
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
data-binding/src/main/scala/org/scalatra/databinding/CommandHandler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.scalatra | ||
package databinding | ||
|
||
import scala.util.control.Exception.allCatch | ||
import grizzled.slf4j.Logger | ||
import validation._ | ||
import scalaz._ | ||
import Scalaz._ | ||
|
||
trait CommandHandler { | ||
@transient private[this] lazy val logger: Logger = Logger[this.type] | ||
def execute[S: Manifest](cmd: ModelCommand[S]): ModelValidation[S] = { | ||
logger.debug("Executing [%s].\n%s" format (cmd.getClass.getName, cmd)) | ||
if (cmd.isValid) { | ||
val res = (allCatch withApply (serverError(cmd.getClass.getName, _))) { | ||
handle.lift(cmd).map(_.map(_.asInstanceOf[S])) | ValidationError("Don't know how to handle: " + cmd.getClass.getName, UnknownError).failNel | ||
} | ||
val ftext = "with %d failures\n%s".format(~res.fail.toOption.map(_.list.size), ~res.fail.toOption.map(_.list)) | ||
logger.debug("Command [%s] executed %s." format (cmd.getClass.getName, res.isSuccess ? "successfully." | ftext)) | ||
res | ||
} else { | ||
val f = cmd.errors.map(_.validation) collect { | ||
case Failure(e) ⇒ e | ||
} | ||
logger.debug("Command [%s] executed with %d failures.\n%s" format (cmd.getClass.getName, f.size, f.toList)) | ||
nel(f.head, f.tail: _*).fail | ||
} | ||
} | ||
|
||
private[this] def serverError[R](cmdName: String, ex: Throwable): ModelValidation[R] = { | ||
logger.error("There was an error while executing " + cmdName, ex) | ||
ValidationError("An error occurred while handling: " + cmdName, UnknownError).failNel[R] | ||
} | ||
|
||
type Handler = PartialFunction[ModelCommand[_], ModelValidation[_]] | ||
|
||
protected def handle: Handler | ||
} |
6 changes: 6 additions & 0 deletions
6
data-binding/src/main/scala/org/scalatra/databinding/ModelCommand.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.scalatra | ||
package databinding | ||
|
||
abstract class ModelCommand[T:Manifest] extends Command { | ||
type ModelType = T | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters