Skip to content

Commit

Permalink
adding todos, adjust methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Lam, Doris T (393A) authored and Lam, Doris T (393A) committed Dec 10, 2018
1 parent b5d9162 commit 0190108
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ public String getError() {
return error;
}

public void setError(String error) {
public ErrorResponse setError(String error) {
this.error = error;
return this;
}

public int getCode() {
return code;
}

public void setCode(int code) {
public ErrorResponse setCode(int code) {
this.code = code;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package org.openmbee.sdvc.crud.controllers.elements;

import java.util.List;
import org.openmbee.sdvc.crud.controllers.BaseJson;
import org.openmbee.sdvc.crud.domains.Node;
import org.openmbee.sdvc.crud.domains.NodeType;

public class ElementJson extends BaseJson {

public static final String INREFIDS = "_inRefIds";

public List<String> getInRefIds() {
return (List<String>) this.get(INREFIDS);
}

public ElementJson setInRefIds(List<String> inRefIds) {
this.put(INREFIDS, inRefIds);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,17 @@ public ResponseEntity<?> handleGet(@PathVariable(required = false) String projec
@PostMapping
public ResponseEntity<? extends BaseResponse> handlePost(
@RequestBody ProjectsRequest projectsPost) {
if (!projectsPost.getProjects().isEmpty()) {
ProjectsResponse response = serviceFactory.getProjectService("cameo")
.post(projectsPost);
return ResponseEntity.ok(response);

if (projectsPost.getProjects().isEmpty()) {
return ResponseEntity.badRequest().body(new ErrorResponse().setError("empty"));
}
ProjectsResponse response = new ProjectsResponse();
for (ProjectJson json: projectsPost.getProjects()) {
//TODO check if already exist, or check project type
ProjectService ps = serviceFactory.getProjectService("cameo");
response.getProjects().add(ps.post(json));
}
logger.debug("Bad Request");
ErrorResponse err = new ErrorResponse();
err.setCode(400);
err.setError("Bad Request");
return ResponseEntity.badRequest().body(err);
return ResponseEntity.ok(response);
}

@DeleteMapping(value = "/{projectId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.openmbee.sdvc.crud.controllers.BaseJson;

public interface BaseElasticDAO {

Map<String, Object> findByElasticId(String elasticId);

List<Map<String, Object>> findByElasticIds(Set<String> elasticIds);

void indexAll(Collection<? extends Map> jsons);
void indexAll(Collection<? extends BaseJson> jsons);

void index(Map<String, Object> json);
void index(BaseJson json);


}
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public List<Map<String, Object>> findByElasticIds(Set<String> elasticIds) {
return maps;
}

public void indexAll(Collection<? extends Map> jsons) {
public void indexAll(Collection<? extends BaseJson> jsons) {

}

public void index(Map<String, Object> json) {
public void index(BaseJson json) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ public Node findBySysmlId(String sysmlId) {
}

public List<Node> findAllBySysmlIds(Collection<String> ids) {
if (ids == null || ids.isEmpty()) {
return new ArrayList<>();
}
String sql = String.format("SELECT * FROM nodes%s WHERE sysmlid IN (%s)",
getSuffix(), "'" + String.join("','", ids) + "'");
return getConnection().query(sql, new NodeRowMapper());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public void generateProjectSchemaFromModels(Project project) {

String projectData = "";
BufferedReader br = null;

//TODO remove use of dummy data file
FileSystemResource schemaResource = new FileSystemResource(tempFile);
ClassPathResource dataResource = new ClassPathResource("project-data.sql");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -74,6 +75,7 @@ public void setNodePostHelper(NodePostHelper nodePostHelper) {
@Override
public ElementsResponse get(String projectId, String refId, String id,
Map<String, String> params) {

DbContextHolder.setContext(projectId, refId);
logger.info("params: " + params);
if (id != null) {
Expand Down Expand Up @@ -112,12 +114,13 @@ public ElementsResponse post(String projectId, String refId, ElementsRequest req

List<Map> rejectedList = new ArrayList<>();
Map<String, ElementJson> responses = new HashMap<>();
Set<String> oldElasticIds = new HashSet<>();
Map<String, Node> toSave = nodePostHelper
.processPostJson(req.getElements(), overwriteJson, now, cmjs, responses, rejectedList,
this);
this, oldElasticIds);

try {
commitChanges(toSave, responses, cmjs, now);
commitChanges(toSave, responses, cmjs, now, oldElasticIds);
} catch (Exception e) {
//TODO db transaction
}
Expand All @@ -128,14 +131,15 @@ public ElementsResponse post(String projectId, String refId, ElementsRequest req
}

protected void commitChanges(Map<String, Node> nodes, Map<String, ElementJson> json,
CommitJson cmjs, Instant now) {
CommitJson cmjs, Instant now, Set<String> oldElasticIds) {
if (!nodes.isEmpty()) {
this.nodeElasticRepository.indexAll(json.values());
this.nodeRepository.saveAll(new ArrayList<>(nodes.values()));

List<Edge> edges = nodePostHelper.getEdgesToSave(nodes, json, this);
this.edgeRepository.saveAll(edges);

//TODO update old elastic ids to remove ref from inRefIds
// DB Commit
Commit commit = new Commit();
commit.setBranchId(cmjs.getRefId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.openmbee.sdvc.core.domains.Project;
import org.openmbee.sdvc.core.repositories.ProjectRepository;
import org.openmbee.sdvc.crud.controllers.projects.ProjectJson;
import org.openmbee.sdvc.crud.controllers.projects.ProjectsRequest;
import org.openmbee.sdvc.crud.controllers.projects.ProjectsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -28,30 +27,26 @@ public void setDatabaseDefinitionService(DatabaseDefinitionService projectOperat
this.projectOperations = projectOperations;
}

public ProjectsResponse post(ProjectsRequest projectsPost) {
logger.info("JSON parsed properly");
ProjectsResponse response = new ProjectsResponse();
for (ProjectJson project : projectsPost.getProjects()) {
logger.info("Saving project: {}", project.getId());
Project proj = new Project();
proj.setProjectId(project.getId());
proj.setProjectName(project.getName());
Project saved = projectRepository.save(proj);

try {
if (projectOperations.createProjectDatabase(proj)) {
response.getProjects().add(project);
}
} catch (SQLException sqlException) {
// Database already exists
response.getProjects().add(project);
} catch (Exception e) {
projectRepository.delete(saved);
logger.error("Couldn't create database: {}", project.getProjectId());
logger.error(e);
public ProjectJson post(ProjectJson project) {
Project proj = new Project();
proj.setProjectId(project.getId());
proj.setProjectName(project.getName());
Project saved = projectRepository.save(proj);

try {
if (projectOperations.createProjectDatabase(proj)) {
//TODO create elastic indexes and mappings
return project;
}
} catch (SQLException sqlException) {
// Database already exists
return project;
} catch (Exception e) {
projectRepository.delete(saved);
logger.error("Couldn't create database: {}", project.getProjectId());
logger.error(e);
}
return response;
return null; //throw exception
}

public ProjectsResponse get(String projectId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ public static void processElementDeleted(ElementJson e, Node n, CommitJson cmjs)
}

public static void processElementAddedOrUpdated(ElementJson e, Node n, CommitJson cmjs) {
e.setProjectId(DbContextHolder.getContext().getProjectId());
e.setRefId(DbContextHolder.getContext().getBranchId());
e.setProjectId(cmjs.getProjectId());
e.setRefId(cmjs.getRefId());
List<String> inRefIds = new ArrayList<>();
inRefIds.add(cmjs.getRefId());
e.setInRefIds(inRefIds);
String elasticId = UUID.randomUUID().toString();
e.setElasticId(elasticId);
e.setCommitId(cmjs.getId());
Expand Down Expand Up @@ -148,14 +151,13 @@ public void setNodeElasticRepository(NodeElasticDAO nodeElasticRepository) {
// create new elastic id for all element json, update modified time, modifier (use dummy for now), set _projectId, _refId, _inRefIds
public Map<String, Node> processPostJson(List<ElementJson> elements, boolean overwriteJson,
Instant now, CommitJson cmjs, Map<String, ElementJson> response, List<Map> rejectedList,
NodeService service) {
NodeService service, Set<String> oldElasticIds) {

Set<String> elasticIds = new HashSet<>();
Map<String, ElementJson> reqElementMap = (Map<String, ElementJson>) Helper
.convertJsonToMap(elements);
List<Node> existingNodes = nodeRepository.findAllBySysmlIds(reqElementMap.keySet());
Map<String, Node> existingNodeMap = new HashMap<>();
Set<String> oldElasticIds = new HashSet<>();
for (Node node : existingNodes) {
logger.info("Got element with id: {}", node.getId());
elasticIds.add(node.getElasticId());
Expand Down Expand Up @@ -235,7 +237,11 @@ public List<Edge> getEdgesToSave(Map<String, Node> nodes, Map<String, ElementJso
NodeService service) {

Set<String> toFind = new HashSet<>();
List<Edge> res = new ArrayList<>();
Map<EdgeType, List<Pair<String, String>>> edges = service.getEdgeInfo(json.values());
if (edges.isEmpty()) {
return res;
}
for (Map.Entry<EdgeType, List<Pair<String, String>>> entry : edges.entrySet()) {
for (Pair<String, String> pair : entry.getValue()) {
toFind.add(pair.getFirst());
Expand All @@ -246,7 +252,7 @@ public List<Edge> getEdgesToSave(Map<String, Node> nodes, Map<String, ElementJso
Map<String, Node> edgeNodes = Helper
.convertNodesToMap(this.nodeRepository.findAllBySysmlIds(toFind));
edgeNodes.putAll(nodes);
List<Edge> res = new ArrayList<>();

for (Map.Entry<EdgeType, List<Pair<String, String>>> entry : edges.entrySet()) {
for (Pair<String, String> pair : entry.getValue()) {
Node parent = edgeNodes.get(pair.getFirst());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.openmbee.sdvc.crud.services;

import org.openmbee.sdvc.crud.controllers.projects.ProjectJson;
import org.openmbee.sdvc.crud.controllers.projects.ProjectsRequest;
import org.openmbee.sdvc.crud.controllers.projects.ProjectsResponse;

public interface ProjectService {

public ProjectsResponse get(String projectId);
ProjectsResponse get(String projectId);

public ProjectsResponse post(ProjectsRequest projectsPost);
ProjectJson post(ProjectJson projectsPost);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.openmbee.sdvc.crud.services;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
Expand All @@ -14,11 +15,25 @@ public void setApplicationContext(ApplicationContext context) {
}

public NodeService getNodeService(String type) {
return (NodeService) context.getBean(type + "NodeService");
try {
NodeService ns = context.getBean(type + "NodeService", NodeService.class);
if (ns != null) {
return ns;
}
} catch (BeansException e) {
}
return context.getBean("defaultNodeService", NodeService.class);
}

public ProjectService getProjectService(String type) {
return (ProjectService) context.getBean(type + "ProjectService");
try {
ProjectService ps = context.getBean(type + "ProjectService", ProjectService.class);
if (ps != null) {
return ps;
}
} catch (BeansException e) {
}
return context.getBean("defaultProjectService", ProjectService.class);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.openmbee.sdvc.sysml.services;

import org.openmbee.sdvc.crud.controllers.projects.ProjectsRequest;
import org.openmbee.sdvc.crud.controllers.projects.ProjectsResponse;
import org.openmbee.sdvc.crud.controllers.projects.ProjectJson;
import org.openmbee.sdvc.crud.services.DefaultProjectService;
import org.openmbee.sdvc.crud.services.ProjectService;
import org.springframework.stereotype.Service;
Expand All @@ -10,8 +9,8 @@
@Service("cameoProjectService")
public class CameoProjectService extends DefaultProjectService implements ProjectService {

public ProjectsResponse post(ProjectsRequest projectsPost) {
//create elastic index with sysml specific mapping
public ProjectJson post(ProjectJson projectsPost) {
//TODO create elastic index with sysml specific mapping
return super.post(projectsPost);
}
}

0 comments on commit 0190108

Please sign in to comment.