2  * ============LICENSE_START=======================================================
 
   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
 
  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=========================================================
 
  21 package org.onap.so.adapters.catalogdb.catalogrest;
 
  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.Collections;
 
  30 import java.util.HashMap;
 
  31 import java.util.List;
 
  34 @XmlRootElement(name = "serviceInfo")
 
  35 public class QueryServiceInfo extends CatalogQuery {
 
  37     protected static Logger logger = LoggerFactory.getLogger(QueryServiceInfo.class);
 
  39     private ServiceInfo serviceInfo;
 
  41     private static final String TEMPLATE =
 
  42             "\n" + "\t{" + "\t\t\"id\"              : <ID>,\n" + "\t\t\"serviceInput\"     : <SERVICE_INPUT>,\n"
 
  43                     + "\t\"serviceProperties\"            : <SERVICE_PROPERTIES>,\n" + "<_SERVICEARTIFACT_>\n";
 
  46     public QueryServiceInfo() {
 
  48         this.serviceInfo = new ServiceInfo();
 
  51     public QueryServiceInfo(List<ServiceInfo> serviceInfos) {
 
  52         if (!CollectionUtils.isEmpty(serviceInfos)) {
 
  53             this.serviceInfo = serviceInfos.get(0);
 
  57     public ServiceInfo getServiceInfo() {
 
  58         return this.serviceInfo;
 
  61     public void setServiceInfo(ServiceInfo serviceInfo) {
 
  62         this.serviceInfo = serviceInfo;
 
  66     public String toString() {
 
  68         return serviceInfo.toString();
 
  72     public String JSON2(boolean isArray, boolean isEmbed) {
 
  73         if (serviceInfo == null) {
 
  74             return "\"serviceInfo\": null";
 
  76         StringBuilder sb = new StringBuilder();
 
  77         sb.append("\"serviceInfo\": ");
 
  79         Map<String, String> valueMap = new HashMap<>();
 
  80         Service service = serviceInfo.getService();
 
  81         put(valueMap, "ID", null == serviceInfo ? null : serviceInfo.getId());
 
  82         put(valueMap, "SERVICE_INPUT", null == serviceInfo ? null : serviceInfo.getServiceInput());
 
  83         put(valueMap, "SERVICE_PROPERTIES", null == serviceInfo ? null : serviceInfo.getServiceProperties());
 
  84         String subitem = new QueryServiceArtifact(service.getServiceArtifactList()).JSON2(true, true);
 
  85         valueMap.put("_SERVICEARTIFACT_", subitem.replaceAll("(?m)^", "\t\t"));
 
  86         sb.append(this.setTemplate(TEMPLATE, valueMap));