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

Adding stored procedures and fixing typo #4

Merged
merged 1 commit into from
Dec 7, 2018
Merged
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
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());