From 72dd27ccf4672b93296e147a0fdb844c948cd61f Mon Sep 17 00:00:00 2001 From: Jason Han Date: Thu, 6 Dec 2018 17:06:36 -0800 Subject: [PATCH] Adding stored procedures and fixing typo --- .../sdvc/crud/config/ElasticsearchConfig.java | 7 -- .../services/DatabaseDefinitionService.java | 75 +++++++++++++++++++ .../crud/services/DefaultNodeService.java | 5 +- crud/src/main/resources/project-data.sql | 8 +- 4 files changed, 82 insertions(+), 13 deletions(-) diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/config/ElasticsearchConfig.java b/crud/src/main/java/org/openmbee/sdvc/crud/config/ElasticsearchConfig.java index 125acaa91..60441c685 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/config/ElasticsearchConfig.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/config/ElasticsearchConfig.java @@ -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 { diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/services/DatabaseDefinitionService.java b/crud/src/main/java/org/openmbee/sdvc/crud/services/DatabaseDefinitionService.java index 76938c514..2c095c11a 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/services/DatabaseDefinitionService.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/services/DatabaseDefinitionService.java @@ -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 crudDataSources; @@ -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); @@ -192,6 +266,7 @@ public void generateProjectSchemaFromModels(Project project) { ResourceDatabasePopulator populator = new ResourceDatabasePopulator(schemaResource, new ByteArrayResource(projectData.getBytes())); + populator.execute(ds); } catch (IOException e) { diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/services/DefaultNodeService.java b/crud/src/main/java/org/openmbee/sdvc/crud/services/DefaultNodeService.java index 9e89579cc..2a122cda8 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/services/DefaultNodeService.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/services/DefaultNodeService.java @@ -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; @@ -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 @@ -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; } diff --git a/crud/src/main/resources/project-data.sql b/crud/src/main/resources/project-data.sql index c3bfe8a91..04de9b944 100644 --- a/crud/src/main/resources/project-data.sql +++ b/crud/src/main/resources/project-data.sql @@ -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()); \ No newline at end of file +INSERT INTO branches (id, branchid, branchname, tag, deleted, timestamp) + VALUES (0, 'master', 'master', false, false, NOW());