| Profiel van 麦田守望者异想空间Foto'sWeblogLijsten | Help |
|
2-1-2009 Creating Workflows in Plone(译)此文译自Plone文档《Creating Workflow in Plone》,原文链接: What is Workflow?什么是工作流 Workflow is the series of interactions that should happen to complete a task. Business organizations have many kinds of workflow. For example, insurance companies process claims, delivery companies track shipments, and schools accept applications for admission. All these tasks involve several people, sometimes take a long time, and vary significantly from organization to organization. 工作流是一系列交互行为,它们共同作用以完成一项任务。商业机构有许多种工作流。例如,保险公司处理索赔,快递公司跟踪货物,和学校接受入学申请等。所有这类任务都会涉及某些参与者,有的时候会持续很长时间,并且不同的组织或者机构之间会不尽相同。 The goal of workflow software is to streamline and track workflow activity. Since different organizations have different workflow processes, workflow software must be flexible and easy to customize. 工作流软件的目标是简化流程(to streamline)和跟踪工作流活动。由于不同的组织有不同的工作流处理过程,因此工作流软件必须是灵活的和易于自定义的。 The DCWorkflow ConceptDCWorkflow概念 DCWorkflow makes a few simple assumptions about your workflow: DCWorkflow对你的工作流做了一些简单的假设:
系统中只有一个单独的对象表示要完成的任务。
每一个给定类型的对象在同一个工作流里面流动
任务被分配个用户角色,而不是个人 DCWorkflow makes it easy to implement workflows that fit this description. If your workflow does not fit these criteria, you should weigh the alternatives. One alternative is AlphaFlow, a more powerful and complex workflow engine for Plone. DCWorkflow使得实现满足上述假设的工作流变得容易。如果你的工作流不符合这些条件,那么你应该考虑其它的替代产品。AlphaFlow是另外一个选择,为Plone而生的更加强大和复杂的工作流引擎。 Workflows for complex use cases can become very unwieldly to build and maintain via the ZMI-based method this tutorial demonstrates. If you are building non-trivial workflows, we recommend you strongly consider using ArchGenXML and a UML modeling tool that will help you make sure your workflows are complete and correct. See the ArchGenXML tutorial, especially the section on workflows, for a great introduction. 对于复杂用例,这篇指南所关注的通过ZMI创建和维护工作流的方法就会显得力不从心了。如果你正在构建产品级别的工作流,我们强烈建议你考虑使用ArchGenXML和UML建模工具,它们会帮助你完整地、正确地完成你的工作流。请参见ArchGenXML指南,它提供了非常棒的介绍,特别是涉及工作流的部分。 Defining Workflow States定义工作流状态 Using workflow states you can add state to your content that is specific to your business process. 使用工作流状态,能够给你的内容对象添加特定于你的商业流程(business process)的状态信息。 CMF comes with a default workflow with three states: private, pending, and published. In the default configuration, all content in the CMF is set to operate in that workflow. When an object is in the private state, only the user who created it and site managers can view and change it. The user is provided with a link to "submit" the content, which puts it in the pending state. Then a user with the "reviewer" role is given the opportunity to either publish or reject the submission, which moves the content object to either the published or private state. CMF带有一个默认的工作流,它有三种状态:私有(private),待审(pending),和已发布(published)。在默认配置里,CMF中的所有内容对象都被设置为在这个工作流中活动。当一个对象处于私有(private)状态的时候,只有创建它的用户和站点管理员才能查看和修改。系统会为这样的用户提供一个链接用来提交(submit)内容,使其进入待审(pending)状态。接下来,具有审核人(reviewer)角色的用户就有机会发布或者驳回提交,即更改内容对象的状态到已发布或者私有。
图示1:简单的状态机图例 Your business process will most likely require a different set of states. For example, a workflow could naturally model the process of ratifying a bill in a state legislature. You could start by creating a Private state to be used while the author is creating a new bill, a Public state used before voting, a Voting state during which time legislators are allowed to cast their votes, a Final Review state which gives the executive branch time to review it, a Vetoed state, and a Passed state. (See Illustration 1.) 你的商业流程在多数情况下可能需要一组不同的状态。例如,可以用工作流很自然的为州议会的议案批准流程建模。从创建私有(private)状态开始,在创建一个新议案的时候使用,投票前使用公开(Public)状态,在议员投票期间使用正在投票(Voting)状态,最终审核(Final Review)状态给州长审核议案的时间,这会产生否决(Vetoed)和已通过(Passed)两个状态。(参见图例1)
图示2:使用泳道设计工作流 One good way to determine what states you need in your business process is to first draw a diagram with "swim lanes". (See Illustration 2.) Draw a diagram with each relevant user role at the head of a column, then draw dotted lines between the columns. In the legislature example, the roles might be Owner, Public, Senator, and President. Draw a state diagram that shows the flow of your content (in the example, a bill) between the different users. Then create a workflow state for each bubble you draw in your diagram. (See Illustrations 3 and 4.) 决定在你的商业流程中使用何种状态的最好的办法是先还一张带有泳道(swim lanes)的图示。(参见图例2)画一张图示,在每一列的顶部放置相关的用户角色,然后在列之间画上点划线。在议会的例子中,角色可能会有所有者(Owner)、公众(Public)、参议员(Senator)和州长(president)。画一个状态图,它展示你的内容(在例子中是议案)在不同用户之间的流动。接着为每一个画在图示中的气泡创建一个工作流状态。(见图例3和4) A technical note: changing the workflow state of an object does not move it to a different location or add Python attributes to the object. Instead, it asks the workflow tool to set the workflow state of the object and the workflow tool can choose how the state will be stored. The default implementation of the workflow tool stores the workflow state in the workflow_history attribute of CMF content objects. 技术注释:更改一个对象的工作流状态,不会把该对象移动到其他地方或者给它添加Python属性。相反,它会要求工作流工具(workflow tool)设置对象的工作流状态,工作流工具自己选择如何存储状态信息。默认的工作流工具的实现使用CMF内容对象的workflow_history属性存储工作流状态。
图示3:单击portal_workflow工具中的“Add Workflow”按钮添加工作流
图示4:添加状态 Defining Workflow Transitions定义工作流变换(Workflow Transition) Transitions are the arrows in a state diagram. Generally, for every arrow you draw in your state diagram, create a transition. Some state diagrams, however, have a lot of arrows pointing to a single state. In that case it might be better to create just one transition that you can reuse in modeling most of the transitions that lead to that state. 状态图示中的箭头即是变换。一般的,每当你在状态图示中画一个箭头,就创建了一个变换。然而,某些状态图示有许多箭头指向一个单独的状态。在这种情况下,更好的办法也许是只创建一个变换,你能够在建模大多数通向那个状态的变换中重用它。 Each transition requires a destination state. Select the destination state from the drop-down box. (See Illustration 5 below.) 每一个变换需要一个目标状态。从下拉列表框中选择目标状态。(参见图示5)
图示5:配置变换 Transitions are usually protected by a guard condition. If you drew swimming lanes as suggested in Illustration 2, notice that many of the arrows cross the swimming lanes. Every time you cross a swimming lane you need a guard condition. A guard condition can be a permission, a role, or a workflow expression (described later). Guard conditions ensure that only users with the required permission, role, or other criteria can move the object to the new state. 变换通常受到保证条件(guard condition)的保护。如果你像图示2建议的那样画了泳道,那么你会注意到许多箭头穿越了泳道。每次穿越泳道,都需要一个保证条件。保证条件可以是许可,角色,或者工作流表达式(稍后介绍)。保证条件确保拥有所需权限、角色或者其他条件的用户才能把对象移动到新的状态。 Most transitions are initiated by a user action. For each transition initiated by a user action, enter the information for the corresponding link that should be displayed to the user in the actions box. The link will only be displayed when the user would be allowed to perform the transition. 大多数变换是由用户操作引起了。针对每一个由用户引发的变换,为相应的链接输入信息,它应该在操作框(action box)中显示给用户。仅当允许用户执行变换时,才会显示链接。 Sometimes you need special states and transitions in your workflow diagram that model actions performed in the background, not by any user. In that case you might need to set up transitions that are initiated automatically. Zope will proceed through automatic transitions whenever the guard condition allows it. 有的时候,你会用一张工作流图示建模那些在后台执行,并且不需要用户干预的操作,同时在图中需要有一些专门的状态和变换。这样你可能需要设置被自动引发的变换。无论保证条件是否允许,Zope都会执行所有自动变换。 Once you have defined all the transitions, go back to your workflow states and define which transitions are allowed to leave those states. 一旦定义了所有的变换,回到你的工作流状态,接着定义允许哪些变换离开这些状态。 Defining Variables定义变量 Often a simple flow of states can't model all the details of a business process. For example, in the bill-passing example, a bill might be allowed to be revised and resubmitted once it is vetoed, but only if it has been vetoed once. If it is vetoed a second time, it is killed for good. To model this behavior, the state machine needs to carry a bit of extra information that "remembers" the past veto. 简单的状态流不能建模所有的商业流程细节。例如,在上述的审核议案的例子中,可能允许审阅一个议案,同时当它被否决之后允许被再次提交,但是仅限一次。如果议案被否决两次,最好将它彻底废除。为这种行为建模,状态机需要留意额外的、记录了过去否决的信息 A variable is a piece of information that transcends states. Most variables are persistent. A variable might hold a counter, a flag, the name of the last user who did some action, or any other simple object. 相对状态而言,变量能够承载更多的信息。大多数变量是可持久的。一个变量可以容纳计数器,标志,上一个执行了某个操作的用户名称,或者任何其他的简单对象。
图示6:默认的Plone工作流中的标准工作流变量 Variables also serve the purpose of exposing metadata to the catalog or the user. There are five variables in the CMF default workflow: actor, action, comments, time, and review_history. The first four are there to keep a record of who executed the last transition, what they did, why, and when. The last variable makes it possible for the user to view the workflow history of an object. (See Illustration 6.) 变量还达到了向目录(catalog)和用户暴露元数据(metadata)的目的。CMF默认的工作流有五个变量:actor,action,comments,time和review_history。前面的四个记录了谁执行了最后的那个变换,他们做了什么,为什么,以及什么时候做的。最后一个变量使用户能够查看对象的工作流历史(workflow history)。(参见图示6) Defining Worklists定义工作列表(Worklists) Some users need notification of work that needs to be completed. For example, in the CMF default workflow, users with the Reviewer role need to be told when there are items pending review, so they can visit them and either publish or reject them. 某些用户需要在工作完成之后得到通知。例如,在CMF的默认工作流中,当有待审核项目的时候,得通知拥有审核人角色的用户,他们能够访问这些项目,并发布或者驳回它们。 One way to accomplish this is with worklists. Worklists add links to users' actions box when there are items in a certain state. The CMF default workflow supplies one worklist. It shows the Pending Review link to reviewers when there are items they are allowed to review. 达到上述目的的一个方法就是使用工作列表(Worklist)。当具有特定状态的项目出现的时候,工作列表即在用户的操作框(user's actions box)中添加链接。CMF的默认工作流提供了一个工作列表。当有允许审核人审核的项目出现的时候,就会给他们显示待审核(Pending Review)链接。 After creating a worklist, enter the name of the state it matches. Use a guard condition to make only certain users see it. Also enter the name and URL of the link to be displayed. 在创建工作列表之后,输入它匹配的状态的名称。使用保证条件(guard condition)只让某些用户能够看到。同时还要输入显示那些项目的URL链接的名称。 Defining Scripts定义脚本 You may need to perform actions like sending an email or invoking another workflow when users execute specific transitions. You can do this by writing scripts. Scripts can be any Zope object, but Scripts (Python) are most likely the best choice. 当用户执行特定的变换的时候,你可能需要执行一些操作,比如发送一封电子邮件,或者触发另外一个工作流。你可以撰写脚本完成这些任务。脚本可以使任何Zope对象,但是Python脚本对象通常是最好的选择。 Scripts are passed one parameter, the state_change object of a workflow expression. Remember that your script is executed with the permissions of the user who invokes the transition, rather than your own permissions, unless you give the script proxy roles. 工作流表达式(Workflow expression)的state_change对象被当作一个参数传入脚本。记住,脚本是在触发变换的用户的权限下执行的,而不是你自己的权限,除非给脚本设置代理角色(proxy roles)。 Once a script is in the Scripts container of a workflow you can visit a transition and select the script to be executed. The script will be executed before the state change and before variables are updated. It can raise exceptions to veto the action or an ObjectMoved or ObjectDeleted exception to tell the workflow that the object has moved or has been deleted. 一旦脚本处于工作流的脚本容器中,你就可以访问变换,并且选择想要执行的脚本。脚本在改变状态之前和更新变量之前被执行。它可以抛出各种异常以拒绝操作,或者抛出ObjectMoved或ObjectDeleted异常告诉工作流,对象已经被移动或者被删除。 Defining Permissions定义权限 In a business process, the workflow state of an object usually affects who is allowed to perform non-workflow actions on the object. For example, in the CMF default workflow, the owner of a piece of content is allowed to edit it when it is in the private state, but not when it is in the published state. Anonymous users are allowed to see a piece of content only if it is in the published state. 在商业流程中,对象的工作流状态经常影响着谁被允许在对象上执行非工作流操作。例如,在CMF的默认工作流,当内容处于私有状态的时候,允许内容的所有者编辑该对象,但是当内容处于已发布状态的时候,则是不允许的。只有当内容处于已发布状态时,才允许匿名用户看到。 CMF accomplishes this by updating the role to permission mappings for objects based on their workflow state. To do this in your own workflow, first determine which permissions should be managed by your workflow by selecting them using the workflow Permissions tab. CMF的实现方法是:基于对象的工作流状态,为对象更新角色到权限的映射关系。在你的工作流中使用这个方法,首先要确定工作流应该管理哪些权限,使用工作流Permissions页选择它们。
图示7:修改状态的权限影射关系 Then visit the Permissions tab of each state and select which roles should have which permissions. This screen is very similar to the familiar Security tab. (See Illustration 7.) Remember to turn off the Acquire Permission checkbox as necessary. 然后,访问每一个状态的Permissions页,选择哪些角色拥有哪些权限。这个屏幕中的内容与熟悉的Security页非常相似。(参见图示7)记住,必要时关闭获取(acquire Permission)权限。 The role to permission mappings are stored on the objects themselves because that is where Zope has always stored them. Unfortunately this means that when you change the role to permission mappings in the workflow you need to make sure the changes are applied to the content objects throughout the system. But there is an easy workaround: the default portal_workflow tool has a button you can click to update the role to permission mappings in all content objects. 角色到权限的映射关系存储在对象中,因为那正是Zope总是存储它们的地方。不幸的是,这意味着当你在工作流中修改了角色到权限的映射关系后,需要确保在整个系统中,其修改应用到了所有内容对象。有一个简单的方法:默认的portal_workflow工具有一个按钮,点击这个按钮能够更新所有内容对象的角色到权限的映射关系。 Workflow Expressions工作流表达式 A workflow expression is a TALES expression. TALES expressions are fully described at:
工作流表达式即TALES表达式。下面的链接完整的描述了TALES表达式。 Some of the contexts have slightly different meanings from what is provided for expressions in page templates. 从为表达式提供的内容看,在页面模板中,一些上下文对象的含义略微有所不同。
here:内容对象
container:包含内容对象的容器 Several other contexts are also provided. 其它几个上下文对象:
state_change:一个专门的对象,包含了有关状态改变的信息
transition:正在执行的变换对象
status:前一个状态
workflow:工作流定义对象
scripts:工作流定义对象(workflow definition object)的脚本 state_change objects provide the following attributes: state_change对象提供了如下属性:
status:包含了工作流状态的映射。
object:正在被工作流修改的对象
workflow:工作流定义对象
transition:正在执行的变换对象
old_state:前一个状态对象
new_state:目标状态对象
kwargs:传递到doActionFor()方法的关键字参数
getHistory():一个方法,返回对象的工作流历史(workflow history)的拷贝
getPortal():返回portal的根
ObjectDeleted和ObjectMoved:可以被脚本抛出的异常,给工作流指出某个对象已经被移动或者被删除
getDateTime():一个方法,返回变换的DateTime属性 |
|
|