/modules/apps/commerce/headless/headless-commerce/headless-commerce-delivery-catalog-client/src/main/java/com/liferay/headless/commerce/delivery/catalog/client/serdes/v1_0/MappedProductSerDes.java

https://github.com/danielreuther/liferay-portal · Java · 780 lines · 605 code · 158 blank · 17 comment · 238 complexity · cccf5b2054234299dafdf250c32dd845 MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.headless.commerce.delivery.catalog.client.serdes.v1_0;
  15. import com.liferay.headless.commerce.delivery.catalog.client.dto.v1_0.MappedProduct;
  16. import com.liferay.headless.commerce.delivery.catalog.client.dto.v1_0.ProductOption;
  17. import com.liferay.headless.commerce.delivery.catalog.client.dto.v1_0.SkuOption;
  18. import com.liferay.headless.commerce.delivery.catalog.client.json.BaseJSONParser;
  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 java.util.stream.Stream;
  25. import javax.annotation.Generated;
  26. /**
  27. * @author Andrea Sbarra
  28. * @generated
  29. */
  30. @Generated("")
  31. public class MappedProductSerDes {
  32. public static MappedProduct toDTO(String json) {
  33. MappedProductJSONParser mappedProductJSONParser =
  34. new MappedProductJSONParser();
  35. return mappedProductJSONParser.parseToDTO(json);
  36. }
  37. public static MappedProduct[] toDTOs(String json) {
  38. MappedProductJSONParser mappedProductJSONParser =
  39. new MappedProductJSONParser();
  40. return mappedProductJSONParser.parseToDTOs(json);
  41. }
  42. public static String toJSON(MappedProduct mappedProduct) {
  43. if (mappedProduct == null) {
  44. return "null";
  45. }
  46. StringBuilder sb = new StringBuilder();
  47. sb.append("{");
  48. if (mappedProduct.getActions() != null) {
  49. if (sb.length() > 1) {
  50. sb.append(", ");
  51. }
  52. sb.append("\"actions\": ");
  53. sb.append(_toJSON(mappedProduct.getActions()));
  54. }
  55. if (mappedProduct.getAvailability() != null) {
  56. if (sb.length() > 1) {
  57. sb.append(", ");
  58. }
  59. sb.append("\"availability\": ");
  60. sb.append(String.valueOf(mappedProduct.getAvailability()));
  61. }
  62. if (mappedProduct.getFirstAvailableReplacementMappedProduct() != null) {
  63. if (sb.length() > 1) {
  64. sb.append(", ");
  65. }
  66. sb.append("\"firstAvailableReplacementMappedProduct\": ");
  67. sb.append(
  68. String.valueOf(
  69. mappedProduct.getFirstAvailableReplacementMappedProduct()));
  70. }
  71. if (mappedProduct.getId() != null) {
  72. if (sb.length() > 1) {
  73. sb.append(", ");
  74. }
  75. sb.append("\"id\": ");
  76. sb.append(mappedProduct.getId());
  77. }
  78. if (mappedProduct.getPrice() != null) {
  79. if (sb.length() > 1) {
  80. sb.append(", ");
  81. }
  82. sb.append("\"price\": ");
  83. sb.append(String.valueOf(mappedProduct.getPrice()));
  84. }
  85. if (mappedProduct.getProductConfiguration() != null) {
  86. if (sb.length() > 1) {
  87. sb.append(", ");
  88. }
  89. sb.append("\"productConfiguration\": ");
  90. sb.append(String.valueOf(mappedProduct.getProductConfiguration()));
  91. }
  92. if (mappedProduct.getProductExternalReferenceCode() != null) {
  93. if (sb.length() > 1) {
  94. sb.append(", ");
  95. }
  96. sb.append("\"productExternalReferenceCode\": ");
  97. sb.append("\"");
  98. sb.append(_escape(mappedProduct.getProductExternalReferenceCode()));
  99. sb.append("\"");
  100. }
  101. if (mappedProduct.getProductId() != null) {
  102. if (sb.length() > 1) {
  103. sb.append(", ");
  104. }
  105. sb.append("\"productId\": ");
  106. sb.append(mappedProduct.getProductId());
  107. }
  108. if (mappedProduct.getProductName() != null) {
  109. if (sb.length() > 1) {
  110. sb.append(", ");
  111. }
  112. sb.append("\"productName\": ");
  113. sb.append(_toJSON(mappedProduct.getProductName()));
  114. }
  115. if (mappedProduct.getProductOptions() != null) {
  116. if (sb.length() > 1) {
  117. sb.append(", ");
  118. }
  119. sb.append("\"productOptions\": ");
  120. sb.append("[");
  121. for (int i = 0; i < mappedProduct.getProductOptions().length; i++) {
  122. sb.append(String.valueOf(mappedProduct.getProductOptions()[i]));
  123. if ((i + 1) < mappedProduct.getProductOptions().length) {
  124. sb.append(", ");
  125. }
  126. }
  127. sb.append("]");
  128. }
  129. if (mappedProduct.getPurchasable() != null) {
  130. if (sb.length() > 1) {
  131. sb.append(", ");
  132. }
  133. sb.append("\"purchasable\": ");
  134. sb.append(mappedProduct.getPurchasable());
  135. }
  136. if (mappedProduct.getQuantity() != null) {
  137. if (sb.length() > 1) {
  138. sb.append(", ");
  139. }
  140. sb.append("\"quantity\": ");
  141. sb.append(mappedProduct.getQuantity());
  142. }
  143. if (mappedProduct.getReplacementMappedProduct() != null) {
  144. if (sb.length() > 1) {
  145. sb.append(", ");
  146. }
  147. sb.append("\"replacementMappedProduct\": ");
  148. sb.append(
  149. String.valueOf(mappedProduct.getReplacementMappedProduct()));
  150. }
  151. if (mappedProduct.getReplacementMessage() != null) {
  152. if (sb.length() > 1) {
  153. sb.append(", ");
  154. }
  155. sb.append("\"replacementMessage\": ");
  156. sb.append("\"");
  157. sb.append(_escape(mappedProduct.getReplacementMessage()));
  158. sb.append("\"");
  159. }
  160. if (mappedProduct.getSequence() != null) {
  161. if (sb.length() > 1) {
  162. sb.append(", ");
  163. }
  164. sb.append("\"sequence\": ");
  165. sb.append("\"");
  166. sb.append(_escape(mappedProduct.getSequence()));
  167. sb.append("\"");
  168. }
  169. if (mappedProduct.getSku() != null) {
  170. if (sb.length() > 1) {
  171. sb.append(", ");
  172. }
  173. sb.append("\"sku\": ");
  174. sb.append("\"");
  175. sb.append(_escape(mappedProduct.getSku()));
  176. sb.append("\"");
  177. }
  178. if (mappedProduct.getSkuExternalReferenceCode() != null) {
  179. if (sb.length() > 1) {
  180. sb.append(", ");
  181. }
  182. sb.append("\"skuExternalReferenceCode\": ");
  183. sb.append("\"");
  184. sb.append(_escape(mappedProduct.getSkuExternalReferenceCode()));
  185. sb.append("\"");
  186. }
  187. if (mappedProduct.getSkuId() != null) {
  188. if (sb.length() > 1) {
  189. sb.append(", ");
  190. }
  191. sb.append("\"skuId\": ");
  192. sb.append(mappedProduct.getSkuId());
  193. }
  194. if (mappedProduct.getSkuOptions() != null) {
  195. if (sb.length() > 1) {
  196. sb.append(", ");
  197. }
  198. sb.append("\"skuOptions\": ");
  199. sb.append("[");
  200. for (int i = 0; i < mappedProduct.getSkuOptions().length; i++) {
  201. sb.append(String.valueOf(mappedProduct.getSkuOptions()[i]));
  202. if ((i + 1) < mappedProduct.getSkuOptions().length) {
  203. sb.append(", ");
  204. }
  205. }
  206. sb.append("]");
  207. }
  208. if (mappedProduct.getThumbnail() != null) {
  209. if (sb.length() > 1) {
  210. sb.append(", ");
  211. }
  212. sb.append("\"thumbnail\": ");
  213. sb.append("\"");
  214. sb.append(_escape(mappedProduct.getThumbnail()));
  215. sb.append("\"");
  216. }
  217. if (mappedProduct.getType() != null) {
  218. if (sb.length() > 1) {
  219. sb.append(", ");
  220. }
  221. sb.append("\"type\": ");
  222. sb.append("\"");
  223. sb.append(mappedProduct.getType());
  224. sb.append("\"");
  225. }
  226. if (mappedProduct.getUrls() != null) {
  227. if (sb.length() > 1) {
  228. sb.append(", ");
  229. }
  230. sb.append("\"urls\": ");
  231. sb.append(_toJSON(mappedProduct.getUrls()));
  232. }
  233. sb.append("}");
  234. return sb.toString();
  235. }
  236. public static Map<String, Object> toMap(String json) {
  237. MappedProductJSONParser mappedProductJSONParser =
  238. new MappedProductJSONParser();
  239. return mappedProductJSONParser.parseToMap(json);
  240. }
  241. public static Map<String, String> toMap(MappedProduct mappedProduct) {
  242. if (mappedProduct == null) {
  243. return null;
  244. }
  245. Map<String, String> map = new TreeMap<>();
  246. if (mappedProduct.getActions() == null) {
  247. map.put("actions", null);
  248. }
  249. else {
  250. map.put("actions", String.valueOf(mappedProduct.getActions()));
  251. }
  252. if (mappedProduct.getAvailability() == null) {
  253. map.put("availability", null);
  254. }
  255. else {
  256. map.put(
  257. "availability",
  258. String.valueOf(mappedProduct.getAvailability()));
  259. }
  260. if (mappedProduct.getFirstAvailableReplacementMappedProduct() == null) {
  261. map.put("firstAvailableReplacementMappedProduct", null);
  262. }
  263. else {
  264. map.put(
  265. "firstAvailableReplacementMappedProduct",
  266. String.valueOf(
  267. mappedProduct.getFirstAvailableReplacementMappedProduct()));
  268. }
  269. if (mappedProduct.getId() == null) {
  270. map.put("id", null);
  271. }
  272. else {
  273. map.put("id", String.valueOf(mappedProduct.getId()));
  274. }
  275. if (mappedProduct.getPrice() == null) {
  276. map.put("price", null);
  277. }
  278. else {
  279. map.put("price", String.valueOf(mappedProduct.getPrice()));
  280. }
  281. if (mappedProduct.getProductConfiguration() == null) {
  282. map.put("productConfiguration", null);
  283. }
  284. else {
  285. map.put(
  286. "productConfiguration",
  287. String.valueOf(mappedProduct.getProductConfiguration()));
  288. }
  289. if (mappedProduct.getProductExternalReferenceCode() == null) {
  290. map.put("productExternalReferenceCode", null);
  291. }
  292. else {
  293. map.put(
  294. "productExternalReferenceCode",
  295. String.valueOf(
  296. mappedProduct.getProductExternalReferenceCode()));
  297. }
  298. if (mappedProduct.getProductId() == null) {
  299. map.put("productId", null);
  300. }
  301. else {
  302. map.put("productId", String.valueOf(mappedProduct.getProductId()));
  303. }
  304. if (mappedProduct.getProductName() == null) {
  305. map.put("productName", null);
  306. }
  307. else {
  308. map.put(
  309. "productName", String.valueOf(mappedProduct.getProductName()));
  310. }
  311. if (mappedProduct.getProductOptions() == null) {
  312. map.put("productOptions", null);
  313. }
  314. else {
  315. map.put(
  316. "productOptions",
  317. String.valueOf(mappedProduct.getProductOptions()));
  318. }
  319. if (mappedProduct.getPurchasable() == null) {
  320. map.put("purchasable", null);
  321. }
  322. else {
  323. map.put(
  324. "purchasable", String.valueOf(mappedProduct.getPurchasable()));
  325. }
  326. if (mappedProduct.getQuantity() == null) {
  327. map.put("quantity", null);
  328. }
  329. else {
  330. map.put("quantity", String.valueOf(mappedProduct.getQuantity()));
  331. }
  332. if (mappedProduct.getReplacementMappedProduct() == null) {
  333. map.put("replacementMappedProduct", null);
  334. }
  335. else {
  336. map.put(
  337. "replacementMappedProduct",
  338. String.valueOf(mappedProduct.getReplacementMappedProduct()));
  339. }
  340. if (mappedProduct.getReplacementMessage() == null) {
  341. map.put("replacementMessage", null);
  342. }
  343. else {
  344. map.put(
  345. "replacementMessage",
  346. String.valueOf(mappedProduct.getReplacementMessage()));
  347. }
  348. if (mappedProduct.getSequence() == null) {
  349. map.put("sequence", null);
  350. }
  351. else {
  352. map.put("sequence", String.valueOf(mappedProduct.getSequence()));
  353. }
  354. if (mappedProduct.getSku() == null) {
  355. map.put("sku", null);
  356. }
  357. else {
  358. map.put("sku", String.valueOf(mappedProduct.getSku()));
  359. }
  360. if (mappedProduct.getSkuExternalReferenceCode() == null) {
  361. map.put("skuExternalReferenceCode", null);
  362. }
  363. else {
  364. map.put(
  365. "skuExternalReferenceCode",
  366. String.valueOf(mappedProduct.getSkuExternalReferenceCode()));
  367. }
  368. if (mappedProduct.getSkuId() == null) {
  369. map.put("skuId", null);
  370. }
  371. else {
  372. map.put("skuId", String.valueOf(mappedProduct.getSkuId()));
  373. }
  374. if (mappedProduct.getSkuOptions() == null) {
  375. map.put("skuOptions", null);
  376. }
  377. else {
  378. map.put(
  379. "skuOptions", String.valueOf(mappedProduct.getSkuOptions()));
  380. }
  381. if (mappedProduct.getThumbnail() == null) {
  382. map.put("thumbnail", null);
  383. }
  384. else {
  385. map.put("thumbnail", String.valueOf(mappedProduct.getThumbnail()));
  386. }
  387. if (mappedProduct.getType() == null) {
  388. map.put("type", null);
  389. }
  390. else {
  391. map.put("type", String.valueOf(mappedProduct.getType()));
  392. }
  393. if (mappedProduct.getUrls() == null) {
  394. map.put("urls", null);
  395. }
  396. else {
  397. map.put("urls", String.valueOf(mappedProduct.getUrls()));
  398. }
  399. return map;
  400. }
  401. public static class MappedProductJSONParser
  402. extends BaseJSONParser<MappedProduct> {
  403. @Override
  404. protected MappedProduct createDTO() {
  405. return new MappedProduct();
  406. }
  407. @Override
  408. protected MappedProduct[] createDTOArray(int size) {
  409. return new MappedProduct[size];
  410. }
  411. @Override
  412. protected void setField(
  413. MappedProduct mappedProduct, String jsonParserFieldName,
  414. Object jsonParserFieldValue) {
  415. if (Objects.equals(jsonParserFieldName, "actions")) {
  416. if (jsonParserFieldValue != null) {
  417. mappedProduct.setActions(
  418. (Map)MappedProductSerDes.toMap(
  419. (String)jsonParserFieldValue));
  420. }
  421. }
  422. else if (Objects.equals(jsonParserFieldName, "availability")) {
  423. if (jsonParserFieldValue != null) {
  424. mappedProduct.setAvailability(
  425. AvailabilitySerDes.toDTO((String)jsonParserFieldValue));
  426. }
  427. }
  428. else if (Objects.equals(
  429. jsonParserFieldName,
  430. "firstAvailableReplacementMappedProduct")) {
  431. if (jsonParserFieldValue != null) {
  432. mappedProduct.setFirstAvailableReplacementMappedProduct(
  433. MappedProductSerDes.toDTO(
  434. (String)jsonParserFieldValue));
  435. }
  436. }
  437. else if (Objects.equals(jsonParserFieldName, "id")) {
  438. if (jsonParserFieldValue != null) {
  439. mappedProduct.setId(
  440. Long.valueOf((String)jsonParserFieldValue));
  441. }
  442. }
  443. else if (Objects.equals(jsonParserFieldName, "price")) {
  444. if (jsonParserFieldValue != null) {
  445. mappedProduct.setPrice(
  446. PriceSerDes.toDTO((String)jsonParserFieldValue));
  447. }
  448. }
  449. else if (Objects.equals(
  450. jsonParserFieldName, "productConfiguration")) {
  451. if (jsonParserFieldValue != null) {
  452. mappedProduct.setProductConfiguration(
  453. ProductConfigurationSerDes.toDTO(
  454. (String)jsonParserFieldValue));
  455. }
  456. }
  457. else if (Objects.equals(
  458. jsonParserFieldName, "productExternalReferenceCode")) {
  459. if (jsonParserFieldValue != null) {
  460. mappedProduct.setProductExternalReferenceCode(
  461. (String)jsonParserFieldValue);
  462. }
  463. }
  464. else if (Objects.equals(jsonParserFieldName, "productId")) {
  465. if (jsonParserFieldValue != null) {
  466. mappedProduct.setProductId(
  467. Long.valueOf((String)jsonParserFieldValue));
  468. }
  469. }
  470. else if (Objects.equals(jsonParserFieldName, "productName")) {
  471. if (jsonParserFieldValue != null) {
  472. mappedProduct.setProductName(
  473. (Map)MappedProductSerDes.toMap(
  474. (String)jsonParserFieldValue));
  475. }
  476. }
  477. else if (Objects.equals(jsonParserFieldName, "productOptions")) {
  478. if (jsonParserFieldValue != null) {
  479. mappedProduct.setProductOptions(
  480. Stream.of(
  481. toStrings((Object[])jsonParserFieldValue)
  482. ).map(
  483. object -> ProductOptionSerDes.toDTO((String)object)
  484. ).toArray(
  485. size -> new ProductOption[size]
  486. ));
  487. }
  488. }
  489. else if (Objects.equals(jsonParserFieldName, "purchasable")) {
  490. if (jsonParserFieldValue != null) {
  491. mappedProduct.setPurchasable((Boolean)jsonParserFieldValue);
  492. }
  493. }
  494. else if (Objects.equals(jsonParserFieldName, "quantity")) {
  495. if (jsonParserFieldValue != null) {
  496. mappedProduct.setQuantity(
  497. Integer.valueOf((String)jsonParserFieldValue));
  498. }
  499. }
  500. else if (Objects.equals(
  501. jsonParserFieldName, "replacementMappedProduct")) {
  502. if (jsonParserFieldValue != null) {
  503. mappedProduct.setReplacementMappedProduct(
  504. MappedProductSerDes.toDTO(
  505. (String)jsonParserFieldValue));
  506. }
  507. }
  508. else if (Objects.equals(
  509. jsonParserFieldName, "replacementMessage")) {
  510. if (jsonParserFieldValue != null) {
  511. mappedProduct.setReplacementMessage(
  512. (String)jsonParserFieldValue);
  513. }
  514. }
  515. else if (Objects.equals(jsonParserFieldName, "sequence")) {
  516. if (jsonParserFieldValue != null) {
  517. mappedProduct.setSequence((String)jsonParserFieldValue);
  518. }
  519. }
  520. else if (Objects.equals(jsonParserFieldName, "sku")) {
  521. if (jsonParserFieldValue != null) {
  522. mappedProduct.setSku((String)jsonParserFieldValue);
  523. }
  524. }
  525. else if (Objects.equals(
  526. jsonParserFieldName, "skuExternalReferenceCode")) {
  527. if (jsonParserFieldValue != null) {
  528. mappedProduct.setSkuExternalReferenceCode(
  529. (String)jsonParserFieldValue);
  530. }
  531. }
  532. else if (Objects.equals(jsonParserFieldName, "skuId")) {
  533. if (jsonParserFieldValue != null) {
  534. mappedProduct.setSkuId(
  535. Long.valueOf((String)jsonParserFieldValue));
  536. }
  537. }
  538. else if (Objects.equals(jsonParserFieldName, "skuOptions")) {
  539. if (jsonParserFieldValue != null) {
  540. mappedProduct.setSkuOptions(
  541. Stream.of(
  542. toStrings((Object[])jsonParserFieldValue)
  543. ).map(
  544. object -> SkuOptionSerDes.toDTO((String)object)
  545. ).toArray(
  546. size -> new SkuOption[size]
  547. ));
  548. }
  549. }
  550. else if (Objects.equals(jsonParserFieldName, "thumbnail")) {
  551. if (jsonParserFieldValue != null) {
  552. mappedProduct.setThumbnail((String)jsonParserFieldValue);
  553. }
  554. }
  555. else if (Objects.equals(jsonParserFieldName, "type")) {
  556. if (jsonParserFieldValue != null) {
  557. mappedProduct.setType(
  558. MappedProduct.Type.create(
  559. (String)jsonParserFieldValue));
  560. }
  561. }
  562. else if (Objects.equals(jsonParserFieldName, "urls")) {
  563. if (jsonParserFieldValue != null) {
  564. mappedProduct.setUrls(
  565. (Map)MappedProductSerDes.toMap(
  566. (String)jsonParserFieldValue));
  567. }
  568. }
  569. }
  570. }
  571. private static String _escape(Object object) {
  572. String string = String.valueOf(object);
  573. for (String[] strings : BaseJSONParser.JSON_ESCAPE_STRINGS) {
  574. string = string.replace(strings[0], strings[1]);
  575. }
  576. return string;
  577. }
  578. private static String _toJSON(Map<String, ?> map) {
  579. StringBuilder sb = new StringBuilder("{");
  580. @SuppressWarnings("unchecked")
  581. Set set = map.entrySet();
  582. @SuppressWarnings("unchecked")
  583. Iterator<Map.Entry<String, ?>> iterator = set.iterator();
  584. while (iterator.hasNext()) {
  585. Map.Entry<String, ?> entry = iterator.next();
  586. sb.append("\"");
  587. sb.append(entry.getKey());
  588. sb.append("\": ");
  589. Object value = entry.getValue();
  590. Class<?> valueClass = value.getClass();
  591. if (value instanceof Map) {
  592. sb.append(_toJSON((Map)value));
  593. }
  594. else if (valueClass.isArray()) {
  595. Object[] values = (Object[])value;
  596. sb.append("[");
  597. for (int i = 0; i < values.length; i++) {
  598. sb.append("\"");
  599. sb.append(_escape(values[i]));
  600. sb.append("\"");
  601. if ((i + 1) < values.length) {
  602. sb.append(", ");
  603. }
  604. }
  605. sb.append("]");
  606. }
  607. else if (value instanceof String) {
  608. sb.append("\"");
  609. sb.append(_escape(entry.getValue()));
  610. sb.append("\"");
  611. }
  612. else {
  613. sb.append(String.valueOf(entry.getValue()));
  614. }
  615. if (iterator.hasNext()) {
  616. sb.append(", ");
  617. }
  618. }
  619. sb.append("}");
  620. return sb.toString();
  621. }
  622. }