diff options
author | Dan Timoney <dtimoney@att.com> | 2023-11-01 19:06:25 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2023-11-01 19:06:25 +0000 |
commit | 44ee391fe4c0f890346ee651a57ef028fa119b0c (patch) | |
tree | 72137395a38cb6d7bb9afafd9ce59c64626964f9 /sdnr/wt/data-provider/dblib/src/main/java/org | |
parent | ea5ba02bdfd1ed6e16683b01f1f499cb2473e37e (diff) | |
parent | cb9bd650a1ba6c1c2f5c7dd410f640b9b6f876c7 (diff) |
Merge "Extend user settings API"
Diffstat (limited to 'sdnr/wt/data-provider/dblib/src/main/java/org')
-rw-r--r-- | sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/HtUserdataManagerBase.java | 101 |
1 files changed, 96 insertions, 5 deletions
diff --git a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/HtUserdataManagerBase.java b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/HtUserdataManagerBase.java index e015dd29f..d6ad35488 100644 --- a/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/HtUserdataManagerBase.java +++ b/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/data/HtUserdataManagerBase.java @@ -24,6 +24,7 @@ package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data; import java.io.File; import java.io.IOException; import java.nio.file.Files; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtUserdataManager; @@ -68,25 +69,115 @@ public abstract class HtUserdataManagerBase implements HtUserdataManager { @Override public String getUserdata(String username, String key) { + final String EMPTY_JSON_OBJECT = "{}"; JSONObject o = new JSONObject(this.getUserdata(username)); - return o.has(key) ? o.get(key).toString() : "{}"; + if(key==null || !key.contains(".")) + return o.has(key) ? o.get(key).toString() : EMPTY_JSON_OBJECT; + final String[] keys = key.split("\\."); + Object tmp; + for (int i=0;i<keys.length-1;i++){ + if(!o.has(keys[i])){ + return EMPTY_JSON_OBJECT; + } + tmp = o.get(keys[i]); + if(!(tmp instanceof JSONObject)) { + return EMPTY_JSON_OBJECT; + } + o = (JSONObject)tmp; + } + tmp= o.has(keys[keys.length-1])? o.get(keys[keys.length-1]):EMPTY_JSON_OBJECT; + if(tmp instanceof JSONObject){ + return ((JSONObject)tmp).toString(); + } + if(tmp instanceof JSONArray){ + return ((JSONArray)tmp).toString(); + } + if( tmp instanceof String){ + return String.format("\"%s\"",tmp); + } + return String.valueOf(tmp); } @Override public boolean setUserdata(String username, String key, String data) { + if(key == null || data == null){ + return false; + } JSONObject o = new JSONObject(this.getUserdata(username)); - o.put(key, new JSONObject(data)); + if(!key.contains(".")) { + o.put(key, new JSONObject(data)); + } + else{ + final String[] keys = key.split("\\."); + JSONObject tmp = o; + Object tmpObject; + for(int i=0;i<keys.length-1;i++){ + if(!tmp.has(keys[i])){ + tmp.put(keys[i], new JSONObject()); + } + tmpObject = tmp.get(keys[i]); + if(!(tmpObject instanceof JSONObject)){ + return false; + } + tmp = (JSONObject) tmpObject; + } + if(data.startsWith("{")){ + tmp.put(keys[keys.length-1], new JSONObject(data)); + } + else if (data.startsWith("[")){ + tmp.put(keys[keys.length-1], new JSONArray(data)); + } + else if(data.startsWith("\"") && data.endsWith("\"")){ + tmp.put(keys[keys.length-1],data.substring(1,data.length()-1)); + } + else if("true".equals(data) || "false".equals(data)){ + tmp.put(keys[keys.length-1],"true".equals(data)); + } + else { + try { + tmp.put(keys[keys.length - 1], Double.parseDouble(data)); + } catch (NumberFormatException e) { + return false; + } + } + } return this.setUserdata(username, o.toString()); } @Override public boolean removeUserdata(String username, String key) { + if(key == null){ + return false; + } JSONObject o = new JSONObject(this.getUserdata(username)); - if (o.has(key)) { - o.remove(key); + if(!key.contains(".")) { + if (o.has(key)) { + o.remove(key); + return this.setUserdata(username, o.toString()); + } + } + else { + final String[] keys = key.split("\\."); + JSONObject tmp = o; + Object tmpObject; + for(int i=0;i<keys.length-1;i++){ + if(!tmp.has(keys[i])){ + return false; + } + tmpObject = tmp.get(keys[i]); + if(!(tmpObject instanceof JSONObject)){ + return false; + } + tmp = (JSONObject) tmpObject; + } + if(!tmp.has(keys[keys.length-1])){ + return false; + } + tmp.remove(keys[keys.length-1]); return this.setUserdata(username, o.toString()); + } - return true; + return false; } protected static JSONObject mergeData(JSONObject o, String key, JSONObject subObject) { |