Search in sources :

Example 11 with Point

use of cern.modesti.point.Point in project modesti by jlsalmon.

the class TestUtil method getDummyPoints.

private static List<Point> getDummyPoints() {
    ArrayList<Point> points = new ArrayList<>();
    Point point1 = new PointImpl();
    Point point2 = new PointImpl();
    point1.setProperties(Maps.newHashMap(ImmutableMap.of("id", "1", "pointDescription", "TEST POINT 1", "pointDatatype", "Boolean")));
    point2.setProperties(Maps.newHashMap(ImmutableMap.of("id", "2", "pointDescription", "TEST POINT 2", "pointDatatype", "Boolean")));
    points.add(point1);
    points.add(point2);
    return points;
}
Also used : ArrayList(java.util.ArrayList) Point(cern.modesti.point.Point) PointImpl(cern.modesti.point.PointImpl)

Example 12 with Point

use of cern.modesti.point.Point in project modesti by jlsalmon.

the class RequestServiceImpl method save.

/**
 * Save an existing request.
 *
 * @param updated the request to save
 * @return the newly saved request
 */
@Override
public Request save(Request updated) {
    Request original = repository.findOne(updated.getId());
    if (original == null) {
        throw new RuntimeException(format("Request #%s was not found", updated.getId()));
    }
    // The request id may not be modified manually.
    if (!Objects.equals(updated.getRequestId(), original.getRequestId())) {
        throw new IllegalArgumentException("Request ID cannot not be updated manually!");
    }
    // The request status may not be modified manually.
    if (!Objects.equals(updated.getStatus(), original.getStatus())) {
        throw new IllegalArgumentException("Request status cannot not be updated manually!");
    }
    // TODO: this shouldn't be necessary, and could cause side effects. Why do we lose properties when saving?
    Map<String, Object> properties = original.getProperties();
    properties.putAll(updated.getProperties());
    updated.setProperties(properties);
    for (Point point : updated.getPoints()) {
        if (point.getLineNo() == null) {
            point.setLineNo((long) (updated.getPoints().indexOf(point) + 1));
        }
    }
    // Invoke any callbacks
    for (RequestEventHandler requestEventHandler : requestEventHandlers) {
        requestEventHandler.onBeforeSave(updated);
    }
    // Apply formatting to the request points
    requestFormatter.format(updated);
    if (updated.getType().equals(RequestType.UPDATE) && (updated.getStatus().equals("IN_PROGRESS") || updated.getStatus().equals("FOR_ADDRESSING") || updated.getStatus().equals("IN_ERROR"))) {
        // Process and store any changes that were made to the request
        ((RequestHistoryServiceImpl) historyService).saveChangeHistory(updated);
    }
    return repository.save((RequestImpl) updated);
}
Also used : Point(cern.modesti.point.Point) RequestEventHandler(cern.modesti.request.spi.RequestEventHandler) RequestHistoryServiceImpl(cern.modesti.request.history.RequestHistoryServiceImpl)

Example 13 with Point

use of cern.modesti.point.Point 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 14 with Point

use of cern.modesti.point.Point in project modesti by jlsalmon.

the class RequestDiffer method diff.

public static ChangeEvent diff(Request modified, Request original, String idProperty) {
    List<Point> originalPointsStillPresentCurrently = deleteRemovedPoints(original.getPoints(), modified.getPoints(), idProperty);
    original.setPoints(originalPointsStillPresentCurrently);
    ChangeEvent event = new ChangeEvent(new DateTime(DateTimeZone.UTC));
    Request modifiedClone = new RequestImpl();
    Request originalClone = new RequestImpl();
    ChangeVisitor visitor = new ChangeVisitor(event, modifiedClone, originalClone);
    Map<Object, Point> modifiedPointMap = getPointsMap(modified.getPoints(), idProperty);
    for (Point originalPoint : original.getPoints()) {
        Point modifiedPoint = modifiedPointMap.get(originalPoint.getValueByPropertyName(idProperty));
        originalClone.setPoints(Arrays.asList((Point[]) new Point[] { originalPoint }));
        if (modifiedPoint != null) {
            modifiedClone.setPoints(Arrays.asList((Point[]) new Point[] { modifiedPoint }));
        }
        ObjectDiffer differ = ObjectDifferBuilder.startBuilding().identity().ofCollectionItems(NodePath.with("points")).via(new PointIdentityStrategy(idProperty)).and().build();
        DiffNode root = differ.compare(modifiedClone, originalClone);
        if (root.hasChanges()) {
            root.visit(visitor);
        }
    }
    return event;
}
Also used : Request(cern.modesti.request.Request) DiffNode(de.danielbechler.diff.node.DiffNode) Point(cern.modesti.point.Point) DateTime(org.joda.time.DateTime) RequestImpl(cern.modesti.request.RequestImpl) ObjectDiffer(de.danielbechler.diff.ObjectDiffer)

