/modules/apps/headless/headless-admin-taxonomy/headless-admin-taxonomy-client/src/main/java/com/liferay/headless/admin/taxonomy/client/serdes/v1_0/TaxonomyCategorySerDes.java

https://github.com/danielreuther/liferay-portal · Java · 745 lines · 572 code · 156 blank · 17 comment · 208 complexity · 83d94202168022dde55f62ef399c4dd1 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.admin.taxonomy.client.serdes.v1_0;
  15. import com.liferay.headless.admin.taxonomy.client.dto.v1_0.TaxonomyCategory;
  16. import com.liferay.headless.admin.taxonomy.client.dto.v1_0.TaxonomyCategoryProperty;
  17. import com.liferay.headless.admin.taxonomy.client.json.BaseJSONParser;
  18. import java.text.DateFormat;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Iterator;
  21. import java.util.Map;
  22. import java.util.Objects;
  23. import java.util.Set;
  24. import java.util.TreeMap;
  25. import java.util.stream.Stream;
  26. import javax.annotation.Generated;
  27. /**
  28. * @author Javier Gamarra
  29. * @generated
  30. */
  31. @Generated("")
  32. public class TaxonomyCategorySerDes {
  33. public static TaxonomyCategory toDTO(String json) {
  34. TaxonomyCategoryJSONParser taxonomyCategoryJSONParser =
  35. new TaxonomyCategoryJSONParser();
  36. return taxonomyCategoryJSONParser.parseToDTO(json);
  37. }
  38. public static TaxonomyCategory[] toDTOs(String json) {
  39. TaxonomyCategoryJSONParser taxonomyCategoryJSONParser =
  40. new TaxonomyCategoryJSONParser();
  41. return taxonomyCategoryJSONParser.parseToDTOs(json);
  42. }
  43. public static String toJSON(TaxonomyCategory taxonomyCategory) {
  44. if (taxonomyCategory == 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 (taxonomyCategory.getActions() != null) {
  52. if (sb.length() > 1) {
  53. sb.append(", ");
  54. }
  55. sb.append("\"actions\": ");
  56. sb.append(_toJSON(taxonomyCategory.getActions()));
  57. }
  58. if (taxonomyCategory.getAvailableLanguages() != null) {
  59. if (sb.length() > 1) {
  60. sb.append(", ");
  61. }
  62. sb.append("\"availableLanguages\": ");
  63. sb.append("[");
  64. for (int i = 0; i < taxonomyCategory.getAvailableLanguages().length;
  65. i++) {
  66. sb.append("\"");
  67. sb.append(_escape(taxonomyCategory.getAvailableLanguages()[i]));
  68. sb.append("\"");
  69. if ((i + 1) < taxonomyCategory.getAvailableLanguages().length) {
  70. sb.append(", ");
  71. }
  72. }
  73. sb.append("]");
  74. }
  75. if (taxonomyCategory.getCreator() != null) {
  76. if (sb.length() > 1) {
  77. sb.append(", ");
  78. }
  79. sb.append("\"creator\": ");
  80. sb.append(String.valueOf(taxonomyCategory.getCreator()));
  81. }
  82. if (taxonomyCategory.getDateCreated() != null) {
  83. if (sb.length() > 1) {
  84. sb.append(", ");
  85. }
  86. sb.append("\"dateCreated\": ");
  87. sb.append("\"");
  88. sb.append(
  89. liferayToJSONDateFormat.format(
  90. taxonomyCategory.getDateCreated()));
  91. sb.append("\"");
  92. }
  93. if (taxonomyCategory.getDateModified() != null) {
  94. if (sb.length() > 1) {
  95. sb.append(", ");
  96. }
  97. sb.append("\"dateModified\": ");
  98. sb.append("\"");
  99. sb.append(
  100. liferayToJSONDateFormat.format(
  101. taxonomyCategory.getDateModified()));
  102. sb.append("\"");
  103. }
  104. if (taxonomyCategory.getDescription() != null) {
  105. if (sb.length() > 1) {
  106. sb.append(", ");
  107. }
  108. sb.append("\"description\": ");
  109. sb.append("\"");
  110. sb.append(_escape(taxonomyCategory.getDescription()));
  111. sb.append("\"");
  112. }
  113. if (taxonomyCategory.getDescription_i18n() != null) {
  114. if (sb.length() > 1) {
  115. sb.append(", ");
  116. }
  117. sb.append("\"description_i18n\": ");
  118. sb.append(_toJSON(taxonomyCategory.getDescription_i18n()));
  119. }
  120. if (taxonomyCategory.getExternalReferenceCode() != null) {
  121. if (sb.length() > 1) {
  122. sb.append(", ");
  123. }
  124. sb.append("\"externalReferenceCode\": ");
  125. sb.append("\"");
  126. sb.append(_escape(taxonomyCategory.getExternalReferenceCode()));
  127. sb.append("\"");
  128. }
  129. if (taxonomyCategory.getId() != null) {
  130. if (sb.length() > 1) {
  131. sb.append(", ");
  132. }
  133. sb.append("\"id\": ");
  134. sb.append("\"");
  135. sb.append(_escape(taxonomyCategory.getId()));
  136. sb.append("\"");
  137. }
  138. if (taxonomyCategory.getName() != null) {
  139. if (sb.length() > 1) {
  140. sb.append(", ");
  141. }
  142. sb.append("\"name\": ");
  143. sb.append("\"");
  144. sb.append(_escape(taxonomyCategory.getName()));
  145. sb.append("\"");
  146. }
  147. if (taxonomyCategory.getName_i18n() != null) {
  148. if (sb.length() > 1) {
  149. sb.append(", ");
  150. }
  151. sb.append("\"name_i18n\": ");
  152. sb.append(_toJSON(taxonomyCategory.getName_i18n()));
  153. }
  154. if (taxonomyCategory.getNumberOfTaxonomyCategories() != null) {
  155. if (sb.length() > 1) {
  156. sb.append(", ");
  157. }
  158. sb.append("\"numberOfTaxonomyCategories\": ");
  159. sb.append(taxonomyCategory.getNumberOfTaxonomyCategories());
  160. }
  161. if (taxonomyCategory.getParentTaxonomyCategory() != null) {
  162. if (sb.length() > 1) {
  163. sb.append(", ");
  164. }
  165. sb.append("\"parentTaxonomyCategory\": ");
  166. sb.append(
  167. String.valueOf(taxonomyCategory.getParentTaxonomyCategory()));
  168. }
  169. if (taxonomyCategory.getParentTaxonomyVocabulary() != null) {
  170. if (sb.length() > 1) {
  171. sb.append(", ");
  172. }
  173. sb.append("\"parentTaxonomyVocabulary\": ");
  174. sb.append(
  175. String.valueOf(taxonomyCategory.getParentTaxonomyVocabulary()));
  176. }
  177. if (taxonomyCategory.getSiteId() != null) {
  178. if (sb.length() > 1) {
  179. sb.append(", ");
  180. }
  181. sb.append("\"siteId\": ");
  182. sb.append(taxonomyCategory.getSiteId());
  183. }
  184. if (taxonomyCategory.getTaxonomyCategoryProperties() != null) {
  185. if (sb.length() > 1) {
  186. sb.append(", ");
  187. }
  188. sb.append("\"taxonomyCategoryProperties\": ");
  189. sb.append("[");
  190. for (int i = 0;
  191. i < taxonomyCategory.getTaxonomyCategoryProperties().length;
  192. i++) {
  193. sb.append(
  194. String.valueOf(
  195. taxonomyCategory.getTaxonomyCategoryProperties()[i]));
  196. if ((i + 1) <
  197. taxonomyCategory.
  198. getTaxonomyCategoryProperties().length) {
  199. sb.append(", ");
  200. }
  201. }
  202. sb.append("]");
  203. }
  204. if (taxonomyCategory.getTaxonomyCategoryUsageCount() != null) {
  205. if (sb.length() > 1) {
  206. sb.append(", ");
  207. }
  208. sb.append("\"taxonomyCategoryUsageCount\": ");
  209. sb.append(taxonomyCategory.getTaxonomyCategoryUsageCount());
  210. }
  211. if (taxonomyCategory.getTaxonomyVocabularyId() != null) {
  212. if (sb.length() > 1) {
  213. sb.append(", ");
  214. }
  215. sb.append("\"taxonomyVocabularyId\": ");
  216. sb.append(taxonomyCategory.getTaxonomyVocabularyId());
  217. }
  218. if (taxonomyCategory.getViewableBy() != null) {
  219. if (sb.length() > 1) {
  220. sb.append(", ");
  221. }
  222. sb.append("\"viewableBy\": ");
  223. sb.append("\"");
  224. sb.append(taxonomyCategory.getViewableBy());
  225. sb.append("\"");
  226. }
  227. sb.append("}");
  228. return sb.toString();
  229. }
  230. public static Map<String, Object> toMap(String json) {
  231. TaxonomyCategoryJSONParser taxonomyCategoryJSONParser =
  232. new TaxonomyCategoryJSONParser();
  233. return taxonomyCategoryJSONParser.parseToMap(json);
  234. }
  235. public static Map<String, String> toMap(TaxonomyCategory taxonomyCategory) {
  236. if (taxonomyCategory == null) {
  237. return null;
  238. }
  239. Map<String, String> map = new TreeMap<>();
  240. DateFormat liferayToJSONDateFormat = new SimpleDateFormat(
  241. "yyyy-MM-dd'T'HH:mm:ssXX");
  242. if (taxonomyCategory.getActions() == null) {
  243. map.put("actions", null);
  244. }
  245. else {
  246. map.put("actions", String.valueOf(taxonomyCategory.getActions()));
  247. }
  248. if (taxonomyCategory.getAvailableLanguages() == null) {
  249. map.put("availableLanguages", null);
  250. }
  251. else {
  252. map.put(
  253. "availableLanguages",
  254. String.valueOf(taxonomyCategory.getAvailableLanguages()));
  255. }
  256. if (taxonomyCategory.getCreator() == null) {
  257. map.put("creator", null);
  258. }
  259. else {
  260. map.put("creator", String.valueOf(taxonomyCategory.getCreator()));
  261. }
  262. if (taxonomyCategory.getDateCreated() == null) {
  263. map.put("dateCreated", null);
  264. }
  265. else {
  266. map.put(
  267. "dateCreated",
  268. liferayToJSONDateFormat.format(
  269. taxonomyCategory.getDateCreated()));
  270. }
  271. if (taxonomyCategory.getDateModified() == null) {
  272. map.put("dateModified", null);
  273. }
  274. else {
  275. map.put(
  276. "dateModified",
  277. liferayToJSONDateFormat.format(
  278. taxonomyCategory.getDateModified()));
  279. }
  280. if (taxonomyCategory.getDescription() == null) {
  281. map.put("description", null);
  282. }
  283. else {
  284. map.put(
  285. "description",
  286. String.valueOf(taxonomyCategory.getDescription()));
  287. }
  288. if (taxonomyCategory.getDescription_i18n() == null) {
  289. map.put("description_i18n", null);
  290. }
  291. else {
  292. map.put(
  293. "description_i18n",
  294. String.valueOf(taxonomyCategory.getDescription_i18n()));
  295. }
  296. if (taxonomyCategory.getExternalReferenceCode() == null) {
  297. map.put("externalReferenceCode", null);
  298. }
  299. else {
  300. map.put(
  301. "externalReferenceCode",
  302. String.valueOf(taxonomyCategory.getExternalReferenceCode()));
  303. }
  304. if (taxonomyCategory.getId() == null) {
  305. map.put("id", null);
  306. }
  307. else {
  308. map.put("id", String.valueOf(taxonomyCategory.getId()));
  309. }
  310. if (taxonomyCategory.getName() == null) {
  311. map.put("name", null);
  312. }
  313. else {
  314. map.put("name", String.valueOf(taxonomyCategory.getName()));
  315. }
  316. if (taxonomyCategory.getName_i18n() == null) {
  317. map.put("name_i18n", null);
  318. }
  319. else {
  320. map.put(
  321. "name_i18n", String.valueOf(taxonomyCategory.getName_i18n()));
  322. }
  323. if (taxonomyCategory.getNumberOfTaxonomyCategories() == null) {
  324. map.put("numberOfTaxonomyCategories", null);
  325. }
  326. else {
  327. map.put(
  328. "numberOfTaxonomyCategories",
  329. String.valueOf(
  330. taxonomyCategory.getNumberOfTaxonomyCategories()));
  331. }
  332. if (taxonomyCategory.getParentTaxonomyCategory() == null) {
  333. map.put("parentTaxonomyCategory", null);
  334. }
  335. else {
  336. map.put(
  337. "parentTaxonomyCategory",
  338. String.valueOf(taxonomyCategory.getParentTaxonomyCategory()));
  339. }
  340. if (taxonomyCategory.getParentTaxonomyVocabulary() == null) {
  341. map.put("parentTaxonomyVocabulary", null);
  342. }
  343. else {
  344. map.put(
  345. "parentTaxonomyVocabulary",
  346. String.valueOf(taxonomyCategory.getParentTaxonomyVocabulary()));
  347. }
  348. if (taxonomyCategory.getSiteId() == null) {
  349. map.put("siteId", null);
  350. }
  351. else {
  352. map.put("siteId", String.valueOf(taxonomyCategory.getSiteId()));
  353. }
  354. if (taxonomyCategory.getTaxonomyCategoryProperties() == null) {
  355. map.put("taxonomyCategoryProperties", null);
  356. }
  357. else {
  358. map.put(
  359. "taxonomyCategoryProperties",
  360. String.valueOf(
  361. taxonomyCategory.getTaxonomyCategoryProperties()));
  362. }
  363. if (taxonomyCategory.getTaxonomyCategoryUsageCount() == null) {
  364. map.put("taxonomyCategoryUsageCount", null);
  365. }
  366. else {
  367. map.put(
  368. "taxonomyCategoryUsageCount",
  369. String.valueOf(
  370. taxonomyCategory.getTaxonomyCategoryUsageCount()));
  371. }
  372. if (taxonomyCategory.getTaxonomyVocabularyId() == null) {
  373. map.put("taxonomyVocabularyId", null);
  374. }
  375. else {
  376. map.put(
  377. "taxonomyVocabularyId",
  378. String.valueOf(taxonomyCategory.getTaxonomyVocabularyId()));
  379. }
  380. if (taxonomyCategory.getViewableBy() == null) {
  381. map.put("viewableBy", null);
  382. }
  383. else {
  384. map.put(
  385. "viewableBy", String.valueOf(taxonomyCategory.getViewableBy()));
  386. }
  387. return map;
  388. }
  389. public static class TaxonomyCategoryJSONParser
  390. extends BaseJSONParser<TaxonomyCategory> {
  391. @Override
  392. protected TaxonomyCategory createDTO() {
  393. return new TaxonomyCategory();
  394. }
  395. @Override
  396. protected TaxonomyCategory[] createDTOArray(int size) {
  397. return new TaxonomyCategory[size];
  398. }
  399. @Override
  400. protected void setField(
  401. TaxonomyCategory taxonomyCategory, String jsonParserFieldName,
  402. Object jsonParserFieldValue) {
  403. if (Objects.equals(jsonParserFieldName, "actions")) {
  404. if (jsonParserFieldValue != null) {
  405. taxonomyCategory.setActions(
  406. (Map)TaxonomyCategorySerDes.toMap(
  407. (String)jsonParserFieldValue));
  408. }
  409. }
  410. else if (Objects.equals(
  411. jsonParserFieldName, "availableLanguages")) {
  412. if (jsonParserFieldValue != null) {
  413. taxonomyCategory.setAvailableLanguages(
  414. toStrings((Object[])jsonParserFieldValue));
  415. }
  416. }
  417. else if (Objects.equals(jsonParserFieldName, "creator")) {
  418. if (jsonParserFieldValue != null) {
  419. taxonomyCategory.setCreator(
  420. CreatorSerDes.toDTO((String)jsonParserFieldValue));
  421. }
  422. }
  423. else if (Objects.equals(jsonParserFieldName, "dateCreated")) {
  424. if (jsonParserFieldValue != null) {
  425. taxonomyCategory.setDateCreated(
  426. toDate((String)jsonParserFieldValue));
  427. }
  428. }
  429. else if (Objects.equals(jsonParserFieldName, "dateModified")) {
  430. if (jsonParserFieldValue != null) {
  431. taxonomyCategory.setDateModified(
  432. toDate((String)jsonParserFieldValue));
  433. }
  434. }
  435. else if (Objects.equals(jsonParserFieldName, "description")) {
  436. if (jsonParserFieldValue != null) {
  437. taxonomyCategory.setDescription(
  438. (String)jsonParserFieldValue);
  439. }
  440. }
  441. else if (Objects.equals(jsonParserFieldName, "description_i18n")) {
  442. if (jsonParserFieldValue != null) {
  443. taxonomyCategory.setDescription_i18n(
  444. (Map)TaxonomyCategorySerDes.toMap(
  445. (String)jsonParserFieldValue));
  446. }
  447. }
  448. else if (Objects.equals(
  449. jsonParserFieldName, "externalReferenceCode")) {
  450. if (jsonParserFieldValue != null) {
  451. taxonomyCategory.setExternalReferenceCode(
  452. (String)jsonParserFieldValue);
  453. }
  454. }
  455. else if (Objects.equals(jsonParserFieldName, "id")) {
  456. if (jsonParserFieldValue != null) {
  457. taxonomyCategory.setId((String)jsonParserFieldValue);
  458. }
  459. }
  460. else if (Objects.equals(jsonParserFieldName, "name")) {
  461. if (jsonParserFieldValue != null) {
  462. taxonomyCategory.setName((String)jsonParserFieldValue);
  463. }
  464. }
  465. else if (Objects.equals(jsonParserFieldName, "name_i18n")) {
  466. if (jsonParserFieldValue != null) {
  467. taxonomyCategory.setName_i18n(
  468. (Map)TaxonomyCategorySerDes.toMap(
  469. (String)jsonParserFieldValue));
  470. }
  471. }
  472. else if (Objects.equals(
  473. jsonParserFieldName, "numberOfTaxonomyCategories")) {
  474. if (jsonParserFieldValue != null) {
  475. taxonomyCategory.setNumberOfTaxonomyCategories(
  476. Integer.valueOf((String)jsonParserFieldValue));
  477. }
  478. }
  479. else if (Objects.equals(
  480. jsonParserFieldName, "parentTaxonomyCategory")) {
  481. if (jsonParserFieldValue != null) {
  482. taxonomyCategory.setParentTaxonomyCategory(
  483. ParentTaxonomyCategorySerDes.toDTO(
  484. (String)jsonParserFieldValue));
  485. }
  486. }
  487. else if (Objects.equals(
  488. jsonParserFieldName, "parentTaxonomyVocabulary")) {
  489. if (jsonParserFieldValue != null) {
  490. taxonomyCategory.setParentTaxonomyVocabulary(
  491. ParentTaxonomyVocabularySerDes.toDTO(
  492. (String)jsonParserFieldValue));
  493. }
  494. }
  495. else if (Objects.equals(jsonParserFieldName, "siteId")) {
  496. if (jsonParserFieldValue != null) {
  497. taxonomyCategory.setSiteId(
  498. Long.valueOf((String)jsonParserFieldValue));
  499. }
  500. }
  501. else if (Objects.equals(
  502. jsonParserFieldName, "taxonomyCategoryProperties")) {
  503. if (jsonParserFieldValue != null) {
  504. taxonomyCategory.setTaxonomyCategoryProperties(
  505. Stream.of(
  506. toStrings((Object[])jsonParserFieldValue)
  507. ).map(
  508. object -> TaxonomyCategoryPropertySerDes.toDTO(
  509. (String)object)
  510. ).toArray(
  511. size -> new TaxonomyCategoryProperty[size]
  512. ));
  513. }
  514. }
  515. else if (Objects.equals(
  516. jsonParserFieldName, "taxonomyCategoryUsageCount")) {
  517. if (jsonParserFieldValue != null) {
  518. taxonomyCategory.setTaxonomyCategoryUsageCount(
  519. Integer.valueOf((String)jsonParserFieldValue));
  520. }
  521. }
  522. else if (Objects.equals(
  523. jsonParserFieldName, "taxonomyVocabularyId")) {
  524. if (jsonParserFieldValue != null) {
  525. taxonomyCategory.setTaxonomyVocabularyId(
  526. Long.valueOf((String)jsonParserFieldValue));
  527. }
  528. }
  529. else if (Objects.equals(jsonParserFieldName, "viewableBy")) {
  530. if (jsonParserFieldValue != null) {
  531. taxonomyCategory.setViewableBy(
  532. TaxonomyCategory.ViewableBy.create(
  533. (String)jsonParserFieldValue));
  534. }
  535. }
  536. }
  537. }
  538. private static String _escape(Object object) {
  539. String string = String.valueOf(object);
  540. for (String[] strings : BaseJSONParser.JSON_ESCAPE_STRINGS) {
  541. string = string.replace(strings[0], strings[1]);
  542. }
  543. return string;
  544. }
  545. private static String _toJSON(Map<String, ?> map) {
  546. StringBuilder sb = new StringBuilder("{");
  547. @SuppressWarnings("unchecked")
  548. Set set = map.entrySet();
  549. @SuppressWarnings("unchecked")
  550. Iterator<Map.Entry<String, ?>> iterator = set.iterator();
  551. while (iterator.hasNext()) {
  552. Map.Entry<String, ?> entry = iterator.next();
  553. sb.append("\"");
  554. sb.append(entry.getKey());
  555. sb.append("\": ");
  556. Object value = entry.getValue();
  557. Class<?> valueClass = value.getClass();
  558. if (value instanceof Map) {
  559. sb.append(_toJSON((Map)value));
  560. }
  561. else if (valueClass.isArray()) {
  562. Object[] values = (Object[])value;
  563. sb.append("[");
  564. for (int i = 0; i < values.length; i++) {
  565. sb.append("\"");
  566. sb.append(_escape(values[i]));
  567. sb.append("\"");
  568. if ((i + 1) < values.length) {
  569. sb.append(", ");
  570. }
  571. }
  572. sb.append("]");
  573. }
  574. else if (value instanceof String) {
  575. sb.append("\"");
  576. sb.append(_escape(entry.getValue()));
  577. sb.append("\"");
  578. }
  579. else {
  580. sb.append(String.valueOf(entry.getValue()));
  581. }
  582. if (iterator.hasNext()) {
  583. sb.append(", ");
  584. }
  585. }
  586. sb.append("}");
  587. return sb.toString();
  588. }
  589. }