⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wsmain.~pas

📁 Workflow Studio是一款专为商业进程管理(BPM)设计的Delphi VCL框架。通过Workflow Studio你可以轻易地将工作流与BPM功能添加到你的应用程序里。这样能使你或你的最
💻 ~PAS
📖 第 1 页 / 共 2 页
字号:
  LoadWorkflowInstance(WI);
  WorkflowStudio.WorkflowEngine.RunWorkflow(WI);
end;

{ TTaskManager }

constructor TTaskManager.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
end;

procedure TTaskManager.CreateTaskInstance(WorkIns: TWorkflowInstance; TaskDef: TTaskDefinition);
var
  TI: TTaskInstance;
  AAssignUsers: TObjectList;
  AAssignedUserId: string;
  c: integer;
  EMailInfo: TEmailInformation;
  AUser: TWorkflowUser;
  AGroup: TWorkflowGroup;
  AAssignment: string;
begin
  AAssignUsers := TObjectList.Create(false);
  try
    {Prepare script engine context}
    WorkflowStudio.ScriptEngine.SetRuntimeContext(WorkIns);

    {Get the list of user keys which the task instance will be created for}
    AAssignment := WorkflowStudio.ScriptEngine.TranslateText(TaskDef.AssignmentRule);
    WorkflowStudio.UserManager.GetAssignedUserList(AAssignment, AAssignUsers);

    {Ensure that a task instance will be created, even if it's for a non-existant user}
    if AAssignUsers.Count = 0 then
      raise EWorkflowException.Create(Format(_str(STaskNotCreatedUserNotFound),
        [AAssignment]));

    {Iterate through all users and create a task instance for each one}
    for c := 0 to AAssignUsers.Count - 1 do
    begin
      AGroup := nil;
      AUser := nil;
      TI := TTaskInstance.Create(nil);
      try

        {Retrieve the object, if it's a group or user, set in the proper variable, and set the
         AAssignedUserKey variable which will be id to be saved in the database}
        if AAssignUsers[c] is TWorkflowUser then
        begin
          AUser := TWorkflowUser(AAssignUsers[c]);
          AAssignedUserId := AUser.UserId;
        end else
        if AAssignUsers[c] is TWorkflowGroup then
        begin
          AGroup := TWorkflowGroup(AAssignUsers[c]);
          AAssignedUserId := AGroup.GroupId;
        end else
          raise EWorkflowException.Create('Internal error in CreateTaskInstance: assignment object is not an user or a group'); 

        {Copy the source Task Definition to the Task definition object inside the Task Instance}
        TI.TaskDef.Assign(TaskDef);

        {evalute expressions in the text properties of task definition}
        TI.TaskDef.Subject := WorkflowStudio.ScriptEngine.TranslateText(TI.TaskDef.Subject);
        TI.TaskDef.Description := WorkflowStudio.ScriptEngine.TranslateText(TI.TaskDef.Description);

        TI.Status := TaskDef.InitialStatus;
        //UserID := '';
        TI.Comments := TI.TaskDef.Description;
        if WorkIns <> nil then
          TI.WorkInsKey := WorkIns.Key;
        TI.DefinitionKey := WorkIns.DefinitionKey;
        TI.UserID := AAssignedUserId;

        {get the new key}
        WorkflowStudio.WorkflowDB.TaskInstanceInsert(TI);;
        TaskDef.TaskInstanceKeys.Add(TI.Key);

        {TASK SAVED!
         Now send e-mail notifications. If it's an group, we must send e-mail to all users in the group.
         If it's an user, just send to the user}
        if TaskDef.MailNotification then
        begin
          //EmailInfo.ToAddr := AUser.Email;
          EMailInfo.From := WorkflowStudio.FromEmail;
          EMailInfo.Subject := TI.TaskDef.Subject;
          EMailInfo.Text := TI.Comments;
          if AGroup <> nil then
            WorkflowStudio.SendGroupMail(TI, AGroup, EMailInfo)
          else
            WorkflowStudio.SendMail(TI, AUser, EMailInfo);
        end;
      finally
        TI.Free;
      end;
    end;
  finally
    AAssignUsers.Free;
  end;
end;

function TTaskManager.IsTaskFinished(AKey: string): boolean;
var
  TI: TTaskInstance;
begin
  TI := TTaskInstance.Create(nil);
  try
    TI.Key := AKey;
    WorkflowStudio.WorkflowDB.TaskInstanceLoad(TI);
    result := TI.Completed;
  finally
    TI.Free;
  end;
end;

procedure TTaskManager.LoadTaskInstance(TaskIns: TTaskInstance);
begin
  WorkflowStudio.WorkflowDB.TaskInstanceLoad(TaskIns);
end;

procedure TTaskManager.LoadTaskInstanceList(ATasks: TTaskInstanceList;
  AFilterType: TTaskFilterType; AFilterKey: string; OnlyCompleted: boolean);
begin
  WorkflowStudio.WorkflowDB.TaskInstanceLoadList(ATasks, AFilterType, AFilterKey, OnlyCompleted);
end;

procedure TTaskManager.SaveTaskInstance(TaskIns: TTaskInstance);
begin
  if TaskIns.Key = '' then
    WorkflowStudio.WorkflowDB.TaskInstanceInsert(TaskIns)
  else
    WorkflowStudio.WorkflowDB.TaskInstanceUpdate(TaskIns);
  WorkflowStudio.WorkflowManager.SignalWorkflowInstance(TaskIns.WorkInsKey);
end;

{ TWorkflowStudio }

constructor TWorkflowStudio.Create;
{$IFDEF TRIAL}
var
  msg: string;
  i: integer;
{$ENDIF}
begin
  inherited Create(AOwner);
  FGroupAssignmentMode := gamMultipleTasks; //default mode
  WorkflowStudio := Self;
  FTaskManager := TTaskManager.Create(nil);
  FWorkflowManager := TWorkflowManager.Create(nil);
  FWorkflowEngine := TWorkflowEngine.Create;
  FUserManager := TWorkflowUserManager.Create(nil);
  {$IFDEF TRIAL}
  if (Now > (39629 + 240)) or (Now < 39629) then
  begin
    msg := 'Thhtirsd fWtourikofkljohwb gSvtfucddirof tvgeyrhsuijohng bh5adse 4erxfp3iersewd2.e dPrldesaaszed xrcefgvigsntherrt,e 3osrq a'+
      'tkoi ucyotnrteiwnquaes decvfavlgubahtnijnmgk jdhogwfndlcosaxda zas qnweewr t'+
      'v4err5sti6oyn4 rf3reo5mt 6hgt5tfp4:r/3/ew4wrw4.dt3mss2seo4frt5wgahrbev.dceowm';
    i := 2;
    while i < length(msg) do                 
    begin
      System.Delete(msg, i, 1);
      inc(i);
    end;
    raise Exception.Create(msg);
  end;
  {$ENDIF}
end;

destructor TWorkflowStudio.Destroy;
begin
  FUserManager.Free;
  FWorkflowEngine.Free;
  FWorkflowManager.Free;
  FTaskManager.Free;
  if FInternalScriptEngine <> nil then
  begin
    FInternalScriptEngine.Free;
    FInternalScriptEngine := nil;
  end;
  if FInternalUserInterface <> nil then
  begin
    FInternalUserInterface.Free;
    FInternalUserInterface := nil;
  end;
  inherited;
end;

function TWorkflowStudio.GetScriptEngine: TWorkflowScriptEngine;
begin
  if FScriptEngine <> nil then
    result := FScriptEngine
  else
  begin
    if FInternalScriptEngine = nil then
      FInternalScriptEngine := ScriptEngineClass.Create(nil);
    result := FInternalScriptEngine;
  end;
end;

function TWorkflowStudio.GetUserInterface: TCustomWorkflowUserInterface;
begin
  if FInternalUserInterface = nil then
    FInternalUserInterface := UserInterfaceClass.Create(nil);
  result := FInternalUserInterface;
end;

function TWorkflowStudio.GeTCustomWorkflowDB: TCustomWorkflowDB;
begin
  if not (csDesigning in ComponentState) and (FWorkflowDB = nil) then
    raise EWorkflowException.Create('WorkflowDB property not specified.')
  else
    result := FWorkflowDB;
end;

{$IFDEF USE_INDY}
function TWorkflowStudio.IndySendMail(EMailInfo: TEmailInformation): boolean;
var
  idMessage: TidMessage;
begin
  result := false;
  idMessage := TidMessage.Create(nil);
  try
    With idMessage do
    begin
      Clear;
      From.Text := EmailInfo.From;
      CCList.Add.Text := EMailInfo.Cc;
      BCCList.Add.Text := EMailInfo.Bcc;
      Recipients.Add.Text := EMailInfo.ToAddr;
      Subject := EMailInfo.Subject;
      Body.Text := EMailInfo.Text;
    end;

    if IndySMTP <> nil then with IndySMTP do
    begin
      Connect;
      try
        Send(idMessage);
        result := true;
      finally
        Disconnect;
      end;
    end;
  finally
    idMessage.Free;
  end;
end;
{$ENDIF}

procedure TWorkflowStudio.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited;
  if (Operation = opRemove) then
  begin
    if AComponent = FWorkflowDB then
      FWorkflowDB := nil;
    if AComponent = FScriptEngine then
      FScriptEngine := nil;
    {$IFDEF USE_INDY}
    if AComponent = FIndySMTP then
      FIndySMTP := nil;
    {$ENDIF}
  end;
end;
               
function TWorkflowStudio.SendMail(TaskIns: TTaskInstance;
  AUser: TWorkflowUser; EMailInfo: TEmailInformation): boolean;
begin
  result := false;
  EMailInfo.ToAddr := AUser.Email;
  if Assigned(FOnSendMail) then
    FOnSendMail(Self, TaskIns, AUser, EMailInfo, result);
  if not result then
  begin
    {$IFDEF USE_INDY}
    result := IndySendMail(EMailInfo);
    {$ENDIF}
  end;
end;

{$IFDEF USE_INDY}
procedure TWorkflowStudio.SetIndySMTP(const Value: TidSMTP);
begin
  if FIndySMTP <> Value then
  begin
    FIndySMTP := Value;
    if Value <> nil then
      Value.FreeNotification(Value);
  end;
end;
{$ENDIF}

procedure TWorkflowStudio.SetScriptEngine(
  const Value: TWorkflowScriptEngine);
begin
  if (FScriptEngine <> Value) then
  begin
    FScriptEngine := Value;
    if Value <> nil then
      Value.FreeNotification(Value);
  end;
end;

procedure TWorkflowStudio.SeTCustomWorkflowDB(const Value: TCustomWorkflowDB);
begin
  if (FWorkflowDB <> Value) then
  begin
    FWorkflowDB := Value;
    if Value <> nil then
      Value.FreeNotification(Value);
  end;
end;

procedure TWorkflowStudio.WorkflowInstanceError(WI: TWorkflowInstance;
  ErrMsg: string; var ShowError: boolean);
begin
  if Assigned(FOnWorkInsError) then
    FOnWorkInsError(Self, WI, ErrMsg, ShowError);
end;

procedure TWorkflowStudio.SetGroupAssignmentMode(
  const Value: TGroupAssignmentMode);
begin
  if FGroupAssignmentMode <> Value then
  begin
    FGroupAssignmentMode := Value;
    
  end;
end;

procedure TWorkflowStudio.SendGroupMail(TaskIns: TTaskInstance;
  AGroup: TWorkflowGroup; EMailInfo: TEmailInformation);
var
  c: integer;
  AUser: TWorkflowUser;
begin
  for c := 0 to AGroup.UserIds.Count - 1 do
  begin
    AUser := AGroup.UserManager.Users.FindById(AGroup.UserIds[c]);
    if AUser <> nil then
      SendMail(TaskIns, AUser, EMailInfo);
  end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -