2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.openecomp.mso.adapters.catalogdb.catalogrest;
22 import org.openecomp.mso.logger.MsoLogger;
23 import org.codehaus.jackson.map.ObjectMapper;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
29 public abstract class CatalogQuery {
30 protected static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
31 private static final boolean IS_EMBED = true;
33 public abstract String JSON2(boolean isArray, boolean isEmbed);
35 protected void put(Map<String, String> valueMap, String key, String value) {
36 valueMap.put(key, value == null? "null": '"'+ value+ '"');
39 protected void put(Map<String, String> valueMap, String key, Integer value) {
40 valueMap.put(key, value == null? "null": value.toString());
43 protected void put(Map<String, String> valueMap, String key, Boolean value) {
44 valueMap.put(key, value == null? "null": value? "true": "false");
47 protected String setTemplate(String template, Map<String, String> valueMap) {
48 LOGGER.debug ("CatalogQuery setTemplate");
49 StringBuffer result = new StringBuffer();
51 String pattern = "<.*>";
52 Pattern r = Pattern.compile(pattern);
53 Matcher m = r.matcher(template);
55 LOGGER.debug ("CatalogQuery template:"+ template);
57 String key = template.substring(m.start()+1, m.end()-1);
58 LOGGER.debug ("CatalogQuery key:"+ key+ " contains key? "+ valueMap.containsKey(key));
59 m.appendReplacement(result, valueMap.containsKey(key)? valueMap.get(key): "\"TBD\"");
62 LOGGER.debug ("CatalogQuery return:"+ result.toString());
63 return result.toString();
67 * The simple, clean, generic way to handle the interface
69 protected String smartToJSON() {
70 String jsonString = null;
72 ObjectMapper mapper = new ObjectMapper();
73 jsonString = mapper.writeValueAsString(this);
76 LOGGER.debug ("jsonString exception:"+e.getMessage());
77 jsonString = "invalid"; //throws instead?
82 public String toJsonString(String version, boolean isArray) {
84 case "v1": return smartToJSON();
85 case "v2": return JSON2(isArray, !IS_EMBED);
87 return ("invalid version: "+ version);