/modules/dxp/apps/portal-workflow/portal-workflow-metrics-rest-client/src/main/java/com/liferay/portal/workflow/metrics/rest/client/serdes/v1_0/SLASerDes.java

http://github.com/liferay/liferay-portal · Java · 442 lines · 326 code · 99 blank · 17 comment · 124 complexity · 65503383f63f20e2c38b89e27712e2ff MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * The contents of this file are subject to the terms of the Liferay Enterprise
  5. * Subscription License ("License"). You may not use this file except in
  6. * compliance with the License. You can obtain a copy of the License by
  7. * contacting Liferay, Inc. See the License for the specific language governing
  8. * permissions and limitations under the License, including but not limited to
  9. * distribution rights of the Software.
  10. *
  11. *
  12. *
  13. */
  14. package com.liferay.portal.workflow.metrics.rest.client.serdes.v1_0;
  15. import com.liferay.portal.workflow.metrics.rest.client.dto.v1_0.SLA;
  16. import com.liferay.portal.workflow.metrics.rest.client.json.BaseJSONParser;
  17. import java.text.DateFormat;
  18. import java.text.SimpleDateFormat;
  19. import java.util.Iterator;
  20. import java.util.Map;
  21. import java.util.Objects;
  22. import java.util.Set;
  23. import java.util.TreeMap;
  24. import javax.annotation.Generated;
  25. /**
  26. * @author Rafael Praxedes
  27. * @generated
  28. */
  29. @Generated("")
  30. public class SLASerDes {
  31. public static SLA toDTO(String json) {
  32. SLAJSONParser slaJSONParser = new SLAJSONParser();
  33. return slaJSONParser.parseToDTO(json);
  34. }
  35. public static SLA[] toDTOs(String json) {
  36. SLAJSONParser slaJSONParser = new SLAJSONParser();
  37. return slaJSONParser.parseToDTOs(json);
  38. }
  39. public static String toJSON(SLA sla) {
  40. if (sla == null) {
  41. return "null";
  42. }
  43. StringBuilder sb = new StringBuilder();
  44. sb.append("{");
  45. DateFormat liferayToJSONDateFormat = new SimpleDateFormat(
  46. "yyyy-MM-dd'T'HH:mm:ss'Z'");
  47. if (sla.getCalendarKey() != null) {
  48. if (sb.length() > 1) {
  49. sb.append(", ");
  50. }
  51. sb.append("\"calendarKey\": ");
  52. sb.append("\"");
  53. sb.append(_escape(sla.getCalendarKey()));
  54. sb.append("\"");
  55. }
  56. if (sla.getDateModified() != null) {
  57. if (sb.length() > 1) {
  58. sb.append(", ");
  59. }
  60. sb.append("\"dateModified\": ");
  61. sb.append("\"");
  62. sb.append(liferayToJSONDateFormat.format(sla.getDateModified()));
  63. sb.append("\"");
  64. }
  65. if (sla.getDescription() != null) {
  66. if (sb.length() > 1) {
  67. sb.append(", ");
  68. }
  69. sb.append("\"description\": ");
  70. sb.append("\"");
  71. sb.append(_escape(sla.getDescription()));
  72. sb.append("\"");
  73. }
  74. if (sla.getDuration() != null) {
  75. if (sb.length() > 1) {
  76. sb.append(", ");
  77. }
  78. sb.append("\"duration\": ");
  79. sb.append(sla.getDuration());
  80. }
  81. if (sla.getId() != null) {
  82. if (sb.length() > 1) {
  83. sb.append(", ");
  84. }
  85. sb.append("\"id\": ");
  86. sb.append(sla.getId());
  87. }
  88. if (sla.getName() != null) {
  89. if (sb.length() > 1) {
  90. sb.append(", ");
  91. }
  92. sb.append("\"name\": ");
  93. sb.append("\"");
  94. sb.append(_escape(sla.getName()));
  95. sb.append("\"");
  96. }
  97. if (sla.getPauseNodeKeys() != null) {
  98. if (sb.length() > 1) {
  99. sb.append(", ");
  100. }
  101. sb.append("\"pauseNodeKeys\": ");
  102. sb.append(String.valueOf(sla.getPauseNodeKeys()));
  103. }
  104. if (sla.getProcessId() != null) {
  105. if (sb.length() > 1) {
  106. sb.append(", ");
  107. }
  108. sb.append("\"processId\": ");
  109. sb.append(sla.getProcessId());
  110. }
  111. if (sla.getStartNodeKeys() != null) {
  112. if (sb.length() > 1) {
  113. sb.append(", ");
  114. }
  115. sb.append("\"startNodeKeys\": ");
  116. sb.append(String.valueOf(sla.getStartNodeKeys()));
  117. }
  118. if (sla.getStatus() != null) {
  119. if (sb.length() > 1) {
  120. sb.append(", ");
  121. }
  122. sb.append("\"status\": ");
  123. sb.append(sla.getStatus());
  124. }
  125. if (sla.getStopNodeKeys() != null) {
  126. if (sb.length() > 1) {
  127. sb.append(", ");
  128. }
  129. sb.append("\"stopNodeKeys\": ");
  130. sb.append(String.valueOf(sla.getStopNodeKeys()));
  131. }
  132. sb.append("}");
  133. return sb.toString();
  134. }
  135. public static Map<String, Object> toMap(String json) {
  136. SLAJSONParser slaJSONParser = new SLAJSONParser();
  137. return slaJSONParser.parseToMap(json);
  138. }
  139. public static Map<String, String> toMap(SLA sla) {
  140. if (sla == null) {
  141. return null;
  142. }
  143. Map<String, String> map = new TreeMap<>();
  144. DateFormat liferayToJSONDateFormat = new SimpleDateFormat(
  145. "yyyy-MM-dd'T'HH:mm:ss'Z'");
  146. if (sla.getCalendarKey() == null) {
  147. map.put("calendarKey", null);
  148. }
  149. else {
  150. map.put("calendarKey", String.valueOf(sla.getCalendarKey()));
  151. }
  152. if (sla.getDateModified() == null) {
  153. map.put("dateModified", null);
  154. }
  155. else {
  156. map.put(
  157. "dateModified",
  158. liferayToJSONDateFormat.format(sla.getDateModified()));
  159. }
  160. if (sla.getDescription() == null) {
  161. map.put("description", null);
  162. }
  163. else {
  164. map.put("description", String.valueOf(sla.getDescription()));
  165. }
  166. if (sla.getDuration() == null) {
  167. map.put("duration", null);
  168. }
  169. else {
  170. map.put("duration", String.valueOf(sla.getDuration()));
  171. }
  172. if (sla.getId() == null) {
  173. map.put("id", null);
  174. }
  175. else {
  176. map.put("id", String.valueOf(sla.getId()));
  177. }
  178. if (sla.getName() == null) {
  179. map.put("name", null);
  180. }
  181. else {
  182. map.put("name", String.valueOf(sla.getName()));
  183. }
  184. if (sla.getPauseNodeKeys() == null) {
  185. map.put("pauseNodeKeys", null);
  186. }
  187. else {
  188. map.put("pauseNodeKeys", String.valueOf(sla.getPauseNodeKeys()));
  189. }
  190. if (sla.getProcessId() == null) {
  191. map.put("processId", null);
  192. }
  193. else {
  194. map.put("processId", String.valueOf(sla.getProcessId()));
  195. }
  196. if (sla.getStartNodeKeys() == null) {
  197. map.put("startNodeKeys", null);
  198. }
  199. else {
  200. map.put("startNodeKeys", String.valueOf(sla.getStartNodeKeys()));
  201. }
  202. if (sla.getStatus() == null) {
  203. map.put("status", null);
  204. }
  205. else {
  206. map.put("status", String.valueOf(sla.getStatus()));
  207. }
  208. if (sla.getStopNodeKeys() == null) {
  209. map.put("stopNodeKeys", null);
  210. }
  211. else {
  212. map.put("stopNodeKeys", String.valueOf(sla.getStopNodeKeys()));
  213. }
  214. return map;
  215. }
  216. public static class SLAJSONParser extends BaseJSONParser<SLA> {
  217. @Override
  218. protected SLA createDTO() {
  219. return new SLA();
  220. }
  221. @Override
  222. protected SLA[] createDTOArray(int size) {
  223. return new SLA[size];
  224. }
  225. @Override
  226. protected void setField(
  227. SLA sla, String jsonParserFieldName, Object jsonParserFieldValue) {
  228. if (Objects.equals(jsonParserFieldName, "calendarKey")) {
  229. if (jsonParserFieldValue != null) {
  230. sla.setCalendarKey((String)jsonParserFieldValue);
  231. }
  232. }
  233. else if (Objects.equals(jsonParserFieldName, "dateModified")) {
  234. if (jsonParserFieldValue != null) {
  235. sla.setDateModified(toDate((String)jsonParserFieldValue));
  236. }
  237. }
  238. else if (Objects.equals(jsonParserFieldName, "description")) {
  239. if (jsonParserFieldValue != null) {
  240. sla.setDescription((String)jsonParserFieldValue);
  241. }
  242. }
  243. else if (Objects.equals(jsonParserFieldName, "duration")) {
  244. if (jsonParserFieldValue != null) {
  245. sla.setDuration(Long.valueOf((String)jsonParserFieldValue));
  246. }
  247. }
  248. else if (Objects.equals(jsonParserFieldName, "id")) {
  249. if (jsonParserFieldValue != null) {
  250. sla.setId(Long.valueOf((String)jsonParserFieldValue));
  251. }
  252. }
  253. else if (Objects.equals(jsonParserFieldName, "name")) {
  254. if (jsonParserFieldValue != null) {
  255. sla.setName((String)jsonParserFieldValue);
  256. }
  257. }
  258. else if (Objects.equals(jsonParserFieldName, "pauseNodeKeys")) {
  259. if (jsonParserFieldValue != null) {
  260. sla.setPauseNodeKeys(
  261. PauseNodeKeysSerDes.toDTO(
  262. (String)jsonParserFieldValue));
  263. }
  264. }
  265. else if (Objects.equals(jsonParserFieldName, "processId")) {
  266. if (jsonParserFieldValue != null) {
  267. sla.setProcessId(
  268. Long.valueOf((String)jsonParserFieldValue));
  269. }
  270. }
  271. else if (Objects.equals(jsonParserFieldName, "startNodeKeys")) {
  272. if (jsonParserFieldValue != null) {
  273. sla.setStartNodeKeys(
  274. StartNodeKeysSerDes.toDTO(
  275. (String)jsonParserFieldValue));
  276. }
  277. }
  278. else if (Objects.equals(jsonParserFieldName, "status")) {
  279. if (jsonParserFieldValue != null) {
  280. sla.setStatus(
  281. Integer.valueOf((String)jsonParserFieldValue));
  282. }
  283. }
  284. else if (Objects.equals(jsonParserFieldName, "stopNodeKeys")) {
  285. if (jsonParserFieldValue != null) {
  286. sla.setStopNodeKeys(
  287. StopNodeKeysSerDes.toDTO((String)jsonParserFieldValue));
  288. }
  289. }
  290. }
  291. }
  292. private static String _escape(Object object) {
  293. String string = String.valueOf(object);
  294. for (String[] strings : BaseJSONParser.JSON_ESCAPE_STRINGS) {
  295. string = string.replace(strings[0], strings[1]);
  296. }
  297. return string;
  298. }
  299. private static String _toJSON(Map<String, ?> map) {
  300. StringBuilder sb = new StringBuilder("{");
  301. @SuppressWarnings("unchecked")
  302. Set set = map.entrySet();
  303. @SuppressWarnings("unchecked")
  304. Iterator<Map.Entry<String, ?>> iterator = set.iterator();
  305. while (iterator.hasNext()) {
  306. Map.Entry<String, ?> entry = iterator.next();
  307. sb.append("\"");
  308. sb.append(entry.getKey());
  309. sb.append("\": ");
  310. Object value = entry.getValue();
  311. Class<?> valueClass = value.getClass();
  312. if (value instanceof Map) {
  313. sb.append(_toJSON((Map)value));
  314. }
  315. else if (valueClass.isArray()) {
  316. Object[] values = (Object[])value;
  317. sb.append("[");
  318. for (int i = 0; i < values.length; i++) {
  319. sb.append("\"");
  320. sb.append(_escape(values[i]));
  321. sb.append("\"");
  322. if ((i + 1) < values.length) {
  323. sb.append(", ");
  324. }
  325. }
  326. sb.append("]");
  327. }
  328. else if (value instanceof String) {
  329. sb.append("\"");
  330. sb.append(_escape(entry.getValue()));
  331. sb.append("\"");
  332. }
  333. else {
  334. sb.append(String.valueOf(entry.getValue()));
  335. }
  336. if (iterator.hasNext()) {
  337. sb.append(", ");
  338. }
  339. }
  340. sb.append("}");
  341. return sb.toString();
  342. }
  343. }