Example 15 with Point

use of cern.modesti.point.Point in project modesti by jlsalmon.

the class RequestDiffer method deleteRemovedPoints.

/**
 * Comparison does not work properly if some rows were removed, so we fix this by removing the same rows from original list,
 * before we start comparing states.
 *
 * @param originalPoints list of original points state
 * @param currentPoints  list of current points state
 * @param idProperty     property which serves as key to match points which we want to compare
 * @return list of original points without the ones missing in modified points
 */
private static List<Point> deleteRemovedPoints(List<Point> originalPoints, List<Point> currentPoints, String idProperty) {
    if (isIdPropertyMissing(originalPoints, currentPoints, idProperty)) {
        return originalPoints;
    }
    Set<String> modifiedPointIds = currentPoints.stream().map(point -> getIdPropertyFromPoint(idProperty, point)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toSet());
    List<Point> originalPointsCurrentlyPresent = originalPoints.stream().filter(point -> modifiedPointIds.contains(getIdPropertyFromPoint(idProperty, point).orElse("non existent value"))).collect(Collectors.toList());
    AtomicLong atomicLong = new AtomicLong(1);
    originalPointsCurrentlyPresent.forEach(point -> point.setLineNo(atomicLong.getAndIncrement()));
    return originalPointsCurrentlyPresent;
}
Also used : ObjectDifferBuilder(de.danielbechler.diff.ObjectDifferBuilder) Arrays(java.util.Arrays) DateTimeZone(org.joda.time.DateTimeZone) RequestImpl(cern.modesti.request.RequestImpl) Point(cern.modesti.point.Point) NodePath(de.danielbechler.diff.path.NodePath) DateTime(org.joda.time.DateTime) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) DiffNode(de.danielbechler.diff.node.DiffNode) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Map(java.util.Map) Request(cern.modesti.request.Request) Optional(java.util.Optional) ObjectDiffer(de.danielbechler.diff.ObjectDiffer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Optional(java.util.Optional) Point(cern.modesti.point.Point)

Aggregations

Point (cern.modesti.point.Point)15 Field (cern.modesti.schema.field.Field)5 ArrayList (java.util.ArrayList)5 Request (cern.modesti.request.Request)4 PointImpl (cern.modesti.point.PointImpl)3 RequestImpl (cern.modesti.request.RequestImpl)3 Category (cern.modesti.schema.category.Category)3 DateTime (org.joda.time.DateTime)3 RequestProvider (cern.modesti.plugin.RequestProvider)2 RequestHistoryServiceImpl (cern.modesti.request.history.RequestHistoryServiceImpl)2 AutocompleteField (cern.modesti.schema.field.AutocompleteField)2 OptionsField (cern.modesti.schema.field.OptionsField)2 ObjectDiffer (de.danielbechler.diff.ObjectDiffer)2 DiffNode (de.danielbechler.diff.node.DiffNode)2 UnsupportedRequestException (cern.modesti.plugin.UnsupportedRequestException)1 RequestEventHandler (cern.modesti.request.spi.RequestEventHandler)1 Schema (cern.modesti.schema.Schema)1 Constraint (cern.modesti.schema.category.Constraint)1 User (cern.modesti.user.User)1 NotAuthorisedException (cern.modesti.workflow.task.NotAuthorisedException)1