Skip to content

Commit

Permalink
Merge pull request #4 from Open-MBEE/feature/storedProcedures
Browse files Browse the repository at this point in the history
Adding stored procedures and fixing typo
  • Loading branch information
HuiJun authored Dec 7, 2018
2 parents a209788 + 72dd27c commit 815d685
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package org.openmbee.sdvc.crud.config;

import org.apache.http.HttpHost;
import org.openmbee.sdvc.core.config.PersistenceJPAConfig;
import org.openmbee.sdvc.core.domains.Project;
import org.openmbee.sdvc.core.repositories.ProjectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.beans.factory.annotation.Value;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.RestClient;


@Configuration
public class ElasticsearchConfig {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,72 @@ public class DatabaseDefinitionService {
private static final Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}");
private static final String COPY_SQL = "INSERT INTO %s SELECT * FROM %s";
private static final String COPY_IDX = "SELECT SETVAL('%s_id_seq', COALESCE((SELECT MAX(id) FROM %s), 1), true)";

private static final String GET_CHILDREN = "CREATE OR REPLACE FUNCTION get_children(integer, integer, text, integer)\n"
+ " RETURNS TABLE(id bigint)\n"
+ "AS $$\n"
+ " begin\n"
+ " return query\n"
+ " execute '\n"
+ " with recursive children(depth, nid, path, cycle, deleted) as (\n"
+ " select 0 as depth, node.id, ARRAY[node.id], false, node.deleted from ' || format('nodes%s', $3) || '\n"
+ " node where node.id = ' || $1 || ' union\n"
+ " select (c.depth + 1) as depth, edge.child as nid, path || cast(edge.child as bigint) as path, edge.child = ANY(path) as cycle, node.deleted as deleted\n"
+ " from ' || format('edges%s', $3) || ' edge, children c, ' || format('nodes%s', $3) || ' node where edge.parent = nid and node.id = edge.child and node.deleted = false and\n"
+ " edge.edgeType = ' || $2 || ' and not cycle and depth < ' || $4 || '\n"
+ " )\n"
+ " select distinct nid from children;';\n"
+ " end;\n"
+ "$$ LANGUAGE plpgsql;";

private static final String GET_PARENTS = "CREATE OR REPLACE FUNCTION get_parents(integer, integer, text)\n"
+ " RETURNS TABLE(id bigint, height integer, root boolean)\n"
+ "AS $$\n"
+ " begin\n"
+ " return query\n"
+ " execute '\n"
+ " with recursive parents(height, nid, path, cycle) as (\n"
+ " select 0, node.id, ARRAY[node.id], false from ' || format('nodes%s', $3) || ' node where node.id = ' || $1 || '\n"
+ " union\n"
+ " select (c.height + 1), edge.parent, path || cast(edge.parent as bigint),\n"
+ " edge.parent = ANY(path) from ' || format('edges%s', $3) || '\n"
+ " edge, parents c where edge.child = nid and edge.edgeType = ' || $2 || '\n"
+ " and not cycle\n"
+ " )\n"
+ " select nid,height,(not exists (select true from edges where child = nid and edgetype = ' || $2 || '))\n"
+ " from parents order by height desc;';\n"
+ " end;\n"
+ "$$ LANGUAGE plpgsql;";

private static final String GET_DOC_GROUPS = "CREATE OR REPLACE FUNCTION get_group_docs(integer, integer, text, integer, integer, integer)\n"
+ " RETURNS TABLE(id bigint)\n"
+ "AS $$\n"
+ " begin\n"
+ " return query\n"
+ " execute '\n"
+ " with recursive children(depth, nid, path, cycle, deleted, ntype) as (\n"
+ " select 0 as depth, node.id, ARRAY[node.id], false, node.deleted, node.nodetype from ' || format('nodes%s', $3) || '\n"
+ " node where node.id = ' || $1 || ' union\n"
+ " select (c.depth + 1) as depth, edge.child as nid, path || cast(edge.child as bigint) as path, edge.child = ANY(path) as cycle, node.deleted as deleted, node.nodetype as ntype\n"
+ " from ' || format('edges%s', $3) || ' edge, children c, ' || format('nodes%s', $3) || ' node where edge.parent = nid and node.id = edge.child and node.deleted = false and\n"
+ " edge.edgeType = ' || $2 || ' and not cycle and depth < ' || $4 || ' and (node.nodetype <> '|| $5 ||' or nid = ' || $1 || ')\n"
+ " )\n"
+ " select distinct nid from children where ntype = ' || $6 || ';';\n"
+ " end;\n"
+ "$$ LANGUAGE plpgsql;";

private static final String GET_IMMEDIATE_PARENTS = "CREATE OR REPLACE FUNCTION get_immediate_parents(integer, integer, text)\n"
+ " RETURNS TABLE(sysmlid text, elasticid text)\n"
+ "AS $$\n"
+ " begin\n"
+ " return query\n"
+ " execute '\n"
+ " select sysmlid, elasticid from nodes' || $3 || ' where id in\n"
+ " (select id from get_parents(' || $1 || ',' || $2 || ',''' || format('%s',$3) ||\n"
+ " ''') where height = 1);';\n"
+ " end;\n"
+ "$$ LANGUAGE plpgsql;";

protected final Logger logger = LogManager.getLogger(getClass());
private EntityManager entityManager;
private Map<String, DataSource> crudDataSources;
Expand Down Expand Up @@ -96,8 +162,16 @@ public boolean createProjectDatabase(Project project) throws SQLException {
try {
jdbcTemplate.execute(queryString);
created.add("Created Database");

generateProjectSchemaFromModels(project);
created.add("Created Tables");

jdbcTemplate = new JdbcTemplate(crudDataSources.get(project.getProjectId()));
jdbcTemplate.execute(GET_CHILDREN);
jdbcTemplate.execute(GET_PARENTS);
jdbcTemplate.execute(GET_IMMEDIATE_PARENTS);
jdbcTemplate.execute(GET_DOC_GROUPS);

} catch (DataAccessException e) {
if (e.getCause().getLocalizedMessage().toLowerCase().contains("exists")) {
generateProjectSchemaFromModels(project);
Expand Down Expand Up @@ -192,6 +266,7 @@ public void generateProjectSchemaFromModels(Project project) {

ResourceDatabasePopulator populator = new ResourceDatabasePopulator(schemaResource,
new ByteArrayResource(projectData.getBytes()));

populator.execute(ds);
} catch (IOException e) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.openmbee.sdvc.crud.domains.CommitType;
import org.openmbee.sdvc.crud.domains.Node;
import org.openmbee.sdvc.crud.repositories.commit.CommitDAO;
import org.openmbee.sdvc.crud.repositories.commit.CommitElasticDAO;
import org.openmbee.sdvc.crud.repositories.node.NodeDAO;
import org.openmbee.sdvc.crud.repositories.node.NodeElasticDAO;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -26,7 +27,7 @@ public class DefaultNodeService implements NodeService {

protected NodeDAO nodeRepository;
protected CommitDAO commitRepository;
protected CommitElasticDAO commitElacticRepository;
protected CommitElasticDAO commitElasticRepository;
protected NodeElasticDAO nodeElasticRepository;

@Autowired
Expand Down Expand Up @@ -90,7 +91,7 @@ public ElementsResponse post(String projectId, String refId, ElementsRequest req
commit.setTimestamp(Instant.now());

this.commitRepository.save(commit);
this.commitElacticRepository.save();
//this.commitElasticRepository.save();
return response;
}

Expand Down
8 changes: 4 additions & 4 deletions crud/src/main/resources/project-data.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
insert into nodes (id, elasticid, initialcommit, lastcommit, nodetype, sysmlid, deleted)
values (0, 'test', 'test', 'test', 1, '${projectId}', false);
INSERT INTO nodes (id, elasticid, initialcommit, lastcommit, nodetype, sysmlid, deleted)
VALUES (0, 'test', 'test', 'test', 1, '${projectId}', false);

insert into branches (id, branchid, branchname, tag, deleted, timestamp)
values (0, 'master', 'master', false, false, NOW());
INSERT INTO branches (id, branchid, branchname, tag, deleted, timestamp)
VALUES (0, 'master', 'master', false, false, NOW());

0 comments on commit 815d685

Please sign in to comment.