2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.aai.rest.util;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Enumeration;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Properties;
32 import org.onap.aai.util.AAIConstants;
34 public class ConvertQueryPropertiesToJson {
36 private final static int maxfilesize = 256000;
38 private void addStart( StringBuilder sb ) {
39 sb.append("{\n \"stored-queries\":[{\n");
42 private void addRequiredQueryProperties( StringBuilder sb, List<String> rqd ) {
43 Iterator it = rqd.iterator();
44 sb.append(" \"query\":{\n \"required-properties\":[");
45 while( it.hasNext()) {
46 sb.append("\"" + it.next() + "\"");
51 sb.append("]\n },\n");
54 private void addAnotherQuery( StringBuilder sb, String queryName, String query, List<String> rqd ) {
55 sb.append(" \"" + queryName + "\":{\n");
56 if ( !rqd.isEmpty()) {
57 addRequiredQueryProperties( sb, rqd);
59 sb.append(" \"stored-query\":\"" + query + "\"\n }\n },{\n");
62 private void addLastQuery( StringBuilder sb, String queryName, String query, List<String> rqd ) {
63 sb.append(" \"" + queryName + "\":{\n");
64 if ( !rqd.isEmpty() ) {
65 addRequiredQueryProperties( sb, rqd);
67 sb.append(" \"stored-query\":\"" + query + "\"\n }\n }]\n}\n");
70 private String get2ndParameter( String paramString) {
71 String endParams = paramString.substring(0, paramString.indexOf(')'));
72 String result = endParams.substring(endParams.indexOf(',') + 1 );
73 String lastParam = result.trim();
74 if ( lastParam.startsWith("\\") || lastParam.startsWith("'") || lastParam.startsWith("new ") ){
81 private List<String> findRqdProperties( String query) {
82 String[] parts = query.split("getVerticesByProperty");
83 List<String> result = new ArrayList<String>();
84 if ( parts.length == 1 )
87 String foundRqdProperty;
88 while ( count++ < parts.length - 1 ) {
89 foundRqdProperty = get2ndParameter(parts[count]);
90 if ( foundRqdProperty != null && !result.contains(foundRqdProperty)) {
91 result.add(foundRqdProperty);
97 public String convertProperties( Properties props ) {
98 Enumeration<?> e = props.propertyNames();
99 StringBuilder sb = new StringBuilder(maxfilesize);
104 while ( e.hasMoreElements()) {
105 queryName = (String)e.nextElement();
106 query = props.getProperty(queryName).trim().replace("\"", "\\\"");
107 rqd = findRqdProperties( query);
108 if ( e.hasMoreElements()) {
109 addAnotherQuery( sb, queryName, query, rqd);
111 addLastQuery( sb, queryName, query, rqd);
115 return sb.toString();
117 public static void main(String[] args) {
118 File queryFile = new File(AAIConstants.AAI_HOME_ETC_QUERY);
119 Properties properties = new Properties();
120 try (FileInputStream fis = new FileInputStream(queryFile)){
121 properties.load(fis);
122 } catch (IOException e) {
124 System.out.println("Error occurred during the processing of query file: " + e);
126 ConvertQueryPropertiesToJson c = new ConvertQueryPropertiesToJson();
127 String json = c.convertProperties(properties);
128 System.out.println("returned json:\n" + json);