Search in sources :

Example 1 with EntityConfig

use of org.obiba.mica.micaConfig.domain.EntityConfig 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

Configuration (com.jayway.jsonpath.Configuration)1 JsonPath (com.jayway.jsonpath.JsonPath)1 Option (com.jayway.jsonpath.Option)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1 JSONArray (net.minidev.json.JSONArray)1 JsonTranslator (org.obiba.core.translator.JsonTranslator)1 PrefixedValueTranslator (org.obiba.core.translator.PrefixedValueTranslator)1 TranslationUtils (org.obiba.core.translator.TranslationUtils)1 Translator (org.obiba.core.translator.Translator)1 RegexHashMap (org.obiba.mica.core.support.RegexHashMap)1 DataCollectionEventConfig (org.obiba.mica.micaConfig.domain.DataCollectionEventConfig)1 EntityConfig (org.obiba.mica.micaConfig.domain.EntityConfig)1 HarmonizationDatasetConfig (org.obiba.mica.micaConfig.domain.HarmonizationDatasetConfig)1