/* * Creates a task pool for a given step */ @Override public void createPoolTasks( Context context, XmlWorkflowItem wi, RoleMembers assignees, Step step, WorkflowActionConfig action) throws SQLException, AuthorizeException { // create a tasklist entry for each eperson for (EPerson anEpa : assignees.getEPersons()) { PoolTask task = poolTaskService.create(context); task.setStepID(step.getId()); task.setWorkflowID(step.getWorkflow().getID()); task.setEperson(anEpa); task.setActionID(action.getId()); task.setWorkflowItem(wi); poolTaskService.update(context, task); // Make sure this user has a task grantUserAllItemPolicies(context, wi.getItem(), anEpa); } for (Group group : assignees.getGroups()) { PoolTask task = poolTaskService.create(context); task.setStepID(step.getId()); task.setWorkflowID(step.getWorkflow().getID()); task.setGroup(group); task.setActionID(action.getId()); task.setWorkflowItem(wi); poolTaskService.update(context, task); // Make sure this user has a task grantGroupAllItemPolicies(context, wi.getItem(), group); } }
/* * Claims an action for a given eperson */ @Override public void createOwnedTask( Context context, XmlWorkflowItem wi, Step step, WorkflowActionConfig action, EPerson e) throws SQLException, AuthorizeException { ClaimedTask task = claimedTaskService.create(context); task.setWorkflowItem(wi); task.setStepID(step.getId()); task.setActionID(action.getId()); task.setOwner(e); task.setWorkflowID(step.getWorkflow().getID()); claimedTaskService.update(context, task); // Make sure this user has a task grantUserAllItemPolicies(context, wi.getItem(), e); }
@Override public void addClaimedUser(Context context, XmlWorkflowItem wfi, Step step, EPerson user) throws SQLException, AuthorizeException, IOException { // Make sure we delete the pooled task for our current user if the task is not a group pooltask PoolTask task = poolTaskService.findByWorkflowIdAndEPerson(context, wfi, user); if (task != null && task.getEperson() != null) { xmlWorkflowService.deletePooledTask(context, wfi, task); } InProgressUser ipu = inProgressUserService.create(context); ipu.setWorkflowItem(wfi); ipu.setUser(user); ipu.setFinished(false); inProgressUserService.update(context, ipu); int totalUsers = inProgressUserService.getNumberOfInProgressUsers(context, wfi) + inProgressUserService.getNumberOfFinishedUsers(context, wfi); if (totalUsers == step.getRequiredUsers()) { // If enough users have claimed/finished this step then remove the tasks xmlWorkflowService.deleteAllPooledTasks(context, wfi); } xmlWorkflowItemService.update(context, wfi); }
protected void activateFirstStep( Context context, Workflow wf, Step firstStep, XmlWorkflowItem wfi) throws AuthorizeException, IOException, SQLException, WorkflowException, WorkflowConfigurationException { WorkflowActionConfig firstActionConfig = firstStep.getUserSelectionMethod(); firstActionConfig.getProcessingAction().activate(context, wfi); log.info( LogManager.getHeader( context, "start_workflow", firstActionConfig.getProcessingAction() + " workflow_item_id=" + wfi.getID() + "item_id=" + wfi.getItem().getID() + "collection_id=" + wfi.getCollection().getID())); // record the start of the workflow w/provenance message recordStart(context, wfi.getItem(), firstActionConfig.getProcessingAction()); // Fire an event ! logWorkflowEvent( context, firstStep.getWorkflow().getID(), null, null, wfi, null, firstStep, firstActionConfig); // If we don't have a UI activate it if (!firstActionConfig.requiresUI()) { ActionResult outcome = firstActionConfig.getProcessingAction().execute(context, wfi, firstStep, null); processOutcome(context, null, wf, firstStep, firstActionConfig, outcome, wfi, true); } }
@Override public XmlWorkflowItem start(Context context, WorkspaceItem wsi) throws SQLException, AuthorizeException, IOException, WorkflowException { try { Item myitem = wsi.getItem(); Collection collection = wsi.getCollection(); Workflow wf = xmlWorkflowFactory.getWorkflow(collection); XmlWorkflowItem wfi = xmlWorkflowItemService.create(context, myitem, collection); wfi.setMultipleFiles(wsi.hasMultipleFiles()); wfi.setMultipleTitles(wsi.hasMultipleTitles()); wfi.setPublishedBefore(wsi.isPublishedBefore()); xmlWorkflowItemService.update(context, wfi); removeUserItemPolicies(context, myitem, myitem.getSubmitter()); grantSubmitterReadPolicies(context, myitem); context.turnOffAuthorisationSystem(); Step firstStep = wf.getFirstStep(); if (firstStep.isValidStep(context, wfi)) { activateFirstStep(context, wf, firstStep, wfi); } else { // Get our next step, if none is found, archive our item firstStep = wf.getNextStep(context, wfi, firstStep, ActionResult.OUTCOME_COMPLETE); if (firstStep == null) { archive(context, wfi); } else { activateFirstStep(context, wf, firstStep, wfi); } } // remove the WorkspaceItem workspaceItemService.deleteWrapper(context, wsi); context.restoreAuthSystemState(); return wfi; } catch (WorkflowConfigurationException e) { throw new WorkflowException(e); } }
@Override public void removeClaimedUser(Context context, XmlWorkflowItem wfi, EPerson user, String stepID) throws SQLException, IOException, WorkflowConfigurationException, AuthorizeException { // Check if we had reached our max number @ this moment int totalUsers = inProgressUserService.getNumberOfInProgressUsers(context, wfi) + inProgressUserService.getNumberOfFinishedUsers(context, wfi); // Then remove the current user from the inProgressUsers inProgressUserService.delete( context, inProgressUserService.findByWorkflowItemAndEPerson(context, wfi, user)); Workflow workflow = workflowFactory.getWorkflow(wfi.getCollection()); Step step = workflow.getStep(stepID); // WorkflowManager.deleteOwnedTask(c, user, wfi, step, step.getActionConfig()); // We had reached our total user, so recreate tasks for the user who don't have one if (totalUsers == step.getRequiredUsers()) { // Create a list of the users we are to ignore List<InProgressUser> toIgnore = inProgressUserService.findByWorkflowItem(context, wfi); // Remove the users to ignore RoleMembers roleMembers = step.getRole().getMembers(context, wfi); // Create a list out all the users we are to pool a task for for (InProgressUser ipu : toIgnore) { roleMembers.removeEperson(ipu.getUser()); } step.getUserSelectionMethod() .getProcessingAction() .regenerateTasks(context, wfi, roleMembers); } else { // If the user previously had a personal PoolTask, this must be regenerated. Therefore we call // the regeneration method // with only one EPerson RoleMembers role = step.getRole().getMembers(context, wfi); List<EPerson> epersons = role.getEPersons(); for (EPerson eperson : epersons) { if (eperson.getID().equals(user.getID())) { RoleMembers memberToRegenerateTasksFor = new RoleMembers(); memberToRegenerateTasksFor.addEPerson(user); step.getUserSelectionMethod() .getProcessingAction() .regenerateTasks(context, wfi, memberToRegenerateTasksFor); break; } } } // Update our item itemService.update(context, wfi.getItem()); }
protected WorkflowActionConfig processNextStep( Context c, EPerson user, Workflow workflow, ActionResult currentOutcome, XmlWorkflowItem wfi, Step nextStep) throws SQLException, IOException, AuthorizeException, WorkflowException, WorkflowConfigurationException { WorkflowActionConfig nextActionConfig; if (nextStep != null) { nextActionConfig = nextStep.getUserSelectionMethod(); nextActionConfig.getProcessingAction().activate(c, wfi); // nextActionConfig.getProcessingAction().generateTasks(); if (nextActionConfig.requiresUI()) { // Since a new step has been started, stop executing actions once one with a user interface // is present. c.restoreAuthSystemState(); return nextActionConfig; } else { ActionResult newOutcome = nextActionConfig.getProcessingAction().execute(c, wfi, nextStep, null); c.restoreAuthSystemState(); return processOutcome(c, user, workflow, nextStep, nextActionConfig, newOutcome, wfi, true); } } else { if (currentOutcome.getResult() != ActionResult.OUTCOME_COMPLETE) { c.restoreAuthSystemState(); throw new WorkflowException( "No alternate step was found for outcome: " + currentOutcome.getResult()); } archive(c, wfi); c.restoreAuthSystemState(); return null; } }
protected void logWorkflowEvent( Context c, String workflowId, String previousStepId, String previousActionConfigId, XmlWorkflowItem wfi, EPerson actor, Step newStep, WorkflowActionConfig newActionConfig) throws SQLException { try { // Fire an event so we can log our action ! Item item = wfi.getItem(); Collection myCollection = wfi.getCollection(); String workflowStepString = null; List<EPerson> currentEpersonOwners = new ArrayList<EPerson>(); List<Group> currentGroupOwners = new ArrayList<Group>(); // These are only null if our item is sent back to the submission if (newStep != null && newActionConfig != null) { workflowStepString = workflowId + "." + newStep.getId() + "." + newActionConfig.getId(); // Retrieve the current owners of the task List<ClaimedTask> claimedTasks = claimedTaskService.find(c, wfi, newStep.getId()); List<PoolTask> pooledTasks = poolTaskService.find(c, wfi); for (PoolTask poolTask : pooledTasks) { if (poolTask.getEperson() != null) { currentEpersonOwners.add(poolTask.getEperson()); } else { currentGroupOwners.add(poolTask.getGroup()); } } for (ClaimedTask claimedTask : claimedTasks) { currentEpersonOwners.add(claimedTask.getOwner()); } } String previousWorkflowStepString = null; if (previousStepId != null && previousActionConfigId != null) { previousWorkflowStepString = workflowId + "." + previousStepId + "." + previousActionConfigId; } // Fire our usage event ! UsageWorkflowEvent usageWorkflowEvent = new UsageWorkflowEvent( c, item, wfi, workflowStepString, previousWorkflowStepString, myCollection, actor); usageWorkflowEvent.setEpersonOwners( currentEpersonOwners.toArray(new EPerson[currentEpersonOwners.size()])); usageWorkflowEvent.setGroupOwners( currentGroupOwners.toArray(new Group[currentGroupOwners.size()])); DSpaceServicesFactory.getInstance().getEventService().fireEvent(usageWorkflowEvent); } catch (Exception e) { // Catch all errors we do not want our workflow to crash because the logging threw an // exception log.error( LogManager.getHeader( c, "Error while logging workflow event", "Workflow Item: " + wfi.getID()), e); } }
@Override public WorkflowActionConfig processOutcome( Context c, EPerson user, Workflow workflow, Step currentStep, WorkflowActionConfig currentActionConfig, ActionResult currentOutcome, XmlWorkflowItem wfi, boolean enteredNewStep) throws IOException, AuthorizeException, SQLException, WorkflowException { if (currentOutcome.getType() == ActionResult.TYPE.TYPE_PAGE || currentOutcome.getType() == ActionResult.TYPE.TYPE_ERROR) { // Our outcome is a page or an error, so return our current action c.restoreAuthSystemState(); return currentActionConfig; } else if (currentOutcome.getType() == ActionResult.TYPE.TYPE_CANCEL || currentOutcome.getType() == ActionResult.TYPE.TYPE_SUBMISSION_PAGE) { // We either pressed the cancel button or got an order to return to the submission page, so // don't return an action // By not returning an action we ensure ourselfs that we go back to the submission page c.restoreAuthSystemState(); return null; } else if (currentOutcome.getType() == ActionResult.TYPE.TYPE_OUTCOME) { Step nextStep = null; WorkflowActionConfig nextActionConfig = null; try { // We have completed our action search & retrieve the next action if (currentOutcome.getResult() == ActionResult.OUTCOME_COMPLETE) { nextActionConfig = currentStep.getNextAction(currentActionConfig); } if (nextActionConfig != null) { // We remain in the current step since an action is found nextStep = currentStep; nextActionConfig.getProcessingAction().activate(c, wfi); if (nextActionConfig.requiresUI() && !enteredNewStep) { createOwnedTask(c, wfi, currentStep, nextActionConfig, user); return nextActionConfig; } else if (nextActionConfig.requiresUI() && enteredNewStep) { // We have entered a new step and have encountered a UI, return null since the current // user doesn't have anything to do with this c.restoreAuthSystemState(); return null; } else { ActionResult newOutcome = nextActionConfig.getProcessingAction().execute(c, wfi, currentStep, null); return processOutcome( c, user, workflow, currentStep, nextActionConfig, newOutcome, wfi, enteredNewStep); } } else if (enteredNewStep) { // If the user finished his/her step, we keep processing until there is a UI step action // or no step at all nextStep = workflow.getNextStep(c, wfi, currentStep, currentOutcome.getResult()); c.turnOffAuthorisationSystem(); nextActionConfig = processNextStep(c, user, workflow, currentOutcome, wfi, nextStep); // If we require a user interface return null so that the user is redirected to the // "submissions page" if (nextActionConfig == null || nextActionConfig.requiresUI()) { return null; } else { return nextActionConfig; } } else { ClaimedTask task = claimedTaskService.findByWorkflowIdAndEPerson(c, wfi, user); // Check if we have a task for this action (might not be the case with automatic steps) // First add it to our list of finished users, since no more actions remain workflowRequirementsService.addFinishedUser(c, wfi, user); c.turnOffAuthorisationSystem(); // Check if our requirements have been met if ((currentStep.isFinished(c, wfi) && currentOutcome.getResult() == ActionResult.OUTCOME_COMPLETE) || currentOutcome.getResult() != ActionResult.OUTCOME_COMPLETE) { // Delete all the table rows containing the users who performed this task workflowRequirementsService.clearInProgressUsers(c, wfi); // Remove all the tasks deleteAllTasks(c, wfi); nextStep = workflow.getNextStep(c, wfi, currentStep, currentOutcome.getResult()); nextActionConfig = processNextStep(c, user, workflow, currentOutcome, wfi, nextStep); // If we require a user interface return null so that the user is redirected to the // "submissions page" if (nextActionConfig == null || nextActionConfig.requiresUI()) { return null; } else { return nextActionConfig; } } else { // We are done with our actions so go to the submissions page but remove action // ClaimedAction first deleteClaimedTask(c, wfi, task); c.restoreAuthSystemState(); nextStep = currentStep; nextActionConfig = currentActionConfig; return null; } } } catch (Exception e) { log.error("error while processing workflow outcome", e); e.printStackTrace(); } finally { if ((nextStep != null && currentStep != null && nextActionConfig != null) || (wfi.getItem().isArchived() && currentStep != null)) { logWorkflowEvent( c, currentStep.getWorkflow().getID(), currentStep.getId(), currentActionConfig.getId(), wfi, user, nextStep, nextActionConfig); } } } log.error(LogManager.getHeader(c, "Invalid step outcome", "Workflow item id: " + wfi.getID())); throw new WorkflowException("Invalid step outcome"); }