diff options
Diffstat (limited to 'aai-traversal/src/main/java/org/onap/aai/rest/util/ConvertQueryPropertiesToJson.java')
-rw-r--r-- | aai-traversal/src/main/java/org/onap/aai/rest/util/ConvertQueryPropertiesToJson.java | 180 |
1 files changed, 82 insertions, 98 deletions
diff --git a/aai-traversal/src/main/java/org/onap/aai/rest/util/ConvertQueryPropertiesToJson.java b/aai-traversal/src/main/java/org/onap/aai/rest/util/ConvertQueryPropertiesToJson.java index e8d1547..3bf9087 100644 --- a/aai-traversal/src/main/java/org/onap/aai/rest/util/ConvertQueryPropertiesToJson.java +++ b/aai-traversal/src/main/java/org/onap/aai/rest/util/ConvertQueryPropertiesToJson.java @@ -8,7 +8,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -32,102 +32,86 @@ import java.util.Properties; import org.onap.aai.util.AAIConstants; public class ConvertQueryPropertiesToJson { - - private static final int maxfilesize = 256000; - - private void addStart(StringBuilder sb) { - sb.append("{\n \"stored-queries\":[{\n"); - } - - private void addRequiredQueryProperties(StringBuilder sb, List<String> rqd) { - Iterator it = rqd.iterator(); - sb.append(" \"query\":{\n \"required-properties\":["); - while (it.hasNext()) { - sb.append("\"" + it.next() + "\""); - if (it.hasNext()) { - sb.append(","); - } - } - sb.append("]\n },\n"); - } - - private void addAnotherQuery(StringBuilder sb, String queryName, String query, List<String> rqd) { - sb.append(" \"" + queryName + "\":{\n"); - if (!rqd.isEmpty()) { - addRequiredQueryProperties(sb, rqd); - } - sb.append(" \"stored-query\":\"" + query + "\"\n }\n },{\n"); - } - - private void addLastQuery(StringBuilder sb, String queryName, String query, List<String> rqd) { - sb.append(" \"" + queryName + "\":{\n"); - if (!rqd.isEmpty()) { - addRequiredQueryProperties(sb, rqd); - } - sb.append(" \"stored-query\":\"" + query + "\"\n }\n }]\n}\n"); - } - - private String get2ndParameter(String paramString) { - String endParams = paramString.substring(0, paramString.indexOf(')')); - String result = endParams.substring(endParams.indexOf(',') + 1); - String lastParam = result.trim(); - if (lastParam.startsWith("\\") || lastParam.startsWith("'") || lastParam.startsWith("new ")) { - return null; - } - - return lastParam; - } - - private List<String> findRqdProperties(String query) { - String[] parts = query.split("getVerticesByProperty"); - List<String> result = new ArrayList<String>(); - if (parts.length == 1) { - return result; - } - int count = 0; - String foundRqdProperty; - while (count++ < parts.length - 1) { - foundRqdProperty = get2ndParameter(parts[count]); - if (foundRqdProperty != null && !result.contains(foundRqdProperty)) { - result.add(foundRqdProperty); - } - } - return result; - } - - public String convertProperties(Properties props) { - Enumeration<?> e = props.propertyNames(); - StringBuilder sb = new StringBuilder(maxfilesize); - String queryName; - String query; - addStart(sb); - List<String> rqd; - while (e.hasMoreElements()) { - queryName = (String) e.nextElement(); - query = props.getProperty(queryName).trim().replace("\"", "\\\""); - rqd = findRqdProperties(query); - if (e.hasMoreElements()) { - addAnotherQuery(sb, queryName, query, rqd); - } else { - addLastQuery(sb, queryName, query, rqd); - } - } - + + private final static int maxfilesize = 256000; + + private void addStart( StringBuilder sb ) { + sb.append("{\n \"stored-queries\":[{\n"); + } + + private void addRequiredQueryProperties( StringBuilder sb, List<String> rqd ) { + Iterator it = rqd.iterator(); + sb.append(" \"query\":{\n \"required-properties\":["); + while( it.hasNext()) { + sb.append("\"" + it.next() + "\""); + if ( it.hasNext()) { + sb.append(","); + } + } + sb.append("]\n },\n"); + } + + private void addAnotherQuery( StringBuilder sb, String queryName, String query, List<String> rqd ) { + sb.append(" \"" + queryName + "\":{\n"); + if ( !rqd.isEmpty()) { + addRequiredQueryProperties( sb, rqd); + } + sb.append(" \"stored-query\":\"" + query + "\"\n }\n },{\n"); + } + + private void addLastQuery( StringBuilder sb, String queryName, String query, List<String> rqd ) { + sb.append(" \"" + queryName + "\":{\n"); + if ( !rqd.isEmpty() ) { + addRequiredQueryProperties( sb, rqd); + } + sb.append(" \"stored-query\":\"" + query + "\"\n }\n }]\n}\n"); + } + + private String get2ndParameter( String paramString) { + String endParams = paramString.substring(0, paramString.indexOf(')')); + String result = endParams.substring(endParams.indexOf(',') + 1 ); + String lastParam = result.trim(); + if ( lastParam.startsWith("\\") || lastParam.startsWith("'") || lastParam.startsWith("new ") ){ + return null; + } + + return lastParam; + } + + private List<String> findRqdProperties( String query) { + String[] parts = query.split("getVerticesByProperty"); + List<String> result = new ArrayList<String>(); + if ( parts.length == 1 ) + return result; + int count = 0; + String foundRqdProperty; + while ( count++ < parts.length - 1 ) { + foundRqdProperty = get2ndParameter(parts[count]); + if ( foundRqdProperty != null && !result.contains(foundRqdProperty)) { + result.add(foundRqdProperty); + } + } + return result; + } + + public String convertProperties( Properties props ) { + Enumeration<?> e = props.propertyNames(); + StringBuilder sb = new StringBuilder(maxfilesize); + String queryName; + String query; + addStart( sb ); + List<String> rqd; + while ( e.hasMoreElements()) { + queryName = (String)e.nextElement(); + query = props.getProperty(queryName).trim().replace("\"", "\\\""); + rqd = findRqdProperties( query); + if ( e.hasMoreElements()) { + addAnotherQuery( sb, queryName, query, rqd); + } else { + addLastQuery( sb, queryName, query, rqd); + } + } + return sb.toString(); - } - - public static void main(String[] args) { - File queryFile = new File(AAIConstants.AAI_HOME_ETC_QUERY); - Properties properties = new Properties(); - try (FileInputStream fis = new FileInputStream(queryFile)) { - properties.load(fis); - } catch (IOException e) { - e.printStackTrace(); - System.out.println("Error occurred during the processing of query file: " + e); - } - ConvertQueryPropertiesToJson c = new ConvertQueryPropertiesToJson(); - String json = c.convertProperties(properties); - System.out.println("returned json:\n" + json); - } - + } } |