ce39b9713a253fc46c911f4c9917818d8b5d0399
[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.ServiceArtifact;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import javax.xml.bind.annotation.XmlRootElement;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 @XmlRootElement(name = "serviceArtifacts")
33 public class QueryServiceArtifact extends CatalogQuery {
34
35     protected static Logger logger = LoggerFactory.getLogger(QueryServiceArtifact.class);
36
37     private List<ServiceArtifact> serviceArtifactList;
38
39     private static final String TEMPLATE = "\t{\n" + "\t\t\"artifactUUID\"         : <ARTIFACT_UUID>,\n"
40             + "\t\t\"name\"                 : <NAME>,\n" + "\t\t\"version\"              : <VERSION>,\n"
41             + "\t\t\"checksum\"     : <CHECKSUM>,\n" + "\t\t\"type\"                  : <TYPE>,\n"
42             + "\t\t\"content\"     : <CONTENT>,\n" + "\t\t\"description\"          : <DESCRIPTION>\n" + "\t}";
43
44     public QueryServiceArtifact() {
45         super();
46         serviceArtifactList = new ArrayList<>();
47     }
48
49     public QueryServiceArtifact(List<ServiceArtifact> alist) {
50         serviceArtifactList = new ArrayList<>();
51         for (ServiceArtifact o : alist) {
52             if (logger.isDebugEnabled())
53                 logger.debug(o.toString());
54             serviceArtifactList.add(o);
55         }
56     }
57
58     public List<ServiceArtifact> getServiceArtifact() {
59         return this.serviceArtifactList;
60     }
61
62     public void setServiceArtifact(List<ServiceArtifact> a) {
63         this.serviceArtifactList = a;
64     }
65
66     @Override
67     public String toString() {
68         StringBuilder sb = new StringBuilder();
69
70         boolean first = true;
71         int i = 1;
72         for (ServiceArtifact o : serviceArtifactList) {
73             sb.append(i).append("\t");
74             if (!first)
75                 sb.append("\n");
76             first = false;
77             sb.append(o);
78         }
79         return sb.toString();
80     }
81
82     @Override
83     public String JSON2(boolean isArray, boolean isEmbed) {
84         StringBuilder sb = new StringBuilder();
85         if (!isEmbed && isArray)
86             sb.append("{ ");
87         if (isArray)
88             sb.append("\"serviceArtifact\": [");
89         Map<String, String> valueMap = new HashMap<>();
90         String sep = "";
91         boolean first = true;
92
93         for (ServiceArtifact o : serviceArtifactList) {
94             if (first)
95                 sb.append("\n");
96             first = false;
97
98             boolean vrNull = o == null;
99
100             put(valueMap, "ARTIFACT_UUID", vrNull ? null : o.getArtifactUUID());
101             put(valueMap, "TYPE", vrNull ? null : o.getType());
102             put(valueMap, "NAME", vrNull ? null : o.getName());
103             put(valueMap, "VERSION", vrNull ? null : o.getVersion());
104             put(valueMap, "DESCRIPTION", vrNull ? null : o.getDescription());
105             put(valueMap, "CONTENT", vrNull ? null : o.getContent());
106             put(valueMap, "CHECKSUM", vrNull ? null : o.getChecksum());
107             sb.append(sep).append(this.setTemplate(TEMPLATE, valueMap));
108             sep = ",\n";
109         }
110         if (!first)
111             sb.append("\n");
112         if (isArray)
113             sb.append("]");
114         if (!isEmbed && isArray)
115             sb.append("}");
116         return sb.toString();
117     }
118 }