Search in sources :

Example 1 with RequestProvider

use of cern.modesti.plugin.RequestProvider in project modesti by jlsalmon.

the class CoreWorkflowServiceImpl method startProcessInstance.

/**
 * Start a new workflow process instance for the given request.
 *
 * @param request the request to be associated with the newly created
 *                workflow process instance
 * @return the newly started process instance object
 */
public ProcessInstance startProcessInstance(final Request request) {
    log.info(format("starting process for %s request %s", request.getDomain(), request.getRequestId()));
    // Figure out which process to start, based on the domain and type
    RequestProvider plugin = requestProviderRegistry.getPluginFor(request, new UnsupportedRequestException(request));
    String processKey = plugin.getMetadata().getProcessKey(request.getType());
    Map<String, Object> variables = new HashMap<>();
    variables.put("requestId", request.getRequestId());
    variables.put("creator", request.getCreator());
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processKey, request.getRequestId(), variables);
    // After initializing the process instance, sets the request status (it might have been modified by some Activiti tasks)
    Request savedRequest = getRequest(request.getRequestId());
    request.setStatus(savedRequest.getStatus());
    request.setErrors(savedRequest.getErrors());
    request.setPoints(savedRequest.getPoints());
    request.setSkipCoreValidation(savedRequest.isSkipCoreValidation());
    return processInstance;
}
Also used : HashMap(java.util.HashMap) RequestProvider(cern.modesti.plugin.RequestProvider) Request(cern.modesti.request.Request) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) UnsupportedRequestException(cern.modesti.plugin.UnsupportedRequestException)

Example 2 with RequestProvider

use of cern.modesti.plugin.RequestProvider in project modesti by jlsalmon.

the class CoreValidationService method validateRequest.

public boolean validateRequest(Request request) {
    try {
        if (RequestType.DELETE.equals(request.getType())) {
            // Delete requests should not be validated
            return true;
        }
        boolean valid = true;
        Schema schema = schemaRepository.findOne(request.getDomain());
        // Reset all points and clear any error messages.
        for (Point point : request.getPoints()) {
            point.setValid(true);
            point.setErrors(new ArrayList<>());
        }
        if (environment.getProperty("modesti.disableValidator", Boolean.class, false) || request.isSkipCoreValidation()) {
            log.info("core validations disabled");
        } else {
            // Concatenate all categories and datasources
            List<Category> categories = new ArrayList<>(schema.getCategories());
            categories.addAll(schema.getDatasources());
            // Validate the mutually exclusive column group specifications.
            if (!validateMutualExclusions(request, categories)) {
                valid = false;
            }
            // column groups and mutually inclusive fields.
            if (!validateConstraints(request, categories)) {
                valid = false;
            }
            // values, min/max length, valid values etc.
            if (!validatePoints(request, categories)) {
                valid = false;
            }
        }
        request.setValid(valid);
        requestService.save(request);
        if (!valid) {
            log.info(format("request #%s failed validation, not invoking custom validator", request.getRequestId()));
            return false;
        }
        log.info(format("request #%s is valid, invoking custom validator", request.getRequestId()));
        RequestProvider plugin = requestProviderRegistry.getPluginFor(request);
        RequestValidator validator = getPluginRequestValidator(plugin.getMetadata().getId());
        if (validator == null) {
            log.info(format("custom validator not provided for request #%s", request.getRequestId()));
            return true;
        }
        valid = validator.validateRequest(request, schema);
        request.setValid(valid);
        requestService.save(request);
        return valid;
    } catch (RuntimeException e) {
        request.setValid(false);
        requestService.save(request);
        log.info(format("Unexpected error during validation phase for request #%s '%s'", request.getRequestId(), e.toString()), e);
        return false;
    }
}
Also used : Category(cern.modesti.schema.category.Category) Schema(cern.modesti.schema.Schema) ArrayList(java.util.ArrayList) RequestProvider(cern.modesti.plugin.RequestProvider) Point(cern.modesti.point.Point)

Example 3 with RequestProvider

use of cern.modesti.plugin.RequestProvider in project modesti by jlsalmon.

the class RequestServiceImpl method insert.

/**
 * Insert (create) a new request.
 * <p>
 * Creating a new request performs the following actions:
 * <ul>
 * <li>
 * Asserts that the currently logged-in user is authorised to create a
 * request for the domain of the request
 * </li>
 * <li>
 * Sets the currently logged-in user as the creator of the request
 * </li>
 * <li>Generates a request id</li>
 * <li>Adds some empty points to the request if none were specified</li>
 * <li>Starts a new workflow process instance using the workflow key of the
 * plugin associated with the request domain</li>
 * </ul>
 *
 * @param request the request to create
 * @return the newly created request with all properties set
 */
