/modules/dxp/apps/commerce/headless/headless-commerce-machine-learning-client/src/main/java/com/liferay/headless/commerce/machine/learning/client/serdes/v1_0/ProductSerDes.java

https://github.com/danielreuther/liferay-portal · Java · 865 lines · 669 code · 179 blank · 17 comment · 266 complexity · af7990999842843c264ccdabd8b1b2b3 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.headless.commerce.machine.learning.client.serdes.v1_0;
  15. import com.liferay.headless.commerce.machine.learning.client.dto.v1_0.Product;
  16. import com.liferay.headless.commerce.machine.learning.client.dto.v1_0.ProductOption;
  17. import com.liferay.headless.commerce.machine.learning.client.dto.v1_0.ProductSpecification;
  18. import com.liferay.headless.commerce.machine.learning.client.dto.v1_0.Sku;
  19. import com.liferay.headless.commerce.machine.learning.client.json.BaseJSONParser;
  20. import java.text.DateFormat;
  21. import java.text.SimpleDateFormat;
  22. import java.util.Iterator;
  23. import java.util.Map;
  24. import java.util.Objects;
  25. import java.util.Set;
  26. import java.util.TreeMap;
  27. import java.util.stream.Stream;
  28. import javax.annotation.Generated;
  29. /**
  30. * @author Riccardo Ferrari
  31. * @generated
  32. */
  33. @Generated("")
  34. public class ProductSerDes {
  35. public static Product toDTO(String json) {
  36. ProductJSONParser productJSONParser = new ProductJSONParser();
  37. return productJSONParser.parseToDTO(json);
  38. }
  39. public static Product[] toDTOs(String json) {
  40. ProductJSONParser productJSONParser = new ProductJSONParser();
  41. return productJSONParser.parseToDTOs(json);
  42. }
  43. public static String toJSON(Product product) {
  44. if (product == null) {
  45. return "null";
  46. }
  47. StringBuilder sb = new StringBuilder();
  48. sb.append("{");
  49. DateFormat liferayToJSONDateFormat = new SimpleDateFormat(
  50. "yyyy-MM-dd'T'HH:mm:ssXX");
  51. if (product.getCatalogId() != null) {
  52. if (sb.length() > 1) {
  53. sb.append(", ");
  54. }
  55. sb.append("\"catalogId\": ");
  56. sb.append(product.getCatalogId());
  57. }
  58. if (product.getCategoryIds() != null) {
  59. if (sb.length() > 1) {
  60. sb.append(", ");
  61. }
  62. sb.append("\"categoryIds\": ");
  63. sb.append("[");
  64. for (int i = 0; i < product.getCategoryIds().length; i++) {
  65. sb.append(product.getCategoryIds()[i]);
  66. if ((i + 1) < product.getCategoryIds().length) {
  67. sb.append(", ");
  68. }
  69. }
  70. sb.append("]");
  71. }
  72. if (product.getCreateDate() != null) {
  73. if (sb.length() > 1) {
  74. sb.append(", ");
  75. }
  76. sb.append("\"createDate\": ");
  77. sb.append("\"");
  78. sb.append(liferayToJSONDateFormat.format(product.getCreateDate()));
  79. sb.append("\"");
  80. }
  81. if (product.getCustomFields() != null) {
  82. if (sb.length() > 1) {
  83. sb.append(", ");
  84. }
  85. sb.append("\"customFields\": ");
  86. sb.append(_toJSON(product.getCustomFields()));
  87. }
  88. if (product.getDescription() != null) {
  89. if (sb.length() > 1) {
  90. sb.append(", ");
  91. }
  92. sb.append("\"description\": ");
  93. sb.append(_toJSON(product.getDescription()));
  94. }
  95. if (product.getDisplayDate() != null) {
  96. if (sb.length() > 1) {
  97. sb.append(", ");
  98. }
  99. sb.append("\"displayDate\": ");
  100. sb.append("\"");
  101. sb.append(liferayToJSONDateFormat.format(product.getDisplayDate()));
  102. sb.append("\"");
  103. }
  104. if (product.getExpirationDate() != null) {
  105. if (sb.length() > 1) {
  106. sb.append(", ");
  107. }
  108. sb.append("\"expirationDate\": ");
  109. sb.append("\"");
  110. sb.append(
  111. liferayToJSONDateFormat.format(product.getExpirationDate()));
  112. sb.append("\"");
  113. }
  114. if (product.getExternalReferenceCode() != null) {
  115. if (sb.length() > 1) {
  116. sb.append(", ");
  117. }
  118. sb.append("\"externalReferenceCode\": ");
  119. sb.append("\"");
  120. sb.append(_escape(product.getExternalReferenceCode()));
  121. sb.append("\"");
  122. }
  123. if (product.getId() != null) {
  124. if (sb.length() > 1) {
  125. sb.append(", ");
  126. }
  127. sb.append("\"id\": ");
  128. sb.append(product.getId());
  129. }
  130. if (product.getMetaDescription() != null) {
  131. if (sb.length() > 1) {
  132. sb.append(", ");
  133. }
  134. sb.append("\"metaDescription\": ");
  135. sb.append(_toJSON(product.getMetaDescription()));
  136. }
  137. if (product.getMetaKeyword() != null) {
  138. if (sb.length() > 1) {
  139. sb.append(", ");
  140. }
  141. sb.append("\"metaKeyword\": ");
  142. sb.append(_toJSON(product.getMetaKeyword()));
  143. }
  144. if (product.getMetaTitle() != null) {
  145. if (sb.length() > 1) {
  146. sb.append(", ");
  147. }
  148. sb.append("\"metaTitle\": ");
  149. sb.append(_toJSON(product.getMetaTitle()));
  150. }
  151. if (product.getModifiedDate() != null) {
  152. if (sb.length() > 1) {
  153. sb.append(", ");
  154. }
  155. sb.append("\"modifiedDate\": ");
  156. sb.append("\"");
  157. sb.append(
  158. liferayToJSONDateFormat.format(product.getModifiedDate()));
  159. sb.append("\"");
  160. }
  161. if (product.getName() != null) {
  162. if (sb.length() > 1) {
  163. sb.append(", ");
  164. }
  165. sb.append("\"name\": ");
  166. sb.append(_toJSON(product.getName()));
  167. }
  168. if (product.getProductChannelIds() != null) {
  169. if (sb.length() > 1) {
  170. sb.append(", ");
  171. }
  172. sb.append("\"productChannelIds\": ");
  173. sb.append("[");
  174. for (int i = 0; i < product.getProductChannelIds().length; i++) {
  175. sb.append(product.getProductChannelIds()[i]);
  176. if ((i + 1) < product.getProductChannelIds().length) {
  177. sb.append(", ");
  178. }
  179. }
  180. sb.append("]");
  181. }
  182. if (product.getProductId() != null) {
  183. if (sb.length() > 1) {
  184. sb.append(", ");
  185. }
  186. sb.append("\"productId\": ");
  187. sb.append(product.getProductId());
  188. }
  189. if (product.getProductOptions() != null) {
  190. if (sb.length() > 1) {
  191. sb.append(", ");
  192. }
  193. sb.append("\"productOptions\": ");
  194. sb.append("[");
  195. for (int i = 0; i < product.getProductOptions().length; i++) {
  196. sb.append(String.valueOf(product.getProductOptions()[i]));
  197. if ((i + 1) < product.getProductOptions().length) {
  198. sb.append(", ");
  199. }
  200. }
  201. sb.append("]");
  202. }
  203. if (product.getProductSpecifications() != null) {
  204. if (sb.length() > 1) {
  205. sb.append(", ");
  206. }
  207. sb.append("\"productSpecifications\": ");
  208. sb.append("[");
  209. for (int i = 0; i < product.getProductSpecifications().length;
  210. i++) {
  211. sb.append(
  212. String.valueOf(product.getProductSpecifications()[i]));
  213. if ((i + 1) < product.getProductSpecifications().length) {
  214. sb.append(", ");
  215. }
  216. }
  217. sb.append("]");
  218. }
  219. if (product.getProductType() != null) {
  220. if (sb.length() > 1) {
  221. sb.append(", ");
  222. }
  223. sb.append("\"productType\": ");
  224. sb.append("\"");
  225. sb.append(_escape(product.getProductType()));
  226. sb.append("\"");
  227. }
  228. if (product.getSkus() != null) {
  229. if (sb.length() > 1) {
  230. sb.append(", ");
  231. }
  232. sb.append("\"skus\": ");
  233. sb.append("[");
  234. for (int i = 0; i < product.getSkus().length; i++) {
  235. sb.append(String.valueOf(product.getSkus()[i]));
  236. if ((i + 1) < product.getSkus().length) {
  237. sb.append(", ");
  238. }
  239. }
  240. sb.append("]");
  241. }
  242. if (product.getStatus() != null) {
  243. if (sb.length() > 1) {
  244. sb.append(", ");
  245. }
  246. sb.append("\"status\": ");
  247. sb.append(product.getStatus());
  248. }
  249. if (product.getSubscriptionEnabled() != null) {
  250. if (sb.length() > 1) {
  251. sb.append(", ");
  252. }
  253. sb.append("\"subscriptionEnabled\": ");
  254. sb.append(product.getSubscriptionEnabled());
  255. }
  256. if (product.getTags() != null) {
  257. if (sb.length() > 1) {
  258. sb.append(", ");
  259. }
  260. sb.append("\"tags\": ");
  261. sb.append("[");
  262. for (int i = 0; i < product.getTags().length; i++) {
  263. sb.append("\"");
  264. sb.append(_escape(product.getTags()[i]));
  265. sb.append("\"");
  266. if ((i + 1) < product.getTags().length) {
  267. sb.append(", ");
  268. }
  269. }
  270. sb.append("]");
  271. }
  272. if (product.getUrls() != null) {
  273. if (sb.length() > 1) {
  274. sb.append(", ");
  275. }
  276. sb.append("\"urls\": ");
  277. sb.append(_toJSON(product.getUrls()));
  278. }
  279. sb.append("}");
  280. return sb.toString();
  281. }
  282. public static Map<String, Object> toMap(String json) {
  283. ProductJSONParser productJSONParser = new ProductJSONParser();
  284. return productJSONParser.parseToMap(json);
  285. }
  286. public static Map<String, String> toMap(Product product) {
  287. if (product == null) {
  288. return null;
  289. }
  290. Map<String, String> map = new TreeMap<>();
  291. DateFormat liferayToJSONDateFormat = new SimpleDateFormat(
  292. "yyyy-MM-dd'T'HH:mm:ssXX");
  293. if (product.getCatalogId() == null) {
  294. map.put("catalogId", null);
  295. }
  296. else {
  297. map.put("catalogId", String.valueOf(product.getCatalogId()));
  298. }
  299. if (product.getCategoryIds() == null) {
  300. map.put("categoryIds", null);
  301. }
  302. else {
  303. map.put("categoryIds", String.valueOf(product.getCategoryIds()));
  304. }
  305. if (product.getCreateDate() == null) {
  306. map.put("createDate", null);
  307. }
  308. else {
  309. map.put(
  310. "createDate",
  311. liferayToJSONDateFormat.format(product.getCreateDate()));
  312. }
  313. if (product.getCustomFields() == null) {
  314. map.put("customFields", null);
  315. }
  316. else {
  317. map.put("customFields", String.valueOf(product.getCustomFields()));
  318. }
  319. if (product.getDescription() == null) {
  320. map.put("description", null);
  321. }
  322. else {
  323. map.put("description", String.valueOf(product.getDescription()));
  324. }
  325. if (product.getDisplayDate() == null) {
  326. map.put("displayDate", null);
  327. }
  328. else {
  329. map.put(
  330. "displayDate",
  331. liferayToJSONDateFormat.format(product.getDisplayDate()));
  332. }
  333. if (product.getExpirationDate() == null) {
  334. map.put("expirationDate", null);
  335. }
  336. else {
  337. map.put(
  338. "expirationDate",
  339. liferayToJSONDateFormat.format(product.getExpirationDate()));
  340. }
  341. if (product.getExternalReferenceCode() == null) {
  342. map.put("externalReferenceCode", null);
  343. }
  344. else {
  345. map.put(
  346. "externalReferenceCode",
  347. String.valueOf(product.getExternalReferenceCode()));
  348. }
  349. if (product.getId() == null) {
  350. map.put("id", null);
  351. }
  352. else {
  353. map.put("id", String.valueOf(product.getId()));
  354. }
  355. if (product.getMetaDescription() == null) {
  356. map.put("metaDescription", null);
  357. }
  358. else {
  359. map.put(
  360. "metaDescription",
  361. String.valueOf(product.getMetaDescription()));
  362. }
  363. if (product.getMetaKeyword() == null) {
  364. map.put("metaKeyword", null);
  365. }
  366. else {
  367. map.put("metaKeyword", String.valueOf(product.getMetaKeyword()));
  368. }
  369. if (product.getMetaTitle() == null) {
  370. map.put("metaTitle", null);
  371. }
  372. else {
  373. map.put("metaTitle", String.valueOf(product.getMetaTitle()));
  374. }
  375. if (product.getModifiedDate() == null) {
  376. map.put("modifiedDate", null);
  377. }
  378. else {
  379. map.put(
  380. "modifiedDate",
  381. liferayToJSONDateFormat.format(product.getModifiedDate()));
  382. }
  383. if (product.getName() == null) {
  384. map.put("name", null);
  385. }
  386. else {
  387. map.put("name", String.valueOf(product.getName()));
  388. }
  389. if (product.getProductChannelIds() == null) {
  390. map.put("productChannelIds", null);
  391. }
  392. else {
  393. map.put(
  394. "productChannelIds",
  395. String.valueOf(product.getProductChannelIds()));
  396. }
  397. if (product.getProductId() == null) {
  398. map.put("productId", null);
  399. }
  400. else {
  401. map.put("productId", String.valueOf(product.getProductId()));
  402. }
  403. if (product.getProductOptions() == null) {
  404. map.put("productOptions", null);
  405. }
  406. else {
  407. map.put(
  408. "productOptions", String.valueOf(product.getProductOptions()));
  409. }
  410. if (product.getProductSpecifications() == null) {
  411. map.put("productSpecifications", null);
  412. }
  413. else {
  414. map.put(
  415. "productSpecifications",
  416. String.valueOf(product.getProductSpecifications()));
  417. }
  418. if (product.getProductType() == null) {
  419. map.put("productType", null);
  420. }
  421. else {
  422. map.put("productType", String.valueOf(product.getProductType()));
  423. }
  424. if (product.getSkus() == null) {
  425. map.put("skus", null);
  426. }
  427. else {
  428. map.put("skus", String.valueOf(product.getSkus()));
  429. }
  430. if (product.getStatus() == null) {
  431. map.put("status", null);
  432. }
  433. else {
  434. map.put("status", String.valueOf(product.getStatus()));
  435. }
  436. if (product.getSubscriptionEnabled() == null) {
  437. map.put("subscriptionEnabled", null);
  438. }
  439. else {
  440. map.put(
  441. "subscriptionEnabled",
  442. String.valueOf(product.getSubscriptionEnabled()));
  443. }
  444. if (product.getTags() == null) {
  445. map.put("tags", null);
  446. }
  447. else {
  448. map.put("tags", String.valueOf(product.getTags()));
  449. }
  450. if (product.getUrls() == null) {
  451. map.put("urls", null);
  452. }
  453. else {
  454. map.put("urls", String.valueOf(product.getUrls()));
  455. }
  456. return map;
  457. }
  458. public static class ProductJSONParser extends BaseJSONParser<Product> {
  459. @Override
  460. protected Product createDTO() {
  461. return new Product();
  462. }
  463. @Override
  464. protected Product[] createDTOArray(int size) {
  465. return new Product[size];
  466. }
  467. @Override
  468. protected void setField(
  469. Product product, String jsonParserFieldName,
  470. Object jsonParserFieldValue) {
  471. if (Objects.equals(jsonParserFieldName, "catalogId")) {
  472. if (jsonParserFieldValue != null) {
  473. product.setCatalogId(
  474. Long.valueOf((String)jsonParserFieldValue));
  475. }
  476. }
  477. else if (Objects.equals(jsonParserFieldName, "categoryIds")) {
  478. if (jsonParserFieldValue != null) {
  479. product.setCategoryIds(
  480. toLongs((Object[])jsonParserFieldValue));
  481. }
  482. }
  483. else if (Objects.equals(jsonParserFieldName, "createDate")) {
  484. if (jsonParserFieldValue != null) {
  485. product.setCreateDate(toDate((String)jsonParserFieldValue));
  486. }
  487. }
  488. else if (Objects.equals(jsonParserFieldName, "customFields")) {
  489. if (jsonParserFieldValue != null) {
  490. product.setCustomFields(
  491. (Map)ProductSerDes.toMap((String)jsonParserFieldValue));
  492. }
  493. }
  494. else if (Objects.equals(jsonParserFieldName, "description")) {
  495. if (jsonParserFieldValue != null) {
  496. product.setDescription(
  497. (Map)ProductSerDes.toMap((String)jsonParserFieldValue));
  498. }
  499. }
  500. else if (Objects.equals(jsonParserFieldName, "displayDate")) {
  501. if (jsonParserFieldValue != null) {
  502. product.setDisplayDate(
  503. toDate((String)jsonParserFieldValue));
  504. }
  505. }
  506. else if (Objects.equals(jsonParserFieldName, "expirationDate")) {
  507. if (jsonParserFieldValue != null) {
  508. product.setExpirationDate(
  509. toDate((String)jsonParserFieldValue));
  510. }
  511. }
  512. else if (Objects.equals(
  513. jsonParserFieldName, "externalReferenceCode")) {
  514. if (jsonParserFieldValue != null) {
  515. product.setExternalReferenceCode(
  516. (String)jsonParserFieldValue);
  517. }
  518. }
  519. else if (Objects.equals(jsonParserFieldName, "id")) {
  520. if (jsonParserFieldValue != null) {
  521. product.setId(Long.valueOf((String)jsonParserFieldValue));
  522. }
  523. }
  524. else if (Objects.equals(jsonParserFieldName, "metaDescription")) {
  525. if (jsonParserFieldValue != null) {
  526. product.setMetaDescription(
  527. (Map)ProductSerDes.toMap((String)jsonParserFieldValue));
  528. }
  529. }
  530. else if (Objects.equals(jsonParserFieldName, "metaKeyword")) {
  531. if (jsonParserFieldValue != null) {
  532. product.setMetaKeyword(
  533. (Map)ProductSerDes.toMap((String)jsonParserFieldValue));
  534. }
  535. }
  536. else if (Objects.equals(jsonParserFieldName, "metaTitle")) {
  537. if (jsonParserFieldValue != null) {
  538. product.setMetaTitle(
  539. (Map)ProductSerDes.toMap((String)jsonParserFieldValue));
  540. }
  541. }
  542. else if (Objects.equals(jsonParserFieldName, "modifiedDate")) {
  543. if (jsonParserFieldValue != null) {
  544. product.setModifiedDate(
  545. toDate((String)jsonParserFieldValue));
  546. }
  547. }
  548. else if (Objects.equals(jsonParserFieldName, "name")) {
  549. if (jsonParserFieldValue != null) {
  550. product.setName(
  551. (Map)ProductSerDes.toMap((String)jsonParserFieldValue));
  552. }
  553. }
  554. else if (Objects.equals(jsonParserFieldName, "productChannelIds")) {
  555. if (jsonParserFieldValue != null) {
  556. product.setProductChannelIds(
  557. toLongs((Object[])jsonParserFieldValue));
  558. }
  559. }
  560. else if (Objects.equals(jsonParserFieldName, "productId")) {
  561. if (jsonParserFieldValue != null) {
  562. product.setProductId(
  563. Long.valueOf((String)jsonParserFieldValue));
  564. }
  565. }
  566. else if (Objects.equals(jsonParserFieldName, "productOptions")) {
  567. if (jsonParserFieldValue != null) {
  568. product.setProductOptions(
  569. Stream.of(
  570. toStrings((Object[])jsonParserFieldValue)
  571. ).map(
  572. object -> ProductOptionSerDes.toDTO((String)object)
  573. ).toArray(
  574. size -> new ProductOption[size]
  575. ));
  576. }
  577. }
  578. else if (Objects.equals(
  579. jsonParserFieldName, "productSpecifications")) {
  580. if (jsonParserFieldValue != null) {
  581. product.setProductSpecifications(
  582. Stream.of(
  583. toStrings((Object[])jsonParserFieldValue)
  584. ).map(
  585. object -> ProductSpecificationSerDes.toDTO(
  586. (String)object)
  587. ).toArray(
  588. size -> new ProductSpecification[size]
  589. ));
  590. }
  591. }
  592. else if (Objects.equals(jsonParserFieldName, "productType")) {
  593. if (jsonParserFieldValue != null) {
  594. product.setProductType((String)jsonParserFieldValue);
  595. }
  596. }
  597. else if (Objects.equals(jsonParserFieldName, "skus")) {
  598. if (jsonParserFieldValue != null) {
  599. product.setSkus(
  600. Stream.of(
  601. toStrings((Object[])jsonParserFieldValue)
  602. ).map(
  603. object -> SkuSerDes.toDTO((String)object)
  604. ).toArray(
  605. size -> new Sku[size]
  606. ));
  607. }
  608. }
  609. else if (Objects.equals(jsonParserFieldName, "status")) {
  610. if (jsonParserFieldValue != null) {
  611. product.setStatus(
  612. Integer.valueOf((String)jsonParserFieldValue));
  613. }
  614. }
  615. else if (Objects.equals(
  616. jsonParserFieldName, "subscriptionEnabled")) {
  617. if (jsonParserFieldValue != null) {
  618. product.setSubscriptionEnabled(
  619. (Boolean)jsonParserFieldValue);
  620. }
  621. }
  622. else if (Objects.equals(jsonParserFieldName, "tags")) {
  623. if (jsonParserFieldValue != null) {
  624. product.setTags(toStrings((Object[])jsonParserFieldValue));
  625. }
  626. }
  627. else if (Objects.equals(jsonParserFieldName, "urls")) {
  628. if (jsonParserFieldValue != null) {
  629. product.setUrls(
  630. (Map)ProductSerDes.toMap((String)jsonParserFieldValue));
  631. }
  632. }
  633. }
  634. }
  635. private static String _escape(Object object) {
  636. String string = String.valueOf(object);
  637. for (String[] strings : BaseJSONParser.JSON_ESCAPE_STRINGS) {
  638. string = string.replace(strings[0], strings[1]);
  639. }
  640. return string;
  641. }
  642. private static String _toJSON(Map<String, ?> map) {
  643. StringBuilder sb = new StringBuilder("{");
  644. @SuppressWarnings("unchecked")
  645. Set set = map.entrySet();
  646. @SuppressWarnings("unchecked")
  647. Iterator<Map.Entry<String, ?>> iterator = set.iterator();
  648. while (iterator.hasNext()) {
  649. Map.Entry<String, ?> entry = iterator.next();
  650. sb.append("\"");
  651. sb.append(entry.getKey());
  652. sb.append("\": ");
  653. Object value = entry.getValue();
  654. Class<?> valueClass = value.getClass();
  655. if (value instanceof Map) {
  656. sb.append(_toJSON((Map)value));
  657. }
  658. else if (valueClass.isArray()) {
  659. Object[] values = (Object[])value;
  660. sb.append("[");
  661. for (int i = 0; i < values.length; i++) {
  662. sb.append("\"");
  663. sb.append(_escape(values[i]));
  664. sb.append("\"");
  665. if ((i + 1) < values.length) {
  666. sb.append(", ");
  667. }
  668. }
  669. sb.append("]");
  670. }
  671. else if (value instanceof String) {
  672. sb.append("\"");
  673. sb.append(_escape(entry.getValue()));
  674. sb.append("\"");
  675. }
  676. else {
  677. sb.append(String.valueOf(entry.getValue()));
  678. }
  679. if (iterator.hasNext()) {
  680. sb.append(", ");
  681. }
  682. }
  683. sb.append("}");
  684. return sb.toString();
  685. }
  686. }