Source Taste: Activiti: StartProcessInstanceClickListener

Full Name: org.activiti.explorer.ui.process.listener.StartProcessInstanceClickListener

public class StartProcessInstanceClickListener implements ClickListener {

  private static final long serialVersionUID = 1L;
  
  protected RuntimeService runtimeService;
  protected TaskService taskService;
  protected FormService formService;
  protected NotificationManager notificationManager;
  
  protected ProcessDefinition processDefinition;
  protected ProcessDefinitionPage parentPage;
  
  
  public StartProcessInstanceClickListener(ProcessDefinition processDefinition, ProcessDefinitionPage processDefinitionPage) {
    this.runtimeService = ProcessEngines.getDefaultProcessEngine().getRuntimeService();
    this.taskService = ProcessEngines.getDefaultProcessEngine().getTaskService();
    this.formService = ProcessEngines.getDefaultProcessEngine().getFormService();
    this.notificationManager = ExplorerApp.get().getNotificationManager(); 
    
    this.processDefinition = processDefinition;
    this.parentPage = processDefinitionPage;
  }

  public void buttonClick(ClickEvent event) {
    // Check if process-definition defines a start-form
    
    StartFormData startFormData = formService.getStartFormData(processDefinition.getId());
    if(startFormData != null && ((startFormData.getFormProperties() != null && startFormData.getFormProperties().size() > 0) || startFormData.getFormKey() != null)) {
      parentPage.showStartForm(processDefinition, startFormData);
    } else {
      // Just start the process-instance since it has no form.
      // TODO: Error handling
      ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
      
      // Show notification of success
      notificationManager.showInformationNotification(Messages.PROCESS_STARTED_NOTIFICATION, getProcessDisplayName(processDefinition));
      
      // Switch to inbox page in case a task of this process was created
      List<Task> loggedInUsersTasks = taskService.createTaskQuery()
        .taskAssignee(ExplorerApp.get().getLoggedInUser().getId())
        .processInstanceId(processInstance.getId())
        .list();
      if (loggedInUsersTasks.size() > 0) {
        ExplorerApp.get().getViewManager().showInboxPage(loggedInUsersTasks.get(0).getId());
      }
    }
  }
  
  protected String getProcessDisplayName(ProcessDefinition processDefinition) {
    if(processDefinition.getName() != null) {
      return processDefinition.getName();
    } else {
      return processDefinition.getKey();
    }
  }

}

Part of org.activiti.explorer.ui.process.ProcessDefinitionPage:

public void showStartForm(ProcessDefinition processDefinition, StartFormData startFormData) {
    if(detailPanel != null) {
      showProcessDefinitionDetail(processDefinition.getId());
    }
    detailPanel.showProcessStartForm(startFormData);
  }

org.activiti.explorer.ui.process.ProcessDefinitionDetailPanel:

public class ProcessDefinitionDetailPanel extends AbstractProcessDefinitionDetailPanel {
  
  private static final long serialVersionUID = 1L;
  
  protected Button startProcessInstanceButton;
  protected Button editProcessDefinitionButton;
  protected FormPropertiesForm processDefinitionStartForm;
  
  public ProcessDefinitionDetailPanel(String processDefinitionId, ProcessDefinitionPage processDefinitionPage) {
    super(processDefinitionId, processDefinitionPage);
  }
  
  protected void initActions(AbstractPage parentPage) {
    ProcessDefinitionPage processDefinitionPage = (ProcessDefinitionPage) parentPage;

    startProcessInstanceButton = new Button(i18nManager.getMessage(Messages.PROCESS_START));
    startProcessInstanceButton.addListener(new StartProcessInstanceClickListener(processDefinition, processDefinitionPage));
    
    editProcessDefinitionButton = new Button(i18nManager.getMessage(Messages.PROCESS_CONVERT));
    editProcessDefinitionButton.addListener(new ConvertProcessDefinitionToModelClickListener(processDefinition));
    
    if(processDefinition.getDiagramResourceName() == null) {
      editProcessDefinitionButton.setEnabled(false);
    }
    
    // Clear toolbar and add 'start' button
    processDefinitionPage.getToolBar().removeAllButtons();
    processDefinitionPage.getToolBar().addButton(startProcessInstanceButton);
    processDefinitionPage.getToolBar().addButton(editProcessDefinitionButton);
  }
  
  public void showProcessStartForm(StartFormData startFormData) {
    if(processDefinitionStartForm == null) {
      processDefinitionStartForm = new FormPropertiesForm();
      processDefinitionStartForm.setSubmitButtonCaption("Start process");
      processDefinitionStartForm.setCancelButtonCaption("Cancel");
      
      // When form is submitted/cancelled, show the info again
      processDefinitionStartForm.addListener(new FormPropertiesEventListener() {
        private static final long serialVersionUID = 1L;
        protected void handleFormSubmit(FormPropertiesEvent event) {
          formService.submitStartFormData(processDefinition.getId(), event.getFormProperties());
          
          // Show notification
          ExplorerApp.get().getMainWindow().showNotification("Process '" + 
                  getProcessDisplayName(processDefinition) + "' started successfully");
          initProcessDefinitionInfo();
        }
        protected void handleFormCancel(FormPropertiesEvent event) {
          initProcessDefinitionInfo();
        }
      });
    }
    processDefinitionStartForm.setFormProperties(startFormData.getFormProperties());
    
    startProcessInstanceButton.setEnabled(false);
    detailContainer.removeAllComponents();
    detailContainer.addComponent(processDefinitionStartForm);
  }
  
  @Override
  public void initProcessDefinitionInfo() {
    super.initProcessDefinitionInfo();
    
    if (startProcessInstanceButton != null) {
      startProcessInstanceButton.setEnabled(true);
    }
  }
  
}


你可能感兴趣的:(Source Taste: Activiti: StartProcessInstanceClickListener)