Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persistence refactor #235

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ jobs:
command: |
docker create -v /etc/newman --name mms_test_configs alpine:3.4 /bin/true
docker cp example/. mms_test_configs:/etc/newman
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run crud.postman_collection.json -e test-env.json --delay-request 500
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run crud.postman_collection.json -e test-env.json --delay-request 1000
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run cameo.postman_collection.json -e test-env.json --delay-request 1000
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run jupyter.postman_collection.json -e test-env.json --delay-request 500
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run localauth.postman_collection.json -e test-env.json --delay-request 500
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run permissions.postman_collection.json -e test-env.json --delay-request 500
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run permissions.postman_collection.json -e test-env.json --delay-request 1000
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run search.postman_collection.json -e test-env.json --delay-request 1000
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run artifacts.postman_collection.json -e test-env.json --delay-request 500
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run artifacts.postman_collection.json -e test-env.json --delay-request 1000
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run elastic.postman_collection.json -e test-env.json --delay-request 500
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run groups.postman_collection.json -e test-env.json --delay-request 500

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.openmbee.mms.artifacts.crud;

public class ArtifactsContext {
private static ThreadLocal<Boolean> artifactContext = new ThreadLocal<>();

public static void setArtifactContext(Boolean isArtifactContext) {
artifactContext.set(isArtifactContext);
}

public static boolean isArtifactContext() {
return Boolean.TRUE.equals(artifactContext.get());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.openmbee.mms.artifacts.crud;

import org.openmbee.mms.artifacts.json.ArtifactJson;
import org.openmbee.mms.core.services.NodeChangeInfo;
import org.openmbee.mms.crud.domain.NodeUpdateFilter;
import org.openmbee.mms.json.ElementJson;
import org.springframework.stereotype.Component;

@Component
public class ArtifactsPersistenceNodeUpdateFilter implements NodeUpdateFilter {
@Override
public boolean filterUpdate(NodeChangeInfo nodeChangeInfo, ElementJson updated, ElementJson existing) {
if(ArtifactsContext.isArtifactContext()) {
return true;
}
//Ensure artifacts aren't cleared or added by the regular element update process
if(existing.containsKey(ArtifactJson.ARTIFACTS)) {
updated.put(ArtifactJson.ARTIFACTS, existing.get(ArtifactJson.ARTIFACTS));
} else {
updated.remove(ArtifactJson.ARTIFACTS);
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public void acceptHook(Object payload) {
ElementUpdateHook elementUpdateHook = (ElementUpdateHook) payload;
if(elementUpdateHook.getElements() != null) {
//Ignore any artifact changes coming in outside of the Artifacts Controller
elementUpdateHook.getElements().parallelStream().forEach(v -> {
v.remove(ArtifactJson.ARTIFACTS);
});
elementUpdateHook.getElements().parallelStream().forEach(v ->
v.remove(ArtifactJson.ARTIFACTS)
);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package org.openmbee.mms.artifacts.service;

import org.openmbee.mms.artifacts.crud.ArtifactsContext;
import org.openmbee.mms.artifacts.storage.ArtifactStorage;
import org.openmbee.mms.artifacts.ArtifactConstants;
import org.openmbee.mms.artifacts.objects.ArtifactResponse;
import org.openmbee.mms.core.dao.ProjectDAO;
import org.openmbee.mms.core.dao.ProjectPersistence;
import org.openmbee.mms.core.exceptions.BadRequestException;
import org.openmbee.mms.core.exceptions.ConflictException;
import org.openmbee.mms.core.exceptions.NotFoundException;
import org.openmbee.mms.core.objects.ElementsRequest;
import org.openmbee.mms.core.objects.ElementsResponse;
import org.openmbee.mms.core.services.NodeService;
import org.openmbee.mms.crud.services.ServiceFactory;
import org.openmbee.mms.data.domains.global.Project;
import org.openmbee.mms.artifacts.json.ArtifactJson;
import org.openmbee.mms.json.ElementJson;
import org.openmbee.mms.json.ProjectJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
Expand All @@ -30,7 +31,7 @@ public class DefaultArtifactService implements ArtifactService {

private ArtifactStorage artifactStorage;
private ServiceFactory serviceFactory;
private ProjectDAO projectRepository;
private ProjectPersistence projectPersistence;

@Autowired
public void setArtifactStorage(ArtifactStorage artifactStorage) {
Expand All @@ -43,16 +44,19 @@ public void setServiceFactory(ServiceFactory serviceFactory) {
}

@Autowired
public void setProjectRepository(ProjectDAO projectRepository) {
this.projectRepository = projectRepository;
public void setProjectPersistence(ProjectPersistence projectPersistence) {
this.projectPersistence = projectPersistence;
}

@Override
public ArtifactResponse get(String projectId, String refId, String id, Map<String, String> params) {
NodeService nodeService = getNodeService(projectId);
ElementJson elementJson = getElement(nodeService, projectId, refId, id, params);

ArtifactJson artifact = getExistingArtifact(ArtifactJson.getArtifacts(elementJson), params, elementJson);
List<ArtifactJson> artifacts = ArtifactJson.getArtifacts(elementJson);
ArtifactJson artifact = new ArtifactJson();
if (artifacts != null) {
artifact = getExistingArtifact(artifacts, params, elementJson);
}
byte[] data = artifactStorage.get(artifact.getLocation(), elementJson, artifact.getMimeType());
ArtifactResponse response = new ArtifactResponse();
response.setData(data);
Expand Down Expand Up @@ -90,7 +94,12 @@ public ElementsResponse createOrUpdate(String projectId, String refId, String id
elementJson = attachOrUpdateArtifact(elementJson, artifactLocation, fileExtension, mimeType, "internal", checksum);
ElementsRequest elementsRequest = new ElementsRequest();
elementsRequest.setElements(Arrays.asList(elementJson));
return nodeService.createOrUpdate(projectId, refId, elementsRequest, params, user);
try {
ArtifactsContext.setArtifactContext(true);
return nodeService.createOrUpdate(projectId, refId, elementsRequest, params, user);
} finally {
ArtifactsContext.setArtifactContext(false);
}
}

@Override
Expand All @@ -99,20 +108,28 @@ public ElementsResponse disassociate(String projectId, String refId, String id,
ElementJson elementJson = getElement(nodeService, projectId, refId, id, params);

List<ArtifactJson> artifacts = ArtifactJson.getArtifacts(elementJson);
if (artifacts == null) {
throw new NotFoundException("Artifacts not found");
}
ArtifactJson artifact = getExistingArtifact(artifacts, params, elementJson);
artifacts.remove(artifact);
ArtifactJson.setArtifacts(elementJson, artifacts);
ElementsRequest elementsRequest = new ElementsRequest();
elementsRequest.setElements(Arrays.asList(elementJson));
return nodeService.createOrUpdate(projectId, refId, elementsRequest, params, user);
try {
ArtifactsContext.setArtifactContext(true);
return nodeService.createOrUpdate(projectId, refId, elementsRequest, params, user);
} finally {
ArtifactsContext.setArtifactContext(false);
}
}

private ElementJson getElement(NodeService nodeService, String projectId, String refId, String id, Map<String, String> params) {

ElementsResponse elementsResponse = nodeService.read(projectId, refId, id, params);
if(elementsResponse.getElements() == null || elementsResponse.getElements().isEmpty()) {
if (elementsResponse.getElements() == null || elementsResponse.getElements().isEmpty()) {
throw new NotFoundException("Element not found");
} else if(elementsResponse.getElements().size() > 1) {
} else if (elementsResponse.getElements().size() > 1) {
throw new ConflictException("Multiple elements found with id " + id);
} else {
return elementsResponse.getElements().get(0);
Expand All @@ -124,10 +141,18 @@ private ElementJson attachOrUpdateArtifact(ElementJson elementJson, String artif
List<ArtifactJson> artifacts = ArtifactJson.getArtifacts(elementJson);
ArtifactJson artifact;
try {
artifact = getExistingArtifact(artifacts, mimeType, null, elementJson);
//nested try/nullcheck OK?
if (artifacts != null) {
artifact = getExistingArtifact(artifacts, mimeType, null, elementJson);
} else {
throw new NotFoundException("Null artifact exception");
}

} catch(NotFoundException ex) {
artifact = new ArtifactJson();
artifacts.add(artifact);
if (artifacts != null) {
artifacts.add(artifact);
}
}

artifact.setLocation(artifactLocation);
Expand All @@ -145,24 +170,24 @@ private ArtifactJson getExistingArtifact(List<ArtifactJson> artifacts, Map<Strin
}

private ArtifactJson getExistingArtifact(List<ArtifactJson> artifacts, String mimeType, String extension, ElementJson element) {
if(mimeType == null && extension == null) {
if (mimeType == null && extension == null) {
throw new BadRequestException("Missing mimetype or extension");
}
//Element representation is unique by mimeType and extension
Optional<ArtifactJson> existing = artifacts.stream().filter(v -> {
return (mimeType != null && mimeType.equals(v.getMimeType())) || (extension != null && extension.equals(v.getExtension()));
}).findFirst();
if(existing.isPresent()) {
Optional<ArtifactJson> existing = artifacts.stream().filter(v ->
(mimeType != null && mimeType.equals(v.getMimeType())) || (extension != null && extension.equals(v.getExtension()))
).findFirst();
if (existing.isPresent()) {
return existing.get();
}
throw new NotFoundException(element);
}

private String getFileExtension(MultipartFile file) {
String originalFilename = file.getOriginalFilename();
if(originalFilename != null) {
if (originalFilename != null) {
int inx = originalFilename.lastIndexOf('.');
if(inx > 0) {
if (inx > 0) {
return originalFilename.substring(inx + 1);
}
}
Expand Down Expand Up @@ -191,8 +216,8 @@ private String getProjectType(String projectId) {
return getProject(projectId).getProjectType();
}

private Project getProject(String projectId) {
Optional<Project> p = projectRepository.findByProjectId(projectId);
private ProjectJson getProject(String projectId) {
Optional<ProjectJson> p = projectPersistence.findById(projectId);
if (p.isPresent()) {
return p.get();
}
Expand Down
13 changes: 9 additions & 4 deletions authenticator/authenticator.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
dependencies {
implementation project(':rdb')
api project(':core')

api commonDependencies.'spring-security-web'

implementation commonDependencies.'servlet-api'
implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.10.5'
implementation commonDependencies.'jjwt-api'

runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.10.5'
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.10.5'
runtimeOnly commonDependencies.'jjwt-impl'
runtimeOnly commonDependencies.'jjwt-jackson'

testImplementation commonDependencies.'spring-boot-starter-test'
}

tasks {
processResources {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ private Date generateExpirationDate() {

private boolean isTokenExpired(String token) {
final Date expirationDate = getExpirationDateFromToken(token);
return expirationDate.before(new Date());
if (expirationDate != null) {
return expirationDate.before(new Date());
}
return true;
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ ext {
'jackson-databind' : "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion",
'jackson-datatype-hibernate5' : "com.fasterxml.jackson.datatype:jackson-datatype-hibernate5:$jacksonVersion",
'jackson-datatype-jsr310' : "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonVersion",
'jjwt-api' : "io.jsonwebtoken:jjwt-api:$jwtTokenVersion",
'jjwt-impl' : "io.jsonwebtoken:jjwt-impl:$jwtTokenVersion",
'jjwt-jackson' : "io.jsonwebtoken:jjwt-jackson:$jwtTokenVersion",
'spring-boot-starter-test' : 'org.springframework.boot:spring-boot-starter-test:2.2.4.RELEASE'
]
}
Expand Down
1 change: 1 addition & 0 deletions cameo/cameo.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
dependencies {
implementation project(':crud')
implementation project(':view')

testImplementation commonDependencies.'spring-boot-starter-test'
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class CameoConstants {
public static final String CLASSIFIERIDS = "classifierIds";
public static final String CLIENTDEPENDENCYIDS = "clientDependencyIds";
public static final String COLLABORATIONUSEIDS = "collaborationUseIds";
public static final String COMMITID = "commitId";
public static final String DATATYPEID = "datatypeId";
public static final String DEFAULTVALUE = "defaultValue";
public static final String DEFININGFEATUREID = "definingFeatureId";
Expand Down Expand Up @@ -46,12 +47,15 @@ public class CameoConstants {
public static final String NAME = "name";
public static final String NAMEEXPRESSION = "nameExpression";
public static final String NAVIGABLEOWNEDENDIDS = "navigableOwnedEndIds";
public static final String NONE = "none";
public static final String OWNEDATTRIBUTEIDS = "ownedAttributeIds";
public static final String OWNEDENDIDS = "ownedEndIds";
public static final String OWNERID = "ownerId";
public static final String PACKAGEIMPORTIDS = "packageImportIds";
public static final String PACKAGEMERGEIDS = "packageMergeIds";
public static final String PARENTID = "_parentId";
public static final String POWERTYPEEXTENTIDS = "powertypeExtentIds";
public static final String PROPERTY = "Property";
public static final String PROPERTYID = "propertyId";
public static final String PROPERTYTYPE = "propertyType";
public static final String PROFILEAPPLICATIONIDS = "profileApplicationIds";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.openmbee.mms.cameo.services;

import org.openmbee.mms.core.config.ContextHolder;
import org.openmbee.mms.core.services.CommitService;
import org.openmbee.mms.crud.services.DefaultCommitService;
import org.openmbee.mms.data.domains.scoped.Commit;
import org.openmbee.mms.json.CommitJson;
import org.springframework.stereotype.Service;

import java.util.List;
Expand All @@ -12,8 +11,7 @@
public class CameoCommitService extends DefaultCommitService implements CommitService {
@Override
public boolean isProjectNew(String projectId) {
ContextHolder.setContext(projectId);
List<Commit> commits = commitRepository.findAll();
List<CommitJson> commits = commitPersistence.findAllByProjectId(projectId);
return commits == null || commits.size() <= 1; // a cameo project gets 1 auto commit, so its still "new"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import java.util.Set;
import org.openmbee.mms.cameo.CameoConstants;
import org.openmbee.mms.cameo.CameoNodeType;
import org.openmbee.mms.core.utils.ElementUtils;
import org.openmbee.mms.json.ElementJson;
import org.springframework.stereotype.Component;

@Component
public class CameoHelper {
public class CameoHelper implements ElementUtils {

@Override
public CameoNodeType getNodeType(ElementJson e) {
if (isDocument(e)) {
return CameoNodeType.DOCUMENT;
Expand Down
Loading
Loading