Skip to content

Commit

Permalink
Merge branch 'release/2022-10-rc2'
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-gomes committed Nov 4, 2022
2 parents 962dcbf + 4951355 commit 94231a7
Show file tree
Hide file tree
Showing 28 changed files with 186,168 additions and 67,695 deletions.
57 changes: 50 additions & 7 deletions app/controllers/CommitController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
* SysML v2 REST/HTTP Pilot Implementation
* Copyright (C) 2020 InterCAX LLC
* Copyright (C) 2020 California Institute of Technology ("Caltech")
* Copyright (C) 2020 InterCAX LLC
* Copyright (C) 2020 California Institute of Technology ("Caltech")
* Copyright (C) 2022 Twingineer LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -23,11 +24,12 @@

import com.fasterxml.jackson.databind.JsonNode;
import config.MetamodelProvider;
import jackson.JacksonHelper;
import jackson.jsonld.JsonLdAdorner;
import jackson.jsonld.RecordAdorners.CommitAdorner;
import jackson.jsonld.RecordAdorners.ProjectContainmentParameters;
import org.omg.sysml.lifecycle.Commit;
import org.omg.sysml.lifecycle.impl.CommitImpl;
import org.omg.sysml.lifecycle.DataVersion;
import play.Environment;
import play.libs.Json;
import play.mvc.Http.Request;
Expand Down Expand Up @@ -79,8 +81,7 @@ private Result postCommitByProject(UUID projectId, @SuppressWarnings("OptionalUs
request,
new ProjectContainmentParameters(projectId),
ld,
Json.mapper(),
writer -> writer.withView(CommitImpl.Views.Compact.class)
Json.mapper()
);
return buildResult(json, ld);
}
Expand All @@ -101,8 +102,7 @@ public Result getCommitsByProject(UUID projectId, Request request) {
request,
new ProjectContainmentParameters(projectId),
ld,
Json.mapper(),
writer -> writer.withView(CommitImpl.Views.Compact.class)
Json.mapper()
);
Result result = buildResult(json, ld);
return uuidResponse(
Expand All @@ -123,6 +123,49 @@ public Result getCommitByProjectAndId(UUID projectId, UUID commitId, Request req
return buildResult(commit.orElse(null), request, new ProjectContainmentParameters(projectId));
}

public Result getChangesByProjectAndCommit(UUID projectId, UUID commitId, Request request) {
if (respondWithJsonLd(request)) {
// TODO implement
return Results.status(NOT_IMPLEMENTED);
}
PageRequest<UUID> pageRequest = uuidRequest(request);
List<DataVersion> changes = commitService.getChangesByProjectIdAndCommitId(
projectId,
commitId,
pageRequest.getAfter(),
pageRequest.getBefore(),
pageRequest.getSize()
);
boolean ld = respondWithJsonLd(request);
JsonNode json = JacksonHelper.collectionToTree(
changes,
List.class,
metamodelProvider.getImplementationClass(DataVersion.class),
Json.mapper()
);
Result result = buildResult(json, ld);
return uuidResponse(
result,
changes.size(),
idx -> changes.get(idx).getId(),
request,
pageRequest
);
}

public Result getChangeByProjectCommitAndId(UUID projectId, UUID commitId, UUID changeId, Request request) {
if (respondWithJsonLd(request)) {
// TODO implement
return Results.status(NOT_IMPLEMENTED);
}
Optional<DataVersion> change = commitService.getChangeByProjectIdCommitIdAndId(projectId, commitId, changeId);
JsonNode json = JacksonHelper.objectToTree(
change,
Json.mapper()
);
return Results.ok(json);
}

@Override
protected JsonLdAdorner<Commit, ProjectContainmentParameters> getAdorner() {
return adorner;
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/ProjectController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SysML v2 REST/HTTP Pilot Implementation
* Copyright (C) 2020 InterCAX LLC
* Copyright (C) 2020 California Institute of Technology ("Caltech")
* Copyright (C) 2021 Twingineer LLC
* Copyright (C) 2021-2022 Twingineer LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -35,6 +35,7 @@
import services.ProjectService;

import javax.inject.Inject;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -95,6 +96,10 @@ public Result getProjects(Request request) {
public Result postProject(Request request) {
JsonNode requestBodyJson = request.body().asJson();
Project requestedObject = Json.fromJson(requestBodyJson, metamodelProvider.getImplementationClass(Project.class));
if (requestedObject.getId() != null || requestedObject.getCreated() != null) {
return Results.badRequest();
}
requestedObject.setCreated(ZonedDateTime.now());
Optional<Project> project = projectService.create(requestedObject);
if (project.isEmpty()) {
return Results.internalServerError();
Expand Down
10 changes: 7 additions & 3 deletions app/dao/CommitDao.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
* SysML v2 REST/HTTP Pilot Implementation
* Copyright (C) 2020 InterCAX LLC
* Copyright (C) 2020 California Institute of Technology ("Caltech")
* Copyright (C) 2020 InterCAX LLC
* Copyright (C) 2020 California Institute of Technology ("Caltech")
* Copyright (C) 2022 Twingineer LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -23,6 +24,7 @@

import org.omg.sysml.lifecycle.Branch;
import org.omg.sysml.lifecycle.Commit;
import org.omg.sysml.lifecycle.DataVersion;
import org.omg.sysml.lifecycle.Project;

import java.util.List;
Expand All @@ -49,5 +51,7 @@ default Optional<Commit> update(Commit commit) {

Optional<Commit> findByProjectAndId(Project project, UUID id);

Optional<Commit> findByProjectAndIdResolved(Project project, UUID id);
List<DataVersion> findChangesByCommit(Commit commit, UUID after, UUID before, int maxResults);

Optional<DataVersion> findChangeByCommitAndId(Commit commit, UUID id);
}
9 changes: 3 additions & 6 deletions app/dao/impl/FlatSchemaDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
package dao.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Streams;
import config.MetamodelProvider;
import dao.SchemaDao;
import jackson.databind.ObjectMapperFactory;
import org.omg.sysml.data.ExternalData;
import org.omg.sysml.data.ExternalRelationship;
import org.omg.sysml.data.ProjectUsage;
import play.libs.Json;

import javax.annotation.Nullable;
import javax.inject.Inject;
Expand All @@ -44,11 +43,9 @@
public class FlatSchemaDao implements SchemaDao {

private final TreeMap<String, JsonNode> map;
private final ObjectMapper mapper;

@Inject
public FlatSchemaDao(MetamodelProvider metamodelProvider, ObjectMapperFactory mapperFactory) {
this.mapper = mapperFactory.getObjectMapper();
public FlatSchemaDao(MetamodelProvider metamodelProvider) {
try (Stream<Class<?>> interfaces = metamodelProvider.getAllInterfaces().stream()) {
map = Streams.concat(
interfaces
Expand All @@ -61,7 +58,7 @@ public FlatSchemaDao(MetamodelProvider metamodelProvider, ObjectMapperFactory ma
)
.map(input -> {
try {
return mapper.reader().readTree(input);
return Json.mapper().reader().readTree(input);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
51 changes: 39 additions & 12 deletions app/dao/impl/jpa/JpaCommitDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SysML v2 REST/HTTP Pilot Implementation
* Copyright (C) 2020 InterCAX LLC
* Copyright (C) 2020 California Institute of Technology ("Caltech")
* Copyright (C) 2021 Twingineer LLC
* Copyright (C) 2021-2022 Twingineer LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -27,22 +27,15 @@
import javabean.JavaBeanHelper;
import jpa.manager.JPAManager;
import org.omg.sysml.lifecycle.*;
import org.omg.sysml.lifecycle.impl.CommitImpl;
import org.omg.sysml.lifecycle.impl.CommitImpl_;
import org.omg.sysml.lifecycle.impl.DataIdentityImpl;
import org.omg.sysml.lifecycle.impl.DataImpl;
import org.omg.sysml.lifecycle.impl.*;
import org.omg.sysml.metamodel.Element;
import org.omg.sysml.metamodel.impl.ElementImpl_;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.*;
import java.lang.reflect.Method;
import java.util.*;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -334,9 +327,43 @@ protected Optional<Commit> findByProjectAndId(Project project, UUID id, EntityMa
}

@Override
public Optional<Commit> findByProjectAndIdResolved(Project project, UUID id) {
public List<DataVersion> findChangesByCommit(Commit commit, UUID after, UUID before, int maxResults) {
return jpaManager.transact(em -> {
return findByProjectAndId(project, id, em).map(JpaCommitDao::resolve);
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<DataVersionImpl> query = builder.createQuery(DataVersionImpl.class);
Root<CommitImpl> commitRoot = query.from(CommitImpl.class);
SetJoin<CommitImpl, DataVersionImpl> dataVersionJoin = commitRoot.join(CommitImpl_.change);
Path<UUID> idPath = dataVersionJoin.get(DataVersionImpl_.id);
Expression<Boolean> where = builder.equal(commitRoot.get(CommitImpl_.id), commit.getId());
query.select(dataVersionJoin);
Paginated<TypedQuery<DataVersionImpl>> paginated = paginateQuery(after, before, maxResults, query, builder, em, idPath, where);
List<DataVersion> result = paginated.get()
.getResultStream()
.collect(Collectors.toList());
if (paginated.isReversed()) {
Collections.reverse(result);
}
return result;
});
}

@Override
public Optional<DataVersion> findChangeByCommitAndId(Commit commit, UUID id) {
return jpaManager.transact(em -> {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<DataVersionImpl> query = builder.createQuery(DataVersionImpl.class);
Root<CommitImpl> commitRoot = query.from(CommitImpl.class);
SetJoin<CommitImpl, DataVersionImpl> dataVersionJoin = commitRoot.join(CommitImpl_.change);
Expression<Boolean> where = builder.and(
builder.equal(commitRoot.get(CommitImpl_.id), commit.getId()),
builder.equal(dataVersionJoin.get(DataVersionImpl_.id), id)
);
query.select(dataVersionJoin).where(where);
try {
return Optional.of(em.createQuery(query).getSingleResult());
} catch (NoResultException e) {
return Optional.empty();
}
});
}
}
9 changes: 4 additions & 5 deletions app/dao/impl/jpa/JpaDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SysML v2 REST/HTTP Pilot Implementation
* Copyright (C) 2020 InterCAX LLC
* Copyright (C) 2020 California Institute of Technology ("Caltech")
* Copyright (C) 2021 Twingineer LLC
* Copyright (C) 2021-2022 Twingineer LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -24,7 +24,6 @@

import dao.Dao;
import jpa.manager.JPAManager;
import org.omg.sysml.lifecycle.Project;

import javax.annotation.Nullable;
import javax.persistence.EntityManager;
Expand Down Expand Up @@ -111,11 +110,11 @@ public boolean isReversed() {
}
}

protected <X> Paginated<TypedQuery<X>> paginateQuery(@Nullable UUID after, @Nullable UUID before, int maxResults, CriteriaQuery<X> query, CriteriaBuilder builder, EntityManager em, Path<UUID> idPath) {
protected static <X> Paginated<TypedQuery<X>> paginateQuery(@Nullable UUID after, @Nullable UUID before, int maxResults, CriteriaQuery<X> query, CriteriaBuilder builder, EntityManager em, Path<UUID> idPath) {
return paginateQuery(after, before, maxResults, query, builder, em, idPath, null);
}

protected <X> Paginated<TypedQuery<X>> paginateQuery(@Nullable UUID after, @Nullable UUID before, int maxResults, CriteriaQuery<X> query, CriteriaBuilder builder, EntityManager em, Path<UUID> idPath, @Nullable Expression<Boolean> where) {
protected static <X> Paginated<TypedQuery<X>> paginateQuery(@Nullable UUID after, @Nullable UUID before, int maxResults, CriteriaQuery<X> query, CriteriaBuilder builder, EntityManager em, Path<UUID> idPath, @Nullable Expression<Boolean> where) {
if (after != null) {
Expression<Boolean> expr = builder.greaterThan(idPath, after);
where = where != null ? builder.and(where, expr) : expr;
Expand All @@ -137,7 +136,7 @@ protected <X> Paginated<TypedQuery<X>> paginateQuery(@Nullable UUID after, @Null
return new Paginated<>(typedQuery, reversed);
}

protected <X> Paginated<Stream<X>> paginateStream(@Nullable UUID after, @Nullable UUID before, int maxResults, Stream<X> stream, Function<X, UUID> idFunction) {
protected static <X> Paginated<Stream<X>> paginateStream(@Nullable UUID after, @Nullable UUID before, int maxResults, Stream<X> stream, Function<X, UUID> idFunction) {
if (after != null) {
stream = stream.filter(x -> idFunction.apply(x).compareTo(after) > 0);
}
Expand Down
37 changes: 32 additions & 5 deletions app/dao/impl/jpa/JpaDataDao.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* SysML v2 REST/HTTP Pilot Implementation
* Copyright (C) 2021 Twingineer LLC
* Copyright (C) 2021-2022 Twingineer LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -33,20 +33,20 @@
import org.omg.sysml.internal.impl.CommitDataVersionIndexImpl;
import org.omg.sysml.internal.impl.CommitDataVersionIndexImpl_;
import org.omg.sysml.internal.impl.WorkingDataVersionImpl;
import org.omg.sysml.internal.impl.WorkingDataVersionImpl_;
import org.omg.sysml.lifecycle.Commit;
import org.omg.sysml.lifecycle.Data;
import org.omg.sysml.lifecycle.DataVersion;
import org.omg.sysml.lifecycle.impl.CommitImpl;
import org.omg.sysml.lifecycle.impl.*;
import org.omg.sysml.query.*;
import org.omg.sysml.query.impl.QueryImpl;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.NoResultException;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.*;
import java.beans.PropertyDescriptor;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -55,6 +55,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static dao.impl.jpa.JpaDao.paginateQuery;

public class JpaDataDao implements DataDao {

// TODO Explore alternative to serializing lazy entity attributes that doesn't involve resolving all proxies one level.
Expand Down Expand Up @@ -104,6 +106,31 @@ public List<Data> findByCommitAndQuery(Commit commit, Query query) {
});
}

List<DataVersion> findChangesByCommit(Commit commit, UUID after, UUID before, int maxResults, boolean excludeUsed, EntityManager em) {
CommitDataVersionIndex commitIndex = getCommitIndex(commit, em);

CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<DataVersionImpl> query = builder.createQuery(DataVersionImpl.class);
Root<CommitDataVersionIndexImpl> commitIndexRoot = query.from(CommitDataVersionIndexImpl.class);
SetJoin<CommitDataVersionIndexImpl, WorkingDataVersionImpl> workingDataVersionJoin = commitIndexRoot.join(CommitDataVersionIndexImpl_.workingDataVersion);
Join<WorkingDataVersionImpl, DataVersionImpl> dataVersionJoin = workingDataVersionJoin.join(WorkingDataVersionImpl_.dataVersion);
Join<DataVersionImpl, DataIdentityImpl> dataIdentityJoin = dataVersionJoin.join(DataVersionImpl_.identity);
Path<UUID> idPath = dataIdentityJoin.get(DataIdentityImpl_.id);
Expression<Boolean> where = builder.equal(commitIndexRoot.get(CommitDataVersionIndexImpl_.id), commitIndex.getId());
if (excludeUsed) {
where = builder.and(where, builder.isNull(workingDataVersionJoin.get(WorkingDataVersionImpl_.source)));
}
query.select(dataVersionJoin);
JpaDao.Paginated<TypedQuery<DataVersionImpl>> paginated = paginateQuery(after, before, maxResults, query, builder, em, idPath, where);
List<DataVersion> result = paginated.get()
.getResultStream()
.collect(Collectors.toList());
if (paginated.isReversed()) {
Collections.reverse(result);
}
return result;
}

protected CommitDataVersionIndex getCommitIndex(Commit commit, EntityManager em) {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<CommitDataVersionIndexImpl> query = builder.createQuery(CommitDataVersionIndexImpl.class);
Expand Down
Loading

0 comments on commit 94231a7

Please sign in to comment.