package com.til.tcc.manager.mongo.entity; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.til.tcc.manager.mongo.config.MongoCollections; import lombok.*; import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.mapping.Document; import java.util.HashMap; import java.util.Map; import java.util.Set; @Data @JsonInclude(value = Include.NON_NULL) @Document(collection = MongoCollections.COLLECTION_AUFS_TRANSLATION) @AllArgsConstructor @NoArgsConstructor public class AufsConfigTranslationEntity extends BaseMongoEntity { //Default Values public static final String IN = "IN"; public static final String DARK = "dark"; public static final String LIGHT = "light"; public static final String INR = "INR"; //Keys Query Node public static final String PC = "pc"; public static final String PLATFORM = "pfm"; public static final String KEY = "key"; public static final String CC = "cc"; public static final String LID = "langId"; public static final String FV = "fv"; public static final String THEME = "theme"; public static final String IS_DEFAULT = "isDefault"; public static final String ENABLE = "enable"; public static final String DELETED = "deleted"; public static final String ID = "_id"; //Data Property @Indexed private String pc; @Indexed private String pfm; @Indexed private String client; @Indexed private String key; @Indexed private Set<String> cc; @Indexed private Set<Integer> lid; @Indexed private Integer fv; @Indexed private Set<String> theme; @Indexed private Boolean isDefault; @Indexed protected Boolean enable = false; @Indexed protected Boolean deleted = false; private Map<String, Object> rootTrans; public void setRootTrans(Map<String, Object> rootTrans) { if (rootTrans != null) { this.rootTrans = new HashMap<>(); rootTrans.forEach((key, value) -> { // Convert dot notation to nested maps if (key.contains(".")) { createNestedStructure(key, value); } else { this.rootTrans.put(key, value); } }); } else { this.rootTrans = null; } } /** * Converts dot-notated keys into proper nested structure * Example: "test1.test2" -> {test1: {test2: value}} */ private void createNestedStructure(String keyPath, Object value) { String[] parts = keyPath.split("\\."); Map<String, Object> current = this.rootTrans; for (int i = 0; i < parts.length - 1; i++) { current = (Map<String, Object>) current.computeIfAbsent(parts[i], k -> new HashMap<>()); } current.put(parts[parts.length - 1], value); } /** * Gets a value by dot-notated path */ public Object getByPath(String path) { if (rootTrans == null || path == null) return null; String[] parts = path.split("\\."); Object current = rootTrans; for (String part : parts) { if (!(current instanceof Map)) return null; current = ((Map<?, ?>) current).get(part); } return current; } /** * Removes a value by dot-notated path */ public void removeByPath(String path) { if (rootTrans == null || path == null) return; String[] parts = path.split("\\."); Map<String, Object> current = rootTrans; for (int i = 0; i < parts.length - 1; i++) { if (!(current.get(parts[i]) instanceof Map)) return; current = (Map<String, Object>) current.get(parts[i]); } current.remove(parts[parts.length - 1]); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter