/modules/apps/headless/headless-admin-user/headless-admin-user-client/src/main/java/com/liferay/headless/admin/user/client/serdes/v1_0/OrganizationSerDes.java

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