Search in sources :

Example 1 with RegexHashMap

use of org.obiba.mica.core.support.RegexHashMap in project mica2 by obiba.

the class EntityConfigKeyTranslationService method getCompleteConfigTranslationMap.

public RegexHashMap getCompleteConfigTranslationMap(String serviceTypename, String locale) {
    RegexHashMap translationMap = new RegexHashMap();
    Translator translator = JsonTranslator.buildSafeTranslator(() -> micaConfigService.getTranslations(locale, false));
    beanFieldToTranslationKeyMap.forEach((key, translationKey) -> translationMap.put(key, translator.translate(translationKey)));
    switch(serviceTypename) {
        case "individual-study":
            Optional<StudyConfig> optionalIndividualStudySchemaForm = individualStudyConfigService.findComplete();
            Optional<PopulationConfig> optionalPopulationSchemaForm = populationConfigService.findComplete();
            Optional<DataCollectionEventConfig> optionalDataCollectionEventSchemaForm = dataCollectionEventConfigService.findComplete();
            if (optionalIndividualStudySchemaForm.isPresent()) {
                StudyConfig individualStudySchemaForm = optionalIndividualStudySchemaForm.get();
                translateSchemaForm(translator, individualStudySchemaForm);
                translationMap.putAll(getTranslationMap(individualStudySchemaForm, ""));
            }
            if (optionalPopulationSchemaForm.isPresent()) {
                PopulationConfig populationSchemaForm = optionalPopulationSchemaForm.get();
                translateSchemaForm(translator, populationSchemaForm);
                translationMap.putAll(getTranslationMap(populationSchemaForm, "^populations\\[\\d+\\]\\."));
            }
            if (optionalDataCollectionEventSchemaForm.isPresent()) {
                DataCollectionEventConfig dataCollectionEventSchemaForm = optionalDataCollectionEventSchemaForm.get();
                translateSchemaForm(translator, dataCollectionEventSchemaForm);
                translationMap.putAll(getTranslationMap(dataCollectionEventSchemaForm, "^populations\\[\\d+\\]\\.dataCollectionEvents\\[\\d+\\]\\."));
            }
            break;
        case "harmonization-study":
            Optional<HarmonizationStudyConfig> optionalHarmonizationStudySchemaForm = harmonizationStudyConfigService.findComplete();
            Optional<HarmonizationPopulationConfig> optionalHarmonizationPopulationSchemaForm = harmonizationPopulationConfigService.findComplete();
            if (optionalHarmonizationStudySchemaForm.isPresent()) {
                HarmonizationStudyConfig harmonizationStudySchemaForm = optionalHarmonizationStudySchemaForm.get();
                translateSchemaForm(translator, harmonizationStudySchemaForm);
                translationMap.putAll(getTranslationMap(harmonizationStudySchemaForm, ""));
            }
            if (optionalHarmonizationPopulationSchemaForm.isPresent()) {
                HarmonizationPopulationConfig harmonizationPopulationSchemaForm = optionalHarmonizationPopulationSchemaForm.get();
                translateSchemaForm(translator, harmonizationPopulationSchemaForm);
                translationMap.putAll(getTranslationMap(harmonizationPopulationSchemaForm, "^populations\\[\\d+\\]\\."));
            }
            break;
        case "network":
            Optional<NetworkConfig> optionalNetworkSchemaForm = networkConfigService.findComplete();
            if (optionalNetworkSchemaForm.isPresent()) {
                NetworkConfig networkSchemaForm = optionalNetworkSchemaForm.get();
                translateSchemaForm(translator, networkSchemaForm);
                translationMap.putAll(getTranslationMap(networkSchemaForm, ""));
            }
            break;
        case "collected-dataset":
            Optional<StudyDatasetConfig> optionalStudyDatasetSchemaForm = studyDatasetConfigService.findComplete();
            if (optionalStudyDatasetSchemaForm.isPresent()) {
                StudyDatasetConfig studyDatasetSchemaForm = optionalStudyDatasetSchemaForm.get();
                translateSchemaForm(translator, studyDatasetSchemaForm);
                translationMap.putAll(getTranslationMap(studyDatasetSchemaForm, ""));
            }
            break;
        case "harmonized-dataset":
            Optional<HarmonizationDatasetConfig> optionalHarmonizationDatasetSchemaForm = harmonizationDatasetConfigService.findComplete();
            if (optionalHarmonizationDatasetSchemaForm.isPresent()) {
                HarmonizationDatasetConfig harmonizationDatasetSchemaForm = optionalHarmonizationDatasetSchemaForm.get();
                translateSchemaForm(translator, harmonizationDatasetSchemaForm);
                translationMap.putAll(getTranslationMap(harmonizationDatasetSchemaForm, ""));
            }
            break;
        case "project":
            Optional<ProjectConfig> optionalProjectSchemaForm = projectConfigService.findComplete();
            if (optionalProjectSchemaForm.isPresent()) {
                ProjectConfig projectSchemaForm = optionalProjectSchemaForm.get();
                translateSchemaForm(translator, projectSchemaForm);
                translationMap.putAll(getTranslationMap(projectSchemaForm, ""));
            }
            break;
        default:
            break;
    }
    return translationMap;
}
Also used : RegexHashMap(org.obiba.mica.core.support.RegexHashMap) PopulationConfig(org.obiba.mica.micaConfig.domain.PopulationConfig) HarmonizationPopulationConfig(org.obiba.mica.micaConfig.domain.HarmonizationPopulationConfig) StudyDatasetConfig(org.obiba.mica.micaConfig.domain.StudyDatasetConfig) NetworkConfig(org.obiba.mica.micaConfig.domain.NetworkConfig) DataCollectionEventConfig(org.obiba.mica.micaConfig.domain.DataCollectionEventConfig) HarmonizationDatasetConfig(org.obiba.mica.micaConfig.domain.HarmonizationDatasetConfig) ProjectConfig(org.obiba.mica.micaConfig.domain.ProjectConfig) HarmonizationPopulationConfig(org.obiba.mica.micaConfig.domain.HarmonizationPopulationConfig) StudyConfig(org.obiba.mica.micaConfig.domain.StudyConfig) HarmonizationStudyConfig(org.obiba.mica.micaConfig.domain.HarmonizationStudyConfig) JsonTranslator(org.obiba.core.translator.JsonTranslator) PrefixedValueTranslator(org.obiba.core.translator.PrefixedValueTranslator) Translator(org.obiba.core.translator.Translator) HarmonizationStudyConfig(org.obiba.mica.micaConfig.domain.HarmonizationStudyConfig)

