d08c2e144494698c38ccb56ae9b5962cc71d3a9f
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (c) 2019, CMCC Technologies Co., Ltd.
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
21 package org.onap.so.adapters.catalogdb.catalogrest;
22
23 import org.onap.so.db.catalog.beans.Service;
24 import org.onap.so.db.catalog.beans.ServiceInfo;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.util.CollectionUtils;
28 import javax.xml.bind.annotation.XmlRootElement;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 @XmlRootElement(name = "serviceInfo")
34 public class QueryServiceInfo extends CatalogQuery {
35
36     protected static Logger logger = LoggerFactory.getLogger(QueryServiceInfo.class);
37
38     private ServiceInfo serviceInfo;
39
40     private static final String TEMPLATE =
41             "\n" + "\t{" + "\t\t\"id\"              : <ID>,\n" + "\t\t\"serviceInput\"     : <SERVICE_INPUT>,\n"
42                     + "\t\"serviceProperties\"            : <SERVICE_PROPERTIES>,\n" + "<_SERVICEARTIFACT_>\n";
43
44
45     public QueryServiceInfo() {
46         super();
47         this.serviceInfo = new ServiceInfo();
48     }
49
50     public QueryServiceInfo(List<ServiceInfo> serviceInfos) {
51         if (!CollectionUtils.isEmpty(serviceInfos)) {
52             this.serviceInfo = serviceInfos.get(0);
53         }
54     }
55
56     public ServiceInfo getServiceInfo() {
57         return this.serviceInfo;
58     }
59
60     public void setServiceInfo(ServiceInfo serviceInfo) {
61         this.serviceInfo = serviceInfo;
62     }
63
64     @Override
65     public String toString() {
66
67         return serviceInfo.toString();
68     }
69
70     @Override
71     public String JSON2(boolean isArray, boolean isEmbed) {
72         if (serviceInfo == null) {
73             return "\"serviceInfo\": null";
74         }
75         StringBuilder sb = new StringBuilder();
76         sb.append("\"serviceInfo\": ");
77         sb.append("\n");
78         Map<String, String> valueMap = new HashMap<>();
79         Service service = serviceInfo.getService();
80         put(valueMap, "ID", null == serviceInfo ? null : serviceInfo.getId());
81         put(valueMap, "SERVICE_INPUT", null == serviceInfo ? null : serviceInfo.getServiceInput());
82         put(valueMap, "SERVICE_PROPERTIES", null == serviceInfo ? null : serviceInfo.getServiceProperties());
83         String subitem = new QueryServiceArtifact(service.getServiceArtifactList()).JSON2(true, true);
84         valueMap.put("_SERVICEARTIFACT_", subitem.replaceAll("(?m)^", "\t\t"));
85         sb.append(this.setTemplate(TEMPLATE, valueMap));
86         sb.append("}");
87         return sb.toString();
88     }
89 }