Change the header to SO
[so.git] / adapters / mso-catalog-db-adapter / src / main / java / org / openecomp / mso / adapters / catalogdb / catalogrest / CatalogQuery.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.mso.adapters.catalogdb.catalogrest;
21
22 import org.openecomp.mso.logger.MsoLogger;
23 import org.codehaus.jackson.map.ObjectMapper;
24
25 import java.util.Map;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28
29 public abstract class CatalogQuery {
30         protected static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
31         private static final boolean IS_EMBED = true;
32
33         public abstract String JSON2(boolean isArray, boolean isEmbed);
34
35         protected void put(Map<String, String> valueMap, String key, String value) {
36                 valueMap.put(key, value == null? "null": '"'+ value+ '"');
37         }
38
39         protected void put(Map<String, String> valueMap, String key, Integer value) {
40                 valueMap.put(key, value == null? "null": value.toString());
41         }
42
43         protected void put(Map<String, String> valueMap, String key, Boolean value) {
44                 valueMap.put(key, value == null? "null": value? "true": "false");
45         }
46
47         protected String setTemplate(String template, Map<String, String> valueMap) {
48                 LOGGER.debug ("CatalogQuery setTemplate");
49                 StringBuffer result = new StringBuffer();
50
51                 String pattern = "<.*>";
52                 Pattern r = Pattern.compile(pattern);
53                 Matcher m = r.matcher(template);
54
55                 LOGGER.debug ("CatalogQuery template:"+ template);
56                 while(m.find()) {
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\"");
60                 }
61                 m.appendTail(result);
62                 LOGGER.debug ("CatalogQuery return:"+ result.toString());
63                 return result.toString();
64         }
65
66         /**
67          * The simple, clean, generic way to handle the interface
68          */
69         protected String smartToJSON() {
70                 String jsonString = null;
71                 try {
72                         ObjectMapper mapper = new ObjectMapper();
73                         jsonString = mapper.writeValueAsString(this);
74                 }
75                 catch (Exception e) {
76                         LOGGER.debug ("jsonString exception:"+e.getMessage());
77                         jsonString = "invalid"; //throws instead?
78                 }
79                 return jsonString;
80         }
81
82         public String toJsonString(String version, boolean isArray) {
83                 switch(version) {
84                 case "v1": return smartToJSON();
85                 case "v2": return JSON2(isArray, !IS_EMBED);
86                 default:
87                         return ("invalid version: "+ version);
88                 }
89         }
90 }