Example 2 with RegexHashMap

use of org.obiba.mica.core.support.RegexHashMap in project mica2 by obiba.

the class AbstractGitPersistableResource method diff.

@GET
@Path("/_diff")
public Response diff(@NotNull @QueryParam("left") String left, @NotNull @QueryParam("right") String right, @QueryParam("locale") @DefaultValue("en") String locale) {
    checkPermission("/draft/" + getService().getTypeName(), "VIEW");
    T1 leftCommit = getService().getFromCommit(getService().findDraft(getId()), left);
    T1 rightCommit = getService().getFromCommit(getService().findDraft(getId()), right);
    Map<String, Object> data = new HashMap<>();
    try {
        MapDifference<String, Object> difference = DocumentDifferenceService.diff(leftCommit, rightCommit);
        RegexHashMap completeConfigTranslationMap = entityConfigKeyTranslationService.getCompleteConfigTranslationMap(getService().getTypeName(), locale);
        data = DocumentDifferenceService.withTranslations(difference, completeConfigTranslationMap);
    } catch (JsonProcessingException e) {
    // 
    }
    return Response.ok(data, MediaType.APPLICATION_JSON_TYPE).build();
}
Also used : RegexHashMap(org.obiba.mica.core.support.RegexHashMap) HashMap(java.util.HashMap) RegexHashMap(org.obiba.mica.core.support.RegexHashMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 3 with RegexHashMap

use of org.obiba.mica.core.support.RegexHashMap in project mica2 by obiba.

the class DocumentDifferenceService method flatten.

public static Map<String, Object> flatten(Object object) throws JsonProcessingException {
    Map<String, Object> map = new RegexHashMap();
    if (object == null)
        return map;
    final String string = mapper.writeValueAsString(object);
    JSONArray array = JsonPath.using(Configuration.builder().options(Option.AS_PATH_LIST).build()).parse(string).read("$..*");
    array.stream().map(Object::toString).filter(key -> !key.startsWith("$['logo']") && !key.startsWith("$['createdDate']") && !key.startsWith("$['lastModifiedDate']") && !key.startsWith("$['membershipSortOrder']")).forEach(key -> {
        Object read = JsonPath.parse(string).read(key);
        if (read != null && !(read instanceof Map) && !(read instanceof List)) {
            String processedKey = key.replaceAll("(\\$\\[')|('\\])", "").replaceAll("(\\[')", ".");
            map.put(processedKey, read);
        }
    });
    return map;
}
Also used : Arrays(java.util.Arrays) ValueDifference(com.google.common.collect.MapDifference.ValueDifference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashMap(java.util.HashMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Option(com.jayway.jsonpath.Option) JsonPath(com.jayway.jsonpath.JsonPath) Maps(com.google.common.collect.Maps) MapDifference(com.google.common.collect.MapDifference) List(java.util.List) Configuration(com.jayway.jsonpath.Configuration) JSONArray(net.minidev.json.JSONArray) Map(java.util.Map) RegexHashMap(org.obiba.mica.core.support.RegexHashMap) RegexHashMap(org.obiba.mica.core.support.RegexHashMap) JSONArray(net.minidev.json.JSONArray) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) RegexHashMap(org.obiba.mica.core.support.RegexHashMap)

Example 4 with RegexHashMap

use of org.obiba.mica.core.support.RegexHashMap in project mica2 by obiba.

the class EntityConfigKeyTranslationService method getTranslationMap.

private RegexHashMap getTranslationMap(EntityConfig config, String prefix) {
    String string = config.getSchema();
    JSONArray normalArray = JsonPath.using(Configuration.builder().options(Option.AS_PATH_LIST, Option.ALWAYS_RETURN_LIST, Option.SUPPRESS_EXCEPTIONS).build()).parse(string).read("$..*");
    JSONArray itemsArray = JsonPath.using(Configuration.builder().options(Option.AS_PATH_LIST, Option.ALWAYS_RETURN_LIST, Option.SUPPRESS_EXCEPTIONS).build()).parse(string).read("$..items");
    RegexHashMap map = new RegexHashMap();
    String joinedLocales = getJoinedLocales();
    normalArray.stream().map(Object::toString).filter(key -> key.endsWith("['title']")).forEach(key -> {
        Object read = JsonPath.parse(string).read(key);
        if (read != null) {
            String cleanKey = key.replaceAll("(\\$\\[')|('\\])", "").replaceAll("(\\[')", ".").replaceAll("\\.title$", "").replaceAll("^properties\\.", "").replaceAll("\\.properties", "");
            String processedKey = (cleanKey.startsWith("_") ? prefix : prefix + "model\\.") + Pattern.quote((cleanKey.startsWith("_") ? cleanKey.substring(1) : cleanKey)) + "(\\[\\d+\\])?" + "(" + joinedLocales + ")?";
            map.put(processedKey, read.toString());
        }
    });
    itemsArray.stream().map(Object::toString).forEach(itemkey -> {
        Object read = JsonPath.parse(string).read(itemkey);
        if (read != null && read instanceof List) {
            JSONArray array = (JSONArray) read;
            array.forEach(arrayItem -> {
                if (arrayItem != null && arrayItem instanceof Map) {
                    Map itemMap = (Map) arrayItem;
                    Object key = itemMap.get("key");
                    Object name = itemMap.get("name");
                    String cleanKey = itemkey.replaceAll("(\\$\\[')|('\\])", "").replaceAll("(\\[')", ".").replaceAll("\\.items$", "").replaceAll("^properties\\.", "").replaceAll("\\.properties", "") + "." + key.toString();
                    String processedKey = (cleanKey.startsWith("_") ? prefix : prefix + "model\\.") + Pattern.quote((cleanKey.startsWith("_") ? cleanKey.substring(1) : cleanKey));
                    map.put(processedKey, name);
                }
            });
        }
    });
    return map;
}
Also used : JsonTranslator(org.obiba.core.translator.JsonTranslator) Arrays(java.util.Arrays) PrefixedValueTranslator(org.obiba.core.translator.PrefixedValueTranslator) StudyDatasetConfig(org.obiba.mica.micaConfig.domain.StudyDatasetConfig) HashMap(java.util.HashMap) PopulationConfig(org.obiba.mica.micaConfig.domain.PopulationConfig) Inject(javax.inject.Inject) HarmonizationPopulationConfig(org.obiba.mica.micaConfig.domain.HarmonizationPopulationConfig) StudyConfig(org.obiba.mica.micaConfig.domain.StudyConfig) HarmonizationStudyConfig(org.obiba.mica.micaConfig.domain.HarmonizationStudyConfig) Configuration(com.jayway.jsonpath.Configuration) Map(java.util.Map) RegexHashMap(org.obiba.mica.core.support.RegexHashMap) Translator(org.obiba.core.translator.Translator) ProjectConfig(org.obiba.mica.micaConfig.domain.ProjectConfig) HarmonizationDatasetConfig(org.obiba.mica.micaConfig.domain.HarmonizationDatasetConfig) DataCollectionEventConfig(org.obiba.mica.micaConfig.domain.DataCollectionEventConfig) EntityConfig(org.obiba.mica.micaConfig.domain.EntityConfig) Option(com.jayway.jsonpath.Option) JsonPath(com.jayway.jsonpath.JsonPath) Collectors(java.util.stream.Collectors) TranslationUtils(org.obiba.core.translator.TranslationUtils) List(java.util.List) Component(org.springframework.stereotype.Component) JSONArray(net.minidev.json.JSONArray) Optional(java.util.Optional) NetworkConfig(org.obiba.mica.micaConfig.domain.NetworkConfig) Pattern(java.util.regex.Pattern) RegexHashMap(org.obiba.mica.core.support.RegexHashMap) JSONArray(net.minidev.json.JSONArray) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) RegexHashMap(org.obiba.mica.core.support.RegexHashMap)

Aggregations

RegexHashMap (org.obiba.mica.core.support.RegexHashMap)4 HashMap (java.util.HashMap)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Configuration (com.jayway.jsonpath.Configuration)2 JsonPath (com.jayway.jsonpath.JsonPath)2 Option (com.jayway.jsonpath.Option)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Map (java.util.Map)2 JSONArray (net.minidev.json.JSONArray)2 JsonTranslator (org.obiba.core.translator.JsonTranslator)2 PrefixedValueTranslator (org.obiba.core.translator.PrefixedValueTranslator)2 Translator (org.obiba.core.translator.Translator)2 DataCollectionEventConfig (org.obiba.mica.micaConfig.domain.DataCollectionEventConfig)2 HarmonizationDatasetConfig (org.obiba.mica.micaConfig.domain.HarmonizationDatasetConfig)2 HarmonizationPopulationConfig (org.obiba.mica.micaConfig.domain.HarmonizationPopulationConfig)2 HarmonizationStudyConfig (org.obiba.mica.micaConfig.domain.HarmonizationStudyConfig)2 NetworkConfig (org.obiba.mica.micaConfig.domain.NetworkConfig)2 PopulationConfig (org.obiba.mica.micaConfig.domain.PopulationConfig)2 ProjectConfig (org.obiba.mica.micaConfig.domain.ProjectConfig)2