Skip to content
This repository was archived by the owner on Apr 25, 2023. It is now read-only.

Commit 5694ae4

Browse files
authored
Merge pull request #43 from HumanBrainProject/feature/delete_stored_queries
Added deletion of stored queries
2 parents fbacdf8 + 927c7a5 commit 5694ae4

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/main/java/org/humanbrainproject/knowledgegraph/commons/propertyGraph/arango/control/ArangoInternalRepository.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
import org.humanbrainproject.knowledgegraph.commons.propertyGraph.arango.control.query.ArangoQueryFactory;
1313
import org.humanbrainproject.knowledgegraph.commons.propertyGraph.arango.entity.ArangoCollectionReference;
1414
import org.humanbrainproject.knowledgegraph.commons.propertyGraph.arango.entity.ArangoDocumentReference;
15+
import org.humanbrainproject.knowledgegraph.commons.propertyGraph.arango.exceptions.StoredQueryNotFoundException;
1516
import org.humanbrainproject.knowledgegraph.commons.vocabulary.ArangoVocabulary;
1617
import org.slf4j.Logger;
1718
import org.slf4j.LoggerFactory;
1819
import org.springframework.beans.factory.annotation.Autowired;
1920
import org.springframework.stereotype.Component;
2021

22+
import javax.ws.rs.NotFoundException;
2123
import java.util.List;
2224
import java.util.Map;
2325

@@ -126,4 +128,18 @@ public boolean doesDocumentExist(ArangoCollectionReference collectionReference,
126128
}
127129
return false;
128130
}
131+
132+
public void removeInternalDocument(ArangoDocumentReference document) throws IllegalAccessException, StoredQueryNotFoundException {
133+
String userId = authorizationContext.getUserId();
134+
if(userId==null){
135+
throw new IllegalAccessException("You have to be authenticated if you want to execute this operation");
136+
}
137+
ArangoDatabase db = getDB();
138+
ArangoCollection collection = db.collection(document.getCollection().getName());
139+
if ( collection.exists() && collection.documentExists(document.getKey())) {
140+
collection.deleteDocument(document.getKey());
141+
} else {
142+
throw new StoredQueryNotFoundException("Query not found");
143+
}
144+
}
129145
}

src/main/java/org/humanbrainproject/knowledgegraph/query/api/QueryAPI.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,23 @@ public ResponseEntity<String> saveSpecificationToDB(@RequestBody String payload,
324324
}
325325
}
326326

327+
@ExternalApi
328+
@ApiOperation(value="Delete a query specification in KG")
329+
@DeleteMapping(value = "/{"+ORG+"}/{"+ DOMAIN+"}/{"+SCHEMA+"}/{"+VERSION+"}/{"+QUERY_ID+"}", produces=MediaType.TEXT_PLAIN)
330+
public ResponseEntity<String> removeSpecificationToDB(@PathVariable(ORG) String org, @PathVariable(DOMAIN) String domain, @PathVariable(SCHEMA) String schema, @PathVariable(VERSION) String version, @ApiParam(value = "Freely defined alias for the query. Please note that only the user who has created the specification initially can update it. If an alias is already occupied, please use another one.", required = true) @PathVariable(QUERY_ID) String id, @ApiParam(value = ParameterConstants.AUTHORIZATION_DOC, required = true) @RequestHeader(value = HttpHeaders.AUTHORIZATION) String authorization) throws Exception {
331+
try {
332+
authorizationContext.populateAuthorizationContext(authorization);
333+
query.removeSpecificationInDb(new StoredQueryReference(new NexusSchemaReference(org, domain, schema, version), id));
334+
return ResponseEntity.ok("Deleted specification from database");
335+
} catch (StoredQueryNotFoundException e){
336+
return ResponseEntity.notFound().build();
337+
} catch (HttpClientErrorException e) {
338+
return ResponseEntity.status(e.getStatusCode()).build();
339+
} catch (IllegalAccessException e){
340+
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage());
341+
}
342+
}
343+
327344
@ApiOperation(value="Execute a stored query and fetch the corresponding instances")
328345
@Deprecated
329346
@GetMapping("/{"+ORG+"}/{"+ DOMAIN+"}/{"+SCHEMA+"}/{"+VERSION+"}/{"+QUERY_ID+"}/instances/deprecated")

src/main/java/org/humanbrainproject/knowledgegraph/query/boundary/ArangoQuery.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ public void storeSpecificationInDb(String specification, StoredQueryReference qu
231231
arangoInternalRepository.insertOrUpdateDocument(document, jsonObject.toString());
232232
}
233233

234+
public void removeSpecificationInDb(StoredQueryReference queryReference) throws IllegalAccessException {
235+
ArangoDocumentReference documentRef = new ArangoDocumentReference(SPECIFICATION_QUERIES, queryReference.getName());
236+
arangoInternalRepository.removeInternalDocument(documentRef);
237+
}
238+
234239
public QueryResult<List<Map>> queryPropertyGraphByStoredSpecificationAndFreemarkerTemplate(StoredQuery storedQuery) throws IOException, JSONException, SolrServerException {
235240
QueryResult<List<Map>> queryResult = queryPropertyGraphByStoredSpecification(storedQuery);
236241
String templatePayload = templating.getTemplateById(storedQuery.getStoredTemplateReference()).getTemplateContent();

0 commit comments

Comments
 (0)