matthias_przyby
Returning Observer

Arbeitsabläufe abfragen

Hallo zusammen,

ich habe auf einer Strukturseite einen Arbeitsablauf aktiviert, welchen ich ausführen kann.

Über ein Skript möchte ich diesen Arbeitsablauf erfragen.

Wie kann ich das tun? Bisher sehe ich keine Möglichkeit:

Ich habe die PageRef pageRef der Strukturseite:

- Ausführen von pageRef.getWorkflowPermissions() gibt mir ein leeres Array zurück

- Ausführen von pageRef.hasPersmissions() ist false

Die Methode pageRef.isWorkflowAllowed(workflowObject, context.getUserService().getUser())) kommt für mich nicht in Frage, da ich es User-unabhängig benötige.

Danke und viele Grüße,

Matthias

0 Kudos
2 Replies
sebastianc
Crownpeak employee

Hallo Matthias,


ich habe etwas ähnliches implementiert (Steuerung der Arbeitsabläufe über eine REST-API).

Hier die für dich (vielleicht) relevanten Quelltext-Ausschnitte:

    /**

     * Get all tasks within a project.

     *

     * @param projectId

     * @param connection

     * @return task if found, otherwise null

     */

    public static Iterable<Task> getProjectTasks(Long projectId,

                                                 Connection connection) {

        SpecialistsBroker specialistsBroker = connection.getBroker();

        ServicesBroker servicesBroker = specialistsBroker.requestSpecialist(ServicesBroker.TYPE);

        SearchService searchService = servicesBroker.getService(SearchService.class);

        TaskQuery taskQuery = TaskQuery.create();

        return searchService.searchTasks(projectId, taskQuery);

    }

    /**

     * Description:     Get all active tasks for a specific user.

     * Usage:           RequestMethod: POST on "/tasks/{projectId}/{userId}"

     *

     * @param user     FirstSpirit Server login

     * @param password FirstSpirit Server password

     * @return String JSON-String

     */

    @RequestMapping(value = "/task/{projectId}/{userId}", method = RequestMethod.POST)

    public ResponseEntity<String> getUserTasks(@RequestParam("user") String user,

                                               @RequestParam("password") String password,

                                               @PathVariable Long projectId,

                                               @PathVariable Long userId) {

        try (Connection connection = fsConnectionUtils.authenticate(user, password)) {

            Iterable<Task> searchResult = WorkflowUtils.getProjectTasks(projectId, connection);

            List<Task> userTasks = new ArrayList<>();

            for (Task task : searchResult) {

                long creatorId = task.getCreator().getId();

                if (creatorId == userId) userTasks.add(task);

            }

            String s = jsonObjectToString(userTasks);

            return new ResponseEntity<String>(s, HttpStatus.OK);

        } catch (IOException e) {

            JsonObject obj = LogUtils.getJSON(Arrays.toString(e.getStackTrace()),

                                              Logger.LogLevel.ERROR,

                                              this.getClass());

            return new ResponseEntity<String>(jsonObjectToString(obj), HttpStatus.OK);

        }

    }

