Search in sources :

Example 1 with JsonContext

use of com.jayway.jsonpath.internal.JsonContext in project mica2 by obiba.

the class SchemaFormContentFileService method getPathFilesMap.

private Map<String, JSONArray> getPathFilesMap(DocumentContext context, Object json) {
    DocumentContext reader = new JsonContext(defaultConfiguration().addOptions(Option.REQUIRE_PROPERTIES)).parse(json);
    JSONArray paths = null;
    try {
        paths = context.read("$..obibaFiles");
    } catch (PathNotFoundException e) {
        return null;
    }
    return paths.stream().collect(Collectors.toMap(Object::toString, p -> (JSONArray) reader.read(p.toString())));
}
Also used : Arrays(java.util.Arrays) Collection(java.util.Collection) Option(com.jayway.jsonpath.Option) NotNull(javax.validation.constraints.NotNull) JsonPath(com.jayway.jsonpath.JsonPath) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) JsonContext(com.jayway.jsonpath.internal.JsonContext) FileStoreService(org.obiba.mica.file.FileStoreService) LinkedHashMap(java.util.LinkedHashMap) Inject(javax.inject.Inject) DocumentContext(com.jayway.jsonpath.DocumentContext) SchemaFormContentAware(org.obiba.mica.core.domain.SchemaFormContentAware) JSONArray(net.minidev.json.JSONArray) Service(org.springframework.stereotype.Service) Map(java.util.Map) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) Configuration.defaultConfiguration(com.jayway.jsonpath.Configuration.defaultConfiguration) Assert(org.springframework.util.Assert) JsonContext(com.jayway.jsonpath.internal.JsonContext) JSONArray(net.minidev.json.JSONArray) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) DocumentContext(com.jayway.jsonpath.DocumentContext)

Example 2 with JsonContext

use of com.jayway.jsonpath.internal.JsonContext in project mica2 by obiba.

the class SchemaFormContentFileService method deleteFiles.

public void deleteFiles(SchemaFormContentAware entity) {
    Object json = defaultConfiguration().jsonProvider().parse(entity.getContent());
    DocumentContext context = JsonPath.using(defaultConfiguration().addOptions(Option.AS_PATH_LIST)).parse(json);
    DocumentContext reader = new JsonContext(defaultConfiguration().addOptions(Option.REQUIRE_PROPERTIES)).parse(json);
    try {
        ((JSONArray) context.read("$..obibaFiles")).stream().map(p -> (JSONArray) reader.read(p.toString())).flatMap(Collection::stream).forEach(file -> fileStoreService.delete(((LinkedHashMap) file).get("id").toString()));
    } catch (PathNotFoundException e) {
    }
}
Also used : JsonContext(com.jayway.jsonpath.internal.JsonContext) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) DocumentContext(com.jayway.jsonpath.DocumentContext)

Aggregations

DocumentContext (com.jayway.jsonpath.DocumentContext)2 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)2 JsonContext (com.jayway.jsonpath.internal.JsonContext)2 Sets (com.google.common.collect.Sets)1 Configuration.defaultConfiguration (com.jayway.jsonpath.Configuration.defaultConfiguration)1 JsonPath (com.jayway.jsonpath.JsonPath)1 Option (com.jayway.jsonpath.Option)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1 NotNull (javax.validation.constraints.NotNull)1 JSONArray (net.minidev.json.JSONArray)1 SchemaFormContentAware (org.obiba.mica.core.domain.SchemaFormContentAware)1 FileStoreService (org.obiba.mica.file.FileStoreService)1 Service (org.springframework.stereotype.Service)1 Assert (org.springframework.util.Assert)1