diff options
Diffstat (limited to 'sdnr/wt/data-provider/dblib')
2 files changed, 96 insertions, 12 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) { diff --git a/sdnr/wt/data-provider/dblib/test2.properties b/sdnr/wt/data-provider/dblib/test2.properties deleted file mode 100644 index e3485ea95..000000000 --- a/sdnr/wt/data-provider/dblib/test2.properties +++ /dev/null @@ -1,7 +0,0 @@ -[mariadb] -url=${SDNRDBURL} -username=${SDNRDBUSERNAME} -password=${SDNRDBPASSWORD} -controllerId=null -suffix= - |