... und noch eine Methode, um einen Arbeitsablauf weiter zu schalten:

    /**

     * Description:     Set a destination for a given transition.

     * Usage:           RequestMethod: POST on "/transition/{projectId}/{taskId}/{transitionId}/{actionId}"

     *

     * @param user     FirstSpirit Server login

     * @param password FirstSpirit Server password

     * @return HttpStatus Code

     */

    @RequestMapping(value = "/transition/{projectId}/{taskId}/{transitionId}/{actionId}",

                    method = RequestMethod.POST)

    public ResponseEntity<String> setTransition(@RequestParam("user") String user,

                                                @RequestParam("password") String password,

                                                @PathVariable long projectId,

                                                @PathVariable int taskId,

                                                @PathVariable int transitionId,

                                                @PathVariable int actionId) {

        try (Connection connection = fsConnectionUtils.authenticate(user, password)) {

            TaskQuery taskQuery = TaskQuery.create();

            SpecialistsBroker specialistsBroker = connection.getBroker();

            ServicesBroker servicesBroker = specialistsBroker.requestSpecialist(ServicesBroker.TYPE);

            BrokerAgent brokerAgent = connection.getBroker().requireSpecialist(BrokerAgent.TYPE);

            SpecialistsBroker brokerByProjectId = brokerAgent.getBrokerByProjectId(projectId);

            WorkflowAgent workflowAgent = brokerByProjectId.requestSpecialist(WorkflowAgent.TYPE);

            SearchService searchService = servicesBroker.getService(SearchService.class);

            Iterable<Task> searchResult = searchService.searchTasks(projectId, taskQuery);

            Task task = WorkflowUtils.findTaskById(taskId, searchResult);

            Preconditions.checkNotNull(task);

            try {

                Iterator<? extends Transition> transitionsIterator

                        = task.getWorkflow().getWorkflowModel().transitions();

                if (transitionsIterator.hasNext()) {

                    TaskState taskState = task.getTaskState();

                    List<? extends Transition> targetTransitions

                            = taskState.getModelState().getTargetTransitions();

                    Transition transition

                            = WorkflowUtils.findTransitionById(transitionId, targetTransitions);

                    Preconditions.checkNotNull(transition);

                    Activity targetActivity = (Activity) transition.getTarget();

                    boolean manual = !targetActivity.isAutomatic();

                    if (manual) {

                        final WorkflowAgent.WorkflowProcessContext process

                                = workflowAgent.process(task, transition);

                        List<? extends Transition> actionTransitions

                                = targetActivity.getTargetTransitions();

                        Transition actionTransition

                                = WorkflowUtils.findTransitionById(actionId, actionTransitions);

                        if (actionTransition == null) {

                            JsonObject obj = LogUtils.getJSON("No action with ID: " +

                                    actionId + " found. Aborting.", LogLevel.DEBUG, this.getClass());

                            return new ResponseEntity<String>(jsonObjectToString(obj),

                                                              HttpStatus.NO_CONTENT);

                        }

                        process.doTransition(actionTransition);

                        JsonObject obj = LogUtils.getJSON("Task processed successfully!",

                                                          LogLevel.DEBUG, this.getClass());

                        return new ResponseEntity<String>(jsonObjectToString(obj),

                                                          HttpStatus.ACCEPTED);

                    }

                } else {

                    JsonObject obj = LogUtils.getJSON("Task with ID: " + taskId +

                                                        " lacks next transition. Aborting.",

                                                        LogLevel.DEBUG, this.getClass());

                    return new ResponseEntity<String>(jsonObjectToString(obj),

                                                      HttpStatus.CONFLICT);

                }

            } catch (LockException e) {

                JsonObject obj = LogUtils.getJSON(Arrays.toString(e.getStackTrace()),

                                                  LogLevel.ERROR, this.getClass());

                return new ResponseEntity<String>(jsonObjectToString(obj),

                                                  HttpStatus.CONFLICT);

            }

        } catch (IOException e) {

            JsonObject obj = LogUtils.getJSON(Arrays.toString(e.getStackTrace()),

                                              LogLevel.ERROR, this.getClass());

            return new ResponseEntity<String>(jsonObjectToString(obj),

                                              HttpStatus.FORBIDDEN);

        }

        JsonObject obj = LogUtils.getJSON("Task with ID: " + taskId + " not found. Aborting.",

                                          LogLevel.DEBUG, this.getClass());

        return new ResponseEntity<String>(jsonObjectToString(obj),

                                          HttpStatus.INTERNAL_SERVER_ERROR);

    }

                          

Den Code kannst du anschließend über eine Executable aus einem Script ausführen.

Damit es Nutzer-unabhängig ist, kannst du in deinem Code einen technischen Benutzer hinterlegen.

Ich hoffe, dass es dir etwas weiter hilft. Smiley Wink

Viele Grüße,

Sebastian

0 Kudos
MichaelaReydt
Community Manager

Hallo Matthias,

benötigst du noch weitere Hilfe oder konnte Sebastians Antwort dir bereits weiterhelfen? In diesem Fall wäre es super, wenn du seine "richtige Antwort" entsprechend markierst.

Viele Grüße

Michaela

0 Kudos