Skip to content

Commit

Permalink
Merge pull request #3 from Open-MBEE/feature/cameo
Browse files Browse the repository at this point in the history
skeleton for cameo
  • Loading branch information
HuiJun authored Dec 10, 2018
2 parents 815d685 + 0190108 commit 52c06fe
Show file tree
Hide file tree
Showing 32 changed files with 1,140 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class BaseJson extends HashMap<String, Object> {
public static final String MODIFIED = "_modified";
public static final String CREATOR = "_creator";
public static final String CREATED = "_created";
public static final String COMMITID = "_commitId";


public String getId() {
return (String) this.get(ID);
Expand Down Expand Up @@ -68,6 +70,15 @@ public BaseJson setModifier(String modifier) {
return this;
}

public String getModified() {
return (String) this.get(MODIFIED);
}

public BaseJson setModified(String modified) {
this.put(MODIFIED, modified);
return this;
}

public String getCreator() {
return (String) this.get(CREATOR);
}
Expand All @@ -86,12 +97,12 @@ public BaseJson setCreated(String created) {
return this;
}

public String getModified() {
return (String) this.get(MODIFIED);
public String getCommitId() {
return(String) this.get(COMMITID);
}

public BaseJson setModified(String modified) {
this.put(MODIFIED, modified);
public BaseJson setCommitId(String commitId) {
this.put(COMMITID, commitId);
return this;
}
}
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,13 +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;

public class ElementJson extends BaseJson {

public Node toNode() {
Node n = new Node();
n.setSysmlId(getId());
return n;
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 @@ -90,6 +90,6 @@ public ResponseEntity<?> handleBulkDelete(
}

private NodeService getNodeService(String projectId) {
return serviceFactory.getNodeService("sysml");
return serviceFactory.getNodeService("cameo");
}
}
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("sysml")
.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 All @@ -58,6 +59,6 @@ public ResponseEntity<? extends BaseResponse> handleDelete(@PathVariable String
}

private ProjectService getProjectService(String projectId) {
return serviceFactory.getProjectService("sysml");
return serviceFactory.getProjectService("cameo");
}
}
8 changes: 8 additions & 0 deletions crud/src/main/java/org/openmbee/sdvc/crud/domains/Edge.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public class Edge {
@Column(columnDefinition = "smallint")
private EdgeType edgeType;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Node getParent() {
return parent;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package org.openmbee.sdvc.crud.repositories;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.openmbee.sdvc.crud.controllers.BaseJson;

public interface BaseElasticDAO {

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

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

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

void index(BaseJson json);


public List<Map<String, Object>> findByElasticIds(Set<String> elasticIds);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.openmbee.sdvc.crud.repositories;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.openmbee.sdvc.crud.config.DbContextHolder;
import org.openmbee.sdvc.crud.controllers.BaseJson;

public abstract class BaseElasticDAOImpl implements BaseElasticDAO {

Expand All @@ -16,6 +19,35 @@ public Map<String, Object> findByElasticId(String elasticId) {
}

public List<Map<String, Object>> findByElasticIds(Set<String> elasticIds) {
return null;
List<Map<String, Object>> maps = new ArrayList<>();
int i = 97;
for (String eid : elasticIds) {
BaseJson json = new BaseJson();
json.setElasticId(eid);
json.setModified("2018-12-08T01:25:00.117-0700");
json.setId(Character.toString((char) i));
json.setName(json.getId());
maps.add(json);
i++;
}
/*
BaseJson baseJson = new BaseJson();
baseJson.setId("testing");
baseJson.setName("element1");
baseJson.setElasticId("8a1ee2ef-078f-4f3f-ae89-66fb5e9e7bba");
baseJson.setModified("2015-07-04T12:08:56.235-0700");
List<Map<String, Object>> maps = new ArrayList<>();
maps.add(baseJson);
*/
return maps;
}

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

}

public void index(BaseJson json) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,23 @@ public PreparedStatement createPreparedStatement(Connection connection)
return null;//TODO error
}
commit.setId(keyHolder.getKey().longValue());
return commit;//findById(keyHolder.getKey().longValue());
return commit;
}

@SuppressWarnings({"unchecked"})
public Commit findById(long id) {
String sql = "SELECT * FROM commits WHERE id = ?";

return (Commit) getConnection()
.queryForObject(sql, new Object[]{id}, new CommitRowMapper());
}

@SuppressWarnings({"unchecked"})
public Commit findByCommitId(String commitId) {
String sql = "SELECT * FROM commits WHERE elasticId = ?";

return (Commit) getConnection()
.queryForObject(sql, new Object[]{commitId}, new CommitRowMapper());
}

@SuppressWarnings({"unchecked"})
public Commit findByRefAndTimestamp(String refId, Instant timestamp) {
List<Commit> res = findByRefAndTimestampAndLimit(refId, timestamp, 1);
if (res.size() > 0) {
Expand All @@ -77,7 +74,6 @@ public Commit findByRefAndTimestamp(String refId, Instant timestamp) {
return null;
}

@SuppressWarnings({"unchecked"})
public Commit findLatestByRef(String refId) {
List<Commit> res = findByRefAndTimestampAndLimit(refId, null, 1);
if (res.size() > 0) {
Expand All @@ -86,8 +82,6 @@ public Commit findLatestByRef(String refId) {
return null;
}


@SuppressWarnings({"unchecked"})
public List<Commit> findAll() {
String sql = "SELECT * FROM commits ORDER BY timestamp DESC";
return getConnection().query(sql, new CommitRowMapper());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ public interface EdgeDAO {

public Edge save(Edge edge);

public List<Edge> saveAll(List<Edge> nodes);

public List<Edge> insertAll(List<Edge> nodes);

public List<Edge> updateAll(List<Edge> nodes);

public Edge findParents(String child, EdgeType et);

public Edge findChildren(String parent, EdgeType et);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package org.openmbee.sdvc.crud.repositories.edge;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.openmbee.sdvc.crud.domains.Edge;
import org.openmbee.sdvc.crud.domains.EdgeType;
import org.openmbee.sdvc.crud.domains.Node;
import org.openmbee.sdvc.crud.repositories.BaseDAOImpl;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Component;

@Component
public class EdgeDAOImpl extends BaseDAOImpl implements EdgeDAO {

private final String INSERT_SQL = "INSERT INTO edges%s (edgeType, child_id, parent_id) VALUES (?, ?, ?)";
private final String UPDATE_SQL = "UPDATE edges%s SET edgeType = ?, child_id = ?, parent_id = ? WHERE id = ?";

public Edge save(Edge edge) {
String sql = String.format(
"INSERT INTO edges%s (edgeType, child_id, parent_id) VALUES (?, ?, ?)",
getSuffix());
String sql = String.format(INSERT_SQL, getSuffix());

getConnection().update(sql,
edge.getEdgeType(),
Expand All @@ -23,6 +32,73 @@ public Edge save(Edge edge) {
return edge;
}

public List<Edge> saveAll(List<Edge> edges) {
List<Edge> newEdges = new ArrayList<>();
List<Edge> updateEdges = new ArrayList<>();

for (Edge e: edges) {
if (e.getId() == null) {
newEdges.add(e);
} else {
updateEdges.add(e);
}
}

if (!newEdges.isEmpty()) {
insertAll(newEdges);
}

if (!updateEdges.isEmpty()) {
updateAll(updateEdges);
}
return edges;
}

public List<Edge> insertAll(List<Edge> edges) {
try {
//jdbctemplate doesn't have get generated keys for batch, need to use raw jdbc, depends on driver
Connection rawConn = getConnection().getDataSource().getConnection();
PreparedStatement ps = rawConn
.prepareStatement(String.format(INSERT_SQL, getSuffix()), new String[]{"id"});
for (Edge e: edges) {
ps.setInt(1, e.getEdgeType().getId());
ps.setLong(2, e.getChild().getId());
ps.setLong(3, e.getParent().getId());
ps.addBatch();
}
ps.executeBatch();
ResultSet rs = ps.getGeneratedKeys();
int i = 0;
while (rs.next()) {
edges.get(i).setId(rs.getLong(1));
i++;
}
} catch (SQLException e) {
//TODO throw exception to caller
}
return edges;
}

public List<Edge> updateAll(List<Edge> edges) {
String updateSql = String.format(UPDATE_SQL, getSuffix());
getConnection().batchUpdate(updateSql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
Edge e = edges.get(i);
ps.setInt(1, e.getEdgeType().getId());
ps.setLong(2, e.getChild().getId());
ps.setLong(3, e.getParent().getId());
ps.setLong(4, e.getId());
}

@Override
public int getBatchSize() {
return edges.size();
}
});
return edges;
}

@SuppressWarnings({"unchecked"})
public Edge findParents(String sysmlId, EdgeType et) {
String sql = String.format("SELECT * FROM nodes%s WHERE sysmlid = ?",
Expand All @@ -41,9 +117,8 @@ public Edge findChildren(String sysmlId, EdgeType et) {
.queryForObject(sql, new Object[]{sysmlId}, new EdgeRowMapper());
}

@SuppressWarnings({"unchecked"})
public List<Edge> findAll() {
String sql = String.format("SELECT * FROM nodes%s WHERE deleted = false",
String sql = String.format("SELECT * FROM edges%s",
getSuffix());

return getConnection().query(sql, new EdgeRowMapper());
Expand Down
Loading

0 comments on commit 52c06fe

Please sign in to comment.