1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
/*******************************************************************************
* Copyright (c) 2013 Boeing.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Boeing - initial API and implementation
*******************************************************************************/
package org.eclipse.osee.orcs.utility;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.jdk.core.type.ResultSet;
import org.eclipse.osee.orcs.OrcsApi;
import org.eclipse.osee.orcs.data.ArtifactReadable;
import org.eclipse.osee.orcs.data.AttributeReadable;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
public class RestUtil {
public static String jsonToPretty(String json) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(json);
String pretty = gson.toJson(je);
return pretty;
}
public static JSONObject toJson(String json) throws Exception {
return new JSONObject(json);
}
public static String jsonToPretty(JSONObject json, boolean pretty) {
return pretty ? jsonToPretty(json.toString()) : json.toString();
}
public static String jsonToPretty(JSONArray json, boolean pretty) {
return pretty ? jsonToPretty(json.toString()) : json.toString();
}
public static JSONArray getDefaultJSonArray(ResultSet<ArtifactReadable> artifacts) throws JSONException {
return getDefaultJSonArrayIterator(artifacts);
}
public static JSONArray getDefaultJSonArrayIterator(Iterable<ArtifactReadable> artifacts) throws JSONException {
JSONArray jsonArray = new JSONArray();
for (ArtifactReadable art : artifacts) {
JSONObject jObj = new JSONObject();
jObj.put("name", art.getName());
jObj.put("uuid", art.getUuid());
jsonArray.put(jObj);
}
return jsonArray;
}
public static JSONObject getDefaultJSon(ArtifactReadable art) throws JSONException {
JSONObject jObj = new JSONObject();
jObj.put("name", art.getName());
jObj.put("uuid", art.getUuid());
return jObj;
}
public static JSONObject getJsonObject(OrcsApi orcsApi, ArtifactReadable artifact) throws Exception, JSONException {
JSONObject jsonObj = getDefaultJSon(artifact);
addAttributes(orcsApi, jsonObj, artifact);
return jsonObj;
}
public static JSONArray getJsonArray(OrcsApi orcsApi, ResultSet<ArtifactReadable> artifacts) throws Exception, JSONException {
JSONArray jsonArray = new JSONArray();
for (ArtifactReadable artifact : artifacts) {
JSONObject jsonObj = getJsonObject(orcsApi, artifact);
jsonArray.put(jsonObj);
}
return jsonArray;
}
public static JSONObject addAttributes(OrcsApi orcsApi, JSONObject jsonObj, ArtifactReadable art) throws Exception {
addAttributesWithValues(orcsApi, jsonObj, art);
return jsonObj;
}
private static JSONObject addAttributesWithValues(OrcsApi orcsApi, JSONObject jsonObj, ArtifactReadable artifact) throws Exception {
addAttribute(orcsApi, jsonObj, artifact);
return jsonObj;
}
private static void addAttribute(OrcsApi orcsApi, JSONObject jsonObj, ArtifactReadable art) throws Exception {
for (IAttributeType attrType : orcsApi.getOrcsTypes().getAttributeTypes().getAll()) {
if (art.isAttributeTypeValid(attrType)) {
ResultSet<? extends AttributeReadable<Object>> attributeVals = art.getAttributes();
if (!attributeVals.isEmpty()) {
if (art.isAttributeTypeValid(attrType) && orcsApi.getOrcsTypes().getAttributeTypes().getMaxOccurrences(
attrType) > 1) {
List<String> attributeValues = new ArrayList<String>();
for (AttributeReadable<?> attrRead : attributeVals) {
String valueStr = String.valueOf(attrRead.getValue());
attributeValues.add(valueStr);
}
if (!attributeValues.isEmpty()) {
jsonObj.put(attrType.getName(), attributeValues);
}
} else if (attributeVals.size() == 1) {
String valueStr = String.valueOf(attributeVals.iterator().next().getValue());
jsonObj.put(attrType.getName(), valueStr);
}
}
}
}
}
}
|