Skip to content

Instantly share code, notes, and snippets.

@linqing
Created December 29, 2011 15:35
Show Gist options
  • Save linqing/1534600 to your computer and use it in GitHub Desktop.
Save linqing/1534600 to your computer and use it in GitHub Desktop.
workflow angine scala version
package star.oa
package service
import impl.mongo.WorkflowEntry
import impl.mongo.CurrentStep
import impl.mongo.HistoryStep
import impl.mongo.WorkflowComment
import star.oa.workflow._
import net.liftweb.http.S
import java.util.Date
import net.liftweb.util.Helpers._
class WorkflowService(workflowEntry: WorkflowEntry, workflowDes: WorkflowDes, env: web.snippet.StatelessE ) {
def doInitialize(action: Action) {
if ( action.restriction.map(web.snippet.E.evalBoolean(_)).getOrElse(true) ) {
val canContinue = action.preFunctions match {
case Some(function) =>
try {
web.snippet.E.evalBoolean(function)
} catch {
case e =>
e.printStackTrace()
S.notice("操作不成功, 错误是:" + e.getMessage())
false
}
case None => true
}
if ( canContinue ) {
val newCurrentStep = CurrentStep(
dueDate = new Date(),
finishDate = new Date(),
startDate = new Date(),
caller = "",
owner = tryo(action.unconditionalResult.map(_.owner.map(web.snippet.E.evalString(_)).getOrElse("")) getOrElse "") openOr ("#下一步拥有者计算错误#"),
status = "OK",
previousStepIds = workflowEntry.currentSteps.map(_.stepId).mkString("", ",", ""),
actionId = 0,
stepId = action.unconditionalResult.map(_.toStep) getOrElse 0,
entryId = ""
)
workflowEntry.currentSteps = List(newCurrentStep)
workflowEntry.isInitialized = true
} else {
S.error("前置操作不成功!")
}
} else {
S.error("流程", "当前不允许执行这个操作[" + action.restrictionDescription.getOrElse(action.name) + "]")
}
}
def doAction(currentStep: CurrentStep, action: Action) {
// 检查操作权限
if ( !action.restriction.map(env.evalBoolean(_)).getOrElse(true) ) {
S.error("流程", "当前不允许执行这个操作[" + action.restrictionDescription.getOrElse(action.name) + "]")
return;
}
// 检查意见栏是否填写
if ( action.commentRequired.getOrElse(false) && env.getDocument().v("tmpComment") == "" ) {
S.error(List(net.liftweb.util.FieldError(new FormFieldIdentifier("意见"), "出错啦[意见][必须填写!]") ))
return;
}
// 执行前置操作
val prefucnctionSuccessful = action.preFunctions match {
case Some(function) =>
try {
env.evalBoolean(function)
} catch {
case e =>
e.printStackTrace()
S.notice("操作不成功, 错误是:" + e.getMessage())
false
}
case None => true
}
if (!prefucnctionSuccessful) {
S.error("前置操作不成功!")
return
}
// 记录意见栏
workflowDes.steps.find(_.id == workflowEntry.currentSteps.head.stepId).foreach { // TODO
step =>
if (step.commentId.isDefined) {
val newComment = WorkflowComment(
"",
step.commentId.get,
"",
env.getCurrentUser().username,
env.getDocument().v("tmpComment"),
new Date()
)
workflowEntry.comments = workflowEntry.comments :+ newComment
env.getDocument().setItemValue("tmpComment", "")
}
}
transitionWorkflow(currentStep, action)
workflowEntry.isInitialized = true
S.notice("已经提交到下一步")
}
def transitionWorkflow(currentStep: CurrentStep, action: Action) {
val newHistoryStep = HistoryStep(dueDate = currentStep.dueDate,
finishDate = new Date(),
startDate = currentStep.startDate,
caller = env.getCurrentUser().username,
owner = currentStep.owner,
status = "OK",
previousStepIds = currentStep.previousStepIds,
actionId = action.id,
stepId = currentStep.stepId,
entryId = "")
workflowEntry.historySteps = workflowEntry.historySteps :+ newHistoryStep
val currentStepOwners = currentStep.owner.split(",").toList
val remainOweners:List[String] = currentStepOwners.filter( owner => (owner != "" && owner != env.getCurrentUser().username))
val nextSteps = if ( !action.completeThisStep.getOrElse(false) && !remainOweners.isEmpty ) {
// 当前还有办理人, 保持到当前步骤
List(currentStep.copy(owner = remainOweners.mkString("", "," ,"")))
} else {
for (result <- action.unconditionalResult.toList ::: action.splits.flatten.toList) yield {
CurrentStep(
dueDate = 4.hours.later,
finishDate = new Date(),
startDate = new Date(),
caller = "",
owner = tryo(result.owner.map(env.evalString(_)).getOrElse("")) openOr ("#下一步拥有者计算错误#"),
status = "OK",
previousStepIds = currentStep.stepId.toString(),
actionId = 0,
stepId = result.toStep,
entryId = ""
)
}
}
workflowEntry.currentSteps = workflowEntry.currentSteps.filterNot(_ == currentStep) ::: nextSteps
val newOwners: List[String] = nextSteps.flatMap(_.owner.split(",")) filterNot (currentStepOwners contains)
import net.liftweb.common._
val newOwnerEmails = newOwners.flatMap { o =>
User.findUserByUserName(o) match {
case Full(user) => Some(user.email.is)
case _ => None
}
}
import net.liftweb.util.Mailer
import Mailer._
newOwnerEmails.foreach{
toEmail =>
Mailer.sendMail(From("oa-zenner@zenner-meters-com"), Subject("通知:OA新待办事项"), To(toEmail),
xmlToMailBodyType(<a href={"http://oa-zenner/form/" + env.getDocument().databaseId + "::" + env.getDocument().documentID}>打开</a>))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment