Skip to content

Commit 8b9434d

Browse files
committed
Merge branch 'release/2022-09'
2 parents d1650f7 + ee293af commit 8b9434d

File tree

733 files changed

+936300
-517
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

733 files changed

+936300
-517
lines changed

app/Module.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import config.MetamodelProvider;
2525
import config.impl.JPAMetamodelProvider;
2626
import dao.*;
27+
import dao.impl.FlatSchemaDao;
2728
import dao.impl.jpa.*;
2829
import jackson.databind.ObjectMapperFactory;
2930
import jackson.databind.impl.HibernateObjectMapperFactory;
@@ -45,5 +46,6 @@ protected void configure() {
4546
bind(QueryDao.class).to(JpaQueryDao.class);
4647
bind(BranchDao.class).to(JpaBranchDao.class);
4748
bind(TagDao.class).to(JpaTagDao.class);
49+
bind(SchemaDao.class).to(FlatSchemaDao.class);
4850
}
4951
}

app/controllers/BaseController.java

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/*
22
* SysML v2 REST/HTTP Pilot Implementation
3-
* Copyright (C) 2020 InterCAX LLC
4-
* Copyright (C) 2020 California Institute of Technology ("Caltech")
3+
* Copyright (C) 2020 InterCAX LLC
4+
* Copyright (C) 2020 California Institute of Technology ("Caltech")
5+
* Copyright (C) 2022 Twingineer LLC
56
*
67
* This program is free software: you can redistribute it and/or modify
78
* it under the terms of the GNU Lesser General Public License as published by
@@ -37,67 +38,88 @@ public abstract class BaseController extends Controller {
3738
protected static char CURSOR_SEPARATOR = '|';
3839
protected static int DEFAULT_PAGE_SIZE = 100;
3940

40-
protected static UUID fromCursor(String cursor) throws IllegalArgumentException {
41+
private static String stringFromCursor(String cursor) throws IllegalArgumentException {
4142
byte[] decoded = Base64.getUrlDecoder().decode(cursor);
4243
int separatorIndex = Bytes.indexOf(decoded, (byte) CURSOR_SEPARATOR);
4344
if (separatorIndex < 0) {
4445
throw new IllegalArgumentException("Provided cursor is malformed");
4546
}
46-
return UUID.fromString(
47-
new String(decoded, separatorIndex + 1, decoded.length - separatorIndex - 1)
48-
);
47+
return new String(decoded, separatorIndex + 1, decoded.length - separatorIndex - 1);
4948
}
5049

51-
protected static String toCursor(UUID id) throws IllegalArgumentException {
50+
private static String stringToCursor(String string) {
5251
String unencoded = String.valueOf(Instant.now().toEpochMilli()) +
5352
CURSOR_SEPARATOR +
54-
id.toString();
53+
string;
5554
return Base64.getUrlEncoder().withoutPadding().encodeToString(unencoded.getBytes());
5655
}
5756

58-
protected static class PageRequest {
59-
private final UUID after;
60-
private final UUID before;
57+
private static UUID uuidFromCursor(String cursor) throws IllegalArgumentException {
58+
return UUID.fromString(stringFromCursor(cursor));
59+
}
60+
61+
private static String uuidToCursor(UUID id) throws IllegalArgumentException {
62+
return stringToCursor(id.toString());
63+
}
64+
65+
protected static class PageRequest<T> {
66+
private final T after;
67+
private final T before;
6168
private final int size;
6269

63-
private PageRequest(UUID after, UUID before, int size) {
70+
private PageRequest(T after, T before, int size) {
71+
if (size <= 0) {
72+
throw new IllegalArgumentException("Page size must be greater than zero");
73+
}
74+
6475
this.after = after;
6576
this.before = before;
6677
this.size = size;
6778
}
6879

69-
public UUID getAfter() {
80+
public T getAfter() {
7081
return after;
7182
}
7283

73-
public UUID getBefore() {
84+
public T getBefore() {
7485
return before;
7586
}
7687

7788
public int getSize() {
7889
return size;
7990
}
91+
}
8092

81-
protected static PageRequest from(Http.Request request) throws IllegalArgumentException {
82-
UUID after = Optional.ofNullable(request.getQueryString("page[after]"))
83-
.map(BaseController::fromCursor)
84-
//.map(UUID::fromString)
85-
.orElse(null);
86-
UUID before = Optional.ofNullable(request.getQueryString("page[before]"))
87-
.map(BaseController::fromCursor)
88-
//.map(UUID::fromString)
89-
.orElse(null);
90-
int size = Optional.ofNullable(request.getQueryString("page[size]"))
91-
.map(Integer::parseInt)
92-
.orElse(DEFAULT_PAGE_SIZE);
93-
if (size <= 0) {
94-
throw new IllegalArgumentException("Page size must be greater than zero");
95-
}
96-
return new PageRequest(after, before, size);
97-
}
93+
protected static PageRequest<UUID> uuidRequest(Http.Request request) throws IllegalArgumentException {
94+
return request(request, BaseController::uuidFromCursor);
95+
}
96+
97+
protected static PageRequest<String> stringRequest(Http.Request request) throws IllegalArgumentException {
98+
return request(request, BaseController::stringFromCursor);
99+
}
100+
101+
private static <T> PageRequest<T> request(Http.Request request, Function<String, T> decoder) throws IllegalArgumentException {
102+
T after = Optional.ofNullable(request.getQueryString("page[after]"))
103+
.map(decoder)
104+
.orElse(null);
105+
T before = Optional.ofNullable(request.getQueryString("page[before]"))
106+
.map(decoder)
107+
.orElse(null);
108+
int size = Optional.ofNullable(request.getQueryString("page[size]"))
109+
.map(Integer::parseInt)
110+
.orElse(DEFAULT_PAGE_SIZE);
111+
return new PageRequest<>(after, before, size);
112+
}
113+
114+
protected static Result uuidResponse(Result result, int resultSize, Function<Integer, UUID> idAtIndex, Http.Request request, PageRequest<UUID> pageRequest) {
115+
return response(result, resultSize, idAtIndex, request, pageRequest, BaseController::uuidToCursor);
116+
}
117+
118+
protected static Result stringResponse(Result result, int resultSize, Function<Integer, String> idAtIndex, Http.Request request, PageRequest<String> pageRequest) {
119+
return response(result, resultSize, idAtIndex, request, pageRequest, BaseController::stringToCursor);
98120
}
99121

100-
protected static Result paginateResult(Result result, int resultSize, Function<Integer, UUID> idAtIndex, Http.Request request, PageRequest pageRequest) {
122+
private static <T> Result response(Result result, int resultSize, Function<Integer, T> idAtIndex, Http.Request request, PageRequest<T> pageRequest, Function<T, String> encoder) {
101123
if (resultSize > 0) {
102124
boolean pageFull = resultSize == pageRequest.getSize();
103125
boolean hasNext = pageFull || pageRequest.getBefore() != null;
@@ -108,7 +130,7 @@ protected static Result paginateResult(Result result, int resultSize, Function<I
108130
linkHeaderValueBuilder.append(String.format("<http://%s%s?page[after]=%s&page[size]=%s>; rel=\"next\"",
109131
request.host(),
110132
request.path(),
111-
toCursor(idAtIndex.apply(resultSize - 1)),
133+
encoder.apply(idAtIndex.apply(resultSize - 1)),
112134
pageRequest.getSize()));
113135
if (hasPrev) {
114136
linkHeaderValueBuilder.append(", ");
@@ -118,7 +140,7 @@ protected static Result paginateResult(Result result, int resultSize, Function<I
118140
linkHeaderValueBuilder.append(String.format("<http://%s%s?page[before]=%s&page[size]=%s>; rel=\"prev\"",
119141
request.host(),
120142
request.path(),
121-
toCursor(idAtIndex.apply(0)),
143+
encoder.apply(idAtIndex.apply(0)),
122144
pageRequest.getSize()));
123145
}
124146
if (linkHeaderValueBuilder.length() > 0) {

app/controllers/BranchController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ public Result postBranchByProject(UUID projectId, Request request) {
6969
}
7070

7171
public Result getBranchesByProject(UUID projectId, Request request) {
72-
PageRequest pageRequest = PageRequest.from(request);
72+
PageRequest<UUID> pageRequest = uuidRequest(request);
7373
List<Branch> branches = branchService.getByProjectId(
7474
projectId,
7575
pageRequest.getAfter(),
7676
pageRequest.getBefore(),
7777
pageRequest.getSize()
7878
);
79-
return paginateResult(
79+
return uuidResponse(
8080
buildResult(branches, List.class, metamodelProvider.getImplementationClass(Branch.class), request, new ProjectContainmentParameters(projectId)),
8181
branches.size(),
8282
idx -> branches.get(idx).getId(),

app/controllers/CommitController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private Result postCommitByProject(UUID projectId, @SuppressWarnings("OptionalUs
8686
}
8787

8888
public Result getCommitsByProject(UUID projectId, Request request) {
89-
PageRequest pageRequest = PageRequest.from(request);
89+
PageRequest<UUID> pageRequest = uuidRequest(request);
9090
List<Commit> commits = commitService.getByProjectId(
9191
projectId,
9292
pageRequest.getAfter(),
@@ -105,7 +105,7 @@ public Result getCommitsByProject(UUID projectId, Request request) {
105105
writer -> writer.withView(CommitImpl.Views.Compact.class)
106106
);
107107
Result result = buildResult(json, ld);
108-
return paginateResult(
108+
return uuidResponse(
109109
result,
110110
commits.size(),
111111
idx -> commits.get(idx).getId(),

app/controllers/ElementController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public ElementController(ElementService elementService, MetamodelProvider metamo
5151
}
5252

5353
public Result getElementsByProjectIdCommitId(UUID projectId, UUID commitId, Optional<Boolean> excludeUsed, Request request) {
54-
PageRequest pageRequest = PageRequest.from(request);
54+
PageRequest<UUID> pageRequest = uuidRequest(request);
5555
List<Element> elements = elementService.getElementsByProjectIdCommitId(projectId, commitId, excludeUsed.orElse(false), pageRequest.getAfter(), pageRequest.getBefore(), pageRequest.getSize());
5656
return buildPaginatedResult(elements, projectId, commitId, request, pageRequest);
5757
}
@@ -62,7 +62,7 @@ public Result getElementByProjectIdCommitIdElementId(UUID projectId, UUID commit
6262
}
6363

6464
public Result getRootsByProjectIdCommitId(UUID projectId, UUID commitId, Optional<Boolean> excludeUsed, Request request) {
65-
PageRequest pageRequest = PageRequest.from(request);
65+
PageRequest<UUID> pageRequest = uuidRequest(request);
6666
List<Element> roots = elementService.getRootsByProjectIdCommitId(projectId, commitId, excludeUsed.orElse(false), pageRequest.getAfter(), pageRequest.getBefore(), pageRequest.getSize());
6767
return buildPaginatedResult(roots, projectId, commitId, request, pageRequest);
6868
}
@@ -72,8 +72,8 @@ public Result getElementByProjectIdCommitIdQualifiedName(UUID projectId, UUID co
7272
return buildResult(element.orElse(null), request, new DataJsonLdAdorner.Parameters(projectId, commitId));
7373
}
7474

75-
private Result buildPaginatedResult(List<Element> elements, UUID projectId, UUID commitId, Request request, PageRequest pageRequest) {
76-
return paginateResult(
75+
private Result buildPaginatedResult(List<Element> elements, UUID projectId, UUID commitId, Request request, PageRequest<UUID> pageRequest) {
76+
return uuidResponse(
7777
buildResult(elements, List.class, metamodelProvider.getImplementationClass(Element.class), request, new DataJsonLdAdorner.Parameters(projectId, commitId)),
7878
elements.size(),
7979
idx -> elements.get(idx).getElementId(),

app/controllers/ProjectController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ public Result deleteProjectById(UUID id, Request request) {
8181
}
8282

8383
public Result getProjects(Request request) {
84-
PageRequest pageRequest = PageRequest.from(request);
84+
PageRequest<UUID> pageRequest = uuidRequest(request);
8585
List<Project> projects = projectService.getAll(pageRequest.getAfter(), pageRequest.getBefore(), pageRequest.getSize());
86-
return paginateResult(
86+
return uuidResponse(
8787
buildResult(projects, List.class, metamodelProvider.getImplementationClass(Project.class), request, null),
8888
projects.size(),
8989
idx -> projects.get(idx).getId(),

app/controllers/QueryController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ public Result postQueryByProject(UUID projectId, Request request) {
6767
}
6868

6969
public Result getQueriesByProject(UUID projectId, Request request) {
70-
PageRequest pageRequest = PageRequest.from(request);
70+
PageRequest<UUID> pageRequest = uuidRequest(request);
7171
List<Query> queries = queryService.getByProjectId(projectId, pageRequest.getAfter(), pageRequest.getBefore(), pageRequest.getSize());
7272
return Optional.of(queries)
7373
.map(collection -> JacksonHelper.collectionToTree(collection, List.class, metamodelProvider.getImplementationClass(Query.class)))
7474
.map(Results::ok)
75-
.map(result -> paginateResult(
75+
.map(result -> uuidResponse(
7676
result,
7777
queries.size(),
7878
idx -> queries.get(idx).getId(),

app/controllers/RelationshipController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public RelationshipController(RelationshipService relationshipService, Metamodel
5353

5454
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
5555
public Result getRelationshipsByProjectIdCommitIdRelatedElementId(UUID projectId, UUID commitId, UUID relatedElementId, Optional<String> direction, Optional<Boolean> excludeUsed, Request request) {
56-
PageRequest pageRequest = PageRequest.from(request);
56+
PageRequest<UUID> pageRequest = uuidRequest(request);
5757
RelationshipDirection relationshipDirection = direction
5858
.flatMap(d -> Arrays.stream(RelationshipDirection.values())
5959
.filter(rd -> rd.toString().equalsIgnoreCase(d))
@@ -74,7 +74,7 @@ public Result getRelationshipsByProjectIdCommitIdRelatedElementId(UUID projectId
7474
}
7575

7676
private Result buildPaginatedResult(List<Relationship> relationships, UUID projectId, UUID commitId, Request request, PageRequest pageRequest) {
77-
return paginateResult(
77+
return uuidResponse(
7878
buildResult(relationships, List.class, metamodelProvider.getImplementationClass(Relationship.class), request, new DataJsonLdAdorner.Parameters(projectId, commitId)),
7979
relationships.size(),
8080
idx -> relationships.get(idx).getElementId(),
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* SysML v2 REST/HTTP Pilot Implementation
3+
* Copyright (C) 2022 Twingineer LLC
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*
18+
* @license LGPL-3.0-or-later <http://spdx.org/licenses/LGPL-3.0-or-later>
19+
*/
20+
21+
package controllers;
22+
23+
import com.fasterxml.jackson.databind.JsonNode;
24+
import com.fasterxml.jackson.databind.node.ObjectNode;
25+
import jackson.jsonld.RecordAdorners.ProjectContainmentParameters;
26+
import org.omg.sysml.lifecycle.Branch;
27+
import play.libs.Json;
28+
import play.mvc.Http.Request;
29+
import play.mvc.Result;
30+
import play.mvc.Results;
31+
import services.SchemaService;
32+
33+
import javax.inject.Inject;
34+
import java.util.*;
35+
36+
public class SchemaController extends BaseController {
37+
38+
private final SchemaService schemaService;
39+
40+
@Inject
41+
public SchemaController(SchemaService schemaService) {
42+
this.schemaService = schemaService;
43+
}
44+
45+
public Result getSchemas(Request request) {
46+
PageRequest<String> pageRequest = stringRequest(request);
47+
List<JsonNode> schemas = schemaService.get(
48+
pageRequest.getAfter(),
49+
pageRequest.getBefore(),
50+
pageRequest.getSize()
51+
);
52+
return stringResponse(
53+
!schemas.isEmpty() ? Results.ok(format(schemas)) : Results.notFound(),
54+
schemas.size(),
55+
idx -> schemas.get(idx).path("$id").asText(null),
56+
request,
57+
pageRequest
58+
);
59+
}
60+
61+
public Result getSchemaById(String schemaId) {
62+
Optional<JsonNode> schema = schemaService.getById(schemaId);
63+
if (schema.isEmpty()) {
64+
return Results.notFound();
65+
}
66+
return Results.ok(schema.get());
67+
}
68+
69+
private JsonNode format(List<JsonNode> schemas) {
70+
ObjectNode object = Json.mapper().createObjectNode();
71+
String schemaValue = schemas.get(0).path("$schema").asText(null);
72+
if (schemaValue != null) {
73+
object.put("$schema", schemaValue);
74+
}
75+
ObjectNode defs = object.putObject("$defs");
76+
for (JsonNode schema : schemas) {
77+
String id = Objects.requireNonNull(schema.path("$id").asText(null));
78+
ObjectNode schemaNode = defs.putObject(id);
79+
Iterator<Map.Entry<String, JsonNode>> fieldIterator = schema.fields();
80+
while (fieldIterator.hasNext()) {
81+
Map.Entry<String, JsonNode> field = fieldIterator.next();
82+
String fieldName = field.getKey();
83+
switch (fieldName) {
84+
case "$schema":
85+
case "$defs":
86+
break;
87+
default:
88+
schemaNode.put(fieldName, field.getValue());
89+
}
90+
}
91+
}
92+
return object;
93+
}
94+
}

app/controllers/TagController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ public Result postTagByProject(UUID projectId, Request request) {
6767
}
6868

6969
public Result getTagsByProject(UUID projectId, Request request) {
70-
PageRequest pageRequest = PageRequest.from(request);
70+
PageRequest<UUID> pageRequest = uuidRequest(request);
7171
List<Tag> tags = tagService.getByProjectId(
7272
projectId,
7373
pageRequest.getAfter(),
7474
pageRequest.getBefore(),
7575
pageRequest.getSize()
7676
);
77-
return paginateResult(
77+
return uuidResponse(
7878
buildResult(tags, List.class, metamodelProvider.getImplementationClass(Tag.class), request, new ProjectContainmentParameters(projectId)),
7979
tags.size(),
8080
idx -> tags.get(idx).getId(),

0 commit comments

Comments
 (0)