From 0f06705fee78196a51c745060d5496fd107aa25d Mon Sep 17 00:00:00 2001 From: "Lam, Doris T (393A)" Date: Wed, 19 Dec 2018 18:09:47 -0800 Subject: [PATCH 1/4] refactor elastic impl into module, use optional for single returns, wrap exceptions, some cleanup --- .../elements/ElementsController.java | 2 +- .../elements/ElementsResponse.java | 9 +- .../crud/repositories/BaseElasticDAOImpl.java | 128 ------------ .../sdvc/crud/repositories/ProjectIndex.java | 6 +- .../commit/CommitElasticDAOImpl.java | 106 ---------- .../repositories/commit/CommitIndexDAO.java | 18 +- .../repositories/node/NodeElasticDAOImpl.java | 184 ----------------- .../crud/repositories/node/NodeIndexDAO.java | 22 +- .../sdvc/crud/services/CommitService.java | 46 +++-- .../services/DatabaseDefinitionService.java | 3 +- .../crud/services/DefaultNodeService.java | 12 +- .../sdvc/crud/services/NodeGetHelper.java | 35 +--- .../sdvc/crud/services/NodeGetInfo.java | 18 +- .../sdvc/crud/services/NodeOperation.java | 116 +++++------ .../sdvc/crud/services/NodeService.java | 2 +- elastic/elastic.gradle | 7 + .../sdvc/elastic/BaseElasticDAOImpl.java | 159 +++++++++++++++ .../sdvc/elastic/CommitElasticDAOImpl.java | 116 +++++++++++ .../sdvc/elastic/NodeElasticDAOImpl.java | 190 ++++++++++++++++++ .../sdvc/elastic}/ProjectElasticImpl.java | 30 ++- example/example.gradle | 1 + .../org/openmbee/sdvc/json/CommitJson.java | 2 +- 22 files changed, 624 insertions(+), 588 deletions(-) delete mode 100644 crud/src/main/java/org/openmbee/sdvc/crud/repositories/BaseElasticDAOImpl.java delete mode 100644 crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitElasticDAOImpl.java delete mode 100644 crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeElasticDAOImpl.java create mode 100644 elastic/elastic.gradle create mode 100644 elastic/src/main/java/org/openmbee/sdvc/elastic/BaseElasticDAOImpl.java create mode 100644 elastic/src/main/java/org/openmbee/sdvc/elastic/CommitElasticDAOImpl.java create mode 100644 elastic/src/main/java/org/openmbee/sdvc/elastic/NodeElasticDAOImpl.java rename {crud/src/main/java/org/openmbee/sdvc/crud/repositories => elastic/src/main/java/org/openmbee/sdvc/elastic}/ProjectElasticImpl.java (65%) diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/controllers/elements/ElementsController.java b/crud/src/main/java/org/openmbee/sdvc/crud/controllers/elements/ElementsController.java index 1cf21dd32..5634436de 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/controllers/elements/ElementsController.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/controllers/elements/ElementsController.java @@ -41,7 +41,7 @@ public ResponseEntity handlePost( if (!req.getElements().isEmpty()) { NodeService nodeService = getNodeService(projectId); - ElementsResponse response = nodeService.create(projectId, refId, req, params); + ElementsResponse response = nodeService.createOrUpdate(projectId, refId, req, params); return ResponseEntity.ok(response); } ElementsResponse err = new ElementsResponse(); diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/controllers/elements/ElementsResponse.java b/crud/src/main/java/org/openmbee/sdvc/crud/controllers/elements/ElementsResponse.java index 7169c1396..a92952784 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/controllers/elements/ElementsResponse.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/controllers/elements/ElementsResponse.java @@ -5,18 +5,19 @@ import java.util.Map; import org.openmbee.sdvc.crud.controllers.BaseResponse; import org.openmbee.sdvc.crud.controllers.Constants; +import org.openmbee.sdvc.json.ElementJson; public class ElementsResponse extends BaseResponse { public ElementsResponse() { - this.put(Constants.ELEMENT_KEY, new ArrayList()); + this.put(Constants.ELEMENT_KEY, new ArrayList()); } - public List getElements() { - return (List) this.get(Constants.ELEMENT_KEY); + public List getElements() { + return (List) this.get(Constants.ELEMENT_KEY); } - public void setElements(List elements) { + public void setElements(List elements) { this.put(Constants.ELEMENT_KEY, elements); } diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/BaseElasticDAOImpl.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/BaseElasticDAOImpl.java deleted file mode 100644 index 9f4f401cd..000000000 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/BaseElasticDAOImpl.java +++ /dev/null @@ -1,128 +0,0 @@ -package org.openmbee.sdvc.crud.repositories; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Optional; -import java.util.Set; - -import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.action.bulk.BulkRequest; -import org.elasticsearch.action.delete.DeleteRequest; -import org.elasticsearch.action.delete.DeleteResponse; -import org.elasticsearch.action.get.GetRequest; -import org.elasticsearch.action.get.GetResponse; -import org.elasticsearch.action.get.MultiGetItemResponse; -import org.elasticsearch.action.get.MultiGetRequest; -import org.elasticsearch.action.get.MultiGetResponse; -import org.elasticsearch.action.index.IndexRequest; -import org.elasticsearch.action.index.IndexResponse; -import org.elasticsearch.client.RequestOptions; -import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.search.builder.SearchSourceBuilder; -import org.elasticsearch.search.fetch.subphase.FetchSourceContext; -import org.openmbee.sdvc.crud.config.DbContextHolder; -import org.openmbee.sdvc.json.BaseJson; -import org.elasticsearch.client.RestHighLevelClient; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.beans.factory.annotation.Value; - -public abstract class BaseElasticDAOImpl { - - protected RestHighLevelClient client; - @Value("${elastic.limit.result}") - protected static int resultLimit; - @Value("${elastic.limit.term}") - protected static int termLimit; - protected static int readTimeout = 1000000000; - protected final String type = "_doc"; - // :TODO save, saveAll --> updates have details of upsert method, should break out into helper method for create/update - - @Autowired - public void setRestHighLevelClient(@Qualifier("clientElastic") RestHighLevelClient client) { - this.client = client; - } - - public String getIndex() { - return DbContextHolder.getContext().getIndex(); - } - - public long count() { - // Returns the number of entities available. - return 0; - } - - public void delete(BaseJson json) throws IOException { - // :TODO deletes by entity - } - - public void deleteById(String index, String indexId) throws IOException { - client.delete(new DeleteRequest(index, this.type, indexId), RequestOptions.DEFAULT); - } - - public void deleteAll(String index, Collection jsons) throws IOException { - BulkRequest bulkIndex = new BulkRequest(); - for (BaseJson json : jsons) { - bulkIndex.add(new DeleteRequest(index, this.type, json.getIndexId())); - } - client.bulk(bulkIndex, RequestOptions.DEFAULT); - } - - public boolean existsById(String index, String indexId) throws IOException { - GetRequest getRequest = new GetRequest(index, this.type, indexId); - getRequest.fetchSourceContext(new FetchSourceContext(false)); - getRequest.storedFields("_none_"); - return client.exists(getRequest, RequestOptions.DEFAULT); - } - - public List> findAll() { - //:TODO Returns all instances of the type. Returns all entities. So not the elasticID - return null; - } - - public Map findById(String index, String indexId) throws IOException { - return client.get(new GetRequest(index, null, indexId), RequestOptions.DEFAULT).getSourceAsMap(); - } - - public List> findAllById(String index, Set indexIds) - throws IOException { - List> listOfResponses = new ArrayList<>(); - - if (indexIds.isEmpty()) { - return listOfResponses; - } - MultiGetRequest request = new MultiGetRequest(); - for (String eid : indexIds) { - request.add(index, this.type, eid); - } - MultiGetResponse response = client.mget(request, RequestOptions.DEFAULT); - - for (MultiGetItemResponse res : response.getResponses()) { - GetResponse item = res.getResponse(); - if (item.isExists()) { - listOfResponses.add(item.getSourceAsMap()); - } else { - //:TODO what do we want to do for missing ids? - continue; - } - } - return listOfResponses; - } - - public void indexAll(String index, Collection jsons) throws IOException { - BulkRequest bulkIndex = new BulkRequest(); - for (BaseJson json : jsons) { - bulkIndex.add(new IndexRequest(index, this.type, json.getIndexId()).source(json)); - } - client.bulk(bulkIndex, RequestOptions.DEFAULT); - } - - public void index(String index, BaseJson json) throws IOException { - client.index(new IndexRequest(index, this.type, json.getIndexId()).source(json), RequestOptions.DEFAULT); - } -} diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/ProjectIndex.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/ProjectIndex.java index cc4955838..1a2dcbc55 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/ProjectIndex.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/ProjectIndex.java @@ -1,11 +1,9 @@ package org.openmbee.sdvc.crud.repositories; -import java.io.IOException; - public interface ProjectIndex { - void create(String index) throws IOException; + void create(String projectId); - void delete(String index) throws IOException; + void delete(String projectId); } diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitElasticDAOImpl.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitElasticDAOImpl.java deleted file mode 100644 index 1f41220e9..000000000 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitElasticDAOImpl.java +++ /dev/null @@ -1,106 +0,0 @@ -package org.openmbee.sdvc.crud.repositories.commit; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import org.elasticsearch.action.search.SearchRequest; -import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.client.RequestOptions; -import org.elasticsearch.index.query.IdsQueryBuilder; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.search.SearchHit; -import org.elasticsearch.search.SearchHits; -import org.elasticsearch.search.builder.SearchSourceBuilder; -import org.elasticsearch.search.sort.FieldSortBuilder; -import org.elasticsearch.search.sort.SortOrder; -import org.openmbee.sdvc.crud.config.DbContextHolder; -import org.openmbee.sdvc.crud.repositories.BaseElasticDAOImpl; -import org.openmbee.sdvc.json.BaseJson; -import org.springframework.stereotype.Component; - -@Component -public class CommitElasticDAOImpl extends BaseElasticDAOImpl implements CommitIndexDAO { - - public void indexAll(Collection jsons) throws IOException { - this.indexAll(DbContextHolder.getContext().getProjectId() + "_commit", jsons); - } - - public void index(BaseJson json) throws IOException { - this.index(DbContextHolder.getContext().getProjectId() + "_commit", json); - } - - public Map findById(String indexId) throws IOException { - return this.findById(DbContextHolder.getContext().getProjectId() + "_commit", indexId); - } - - public List> findAllById(Set indexIds) throws IOException { - return this.findAllById(DbContextHolder.getContext().getProjectId() + "_commit", indexIds); - } - - public void deleteById(String indexId) throws IOException { - this.deleteById(DbContextHolder.getContext().getProjectId() + "_commit", indexId); - } - - public void deleteAll(Collection jsons) throws IOException { - this.deleteAll(DbContextHolder.getContext().getProjectId() + "_commit", jsons); - } - - public boolean existsById(String indexId) throws IOException { - return this.existsById(DbContextHolder.getContext().getProjectId() + "_commit", indexId); - } - - /** - * Gets the JSON document of a bool : should commit query result printed as Json looks like: { - * "bool":{"should":[{"term":{"added.id":sysmlid}}, {"term":{"updated.id":sysmlid}}, - * {"term":{"deleted.id":sysmlid}}]} } - * - * @param id the sysmlid to add to the term search - * @return QueryBuilder q - */ - private QueryBuilder getCommitHistoryQuery(String id, Set commitIds) { - QueryBuilder addedQuery = QueryBuilders.termQuery("added.id", id); - QueryBuilder updatedQuery = QueryBuilders.termQuery("updated.id", id); - QueryBuilder deletedQuery = QueryBuilders.termQuery("deleted.id", id); - QueryBuilder query = QueryBuilders.boolQuery().should(addedQuery).should(updatedQuery) - .should(deletedQuery).filter(QueryBuilders.termsQuery("_uid",commitIds)); - // :TODO you may need a minimum should match - return query; - } - - /** - * Returns the commit history of a element (1) - *

Returns a list of commit metadata for the specificed id un-filtered by branch - * (2) - *

- * - * @param nodeId sysmlId (3) - * @return JSONArray array or empty json array - */ - @Override - public List> elementHistory(String index, String nodeId, - Set commitIds) throws IOException { - List> commits = new ArrayList>(); - SearchRequest searchRequest = new SearchRequest(); - QueryBuilder query = getCommitHistoryQuery(nodeId, commitIds); - SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); - sourceBuilder.query(query); - sourceBuilder.size(this.resultLimit); - sourceBuilder.sort(new FieldSortBuilder("created").order(SortOrder.DESC)); - // :TODO check query output, public SearchSourceBuilder postFilter​(QueryBuilder postFilter) - searchRequest.source(sourceBuilder); - SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); - SearchHits hits = searchResponse.getHits(); - SearchHit[] searchHits = hits.getHits(); //can be null - for (SearchHit hit : searchHits) { - Map source = hit.getSourceAsMap();// gets "_source" - commits.add(source); - } - return commits; - } - -} diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitIndexDAO.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitIndexDAO.java index e4b2fc41b..7fc396d15 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitIndexDAO.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitIndexDAO.java @@ -4,25 +4,27 @@ import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import org.openmbee.sdvc.json.BaseJson; +import org.openmbee.sdvc.json.CommitJson; public interface CommitIndexDAO { - void indexAll(Collection jsons) throws IOException; + void indexAll(Collection jsons); - void index(BaseJson json) throws IOException; + void index(BaseJson json); - Map findById(String indexId) throws IOException; + Optional findById(String indexId); - List> findAllById(Set indexIds) throws IOException; + List findAllById(Set indexIds); - void deleteById(String indexId) throws IOException; + void deleteById(String indexId); - void deleteAll(Collection jsons) throws IOException; + void deleteAll(Collection jsons); - boolean existsById(String indexId)throws IOException; + boolean existsById(String indexId); - List> elementHistory(String index, String id, Set commitIds) throws IOException; + List elementHistory(String id, Set commitIds); } diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeElasticDAOImpl.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeElasticDAOImpl.java deleted file mode 100644 index f2791acc9..000000000 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeElasticDAOImpl.java +++ /dev/null @@ -1,184 +0,0 @@ -package org.openmbee.sdvc.crud.repositories.node; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import org.elasticsearch.action.search.ClearScrollRequest; -import org.elasticsearch.action.search.ClearScrollResponse; -import org.elasticsearch.action.search.SearchRequest; -import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.action.search.SearchScrollRequest; -import org.elasticsearch.client.RequestOptions; -import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.search.Scroll; -import org.elasticsearch.search.SearchHit; -import org.elasticsearch.search.SearchHits; -import org.elasticsearch.search.builder.SearchSourceBuilder; -import org.elasticsearch.search.sort.FieldSortBuilder; -import org.elasticsearch.search.sort.SortOrder; -import org.openmbee.sdvc.crud.config.DbContextHolder; -import org.openmbee.sdvc.crud.repositories.BaseElasticDAOImpl; -import org.openmbee.sdvc.json.BaseJson; -import org.springframework.stereotype.Component; - -@Component -public class NodeElasticDAOImpl extends BaseElasticDAOImpl implements NodeIndexDAO { - - public void indexAll(Collection jsons) throws IOException { - this.indexAll(DbContextHolder.getContext().getProjectId() + "_node", jsons); - } - - public void index(BaseJson json) throws IOException { - this.index(DbContextHolder.getContext().getProjectId() + "_node", json); - } - - public Map findById(String indexId) throws IOException { - return this.findById(DbContextHolder.getContext().getProjectId() + "_node", indexId); - } - - public List> findAllById(Set indexIds) throws IOException { - return this.findAllById(DbContextHolder.getContext().getProjectId() + "_node", indexIds); - } - - public void deleteById(String indexId) throws IOException { - this.deleteById(DbContextHolder.getContext().getProjectId() + "_node", indexId); - } - - public void deleteAll(Collection jsons) throws IOException { - this.deleteAll(DbContextHolder.getContext().getProjectId() + "_node", jsons); - } - - public boolean existsById(String indexId) throws IOException { - return this.existsById("projectId_node", indexId); - } - - @Override - public Map getByCommitId(String commitIndexId, String nodeId, String index) - throws IOException { - Map commits; - SearchRequest searchRequest = new SearchRequest(); - // searches the elements for the reference with the current commitId (elasticId) and sysmlid Id - QueryBuilder query = QueryBuilders.boolQuery() - .must(QueryBuilders.termQuery("commitId", commitIndexId)) - .must(QueryBuilders.termQuery("id", nodeId)); - SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); - sourceBuilder.query(query); - SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); - commits = searchResponse.getHits().getAt(0).getSourceAsMap(); - return commits; - } - - @Override - public List> getElementsLessThanOrEqualTimestamp(String nodeId, - String timestamp, List refsCommitIds, - String index) throws IOException { - List> elements = new ArrayList<>(); - final Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1L)); - SearchRequest searchRequest = new SearchRequest(index); - SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); - // Query - QueryBuilder query = QueryBuilders.boolQuery() - .filter(QueryBuilders.termsQuery("_commitId", refsCommitIds)) - .filter(QueryBuilders.termQuery("_uid", nodeId)) - .filter(QueryBuilders.rangeQuery("_modified").lte(timestamp)); - searchSourceBuilder.query(query); - searchSourceBuilder.sort(new FieldSortBuilder("_modified").order(SortOrder.DESC)); - searchSourceBuilder.size(1); - searchSourceBuilder.size(this.resultLimit); - searchRequest.source(searchSourceBuilder); - searchRequest.scroll(scroll); - - SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); - String scrollId = searchResponse.getScrollId(); - SearchHit[] searchHits = searchResponse.getHits().getHits(); - // :TODO going to have to iterate through the inital results I think, have to test. - - while (searchHits != null && searchHits.length > 0) { - SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId); - scrollRequest.scroll(scroll); - searchResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT); - scrollId = searchResponse.getScrollId(); - searchHits = searchResponse.getHits().getHits(); - for (SearchHit hit : searchHits) { - elements.add(hit.getSourceAsMap()); - } - } - - // Clear the scroll value - ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); - clearScrollRequest.addScrollId(scrollId); - ClearScrollResponse clearScrollResponse = client - .clearScroll(clearScrollRequest, RequestOptions.DEFAULT); - boolean succeeded = clearScrollResponse.isSucceeded(); - return elements; - } - // Create filter array -// int count = 0; -// while (count < refsCommitIds.size()) { -// List sub = refsCommitIds.subList(count, Math.min(refsCommitIds.size(), count + termLimit)); -// -// JsonArray filter = new JsonArray(); -// JsonObject filt1 = new JsonObject(); -// JsonObject filtv = new JsonObject(); -// JsonObject filtv1 = new JsonObject(); -// filter.add(filt1); -// filt1.add("range", filtv); -// filtv.add("_modified", filtv1); -// filtv1.addProperty("lte", timestamp); -// JsonObject filt2 = new JsonObject(); -// JsonObject filt2v = new JsonObject(); -// filter.add(filt2); -// filt2.add("terms", filt2v); -// JsonUtil.addStringList(filt2v, Sjm.COMMITID, sub); -// JsonObject filt3 = new JsonObject(); -// JsonObject filt3v = new JsonObject(); -// filter.add(filt3); -// filt3.add("term", filt3v); -// filt3v.addProperty(Sjm.SYSMLID, sysmlId); -// -// // Create sort -// JsonArray sort = new JsonArray(); -// JsonObject modified = new JsonObject(); -// JsonObject modifiedSortOpt = new JsonObject(); -// sort.add(modified); -// modified.add("_modified", modifiedSortOpt); -// modifiedSortOpt.addProperty("order", "desc"); -// -// // Add filter to bool, then bool to query -// JsonObject query = new JsonObject(); -// JsonObject queryv = new JsonObject(); -// JsonObject bool = new JsonObject(); -// query.add("sort", sort); -// query.add("query", queryv); -// queryv.add("bool", bool); -// bool.add("filter", filter); -// // Add size limit -// query.addProperty("size", "1"); -// -// Search search = -// new Search.Builder(query.toString()).addIndex(index.toLowerCase().replaceAll("\\s+", "")).build(); -// SearchResult result; -// try { -// result = client.execute(search); -// -// if (result.getTotal() > 0) { -// JsonArray hits = result.getJsonObject().getAsJsonObject("hits").getAsJsonArray("hits"); -// if (hits.size() > 0) { -// return hits.read(0).getAsJsonObject().getAsJsonObject("_source"); -// } -// } -// } catch (IOException e) { -// logger.error(String.format("%s", LogUtil.getStackTrace(e))); -// } -// count += termLimit; -// } -// return null; -} - diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeIndexDAO.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeIndexDAO.java index b822719cf..8e7d1e006 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeIndexDAO.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeIndexDAO.java @@ -7,27 +7,27 @@ import java.util.Optional; import java.util.Set; import org.openmbee.sdvc.json.BaseJson; +import org.openmbee.sdvc.json.ElementJson; public interface NodeIndexDAO { - void indexAll(Collection jsons) throws IOException; + void indexAll(Collection jsons); - void index(BaseJson json) throws IOException; + void index(BaseJson json); - Map findById(String indexId) throws IOException; + Optional findById(String indexId); - List> findAllById(Set indexIds) throws IOException; + List findAllById(Set indexIds); - void deleteById(String indexId) throws IOException; + void deleteById(String indexId); - void deleteAll(Collection jsons) throws IOException; + void deleteAll(Collection jsons); - boolean existsById(String indexId) throws IOException; + boolean existsById(String indexId); - Map getByCommitId(String commitIndexId, String nodeId, String index) - throws IOException; + Optional getByCommitId(String commitIndexId, String nodeId); - List> getElementsLessThanOrEqualTimestamp(String nodeId, String timestamp, - List refsCommitIds, String index) throws IOException; + Optional getElementLessThanOrEqualTimestamp(String nodeId, String timestamp, + List refsCommitIds); } diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/services/CommitService.java b/crud/src/main/java/org/openmbee/sdvc/crud/services/CommitService.java index cd2297688..cf0ca106c 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/services/CommitService.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/services/CommitService.java @@ -8,6 +8,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import org.openmbee.sdvc.crud.config.DbContextHolder; import org.openmbee.sdvc.json.CommitJson; @@ -69,31 +70,37 @@ public CommitsResponse getRefCommits(String projectId, String refId, public CommitsResponse getCommit(String projectId, String commitId) { DbContextHolder.setContext(projectId); - Map commit = null; - + CommitsResponse res = new CommitsResponse(); try { - - commit = commitIndex.findById(commitId); - + Optional commit = commitIndex.findById(commitId); + if (commit.isPresent()) { + res.getCommits().add(commit.get()); + } else { + res.setCode(404); + } } catch (Exception e) { e.printStackTrace(); - return null; + res.setCode(500); } - List resJson = new ArrayList<>(); - CommitJson c = new CommitJson(); - c.putAll(commit); - resJson.add(c); - CommitsResponse res = new CommitsResponse(); - res.setCommits(resJson); return res; } public CommitsResponse getElementCommits(String projectId, String refId, String elementId, Map params) { DbContextHolder.setContext(projectId); - List refCommits = commitRepository.findByRefAndTimestampAndLimit(refId, null, 0); - //TODO either search elastic using the commit ids or read all elements from elastic first and filter by the ref's commit ids CommitsResponse res = new CommitsResponse(); + try { + List refCommits = commitRepository + .findByRefAndTimestampAndLimit(refId, null, 0); + Set commitIds = new HashSet<>(); + for (Commit commit: refCommits) { + commitIds.add(commit.getIndexId()); + } + res.getCommits().addAll(commitIndex.elementHistory(elementId, commitIds)); + } catch (Exception e) { + e.printStackTrace(); + res.setCode(500); + } return res; } @@ -103,16 +110,13 @@ public CommitsResponse getCommits(String projectId, CommitsRequest req) { for (CommitJson j : req.getCommits()) { ids.add(j.getId()); } + CommitsResponse res = new CommitsResponse(); try { - - List> jsons = commitIndex.findAllById(ids); - //TODO - CommitsResponse res = new CommitsResponse(); - return res; - + res.getCommits().addAll(commitIndex.findAllById(ids)); } catch (Exception e) { e.printStackTrace(); - return null; + res.setCode(500); } + return res; } } 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 f1ad95ca5..500a2e107 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 @@ -208,6 +208,7 @@ public void generateProjectSchemaFromModels(Project project) { metadata.getMetadataBuilder().build()); try (Connection conn = ds.getConnection()) { + /* TODO rethink if project itself should be a node try (PreparedStatement ps = conn.prepareStatement(INITIAL_PROJECT)) { ps.setString(1, project.getProjectId()); ps.setString(2, "test"); @@ -215,7 +216,7 @@ public void generateProjectSchemaFromModels(Project project) { ps.setString(4, "test"); ps.setInt(5, 1); ps.execute(); - } + }*/ try (PreparedStatement ps = conn.prepareStatement(INITIAL_REF)) { ps.execute(); 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 ce11fb4dc..f5755ba44 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 @@ -1,6 +1,5 @@ package org.openmbee.sdvc.crud.services; -import java.io.IOException; import java.time.Instant; import java.util.ArrayList; import java.util.List; @@ -102,13 +101,14 @@ public ElementsResponse read(String projectId, String refId, String id, DbContextHolder.setContext(projectId, refId); ElementsResponse response = new ElementsResponse(); - response.getElements().addAll(nodeGetHelper.processGetAll().values()); + response.getElements().addAll(nodeGetHelper.processGetAll()); return response; } } @Override - public ElementsResponse read(String projectId, String refId, ElementsRequest req, Map params) { + public ElementsResponse read(String projectId, String refId, ElementsRequest req, + Map params) { // params commit it read element at a commit id // find a specific element at a commit @@ -122,13 +122,13 @@ public ElementsResponse read(String projectId, String refId, ElementsRequest req NodeGetInfo info = nodeGetHelper.processGetJson(req.getElements()); ElementsResponse response = new ElementsResponse(); - response.getElements().addAll(info.getExistingElementMap().values()); + response.getElements().addAll(info.getActiveElementMap().values()); response.put("rejected", info.getRejected()); return response; } @Override - public ElementsResponse create(String projectId, String refId, ElementsRequest req, + public ElementsResponse createOrUpdate(String projectId, String refId, ElementsRequest req, Map params) { DbContextHolder.setContext(projectId, refId); @@ -149,7 +149,7 @@ public ElementsResponse create(String projectId, String refId, ElementsRequest r return response; } - protected void commitChanges(NodeChangeInfo info) throws IOException { + protected void commitChanges(NodeChangeInfo info) { Map nodes = info.getToSaveNodeMap(); Map json = info.getUpdatedMap(); CommitJson cmjs = info.getCommitJson(); diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/services/NodeGetHelper.java b/crud/src/main/java/org/openmbee/sdvc/crud/services/NodeGetHelper.java index 61ad33d8e..1f2bbe781 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/services/NodeGetHelper.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/services/NodeGetHelper.java @@ -5,7 +5,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import org.openmbee.sdvc.crud.controllers.BaseController; import org.openmbee.sdvc.crud.domains.Node; import org.openmbee.sdvc.json.ElementJson; import org.springframework.stereotype.Service; @@ -21,7 +20,7 @@ public NodeGetInfo processGetJson(List elements) { continue; } Node node = info.getExistingNodeMap().get(nodeId); - Map indexElement = info.getExistingElementMap().get(nodeId); + ElementJson indexElement = info.getExistingElementMap().get(nodeId); if (node.isDeleted()) { Map reject = new HashMap<>(); reject.put("code", 410); @@ -31,43 +30,17 @@ public NodeGetInfo processGetJson(List elements) { info.getRejected().add(reject); continue; } + info.getActiveElementMap().put(nodeId, indexElement); } return info; } - public Map> processGetAll() { + public List processGetAll() { Set indexIds = new HashSet<>(); List existingNodes = nodeRepository.findAll(); - Map existingNodeMap = new HashMap<>(); for (Node node : existingNodes) { indexIds.add(node.getIndexId()); - existingNodeMap.put(node.getNodeId(), node); } - // bulk read existing elements in elastic - try { - - List> existingElements = nodeIndex.findAllById(indexIds); - Map> existingElementMap = convertToMap(existingElements, ElementJson.ID); - return existingElementMap; - - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - - // TODO: Remove all this - public static Map> convertToMap(List> elements, String key) { - Map> result = new HashMap<>(); - for (Map elem : elements) { - if (elem == null) { - continue; - } - String id = (String) elem.get(key); - if (id != null && !id.equals("")) { - result.put(id, elem); - } - } - return result; + return nodeIndex.findAllById(indexIds); } } diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/services/NodeGetInfo.java b/crud/src/main/java/org/openmbee/sdvc/crud/services/NodeGetInfo.java index a3bb17d0b..73cb91895 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/services/NodeGetInfo.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/services/NodeGetInfo.java @@ -16,9 +16,9 @@ public class NodeGetInfo { // string is node id // map of element json objects - Map> existingElementMap; + Map existingElementMap; - Map Map; + Map activeElementMap; List rejected; @@ -50,12 +50,22 @@ public NodeGetInfo setExistingNodeMap(Map existingNodeMap) { return this; } - public Map> getExistingElementMap() { + public Map getActiveElementMap() { + return activeElementMap; + } + + public NodeGetInfo setActiveElementMap( + Map activeElementMap) { + this.activeElementMap = activeElementMap; + return this; + } + + public Map getExistingElementMap() { return existingElementMap; } public NodeGetInfo setExistingElementMap( - Map> existingElementMap) { + Map existingElementMap) { this.existingElementMap = existingElementMap; return this; } diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/services/NodeOperation.java b/crud/src/main/java/org/openmbee/sdvc/crud/services/NodeOperation.java index 63f150806..6466492af 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/services/NodeOperation.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/services/NodeOperation.java @@ -26,13 +26,12 @@ @Service public class NodeOperation { + protected final Logger logger = LogManager.getLogger(getClass()); protected NodeDAO nodeRepository; protected NodeIndexDAO nodeIndex; - protected DateTimeFormatter formatter = DateTimeFormatter .ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ").withZone( ZoneId.systemDefault()); - protected final Logger logger = LogManager.getLogger(getClass()); @Autowired public void setNodeRepository(NodeDAO nodeRepository) { @@ -54,44 +53,40 @@ public void initCommitJson(CommitJson cmjs, Instant now) { } public NodeChangeInfo initInfo(List elements, CommitJson cmjs) { - try { - Set indexIds = new HashSet<>(); - Map reqElementMap = (Map) convertJsonToMap(elements); - List existingNodes = nodeRepository.findAllByNodeIds(reqElementMap.keySet()); - Map existingNodeMap = new HashMap<>(); - for (Node node : existingNodes) { - indexIds.add(node.getIndexId()); - existingNodeMap.put(node.getNodeId(), node); - } - // bulk read existing elements in elastic - List> existingElements = nodeIndex.findAllById(indexIds); - Map> existingElementMap = convertToMap(existingElements, ElementJson.ID); - Instant now = Instant.now(); - if (cmjs != null) { - initCommitJson(cmjs, now); - } + Set indexIds = new HashSet<>(); + Map reqElementMap = convertJsonToMap(elements); + List existingNodes = nodeRepository.findAllByNodeIds(reqElementMap.keySet()); + Map existingNodeMap = new HashMap<>(); + for (Node node : existingNodes) { + indexIds.add(node.getIndexId()); + existingNodeMap.put(node.getNodeId(), node); + } + // bulk read existing elements in elastic + List existingElements = nodeIndex.findAllById(indexIds); + Map existingElementMap = convertJsonToMap(existingElements); - NodeChangeInfo info = new NodeChangeInfo(); - info.setCommitJson(cmjs); - info.setUpdatedMap(new HashMap<>()); - info.setDeletedMap(new HashMap<>()); - info.setExistingElementMap(existingElementMap); - info.setExistingNodeMap(existingNodeMap); - info.setReqElementMap(reqElementMap); - info.setReqIndexIds(indexIds); - info.setToSaveNodeMap(new HashMap<>()); - info.setRejected(new ArrayList<>()); - info.setNow(now); - info.setOldIndexIds(new HashSet<>()); - info.setEdgesToDelete(new HashMap<>()); - info.setEdgesToSave(new HashMap<>()); - - return info; - } catch (Exception e) { - e.printStackTrace(); - return null; + Instant now = Instant.now(); + if (cmjs != null) { + initCommitJson(cmjs, now); } + + NodeChangeInfo info = new NodeChangeInfo(); + info.setCommitJson(cmjs); + info.setUpdatedMap(new HashMap<>()); + info.setDeletedMap(new HashMap<>()); + info.setExistingElementMap(existingElementMap); + info.setExistingNodeMap(existingNodeMap); + info.setReqElementMap(reqElementMap); + info.setReqIndexIds(indexIds); + info.setToSaveNodeMap(new HashMap<>()); + info.setRejected(new ArrayList<>()); + info.setNow(now); + info.setOldIndexIds(new HashSet<>()); + info.setEdgesToDelete(new HashMap<>()); + info.setEdgesToSave(new HashMap<>()); + info.setActiveElementMap(new HashMap<>()); + return info; } public void processElementAdded(ElementJson e, Node n, CommitJson cmjs) { @@ -189,10 +184,23 @@ public List getEdgesToSave(NodeChangeInfo info) { return res; } - public static Map convertJsonToMap( - List elements) { - Map result = new HashMap<>(); - for (BaseJson elem : elements) { + public boolean existingNodeContainsNodeId(NodeGetInfo info, String nodeId) { + if (!info.getExistingNodeMap().containsKey(nodeId)) { + Map reject = new HashMap<>(); + reject.put("code", 404); + reject.put("message", "not found"); + reject.put("id", nodeId); + info.getRejected().add(reject); + return false; + } + return true; + } + + + public static Map convertJsonToMap( + List elements) { + Map result = new HashMap<>(); + for (ElementJson elem : elements) { if (elem == null) { continue; } @@ -215,30 +223,4 @@ public static Map convertNodesToMap(List nodes) { } return result; } - - public static Map> convertToMap(List> elements, String key) { - Map> result = new HashMap<>(); - for (Map elem : elements) { - if (elem == null) { - continue; - } - String id = (String) elem.get(key); - if (id != null && !id.equals("")) { - result.put(id, elem); - } - } - return result; - } - - public boolean existingNodeContainsNodeId(NodeGetInfo info, String nodeId) { - if (!info.getExistingNodeMap().containsKey(nodeId)) { - Map reject = new HashMap<>(); - reject.put("code", 404); - reject.put("message", "not found"); - reject.put("id", nodeId); - info.getRejected().add(reject); - return false; - } - return true; - } } diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/services/NodeService.java b/crud/src/main/java/org/openmbee/sdvc/crud/services/NodeService.java index bf168924f..fabf15b7b 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/services/NodeService.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/services/NodeService.java @@ -12,7 +12,7 @@ public interface NodeService { ElementsResponse read(String projectId, String refId, ElementsRequest req, Map params); - ElementsResponse create(String projectId, String refId, ElementsRequest req, + ElementsResponse createOrUpdate(String projectId, String refId, ElementsRequest req, Map params); void extraProcessPostedElement(ElementJson element, Node node, NodeChangeInfo info); diff --git a/elastic/elastic.gradle b/elastic/elastic.gradle new file mode 100644 index 000000000..cba324e73 --- /dev/null +++ b/elastic/elastic.gradle @@ -0,0 +1,7 @@ +apply plugin: 'io.spring.convention.spring-module' + +dependencies { + compile project(':crud') + compile 'org.elasticsearch:elasticsearch:6.5.1' + compile 'org.elasticsearch.client:elasticsearch-rest-high-level-client:6.5.1' +} diff --git a/elastic/src/main/java/org/openmbee/sdvc/elastic/BaseElasticDAOImpl.java b/elastic/src/main/java/org/openmbee/sdvc/elastic/BaseElasticDAOImpl.java new file mode 100644 index 000000000..3cdb775c7 --- /dev/null +++ b/elastic/src/main/java/org/openmbee/sdvc/elastic/BaseElasticDAOImpl.java @@ -0,0 +1,159 @@ +package org.openmbee.sdvc.elastic; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import org.elasticsearch.action.bulk.BulkRequest; +import org.elasticsearch.action.delete.DeleteRequest; +import org.elasticsearch.action.get.GetRequest; +import org.elasticsearch.action.get.GetResponse; +import org.elasticsearch.action.get.MultiGetItemResponse; +import org.elasticsearch.action.get.MultiGetRequest; +import org.elasticsearch.action.get.MultiGetResponse; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.search.fetch.subphase.FetchSourceContext; +import org.openmbee.sdvc.crud.config.DbContextHolder; +import org.openmbee.sdvc.json.BaseJson; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; + +public abstract class BaseElasticDAOImpl> { + + @Value("${elastic.limit.result}") + protected static int resultLimit; + @Value("${elastic.limit.term}") + protected static int termLimit; + protected static int readTimeout = 1000000000; + protected final String type = "_doc"; + protected RestHighLevelClient client; + // :TODO save, saveAll --> updates have details of upsert method, should break out into helper method for create/update + + @Autowired + public void setRestHighLevelClient(@Qualifier("clientElastic") RestHighLevelClient client) { + this.client = client; + } + + protected String getIndex() { + return DbContextHolder.getContext().getProjectId().toLowerCase(); + } + + protected abstract E newInstance(); + + public long count() { + // Returns the number of entities available. + return 0; + } + + public void delete(BaseJson json) { + // :TODO deletes by entity + } + + public void deleteById(String index, String indexId) { + try { + client.delete(new DeleteRequest(index, this.type, indexId), RequestOptions.DEFAULT); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public void deleteAll(String index, Collection jsons) { + try { + BulkRequest bulkIndex = new BulkRequest(); + for (BaseJson json : jsons) { + bulkIndex.add(new DeleteRequest(index, this.type, json.getIndexId())); + } + client.bulk(bulkIndex, RequestOptions.DEFAULT); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public boolean existsById(String index, String indexId) { + try { + GetRequest getRequest = new GetRequest(index, this.type, indexId); + getRequest.fetchSourceContext(new FetchSourceContext(false)); + getRequest.storedFields("_none_"); + return client.exists(getRequest, RequestOptions.DEFAULT); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public List findAll() { + //:TODO Returns all instances of the type. Returns all entities. So not the elasticID + return null; + } + + public Optional findById(String index, String indexId) { + try { + GetResponse res = client + .get(new GetRequest(index, type, indexId), RequestOptions.DEFAULT); + if (res.isExists()) { + E ob = newInstance(); + ob.putAll(res.getSourceAsMap()); + return Optional.of(ob); + } else { + return Optional.empty(); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public List findAllById(String index, Set indexIds) { + try { + List listOfResponses = new ArrayList<>(); + + if (indexIds.isEmpty()) { + return listOfResponses; + } + MultiGetRequest request = new MultiGetRequest(); + for (String eid : indexIds) { + request.add(index, this.type, eid); + } + MultiGetResponse response = client.mget(request, RequestOptions.DEFAULT); + + for (MultiGetItemResponse res : response.getResponses()) { + GetResponse item = res.getResponse(); + if (item.isExists()) { + E ob = newInstance(); + ob.putAll(item.getSourceAsMap()); + listOfResponses.add(ob); + } else { + continue; + } + } + return listOfResponses; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public void indexAll(String index, Collection jsons) { + try { + BulkRequest bulkIndex = new BulkRequest(); + for (BaseJson json : jsons) { + bulkIndex.add(new IndexRequest(index, this.type, json.getIndexId()).source(json)); + } + client.bulk(bulkIndex, RequestOptions.DEFAULT); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public void index(String index, BaseJson json) { + try { + client.index(new IndexRequest(index, this.type, json.getIndexId()).source(json), + RequestOptions.DEFAULT); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/elastic/src/main/java/org/openmbee/sdvc/elastic/CommitElasticDAOImpl.java b/elastic/src/main/java/org/openmbee/sdvc/elastic/CommitElasticDAOImpl.java new file mode 100644 index 000000000..fa0581788 --- /dev/null +++ b/elastic/src/main/java/org/openmbee/sdvc/elastic/CommitElasticDAOImpl.java @@ -0,0 +1,116 @@ +package org.openmbee.sdvc.elastic; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHits; +import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.search.sort.FieldSortBuilder; +import org.elasticsearch.search.sort.SortOrder; +import org.openmbee.sdvc.crud.repositories.commit.CommitIndexDAO; +import org.openmbee.sdvc.json.BaseJson; +import org.openmbee.sdvc.json.CommitJson; +import org.springframework.stereotype.Component; + +@Component +public class CommitElasticDAOImpl extends BaseElasticDAOImpl implements CommitIndexDAO { + + protected CommitJson newInstance() { + return new CommitJson(); + } + + public void indexAll(Collection jsons) { + this.indexAll(getIndex() + "_commit", jsons); + } + + public void index(BaseJson json) { + this.index(getIndex() + "_commit", json); + } + + public Optional findById(String indexId) { + return this.findById(getIndex() + "_commit", indexId); + } + + public List findAllById(Set indexIds) { + return this.findAllById(getIndex() + "_commit", indexIds); + } + + public void deleteById(String indexId) { + this.deleteById(getIndex() + "_commit", indexId); + } + + public void deleteAll(Collection jsons) { + this.deleteAll(getIndex() + "_commit", jsons); + } + + public boolean existsById(String indexId) { + return this.existsById(getIndex() + "_commit", indexId); + } + + /** + * Gets the JSON document of a bool : should commit query result printed as Json looks like: { + * "bool":{"should":[{"term":{"added.id":sysmlid}}, {"term":{"updated.id":sysmlid}}, + * {"term":{"deleted.id":sysmlid}}]} } + * + * @param id the sysmlid to add to the term search + * @return QueryBuilder q + */ + private QueryBuilder getCommitHistoryQuery(String id, Set commitIds) { + QueryBuilder addedQuery = QueryBuilders.termQuery("added.id", id); + QueryBuilder updatedQuery = QueryBuilders.termQuery("updated.id", id); + QueryBuilder deletedQuery = QueryBuilders.termQuery("deleted.id", id); + QueryBuilder query = QueryBuilders.boolQuery() + .should(addedQuery) + .should(updatedQuery) + .should(deletedQuery) + .filter(QueryBuilders.termsQuery("id", commitIds)) + .minimumShouldMatch(1); + return query; + } + + /** + * Returns the commit history of a element (1) + *

Returns a list of commit metadata for the specificed id un-filtered by branch + * (2) + *

+ * + * @param nodeId sysmlId (3) + * @return JSONArray array or empty json array + */ + @Override + public List elementHistory(String nodeId, Set commitIds) { + try { + List commits = new ArrayList<>(); + SearchRequest searchRequest = new SearchRequest(getIndex() + "_commit"); + QueryBuilder query = getCommitHistoryQuery(nodeId, commitIds); + SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); + sourceBuilder.query(query); + //sourceBuilder.size(this.resultLimit); //this caused nothing to return + sourceBuilder.sort(new FieldSortBuilder("_created").order(SortOrder.DESC)); + // :TODO check query output, public SearchSourceBuilder postFilter​(QueryBuilder postFilter) + searchRequest.source(sourceBuilder); + SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); + SearchHits hits = searchResponse.getHits(); + SearchHit[] searchHits = hits.getHits(); //can be null + for (SearchHit hit : searchHits) { + Map source = hit.getSourceAsMap();// gets "_source" + CommitJson ob = newInstance(); + ob.putAll(source); + commits.add(ob); + } + return commits; + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/elastic/src/main/java/org/openmbee/sdvc/elastic/NodeElasticDAOImpl.java b/elastic/src/main/java/org/openmbee/sdvc/elastic/NodeElasticDAOImpl.java new file mode 100644 index 000000000..7e9b1e40e --- /dev/null +++ b/elastic/src/main/java/org/openmbee/sdvc/elastic/NodeElasticDAOImpl.java @@ -0,0 +1,190 @@ +package org.openmbee.sdvc.elastic; + +import java.io.IOException; +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.search.Scroll; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.search.sort.FieldSortBuilder; +import org.elasticsearch.search.sort.SortOrder; +import org.openmbee.sdvc.crud.repositories.node.NodeIndexDAO; +import org.openmbee.sdvc.json.BaseJson; +import org.openmbee.sdvc.json.ElementJson; +import org.springframework.stereotype.Component; + +@Component +public class NodeElasticDAOImpl extends BaseElasticDAOImpl implements NodeIndexDAO { + + protected ElementJson newInstance() { + return new ElementJson(); + } + + public void indexAll(Collection jsons) { + this.indexAll(getIndex() + "_node", jsons); + } + + public void index(BaseJson json) { + this.index(getIndex() + "_node", json); + } + + public Optional findById(String indexId) { + return this.findById(getIndex() + "_node", indexId); + } + + public List findAllById(Set indexIds) { + return this.findAllById(getIndex() + "_node", indexIds); + } + + public void deleteById(String indexId) { + this.deleteById(getIndex() + "_node", indexId); + } + + public void deleteAll(Collection jsons) { + this.deleteAll(getIndex() + "_node", jsons); + } + + public boolean existsById(String indexId) { + return this.existsById(getIndex() + "_node", indexId); + } + + @Override + public Optional getByCommitId(String commitIndexId, String nodeId) { + try { + SearchRequest searchRequest = new SearchRequest(getIndex() + "_node"); + // searches the elements for the reference with the current commitId (elasticId) and sysmlid Id + QueryBuilder query = QueryBuilders.boolQuery() + .must(QueryBuilders.termQuery("_commitId", commitIndexId)) + .must(QueryBuilders.termQuery("id", nodeId)); + SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); + sourceBuilder.query(query); + SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); + if (searchResponse.getHits().getTotalHits() == 0) { + return Optional.empty(); + } + ElementJson ob = newInstance(); + ob.putAll(searchResponse.getHits().getAt(0).getSourceAsMap()); + return Optional.of(ob); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public Optional getElementLessThanOrEqualTimestamp(String nodeId, + String timestamp, List refsCommitIds) { + try { + final Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1L)); + SearchRequest searchRequest = new SearchRequest(getIndex() + "_node"); + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); + // Query + QueryBuilder query = QueryBuilders.boolQuery() + .filter(QueryBuilders.termsQuery("_commitId", refsCommitIds)) + .filter(QueryBuilders.termQuery("id", nodeId)) + .filter(QueryBuilders.rangeQuery("_modified").lte(timestamp)); + searchSourceBuilder.query(query); + searchSourceBuilder.sort(new FieldSortBuilder("_modified").order(SortOrder.DESC)); + searchSourceBuilder.size(1); + //searchSourceBuilder.size(this.resultLimit); + searchRequest.source(searchSourceBuilder); + //searchRequest.scroll(scroll); + + SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); + //String scrollId = searchResponse.getScrollId(); + SearchHit[] searchHits = searchResponse.getHits().getHits(); + // :TODO going to have to iterate through the inital results I think, have to test. + // TODO This only returns one element, no need for scroll + /* + while (searchHits != null && searchHits.length > 0) { + SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId); + scrollRequest.scroll(scroll); + searchResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT); + scrollId = searchResponse.getScrollId(); + searchHits = searchResponse.getHits().getHits(); + for (SearchHit hit : searchHits) { + elements.add(hit.getSourceAsMap()); + } + } + + // Clear the scroll value + ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); + clearScrollRequest.addScrollId(scrollId); + ClearScrollResponse clearScrollResponse = client + .clearScroll(clearScrollRequest, RequestOptions.DEFAULT); + boolean succeeded = clearScrollResponse.isSucceeded();*/ + return Optional.empty(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + // Create filter array +// int count = 0; +// while (count < refsCommitIds.size()) { +// List sub = refsCommitIds.subList(count, Math.min(refsCommitIds.size(), count + termLimit)); +// +// JsonArray filter = new JsonArray(); +// JsonObject filt1 = new JsonObject(); +// JsonObject filtv = new JsonObject(); +// JsonObject filtv1 = new JsonObject(); +// filter.add(filt1); +// filt1.add("range", filtv); +// filtv.add("_modified", filtv1); +// filtv1.addProperty("lte", timestamp); +// JsonObject filt2 = new JsonObject(); +// JsonObject filt2v = new JsonObject(); +// filter.add(filt2); +// filt2.add("terms", filt2v); +// JsonUtil.addStringList(filt2v, Sjm.COMMITID, sub); +// JsonObject filt3 = new JsonObject(); +// JsonObject filt3v = new JsonObject(); +// filter.add(filt3); +// filt3.add("term", filt3v); +// filt3v.addProperty(Sjm.SYSMLID, sysmlId); +// +// // Create sort +// JsonArray sort = new JsonArray(); +// JsonObject modified = new JsonObject(); +// JsonObject modifiedSortOpt = new JsonObject(); +// sort.add(modified); +// modified.add("_modified", modifiedSortOpt); +// modifiedSortOpt.addProperty("order", "desc"); +// +// // Add filter to bool, then bool to query +// JsonObject query = new JsonObject(); +// JsonObject queryv = new JsonObject(); +// JsonObject bool = new JsonObject(); +// query.add("sort", sort); +// query.add("query", queryv); +// queryv.add("bool", bool); +// bool.add("filter", filter); +// // Add size limit +// query.addProperty("size", "1"); +// +// Search search = +// new Search.Builder(query.toString()).addIndex(index.toLowerCase().replaceAll("\\s+", "")).build(); +// SearchResult result; +// try { +// result = client.execute(search); +// +// if (result.getTotal() > 0) { +// JsonArray hits = result.getJsonObject().getAsJsonObject("hits").getAsJsonArray("hits"); +// if (hits.size() > 0) { +// return hits.read(0).getAsJsonObject().getAsJsonObject("_source"); +// } +// } +// } catch (IOException e) { +// logger.error(String.format("%s", LogUtil.getStackTrace(e))); +// } +// count += termLimit; +// } +// return null; +} + diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/ProjectElasticImpl.java b/elastic/src/main/java/org/openmbee/sdvc/elastic/ProjectElasticImpl.java similarity index 65% rename from crud/src/main/java/org/openmbee/sdvc/crud/repositories/ProjectElasticImpl.java rename to elastic/src/main/java/org/openmbee/sdvc/elastic/ProjectElasticImpl.java index 3b69fec03..5f908ebd6 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/ProjectElasticImpl.java +++ b/elastic/src/main/java/org/openmbee/sdvc/elastic/ProjectElasticImpl.java @@ -1,4 +1,4 @@ -package org.openmbee.sdvc.crud.repositories; +package org.openmbee.sdvc.elastic; import java.io.IOException; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; @@ -6,6 +6,7 @@ import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; +import org.openmbee.sdvc.crud.repositories.ProjectIndex; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @@ -21,12 +22,17 @@ public void setRestHighLevelClient(@Qualifier("clientElastic") RestHighLevelClie } @Override - public void create(String index) throws IOException { - CreateIndexRequest commitIndex = new CreateIndexRequest(index + "_commit"); - commitIndex.mapping("_doc", getCommitMapAsString(), XContentType.JSON); - CreateIndexRequest nodeIndex = new CreateIndexRequest(index + "_node"); - createIndex(commitIndex); - createIndex(nodeIndex); + public void create(String index) { + try { + CreateIndexRequest commitIndex = new CreateIndexRequest(index + "_commit"); + commitIndex.mapping("_doc", getCommitMapAsString(), XContentType.JSON); + CreateIndexRequest nodeIndex = new CreateIndexRequest(index + "_node"); + //TODO add mapping function for node + createIndex(commitIndex); + createIndex(nodeIndex); + } catch (IOException e) { + throw new RuntimeException(e); + } } private void createIndex(CreateIndexRequest request) throws IOException { @@ -34,9 +40,13 @@ private void createIndex(CreateIndexRequest request) throws IOException { } @Override - public void delete(String index) throws IOException { - DeleteIndexRequest request = new DeleteIndexRequest(index); - client.indices().delete(request, RequestOptions.DEFAULT).isAcknowledged(); + public void delete(String index) { + try { + DeleteIndexRequest request = new DeleteIndexRequest(index); + client.indices().delete(request, RequestOptions.DEFAULT).isAcknowledged(); + } catch (IOException e) { + throw new RuntimeException(e); + } } private String getCommitMapAsString() { diff --git a/example/example.gradle b/example/example.gradle index cd0f8fa05..921471797 100644 --- a/example/example.gradle +++ b/example/example.gradle @@ -19,6 +19,7 @@ dependencies { compile( project(':ldap'), project(':cameo'), + project(':elastic'), 'org.springframework.boot:spring-boot-starter-web', 'org.postgresql:postgresql:42.2.5', 'io.springfox:springfox-swagger2:2.7.0', diff --git a/json/src/main/java/org/openmbee/sdvc/json/CommitJson.java b/json/src/main/java/org/openmbee/sdvc/json/CommitJson.java index 8050968bd..3f6bbf956 100644 --- a/json/src/main/java/org/openmbee/sdvc/json/CommitJson.java +++ b/json/src/main/java/org/openmbee/sdvc/json/CommitJson.java @@ -9,7 +9,7 @@ public class CommitJson extends BaseJson { public static final String ADDED = "added"; public static final String DELETED = "deleted"; public static final String UPDATED = "updated"; - public static final String PREVIOUS = "previousElasticId"; + public static final String PREVIOUS = "previousIndexId"; public static final String TYPE = "type"; public static final String SOURCE = "source"; From e5a531ab8d94dce87f7b04dec0639b9ae7fb36b5 Mon Sep 17 00:00:00 2001 From: Jason Han Date: Wed, 19 Dec 2018 18:48:48 -0800 Subject: [PATCH 2/4] WIP: return Optional in respositories. Want to go home commit --- .../repositories/OrganizationRepository.java | 5 ++-- .../core/repositories/ProjectRepository.java | 5 ++-- .../core/repositories/RoleRepository.java | 3 +- .../core/repositories/UserRepository.java | 5 ++-- .../crud/repositories/branch/BranchDAO.java | 7 +++-- .../repositories/branch/BranchDAOImpl.java | 13 +++++---- .../crud/repositories/commit/CommitDAO.java | 11 +++---- .../repositories/commit/CommitDAOImpl.java | 29 ++++++++++--------- .../sdvc/crud/repositories/edge/EdgeDAO.java | 8 ++--- .../crud/repositories/edge/EdgeDAOImpl.java | 16 +++++----- .../sdvc/crud/repositories/node/NodeDAO.java | 7 +++-- .../crud/repositories/node/NodeDAOImpl.java | 14 +++++---- 12 files changed, 66 insertions(+), 57 deletions(-) diff --git a/core/src/main/java/org/openmbee/sdvc/core/repositories/OrganizationRepository.java b/core/src/main/java/org/openmbee/sdvc/core/repositories/OrganizationRepository.java index 9007f582d..31bc86895 100644 --- a/core/src/main/java/org/openmbee/sdvc/core/repositories/OrganizationRepository.java +++ b/core/src/main/java/org/openmbee/sdvc/core/repositories/OrganizationRepository.java @@ -1,5 +1,6 @@ package org.openmbee.sdvc.core.repositories; +import java.util.Optional; import org.openmbee.sdvc.core.domains.Organization; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -7,8 +8,8 @@ @Repository public interface OrganizationRepository extends JpaRepository { - Organization findByOrganizationId(String id); + Optional findByOrganizationId(String id); - Organization findByOrganizationName(String name); + Optional findByOrganizationName(String name); } diff --git a/core/src/main/java/org/openmbee/sdvc/core/repositories/ProjectRepository.java b/core/src/main/java/org/openmbee/sdvc/core/repositories/ProjectRepository.java index 9cc65cc57..ab17b219a 100644 --- a/core/src/main/java/org/openmbee/sdvc/core/repositories/ProjectRepository.java +++ b/core/src/main/java/org/openmbee/sdvc/core/repositories/ProjectRepository.java @@ -1,5 +1,6 @@ package org.openmbee.sdvc.core.repositories; +import java.util.Optional; import org.openmbee.sdvc.core.domains.Project; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -7,8 +8,8 @@ @Repository public interface ProjectRepository extends JpaRepository { - Project findByProjectId(String id); + Optional findByProjectId(String id); - Project findByProjectName(String name); + Optional findByProjectName(String name); } diff --git a/core/src/main/java/org/openmbee/sdvc/core/repositories/RoleRepository.java b/core/src/main/java/org/openmbee/sdvc/core/repositories/RoleRepository.java index ef37ce808..17c15744b 100644 --- a/core/src/main/java/org/openmbee/sdvc/core/repositories/RoleRepository.java +++ b/core/src/main/java/org/openmbee/sdvc/core/repositories/RoleRepository.java @@ -1,5 +1,6 @@ package org.openmbee.sdvc.core.repositories; +import java.util.Optional; import org.openmbee.sdvc.core.domains.Role; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -7,6 +8,6 @@ @Repository public interface RoleRepository extends JpaRepository { - Role findByName(String name); + Optional findByName(String name); } diff --git a/core/src/main/java/org/openmbee/sdvc/core/repositories/UserRepository.java b/core/src/main/java/org/openmbee/sdvc/core/repositories/UserRepository.java index f8e0c5a04..d148a0b87 100644 --- a/core/src/main/java/org/openmbee/sdvc/core/repositories/UserRepository.java +++ b/core/src/main/java/org/openmbee/sdvc/core/repositories/UserRepository.java @@ -1,5 +1,6 @@ package org.openmbee.sdvc.core.repositories; +import java.util.Optional; import org.openmbee.sdvc.core.domains.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -7,8 +8,8 @@ @Repository public interface UserRepository extends JpaRepository { - User findByEmail(String email); + Optional findByEmail(String email); - User findByUsername(String username); + Optional findByUsername(String username); } diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAO.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAO.java index 73875ac5b..8f269b794 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAO.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAO.java @@ -1,15 +1,16 @@ package org.openmbee.sdvc.crud.repositories.branch; import java.util.List; +import java.util.Optional; import org.openmbee.sdvc.crud.domains.Branch; public interface BranchDAO { - public Branch save(Branch branch); + public Optional save(Branch branch); - public Branch findById(long id); + public Optional findById(long id); - public Branch findByBranchId(String sysmlid); + public Optional findByBranchId(String sysmlid); public List findAll(); diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAOImpl.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAOImpl.java index 7d25a334a..4652e874c 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAOImpl.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAOImpl.java @@ -5,6 +5,7 @@ import java.sql.SQLException; import java.sql.Timestamp; import java.util.List; +import java.util.Optional; import org.openmbee.sdvc.crud.domains.Branch; import org.openmbee.sdvc.crud.repositories.BaseDAOImpl; import org.springframework.jdbc.core.PreparedStatementCreator; @@ -22,7 +23,7 @@ public void setCommitRepository(CommitDAO commitRepository) { this.commitRepository = commitRepository; } */ - public Branch save(Branch branch) { + public Optional save(Branch branch) { String sql = "INSERT INTO branches (description, branchId, branchName, parentRefId, parentCommit, timestamp, tag, deleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; /* if (branch.getParentRefId() != null && branch.getParentRef() == null) { @@ -63,22 +64,22 @@ public PreparedStatement createPreparedStatement(Connection connection) return null; } branch.setId(keyHolder.getKey().longValue()); - return branch; + return Optional.of(branch); } @SuppressWarnings({"unchecked"}) - public Branch findById(long id) { + public Optional findById(long id) { String sql = "SELECT * FROM branches WHERE id = ?"; - return (Branch) getConnection() + return (Optional) getConnection() .queryForObject(sql, new Object[]{id}, new BranchRowMapper()); } @SuppressWarnings({"unchecked"}) - public Branch findByBranchId(String branchId) { + public Optional findByBranchId(String branchId) { String sql = "SELECT * FROM branches WHERE branchId = ?"; - return (Branch) getConnection() + return (Optional) getConnection() .queryForObject(sql, new Object[]{branchId}, new BranchRowMapper()); } diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAO.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAO.java index 5ac34329b..9c4ee62ae 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAO.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAO.java @@ -2,19 +2,20 @@ import java.time.Instant; import java.util.List; +import java.util.Optional; import org.openmbee.sdvc.crud.domains.Commit; public interface CommitDAO { - public Commit save(Commit commit); + public Optional save(Commit commit); - public Commit findById(long id); + public Optional findById(long id); - public Commit findByCommitId(String commitId); + public Optional findByCommitId(String commitId); - public Commit findByRefAndTimestamp(String refId, Instant timestamp); + public Optional findByRefAndTimestamp(String refId, Instant timestamp); - public Commit findLatestByRef(String refId); + public Optional findLatestByRef(String refId); public List findAll(); diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAOImpl.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAOImpl.java index 4c491ed54..fe47c7121 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAOImpl.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAOImpl.java @@ -7,6 +7,7 @@ import java.time.Instant; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import org.openmbee.sdvc.crud.domains.Branch; import org.openmbee.sdvc.crud.domains.Commit; import org.openmbee.sdvc.crud.repositories.BaseDAOImpl; @@ -27,7 +28,7 @@ public void setBranchRepository(BranchDAO branchRepository) { this.branchRepository = branchRepository; } - public Commit save(Commit commit) { + public Optional save(Commit commit) { String sql = "INSERT INTO commits (commitType, creator, indexId, branchId, timestamp, comment) VALUES (?, ?, ?, ?, ?, ?)"; KeyHolder keyHolder = new GeneratedKeyHolder(); @@ -50,37 +51,37 @@ public PreparedStatement createPreparedStatement(Connection connection) return null;//TODO error } commit.setId(keyHolder.getKey().longValue()); - return commit; + return Optional.of(commit); } - public Commit findById(long id) { + public Optional findById(long id) { String sql = "SELECT * FROM commits WHERE id = ?"; - return (Commit) getConnection() + return (Optional) getConnection() .queryForObject(sql, new Object[]{id}, new CommitRowMapper()); } - public Commit findByCommitId(String commitId) { + public Optional findByCommitId(String commitId) { String sql = "SELECT * FROM commits WHERE indexid = ?"; - return (Commit) getConnection() + return (Optional) getConnection() .queryForObject(sql, new Object[]{commitId}, new CommitRowMapper()); } - public Commit findByRefAndTimestamp(String refId, Instant timestamp) { + public Optional findByRefAndTimestamp(String refId, Instant timestamp) { List res = findByRefAndTimestampAndLimit(refId, timestamp, 1); - if (res.size() > 0) { - return res.get(0); + if (!res.isEmpty()) { + return Optional.of(res.get(0)); } - return null; + return Optional.empty(); } - public Commit findLatestByRef(String refId) { + public Optional findLatestByRef(String refId) { List res = findByRefAndTimestampAndLimit(refId, null, 1); - if (res.size() > 0) { - return res.get(0); + if (!res.isEmpty()) { + return Optional.of(res.get(0)); } - return null; + return Optional.empty(); } public List findAll() { diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAO.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAO.java index 25bc5eb83..d26911496 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAO.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAO.java @@ -1,12 +1,12 @@ package org.openmbee.sdvc.crud.repositories.edge; import java.util.List; +import java.util.Optional; import org.openmbee.sdvc.crud.domains.Edge; -import org.openmbee.sdvc.crud.domains.EdgeType; public interface EdgeDAO { - Edge save(Edge edge); + Optional save(Edge edge); List saveAll(List nodes); @@ -14,9 +14,9 @@ public interface EdgeDAO { List updateAll(List nodes); - Edge findParents(String child, Integer et); + Optional findParents(String child, Integer et); - Edge findChildren(String parent, Integer et); + Optional findChildren(String parent, Integer et); List findAll(); } diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAOImpl.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAOImpl.java index 3916f7b64..0019745d0 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAOImpl.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAOImpl.java @@ -5,11 +5,9 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; -import java.util.Collection; import java.util.List; +import java.util.Optional; 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; @@ -20,7 +18,7 @@ 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) { + public Optional save(Edge edge) { String sql = String.format(INSERT_SQL, getSuffix()); getConnection().update(sql, @@ -29,7 +27,7 @@ public Edge save(Edge edge) { edge.getParent().getId() ); - return edge; + return Optional.of(edge); } public List saveAll(List edges) { @@ -100,20 +98,20 @@ public int getBatchSize() { } @SuppressWarnings({"unchecked"}) - public Edge findParents(String sysmlId, Integer et) { + public Optional findParents(String sysmlId, Integer et) { String sql = String.format("SELECT * FROM nodes%s WHERE sysmlid = ?", getSuffix()); - return (Edge) getConnection() + return (Optional) getConnection() .queryForObject(sql, new Object[]{sysmlId}, new EdgeRowMapper()); } @SuppressWarnings({"unchecked"}) - public Edge findChildren(String sysmlId, Integer et) { + public Optional findChildren(String sysmlId, Integer et) { String sql = String.format("SELECT * FROM nodes%s WHERE sysmlid = ?", getSuffix()); - return (Edge) getConnection() + return (Optional) getConnection() .queryForObject(sql, new Object[]{sysmlId}, new EdgeRowMapper()); } diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAO.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAO.java index 0fe559db8..ee0f80b50 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAO.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAO.java @@ -2,11 +2,12 @@ import java.util.Collection; import java.util.List; +import java.util.Optional; import org.openmbee.sdvc.crud.domains.Node; public interface NodeDAO { - public Node save(Node node); + public Optional save(Node node); public List saveAll(List nodes); @@ -14,9 +15,9 @@ public interface NodeDAO { public List updateAll(List nodes); - public Node findById(long id); + public Optional findById(long id); - public Node findByNodeId(String sysmlid); + public Optional findByNodeId(String sysmlid); public List findAllByNodeIds(Collection ids); diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAOImpl.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAOImpl.java index 122ee10bb..e871451dd 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAOImpl.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAOImpl.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Optional; import org.openmbee.sdvc.crud.domains.Node; import org.openmbee.sdvc.crud.repositories.BaseDAOImpl; import org.springframework.jdbc.core.BatchPreparedStatementSetter; @@ -21,7 +22,7 @@ public class NodeDAOImpl extends BaseDAOImpl implements NodeDAO { private final String INSERT_SQL = "INSERT INTO nodes%s (nodeid, indexid, lastcommit, initialcommit, deleted, nodetype) VALUES (?, ?, ?, ?, ?, ?)"; private final String UPDATE_SQL = "UPDATE nodes%s SET nodeid = ?, indexid = ?, lastcommit = ?, initialcommit = ?, deleted = ?, nodetype = ? WHERE id = ?"; - public Node save(Node node) { + public Optional save(Node node) { if (node.getId() == null) { KeyHolder keyHolder = new GeneratedKeyHolder(); @@ -62,7 +63,7 @@ public PreparedStatement createPreparedStatement(Connection connection) } }); } - return node; + return Optional.of(node); } //TODO handle errors @@ -140,23 +141,24 @@ public int getBatchSize() { } @SuppressWarnings({"unchecked"}) - public Node findById(long id) { + public Optional findById(long id) { String sql = String.format("SELECT * FROM nodes%s WHERE id = ?", getSuffix()); - return (Node) getConnection() + return (Optional) getConnection() .queryForObject(sql, new Object[]{id}, new NodeRowMapper()); } @SuppressWarnings({"unchecked"}) - public Node findByNodeId(String nodeId) { + public Optional findByNodeId(String nodeId) { String sql = String.format("SELECT * FROM nodes%s WHERE nodeid = ?", getSuffix()); - return (Node) getConnection() + return (Optional) getConnection() .queryForObject(sql, new Object[]{nodeId}, new NodeRowMapper()); } + @SuppressWarnings({"unchecked"}) public List findAllByNodeIds(Collection ids) { if (ids == null || ids.isEmpty()) { return new ArrayList<>(); From ed7972610cb27645354b20c33e0a551695a40b4b Mon Sep 17 00:00:00 2001 From: Jason Han Date: Thu, 20 Dec 2018 14:24:53 -0800 Subject: [PATCH 3/4] Fixing Optional return values --- .../services/UserDetailsServiceImpl.java | 7 +++--- .../authenticator/services/UserService.java | 5 +++-- .../services/UserServiceImpl.java | 5 +++-- .../controllers/branches/BranchesPost.java | 12 +++++++--- .../crud/controllers/orgs/OrgsController.java | 11 ++++++---- .../projects/ProjectsController.java | 11 ++++++---- .../crud/repositories/branch/BranchDAO.java | 2 +- .../repositories/branch/BranchDAOImpl.java | 4 ++-- .../crud/repositories/commit/CommitDAO.java | 2 +- .../repositories/commit/CommitDAOImpl.java | 13 ++++++----- .../sdvc/crud/repositories/edge/EdgeDAO.java | 2 +- .../crud/repositories/edge/EdgeDAOImpl.java | 4 ++-- .../sdvc/crud/repositories/node/NodeDAO.java | 2 +- .../crud/repositories/node/NodeDAOImpl.java | 4 ++-- .../crud/services/DefaultProjectService.java | 22 ++++++++++--------- .../elastic}/config/ElasticsearchConfig.java | 2 +- .../sdvc/ldap/LdapSecurityConfig.java | 5 +++-- 17 files changed, 67 insertions(+), 46 deletions(-) rename {crud/src/main/java/org/openmbee/sdvc/crud => elastic/src/main/java/org/openmbee/sdvc/elastic}/config/ElasticsearchConfig.java (95%) diff --git a/authenticator/src/main/java/org/openmbee/sdvc/authenticator/services/UserDetailsServiceImpl.java b/authenticator/src/main/java/org/openmbee/sdvc/authenticator/services/UserDetailsServiceImpl.java index 6d530fe77..957fabd32 100644 --- a/authenticator/src/main/java/org/openmbee/sdvc/authenticator/services/UserDetailsServiceImpl.java +++ b/authenticator/src/main/java/org/openmbee/sdvc/authenticator/services/UserDetailsServiceImpl.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Optional; import org.openmbee.sdvc.authenticator.security.UserDetailsImpl; import org.openmbee.sdvc.core.domains.Privilege; import org.openmbee.sdvc.core.domains.Role; @@ -35,13 +36,13 @@ public void setPasswordEncoder(PasswordEncoder passwordEncoder) { @Override public UserDetailsImpl loadUserByUsername(String username) throws UsernameNotFoundException { - User user = userRepository.findByUsername(username); + Optional user = userRepository.findByUsername(username); - if (user == null) { + if (!user.isPresent()) { throw new UsernameNotFoundException( String.format("No user found with username '%s'.", username)); } - return new UserDetailsImpl(user); + return new UserDetailsImpl(user.get()); } private Collection getAuthorities( diff --git a/authenticator/src/main/java/org/openmbee/sdvc/authenticator/services/UserService.java b/authenticator/src/main/java/org/openmbee/sdvc/authenticator/services/UserService.java index c1b85e366..8483d6692 100644 --- a/authenticator/src/main/java/org/openmbee/sdvc/authenticator/services/UserService.java +++ b/authenticator/src/main/java/org/openmbee/sdvc/authenticator/services/UserService.java @@ -1,5 +1,6 @@ package org.openmbee.sdvc.authenticator.services; +import java.util.Optional; import org.openmbee.sdvc.core.domains.User; public interface UserService { @@ -8,9 +9,9 @@ public interface UserService { void delete(User user); - User findByEmail(String email); + Optional findByEmail(String email); - User findByUsername(String username); + Optional findByUsername(String username); Iterable findAll(); } diff --git a/authenticator/src/main/java/org/openmbee/sdvc/authenticator/services/UserServiceImpl.java b/authenticator/src/main/java/org/openmbee/sdvc/authenticator/services/UserServiceImpl.java index 92c5d4ea4..49e85d7d6 100644 --- a/authenticator/src/main/java/org/openmbee/sdvc/authenticator/services/UserServiceImpl.java +++ b/authenticator/src/main/java/org/openmbee/sdvc/authenticator/services/UserServiceImpl.java @@ -1,5 +1,6 @@ package org.openmbee.sdvc.authenticator.services; +import java.util.Optional; import org.openmbee.sdvc.core.domains.User; import org.openmbee.sdvc.core.repositories.UserRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -27,11 +28,11 @@ public Iterable findAll() { return userRepository.findAll(); } - public User findByEmail(String email) { + public Optional findByEmail(String email) { return userRepository.findByEmail(email); } - public User findByUsername(String username) { + public Optional findByUsername(String username) { return userRepository.findByUsername(username); } diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/controllers/branches/BranchesPost.java b/crud/src/main/java/org/openmbee/sdvc/crud/controllers/branches/BranchesPost.java index 707e6be5e..a7b82686c 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/controllers/branches/BranchesPost.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/controllers/branches/BranchesPost.java @@ -1,6 +1,7 @@ package org.openmbee.sdvc.crud.controllers.branches; import java.time.Instant; +import java.util.Optional; import org.openmbee.sdvc.crud.config.DbContextHolder; import org.openmbee.sdvc.crud.controllers.BaseController; import org.openmbee.sdvc.crud.controllers.BaseResponse; @@ -62,11 +63,16 @@ public ResponseEntity handleRequest( } if (branch.getParentCommitId() != null) { - Commit parentCommit = commitRepository + Optional parentCommit = commitRepository .findByCommitId(branch.getParentCommitId()); - b.setParentCommit(parentCommit.getId()); + if (parentCommit.isPresent()) { + b.setParentCommit(parentCommit.get().getId()); + } } else { - b.setParentCommit(commitRepository.findLatestByRef(b.getParentRefId()).getId()); + Optional parentCommit = commitRepository.findLatestByRef(b.getParentRefId()); + if (parentCommit.isPresent()) { + b.setParentCommit(parentCommit.get().getId()); + } } b.setTimestamp(now); diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/controllers/orgs/OrgsController.java b/crud/src/main/java/org/openmbee/sdvc/crud/controllers/orgs/OrgsController.java index d3137ea94..78f31a7c1 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/controllers/orgs/OrgsController.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/controllers/orgs/OrgsController.java @@ -1,6 +1,7 @@ package org.openmbee.sdvc.crud.controllers.orgs; import java.util.List; +import java.util.Optional; import javax.transaction.Transactional; import org.openmbee.sdvc.core.domains.Organization; import org.openmbee.sdvc.core.repositories.OrganizationRepository; @@ -34,10 +35,12 @@ public ResponseEntity handleGet(@PathVariable(required = false) String orgId) if (orgId != null) { logger.debug("OrgId given: ", orgId); - Organization org = organizationRepository.findByOrganizationId(orgId); - OrgJson orgJson = new OrgJson(); - orgJson.merge(convertToMap(org)); - response.getOrgs().add(orgJson); + Optional orgOption = organizationRepository.findByOrganizationId(orgId); + if (orgOption.isPresent()) { + OrgJson orgJson = new OrgJson(); + orgJson.merge(convertToMap(orgOption.get())); + response.getOrgs().add(orgJson); + } return ResponseEntity.ok(response); } else { logger.debug("No OrgId given"); diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/controllers/projects/ProjectsController.java b/crud/src/main/java/org/openmbee/sdvc/crud/controllers/projects/ProjectsController.java index d47636514..9c5b94293 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/controllers/projects/ProjectsController.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/controllers/projects/ProjectsController.java @@ -1,6 +1,7 @@ package org.openmbee.sdvc.crud.controllers.projects; import java.util.List; +import java.util.Optional; import javax.transaction.Transactional; import org.openmbee.sdvc.core.domains.Project; import org.openmbee.sdvc.core.repositories.ProjectRepository; @@ -36,10 +37,12 @@ public ResponseEntity handleGet(@PathVariable(required = if (projectId != null) { logger.debug("ProjectId given: ", projectId); - Project org = projectRepository.findByProjectId(projectId); - ProjectJson projectJson = new ProjectJson(); - projectJson.merge(convertToMap(org)); - response.getProjects().add(projectJson); + Optional orgOption = projectRepository.findByProjectId(projectId); + if (orgOption.isPresent()) { + ProjectJson projectJson = new ProjectJson(); + projectJson.merge(convertToMap(orgOption.get())); + response.getProjects().add(projectJson); + } } else { logger.debug("No ProjectId given"); List allOrgs = projectRepository.findAll(); diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAO.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAO.java index 8f269b794..e638daab5 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAO.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAO.java @@ -6,7 +6,7 @@ public interface BranchDAO { - public Optional save(Branch branch); + public Branch save(Branch branch); public Optional findById(long id); diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAOImpl.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAOImpl.java index 4652e874c..f2a054e5c 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAOImpl.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAOImpl.java @@ -23,7 +23,7 @@ public void setCommitRepository(CommitDAO commitRepository) { this.commitRepository = commitRepository; } */ - public Optional save(Branch branch) { + public Branch save(Branch branch) { String sql = "INSERT INTO branches (description, branchId, branchName, parentRefId, parentCommit, timestamp, tag, deleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; /* if (branch.getParentRefId() != null && branch.getParentRef() == null) { @@ -64,7 +64,7 @@ public PreparedStatement createPreparedStatement(Connection connection) return null; } branch.setId(keyHolder.getKey().longValue()); - return Optional.of(branch); + return branch; } @SuppressWarnings({"unchecked"}) diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAO.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAO.java index 9c4ee62ae..2e55dc052 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAO.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAO.java @@ -7,7 +7,7 @@ public interface CommitDAO { - public Optional save(Commit commit); + public Commit save(Commit commit); public Optional findById(long id); diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAOImpl.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAOImpl.java index fe47c7121..5db52f77b 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAOImpl.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAOImpl.java @@ -28,7 +28,7 @@ public void setBranchRepository(BranchDAO branchRepository) { this.branchRepository = branchRepository; } - public Optional save(Commit commit) { + public Commit save(Commit commit) { String sql = "INSERT INTO commits (commitType, creator, indexId, branchId, timestamp, comment) VALUES (?, ?, ?, ?, ?, ?)"; KeyHolder keyHolder = new GeneratedKeyHolder(); @@ -51,7 +51,7 @@ public PreparedStatement createPreparedStatement(Connection connection) return null;//TODO error } commit.setId(keyHolder.getKey().longValue()); - return Optional.of(commit); + return commit; } public Optional findById(long id) { @@ -98,9 +98,12 @@ public List findByRefAndTimestampAndLimit(String refId, Instant timestam int currentLimit = limit == 0 ? 0 : limit - commits.size(); List next = findByRefAndLimit(currentRef, currentCid, timestamp, currentLimit); commits.addAll(next); - Branch ref = branchRepository.findByBranchId(currentRef); - currentRef = ref.getParentRefId(); - currentCid = ref.getParentCommit(); + Optional ref = branchRepository.findByBranchId(currentRef); + if (ref.isPresent()) { + currentRef = ref.get().getParentRefId(); + currentCid = ref.get().getParentCommit(); + } + if (currentRef != null && currentRef.equals("")) { currentRef = null; } diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAO.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAO.java index d26911496..874149c95 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAO.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAO.java @@ -6,7 +6,7 @@ public interface EdgeDAO { - Optional save(Edge edge); + Edge save(Edge edge); List saveAll(List nodes); diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAOImpl.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAOImpl.java index 0019745d0..ce14f6abc 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAOImpl.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAOImpl.java @@ -18,7 +18,7 @@ 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 Optional save(Edge edge) { + public Edge save(Edge edge) { String sql = String.format(INSERT_SQL, getSuffix()); getConnection().update(sql, @@ -27,7 +27,7 @@ public Optional save(Edge edge) { edge.getParent().getId() ); - return Optional.of(edge); + return edge; } public List saveAll(List edges) { diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAO.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAO.java index ee0f80b50..7647566c1 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAO.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAO.java @@ -7,7 +7,7 @@ public interface NodeDAO { - public Optional save(Node node); + public Node save(Node node); public List saveAll(List nodes); diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAOImpl.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAOImpl.java index e871451dd..30ed25fd6 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAOImpl.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAOImpl.java @@ -22,7 +22,7 @@ public class NodeDAOImpl extends BaseDAOImpl implements NodeDAO { private final String INSERT_SQL = "INSERT INTO nodes%s (nodeid, indexid, lastcommit, initialcommit, deleted, nodetype) VALUES (?, ?, ?, ?, ?, ?)"; private final String UPDATE_SQL = "UPDATE nodes%s SET nodeid = ?, indexid = ?, lastcommit = ?, initialcommit = ?, deleted = ?, nodetype = ? WHERE id = ?"; - public Optional save(Node node) { + public Node save(Node node) { if (node.getId() == null) { KeyHolder keyHolder = new GeneratedKeyHolder(); @@ -63,7 +63,7 @@ public PreparedStatement createPreparedStatement(Connection connection) } }); } - return Optional.of(node); + return node; } //TODO handle errors diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/services/DefaultProjectService.java b/crud/src/main/java/org/openmbee/sdvc/crud/services/DefaultProjectService.java index 1a9fffa4d..7b3b23082 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/services/DefaultProjectService.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/services/DefaultProjectService.java @@ -1,6 +1,7 @@ package org.openmbee.sdvc.crud.services; import java.sql.SQLException; +import java.util.Optional; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.openmbee.sdvc.core.domains.Organization; @@ -47,15 +48,15 @@ public ProjectJson create(ProjectJson project) { return null; } - Organization org = orgRepository.findByOrganizationId(project.getOrgId()); - if (org == null || org.getOrganizationId().isEmpty()) { + Optional org = orgRepository.findByOrganizationId(project.getOrgId()); + if (!org.isPresent() || org.get().getOrganizationId().isEmpty()) { return null; } Project proj = new Project(); proj.setProjectId(project.getId()); proj.setProjectName(project.getName()); - proj.setOrganization(org); + proj.setOrganization(org.get()); Project saved = projectRepository.save(proj); try { @@ -76,14 +77,15 @@ public ProjectJson create(ProjectJson project) { } public ProjectJson update(ProjectJson project) { - Project proj = projectRepository.findByProjectId(project.getProjectId()); - if (proj != null && !project.getId().isEmpty()) { + Optional projOption = projectRepository.findByProjectId(project.getProjectId()); + if (projOption.isPresent() && !projOption.get().getProjectId().isEmpty()) { + Project proj = projOption.get(); proj.setProjectId(project.getProjectId()); proj.setProjectName(project.getName()); if (!project.getOrgId().isEmpty()) { - Organization org = orgRepository.findByOrganizationId(project.getOrgId()); - if (org != null && !org.getOrganizationId().isEmpty()) { - proj.setOrganization(org); + Optional org = orgRepository.findByOrganizationId(project.getOrgId()); + if (org.isPresent() && !org.get().getOrganizationId().isEmpty()) { + proj.setOrganization(org.get()); } } projectRepository.save(proj); @@ -97,7 +99,7 @@ public ProjectsResponse read(String projectId) { } public boolean exists(String projectId) { - Project project = this.projectRepository.findByProjectId(projectId); - return project != null && project.getProjectId().equals(projectId); + Optional project = this.projectRepository.findByProjectId(projectId); + return project.isPresent() && project.get().getProjectId().equals(projectId); } } diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/config/ElasticsearchConfig.java b/elastic/src/main/java/org/openmbee/sdvc/elastic/config/ElasticsearchConfig.java similarity index 95% rename from crud/src/main/java/org/openmbee/sdvc/crud/config/ElasticsearchConfig.java rename to elastic/src/main/java/org/openmbee/sdvc/elastic/config/ElasticsearchConfig.java index c38d437a1..32b976758 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/config/ElasticsearchConfig.java +++ b/elastic/src/main/java/org/openmbee/sdvc/elastic/config/ElasticsearchConfig.java @@ -1,4 +1,4 @@ -package org.openmbee.sdvc.crud.config; +package org.openmbee.sdvc.elastic.config; import org.apache.http.HttpHost; import org.springframework.context.annotation.Bean; diff --git a/ldap/src/main/java/org/openmbee/sdvc/ldap/LdapSecurityConfig.java b/ldap/src/main/java/org/openmbee/sdvc/ldap/LdapSecurityConfig.java index ca1d39840..fccb44921 100644 --- a/ldap/src/main/java/org/openmbee/sdvc/ldap/LdapSecurityConfig.java +++ b/ldap/src/main/java/org/openmbee/sdvc/ldap/LdapSecurityConfig.java @@ -1,6 +1,7 @@ package org.openmbee.sdvc.ldap; import java.util.Collection; +import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; import javax.naming.ldap.LdapName; @@ -105,8 +106,8 @@ private CustomLdapAuthoritiesPopulator(ContextSource contextSource) { public Collection getGrantedAuthorities( DirContextOperations userData, String username) { - User user = userRepository.findByUsername(username); - if (user == null) { + Optional user = userRepository.findByUsername(username); + if (!user.isPresent()) { User newUser = new User(); newUser.setEmail(userData.getStringAttribute(userAttributesEmail)); newUser.setUsername(userData.getStringAttribute(userAttributesUsername)); From 867b38362db10088a4d7941cc1c290b231ecb466 Mon Sep 17 00:00:00 2001 From: "Lam, Doris T (393A)" Date: Fri, 21 Dec 2018 21:44:16 -0800 Subject: [PATCH 4/4] more cleanup --- .../sdvc/core/domains/Organization.java | 2 ++ .../crud/controllers/orgs/OrgsController.java | 19 ++++++++------- .../projects/ProjectsController.java | 14 ++++++----- .../org/openmbee/sdvc/crud/domains/Node.java | 1 + .../repositories/branch/BranchDAOImpl.java | 13 +++++------ .../repositories/branch/BranchRowMapper.java | 2 +- .../repositories/commit/CommitDAOImpl.java | 12 ++++++---- .../repositories/commit/CommitRowMapper.java | 2 +- .../crud/repositories/edge/EdgeDAOImpl.java | 1 + .../crud/repositories/node/NodeDAOImpl.java | 16 ++++++------- .../crud/repositories/node/NodeIndexDAO.java | 2 -- .../crud/repositories/node/NodeRowMapper.java | 2 +- .../crud/services/DefaultProjectService.java | 7 +++--- .../sdvc/elastic/BaseElasticDAOImpl.java | 2 +- .../sdvc/elastic/CommitElasticDAOImpl.java | 21 ++++++++++------- .../sdvc/elastic/NodeElasticDAOImpl.java | 23 +++++++++++-------- 16 files changed, 78 insertions(+), 61 deletions(-) diff --git a/core/src/main/java/org/openmbee/sdvc/core/domains/Organization.java b/core/src/main/java/org/openmbee/sdvc/core/domains/Organization.java index f9bca6837..067f16095 100644 --- a/core/src/main/java/org/openmbee/sdvc/core/domains/Organization.java +++ b/core/src/main/java/org/openmbee/sdvc/core/domains/Organization.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonProperty; import java.util.Collection; +import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.OneToMany; import javax.persistence.Table; @@ -15,6 +16,7 @@ public class Organization extends Base { private String organizationName; @JsonProperty("id") + @Column(unique = true) private String organizationId; @OneToMany(mappedBy = "organization") diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/controllers/orgs/OrgsController.java b/crud/src/main/java/org/openmbee/sdvc/crud/controllers/orgs/OrgsController.java index 78f31a7c1..a39c8e5ab 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/controllers/orgs/OrgsController.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/controllers/orgs/OrgsController.java @@ -36,12 +36,12 @@ public ResponseEntity handleGet(@PathVariable(required = false) String orgId) if (orgId != null) { logger.debug("OrgId given: ", orgId); Optional orgOption = organizationRepository.findByOrganizationId(orgId); - if (orgOption.isPresent()) { - OrgJson orgJson = new OrgJson(); - orgJson.merge(convertToMap(orgOption.get())); - response.getOrgs().add(orgJson); + if (!orgOption.isPresent()) { + return ResponseEntity.notFound().build(); } - return ResponseEntity.ok(response); + OrgJson orgJson = new OrgJson(); + orgJson.merge(convertToMap(orgOption.get())); + response.getOrgs().add(orgJson); } else { logger.debug("No OrgId given"); List allOrgs = organizationRepository.findAll(); @@ -50,8 +50,8 @@ public ResponseEntity handleGet(@PathVariable(required = false) String orgId) orgJson.merge(convertToMap(org)); response.getOrgs().add(orgJson); } - return ResponseEntity.ok(response); } + return ResponseEntity.ok(response); } @PostMapping @@ -64,7 +64,9 @@ public ResponseEntity handlePost( OrganizationsResponse response = new OrganizationsResponse(); for (OrgJson org : orgPost.getOrgs()) { - Organization o = new Organization(); + //TODO reject if orgId isn't there + Organization o = organizationRepository.findByOrganizationId(org.getId()) + .orElse(new Organization()); o.setOrganizationId(org.getId()); o.setOrganizationName(org.getName()); logger.info("Saving organization: {}", o.getOrganizationId()); @@ -72,10 +74,7 @@ public ResponseEntity handlePost( org.merge(convertToMap(saved)); response.getOrgs().add(org); } - return ResponseEntity.ok(response); - } else { - } logger.debug("Bad Request"); OrganizationsResponse err = new OrganizationsResponse(); diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/controllers/projects/ProjectsController.java b/crud/src/main/java/org/openmbee/sdvc/crud/controllers/projects/ProjectsController.java index 9c5b94293..ddb37c1c3 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/controllers/projects/ProjectsController.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/controllers/projects/ProjectsController.java @@ -32,17 +32,19 @@ public ProjectsController(ProjectRepository projectRepository) { @GetMapping(value = {"", "/{projectId}"}) @Transactional - public ResponseEntity handleGet(@PathVariable(required = false) String projectId) { + public ResponseEntity handleGet( + @PathVariable(required = false) String projectId) { ProjectsResponse response = new ProjectsResponse(); if (projectId != null) { logger.debug("ProjectId given: ", projectId); Optional orgOption = projectRepository.findByProjectId(projectId); - if (orgOption.isPresent()) { - ProjectJson projectJson = new ProjectJson(); - projectJson.merge(convertToMap(orgOption.get())); - response.getProjects().add(projectJson); + if (!orgOption.isPresent()) { + return ResponseEntity.notFound().build(); } + ProjectJson projectJson = new ProjectJson(); + projectJson.merge(convertToMap(orgOption.get())); + response.getProjects().add(projectJson); } else { logger.debug("No ProjectId given"); List allOrgs = projectRepository.findAll(); @@ -52,7 +54,6 @@ public ResponseEntity handleGet(@PathVariable(required = response.getProjects().add(projectJson); } } - return ResponseEntity.ok(response); } @@ -68,6 +69,7 @@ public ResponseEntity handlePost( } ProjectsResponse response = new ProjectsResponse(); for (ProjectJson json: projectsPost.getProjects()) { + //TODO reject if projectId isn't there ProjectService ps = getProjectService(json.getProjectId()); if (!ps.exists(json.getProjectId())) { response.getProjects().add(ps.create(json)); diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/domains/Node.java b/crud/src/main/java/org/openmbee/sdvc/crud/domains/Node.java index 050354bf7..c60dcddb2 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/domains/Node.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/domains/Node.java @@ -16,6 +16,7 @@ public class Node { @Column(name = "id", updatable = false, nullable = false) Long id; + @Column(unique = true) private String nodeId; private String indexId; private String lastCommit; diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAOImpl.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAOImpl.java index f2a054e5c..f3ae4d7fc 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAOImpl.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchDAOImpl.java @@ -67,23 +67,22 @@ public PreparedStatement createPreparedStatement(Connection connection) return branch; } - @SuppressWarnings({"unchecked"}) public Optional findById(long id) { String sql = "SELECT * FROM branches WHERE id = ?"; - return (Optional) getConnection() - .queryForObject(sql, new Object[]{id}, new BranchRowMapper()); + List l = getConnection() + .query(sql, new Object[]{id}, new BranchRowMapper()); + return l.isEmpty() ? Optional.empty() : Optional.of(l.get(0)); } - @SuppressWarnings({"unchecked"}) public Optional findByBranchId(String branchId) { String sql = "SELECT * FROM branches WHERE branchId = ?"; - return (Optional) getConnection() - .queryForObject(sql, new Object[]{branchId}, new BranchRowMapper()); + List l = getConnection() + .query(sql, new Object[]{branchId}, new BranchRowMapper()); + return l.isEmpty() ? Optional.empty() : Optional.of(l.get(0)); } - @SuppressWarnings({"unchecked"}) public List findAll() { String sql = "SELECT * FROM branches WHERE deleted = false"; diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchRowMapper.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchRowMapper.java index 55780b44b..b99dd6cd0 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchRowMapper.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/branch/BranchRowMapper.java @@ -10,7 +10,7 @@ import org.springframework.cglib.beans.BeanMap; import org.springframework.jdbc.core.RowMapper; -public class BranchRowMapper implements RowMapper { +public class BranchRowMapper implements RowMapper { static final String BRANCHES = "branches"; diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAOImpl.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAOImpl.java index 5db52f77b..4a511d0d6 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAOImpl.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitDAOImpl.java @@ -57,15 +57,19 @@ public PreparedStatement createPreparedStatement(Connection connection) public Optional findById(long id) { String sql = "SELECT * FROM commits WHERE id = ?"; - return (Optional) getConnection() - .queryForObject(sql, new Object[]{id}, new CommitRowMapper()); + List l = getConnection() + .query(sql, new Object[]{id}, new CommitRowMapper()); + return l.isEmpty() ? Optional.empty() : Optional.of(l.get(0)); + } public Optional findByCommitId(String commitId) { String sql = "SELECT * FROM commits WHERE indexid = ?"; - return (Optional) getConnection() - .queryForObject(sql, new Object[]{commitId}, new CommitRowMapper()); + List l = getConnection() + .query(sql, new Object[]{commitId}, new CommitRowMapper()); + return l.isEmpty() ? Optional.empty() : Optional.of(l.get(0)); + } public Optional findByRefAndTimestamp(String refId, Instant timestamp) { diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitRowMapper.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitRowMapper.java index adcb28945..aab5d7f8c 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitRowMapper.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/commit/CommitRowMapper.java @@ -11,7 +11,7 @@ import org.springframework.cglib.beans.BeanMap; import org.springframework.jdbc.core.RowMapper; -public class CommitRowMapper implements RowMapper { +public class CommitRowMapper implements RowMapper { static final String COMMITS = "commits"; diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAOImpl.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAOImpl.java index ce14f6abc..514e89c65 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAOImpl.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/edge/EdgeDAOImpl.java @@ -97,6 +97,7 @@ public int getBatchSize() { return edges; } + //TODO these are wrong @SuppressWarnings({"unchecked"}) public Optional findParents(String sysmlId, Integer et) { String sql = String.format("SELECT * FROM nodes%s WHERE sysmlid = ?", diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAOImpl.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAOImpl.java index 30ed25fd6..fc7eb32cd 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAOImpl.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeDAOImpl.java @@ -140,25 +140,26 @@ public int getBatchSize() { return nodes; } - @SuppressWarnings({"unchecked"}) public Optional findById(long id) { String sql = String.format("SELECT * FROM nodes%s WHERE id = ?", getSuffix()); - return (Optional) getConnection() - .queryForObject(sql, new Object[]{id}, new NodeRowMapper()); + List l = getConnection() + .query(sql, new Object[]{id}, new NodeRowMapper()); + return l.isEmpty() ? Optional.empty() : Optional.of(l.get(0)); + } - @SuppressWarnings({"unchecked"}) public Optional findByNodeId(String nodeId) { String sql = String.format("SELECT * FROM nodes%s WHERE nodeid = ?", getSuffix()); - return (Optional) getConnection() - .queryForObject(sql, new Object[]{nodeId}, new NodeRowMapper()); + List l = getConnection() + .query(sql, new Object[]{nodeId}, new NodeRowMapper()); + return l.isEmpty() ? Optional.empty() : Optional.of(l.get(0)); + } - @SuppressWarnings({"unchecked"}) public List findAllByNodeIds(Collection ids) { if (ids == null || ids.isEmpty()) { return new ArrayList<>(); @@ -168,7 +169,6 @@ public List findAllByNodeIds(Collection ids) { return getConnection().query(sql, new NodeRowMapper()); } - @SuppressWarnings({"unchecked"}) public List findAll() { String sql = String.format("SELECT * FROM nodes%s WHERE deleted = FALSE", getSuffix()); diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeIndexDAO.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeIndexDAO.java index 8e7d1e006..eb9e4a85b 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeIndexDAO.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeIndexDAO.java @@ -1,9 +1,7 @@ package org.openmbee.sdvc.crud.repositories.node; -import java.io.IOException; import java.util.Collection; import java.util.List; -import java.util.Map; import java.util.Optional; import java.util.Set; import org.openmbee.sdvc.json.BaseJson; diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeRowMapper.java b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeRowMapper.java index f0f5b7a0c..1f5ace823 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeRowMapper.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/repositories/node/NodeRowMapper.java @@ -10,7 +10,7 @@ import org.springframework.cglib.beans.BeanMap; import org.springframework.jdbc.core.RowMapper; -public class NodeRowMapper implements RowMapper { +public class NodeRowMapper implements RowMapper { static final String NODES = "nodes"; diff --git a/crud/src/main/java/org/openmbee/sdvc/crud/services/DefaultProjectService.java b/crud/src/main/java/org/openmbee/sdvc/crud/services/DefaultProjectService.java index 7b3b23082..5b454d0bd 100644 --- a/crud/src/main/java/org/openmbee/sdvc/crud/services/DefaultProjectService.java +++ b/crud/src/main/java/org/openmbee/sdvc/crud/services/DefaultProjectService.java @@ -45,11 +45,13 @@ public void setProjectIndex(ProjectIndex projectIndex) { public ProjectJson create(ProjectJson project) { if (project.getOrgId() == null || project.getOrgId().isEmpty()) { + //TODO throw exception return null; } Optional org = orgRepository.findByOrganizationId(project.getOrgId()); if (!org.isPresent() || org.get().getOrganizationId().isEmpty()) { + //TODO throw exception return null; } @@ -61,7 +63,6 @@ public ProjectJson create(ProjectJson project) { try { if (projectOperations.createProjectDatabase(proj)) { - //TODO create elastic indexes and mappings projectIndex.create(proj.getProjectId()); return project; } @@ -73,7 +74,7 @@ public ProjectJson create(ProjectJson project) { logger.error("Couldn't create database: {}", project.getProjectId()); logger.error(e); } - return null; //throw exception + return null; //TODO throw exception } public ProjectJson update(ProjectJson project) { @@ -86,7 +87,7 @@ public ProjectJson update(ProjectJson project) { Optional org = orgRepository.findByOrganizationId(project.getOrgId()); if (org.isPresent() && !org.get().getOrganizationId().isEmpty()) { proj.setOrganization(org.get()); - } + } //TODO else reject? } projectRepository.save(proj); return project; diff --git a/elastic/src/main/java/org/openmbee/sdvc/elastic/BaseElasticDAOImpl.java b/elastic/src/main/java/org/openmbee/sdvc/elastic/BaseElasticDAOImpl.java index 3cdb775c7..9b9f3f32c 100644 --- a/elastic/src/main/java/org/openmbee/sdvc/elastic/BaseElasticDAOImpl.java +++ b/elastic/src/main/java/org/openmbee/sdvc/elastic/BaseElasticDAOImpl.java @@ -26,7 +26,7 @@ public abstract class BaseElasticDAOImpl> { - @Value("${elastic.limit.result}") + @Value("${elastic.limit.result}") //TODO can't inject static vars protected static int resultLimit; @Value("${elastic.limit.term}") protected static int termLimit; diff --git a/elastic/src/main/java/org/openmbee/sdvc/elastic/CommitElasticDAOImpl.java b/elastic/src/main/java/org/openmbee/sdvc/elastic/CommitElasticDAOImpl.java index fa0581788..30ca9a7ca 100644 --- a/elastic/src/main/java/org/openmbee/sdvc/elastic/CommitElasticDAOImpl.java +++ b/elastic/src/main/java/org/openmbee/sdvc/elastic/CommitElasticDAOImpl.java @@ -30,31 +30,31 @@ protected CommitJson newInstance() { } public void indexAll(Collection jsons) { - this.indexAll(getIndex() + "_commit", jsons); + this.indexAll(getIndex(), jsons); } public void index(BaseJson json) { - this.index(getIndex() + "_commit", json); + this.index(getIndex(), json); } public Optional findById(String indexId) { - return this.findById(getIndex() + "_commit", indexId); + return this.findById(getIndex(), indexId); } public List findAllById(Set indexIds) { - return this.findAllById(getIndex() + "_commit", indexIds); + return this.findAllById(getIndex(), indexIds); } public void deleteById(String indexId) { - this.deleteById(getIndex() + "_commit", indexId); + this.deleteById(getIndex(), indexId); } public void deleteAll(Collection jsons) { - this.deleteAll(getIndex() + "_commit", jsons); + this.deleteAll(getIndex(), jsons); } public boolean existsById(String indexId) { - return this.existsById(getIndex() + "_commit", indexId); + return this.existsById(getIndex(), indexId); } /** @@ -91,7 +91,7 @@ private QueryBuilder getCommitHistoryQuery(String id, Set commitIds) { public List elementHistory(String nodeId, Set commitIds) { try { List commits = new ArrayList<>(); - SearchRequest searchRequest = new SearchRequest(getIndex() + "_commit"); + SearchRequest searchRequest = new SearchRequest(getIndex()); QueryBuilder query = getCommitHistoryQuery(nodeId, commitIds); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(query); @@ -113,4 +113,9 @@ public List elementHistory(String nodeId, Set commitIds) { throw new RuntimeException(e); } } + + @Override + protected String getIndex() { + return super.getIndex() + "_commit"; + } } diff --git a/elastic/src/main/java/org/openmbee/sdvc/elastic/NodeElasticDAOImpl.java b/elastic/src/main/java/org/openmbee/sdvc/elastic/NodeElasticDAOImpl.java index 7e9b1e40e..e7df7f8c1 100644 --- a/elastic/src/main/java/org/openmbee/sdvc/elastic/NodeElasticDAOImpl.java +++ b/elastic/src/main/java/org/openmbee/sdvc/elastic/NodeElasticDAOImpl.java @@ -29,37 +29,37 @@ protected ElementJson newInstance() { } public void indexAll(Collection jsons) { - this.indexAll(getIndex() + "_node", jsons); + this.indexAll(getIndex(), jsons); } public void index(BaseJson json) { - this.index(getIndex() + "_node", json); + this.index(getIndex(), json); } public Optional findById(String indexId) { - return this.findById(getIndex() + "_node", indexId); + return this.findById(getIndex(), indexId); } public List findAllById(Set indexIds) { - return this.findAllById(getIndex() + "_node", indexIds); + return this.findAllById(getIndex(), indexIds); } public void deleteById(String indexId) { - this.deleteById(getIndex() + "_node", indexId); + this.deleteById(getIndex(), indexId); } public void deleteAll(Collection jsons) { - this.deleteAll(getIndex() + "_node", jsons); + this.deleteAll(getIndex(), jsons); } public boolean existsById(String indexId) { - return this.existsById(getIndex() + "_node", indexId); + return this.existsById(getIndex(), indexId); } @Override public Optional getByCommitId(String commitIndexId, String nodeId) { try { - SearchRequest searchRequest = new SearchRequest(getIndex() + "_node"); + SearchRequest searchRequest = new SearchRequest(getIndex()); // searches the elements for the reference with the current commitId (elasticId) and sysmlid Id QueryBuilder query = QueryBuilders.boolQuery() .must(QueryBuilders.termQuery("_commitId", commitIndexId)) @@ -83,7 +83,7 @@ public Optional getElementLessThanOrEqualTimestamp(String nodeId, String timestamp, List refsCommitIds) { try { final Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1L)); - SearchRequest searchRequest = new SearchRequest(getIndex() + "_node"); + SearchRequest searchRequest = new SearchRequest(getIndex()); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); // Query QueryBuilder query = QueryBuilders.boolQuery() @@ -125,6 +125,11 @@ public Optional getElementLessThanOrEqualTimestamp(String nodeId, throw new RuntimeException(e); } } + + @Override + protected String getIndex() { + return super.getIndex() + "_node"; + } // Create filter array // int count = 0; // while (count < refsCommitIds.size()) {