@Override
public Request insert(Request request) {
    // Do not create a request if there is no appropriate domain
    RequestProvider plugin = requestProviderRegistry.getPluginFor(request, new UnsupportedRequestException(request));
    User user = userService.getCurrentUser();
    // Assert that the current user is allowed to create a request for this domain
    if (!authService.canCreate(plugin, request, user)) {
        throw new NotAuthorisedException(format("User \"%s\" is not authorised to create requests for domain \"%s\". " + "Authorisation group is \"%s\".", user.getUsername(), request.getDomain(), plugin.getMetadata().getAuthorisationGroup(request)));
    }
    // Set the creator as the current logged in user
    request.setCreator(user.getUsername());
    ((RequestImpl) request).setRequestId(counterService.getNextSequence(CounterService.REQUEST_ID_SEQUENCE).toString());
    log.trace(format("generated request id: %s", request.getRequestId()));
    ((RequestImpl) request).setCreatedAt(new DateTime());
    if (request.getPoints() == null) {
        request.setPoints(new ArrayList<>());
    }
    // Apply formatting to the request points
    requestFormatter.format(request);
    // Add some empty points if there aren't any yet
    if (request.getPoints().isEmpty()) {
        for (int i = 0; i < 50; i++) {
            Point point = new PointImpl((long) (i + 1));
            request.addPoint(point);
        }
    }
    for (Point point : request.getPoints()) {
        if (point.getLineNo() == null) {
            point.setLineNo((long) (request.getPoints().indexOf(point) + 1));
        }
    }
    request = repository.save((RequestImpl) request);
    if (request.getType().equals(RequestType.UPDATE)) {
        // Store an initial, empty change history
        ((RequestHistoryServiceImpl) historyService).initialiseChangeHistory(request);
    }
    // Kick off the workflow process
    workflowService.startProcessInstance(request);
    return request;
}
Also used : User(cern.modesti.user.User) NotAuthorisedException(cern.modesti.workflow.task.NotAuthorisedException) RequestProvider(cern.modesti.plugin.RequestProvider) Point(cern.modesti.point.Point) UnsupportedRequestException(cern.modesti.plugin.UnsupportedRequestException) DateTime(org.joda.time.DateTime) Point(cern.modesti.point.Point) PointImpl(cern.modesti.point.PointImpl) RequestHistoryServiceImpl(cern.modesti.request.history.RequestHistoryServiceImpl)

Example 4 with RequestProvider

use of cern.modesti.plugin.RequestProvider in project modesti by jlsalmon.

the class AuthService method canDelete.

/**
 * Default is only creator is allowed to delete. Administrators are always allowed to delete.
 * Plugins can implement the {@link AuthorizationProvider} to overwrite the {@link AuthorizationProvider#canDelete(Request)} behaviour.
 *
 * @param request the request object
 * @param user    the user to authorise
 *
 * @return true if the user is authorised, false otherwise
 */
public boolean canDelete(Request request, User user) {
    RequestProvider plugin = requestProviderRegistry.getPluginFor(request, new UnsupportedRequestException(request));
    String requestPluginId = plugin.getMetadata().getId();
    if (isAdministrator(user)) {
        return true;
    }
    String pluginAuthrorizationGroup = plugin.getMetadata().getAuthorisationGroup(request);
    if (hasRole(user, pluginAuthrorizationGroup)) {
        return true;
    }
    AuthorizationProvider authProvider = getPluginAuthorizationProvider(requestPluginId);
    if (authProvider != null) {
        return authProvider.canDelete(request);
    }
    return request.getCreator().equals(user.getUsername());
}
Also used : RequestProvider(cern.modesti.plugin.RequestProvider) AuthorizationProvider(cern.modesti.plugin.spi.AuthorizationProvider) UnsupportedRequestException(cern.modesti.plugin.UnsupportedRequestException)

Aggregations

RequestProvider (cern.modesti.plugin.RequestProvider)4 UnsupportedRequestException (cern.modesti.plugin.UnsupportedRequestException)3 Point (cern.modesti.point.Point)2 AuthorizationProvider (cern.modesti.plugin.spi.AuthorizationProvider)1 PointImpl (cern.modesti.point.PointImpl)1 Request (cern.modesti.request.Request)1 RequestHistoryServiceImpl (cern.modesti.request.history.RequestHistoryServiceImpl)1 Schema (cern.modesti.schema.Schema)1 Category (cern.modesti.schema.category.Category)1 User (cern.modesti.user.User)1 NotAuthorisedException (cern.modesti.workflow.task.NotAuthorisedException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)1 DateTime (org.joda.time.DateTime)1