/modules/apps/headless/headless-admin-workflow/headless-admin-workflow-client/src/main/java/com/liferay/headless/admin/workflow/client/resource/v1_0/WorkflowInstanceResource.java

https://github.com/brunobasto/liferay-portal · Java · 505 lines · 355 code · 133 blank · 17 comment · 30 complexity · d43faf5af908f388fdd7d8deb0e27370 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.workflow.client.resource.v1_0;
  15. import com.liferay.headless.admin.workflow.client.dto.v1_0.ChangeTransition;
  16. import com.liferay.headless.admin.workflow.client.dto.v1_0.WorkflowInstance;
  17. import com.liferay.headless.admin.workflow.client.dto.v1_0.WorkflowInstanceSubmit;
  18. import com.liferay.headless.admin.workflow.client.http.HttpInvoker;
  19. import com.liferay.headless.admin.workflow.client.pagination.Page;
  20. import com.liferay.headless.admin.workflow.client.pagination.Pagination;
  21. import com.liferay.headless.admin.workflow.client.problem.Problem;
  22. import com.liferay.headless.admin.workflow.client.serdes.v1_0.WorkflowInstanceSerDes;
  23. import java.util.LinkedHashMap;
  24. import java.util.Locale;
  25. import java.util.Map;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import javax.annotation.Generated;
  29. /**
  30. * @author Javier Gamarra
  31. * @generated
  32. */
  33. @Generated("")
  34. public interface WorkflowInstanceResource {
  35. public static Builder builder() {
  36. return new Builder();
  37. }
  38. public Page<WorkflowInstance> getWorkflowInstancesPage(
  39. String[] assetClassNames, Long[] assetPrimaryKeys,
  40. Boolean completed, Pagination pagination)
  41. throws Exception;
  42. public HttpInvoker.HttpResponse getWorkflowInstancesPageHttpResponse(
  43. String[] assetClassNames, Long[] assetPrimaryKeys,
  44. Boolean completed, Pagination pagination)
  45. throws Exception;
  46. public WorkflowInstance postWorkflowInstanceSubmit(
  47. WorkflowInstanceSubmit workflowInstanceSubmit)
  48. throws Exception;
  49. public HttpInvoker.HttpResponse postWorkflowInstanceSubmitHttpResponse(
  50. WorkflowInstanceSubmit workflowInstanceSubmit)
  51. throws Exception;
  52. public void deleteWorkflowInstance(Long workflowInstanceId)
  53. throws Exception;
  54. public HttpInvoker.HttpResponse deleteWorkflowInstanceHttpResponse(
  55. Long workflowInstanceId)
  56. throws Exception;
  57. public WorkflowInstance getWorkflowInstance(Long workflowInstanceId)
  58. throws Exception;
  59. public HttpInvoker.HttpResponse getWorkflowInstanceHttpResponse(
  60. Long workflowInstanceId)
  61. throws Exception;
  62. public WorkflowInstance postWorkflowInstanceChangeTransition(
  63. Long workflowInstanceId, ChangeTransition changeTransition)
  64. throws Exception;
  65. public HttpInvoker.HttpResponse
  66. postWorkflowInstanceChangeTransitionHttpResponse(
  67. Long workflowInstanceId, ChangeTransition changeTransition)
  68. throws Exception;
  69. public static class Builder {
  70. public Builder authentication(String login, String password) {
  71. _login = login;
  72. _password = password;
  73. return this;
  74. }
  75. public WorkflowInstanceResource build() {
  76. return new WorkflowInstanceResourceImpl(this);
  77. }
  78. public Builder endpoint(String host, int port, String scheme) {
  79. _host = host;
  80. _port = port;
  81. _scheme = scheme;
  82. return this;
  83. }
  84. public Builder header(String key, String value) {
  85. _headers.put(key, value);
  86. return this;
  87. }
  88. public Builder locale(Locale locale) {
  89. _locale = locale;
  90. return this;
  91. }
  92. public Builder parameter(String key, String value) {
  93. _parameters.put(key, value);
  94. return this;
  95. }
  96. private Builder() {
  97. }
  98. private Map<String, String> _headers = new LinkedHashMap<>();
  99. private String _host = "localhost";
  100. private Locale _locale;
  101. private String _login = "";
  102. private String _password = "";
  103. private Map<String, String> _parameters = new LinkedHashMap<>();
  104. private int _port = 8080;
  105. private String _scheme = "http";
  106. }
  107. public static class WorkflowInstanceResourceImpl
  108. implements WorkflowInstanceResource {
  109. public Page<WorkflowInstance> getWorkflowInstancesPage(
  110. String[] assetClassNames, Long[] assetPrimaryKeys,
  111. Boolean completed, Pagination pagination)
  112. throws Exception {
  113. HttpInvoker.HttpResponse httpResponse =
  114. getWorkflowInstancesPageHttpResponse(
  115. assetClassNames, assetPrimaryKeys, completed, pagination);
  116. String content = httpResponse.getContent();
  117. _logger.fine("HTTP response content: " + content);
  118. _logger.fine("HTTP response message: " + httpResponse.getMessage());
  119. _logger.fine(
  120. "HTTP response status code: " + httpResponse.getStatusCode());
  121. try {
  122. return Page.of(content, WorkflowInstanceSerDes::toDTO);
  123. }
  124. catch (Exception e) {
  125. _logger.log(
  126. Level.WARNING,
  127. "Unable to process HTTP response: " + content, e);
  128. throw new Problem.ProblemException(Problem.toDTO(content));
  129. }
  130. }
  131. public HttpInvoker.HttpResponse getWorkflowInstancesPageHttpResponse(
  132. String[] assetClassNames, Long[] assetPrimaryKeys,
  133. Boolean completed, Pagination pagination)
  134. throws Exception {
  135. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  136. if (_builder._locale != null) {
  137. httpInvoker.header(
  138. "Accept-Language", _builder._locale.toLanguageTag());
  139. }
  140. for (Map.Entry<String, String> entry :
  141. _builder._headers.entrySet()) {
  142. httpInvoker.header(entry.getKey(), entry.getValue());
  143. }
  144. for (Map.Entry<String, String> entry :
  145. _builder._parameters.entrySet()) {
  146. httpInvoker.parameter(entry.getKey(), entry.getValue());
  147. }
  148. httpInvoker.httpMethod(HttpInvoker.HttpMethod.GET);
  149. if (assetClassNames != null) {
  150. for (int i = 0; i < assetClassNames.length; i++) {
  151. httpInvoker.parameter(
  152. "assetClassNames", String.valueOf(assetClassNames[i]));
  153. }
  154. }
  155. if (assetPrimaryKeys != null) {
  156. for (int i = 0; i < assetPrimaryKeys.length; i++) {
  157. httpInvoker.parameter(
  158. "assetPrimaryKeys",
  159. String.valueOf(assetPrimaryKeys[i]));
  160. }
  161. }
  162. if (completed != null) {
  163. httpInvoker.parameter("completed", String.valueOf(completed));
  164. }
  165. if (pagination != null) {
  166. httpInvoker.parameter(
  167. "page", String.valueOf(pagination.getPage()));
  168. httpInvoker.parameter(
  169. "pageSize", String.valueOf(pagination.getPageSize()));
  170. }
  171. httpInvoker.path(
  172. _builder._scheme + "://" + _builder._host + ":" +
  173. _builder._port +
  174. "/o/headless-admin-workflow/v1.0/workflow-instances");
  175. httpInvoker.userNameAndPassword(
  176. _builder._login + ":" + _builder._password);
  177. return httpInvoker.invoke();
  178. }
  179. public WorkflowInstance postWorkflowInstanceSubmit(
  180. WorkflowInstanceSubmit workflowInstanceSubmit)
  181. throws Exception {
  182. HttpInvoker.HttpResponse httpResponse =
  183. postWorkflowInstanceSubmitHttpResponse(workflowInstanceSubmit);
  184. String content = httpResponse.getContent();
  185. _logger.fine("HTTP response content: " + content);
  186. _logger.fine("HTTP response message: " + httpResponse.getMessage());
  187. _logger.fine(
  188. "HTTP response status code: " + httpResponse.getStatusCode());
  189. try {
  190. return WorkflowInstanceSerDes.toDTO(content);
  191. }
  192. catch (Exception e) {
  193. _logger.log(
  194. Level.WARNING,
  195. "Unable to process HTTP response: " + content, e);
  196. throw new Problem.ProblemException(Problem.toDTO(content));
  197. }
  198. }
  199. public HttpInvoker.HttpResponse postWorkflowInstanceSubmitHttpResponse(
  200. WorkflowInstanceSubmit workflowInstanceSubmit)
  201. throws Exception {
  202. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  203. httpInvoker.body(
  204. workflowInstanceSubmit.toString(), "application/json");
  205. if (_builder._locale != null) {
  206. httpInvoker.header(
  207. "Accept-Language", _builder._locale.toLanguageTag());
  208. }
  209. for (Map.Entry<String, String> entry :
  210. _builder._headers.entrySet()) {
  211. httpInvoker.header(entry.getKey(), entry.getValue());
  212. }
  213. for (Map.Entry<String, String> entry :
  214. _builder._parameters.entrySet()) {
  215. httpInvoker.parameter(entry.getKey(), entry.getValue());
  216. }
  217. httpInvoker.httpMethod(HttpInvoker.HttpMethod.POST);
  218. httpInvoker.path(
  219. _builder._scheme + "://" + _builder._host + ":" +
  220. _builder._port +
  221. "/o/headless-admin-workflow/v1.0/workflow-instances/submit");
  222. httpInvoker.userNameAndPassword(
  223. _builder._login + ":" + _builder._password);
  224. return httpInvoker.invoke();
  225. }
  226. public void deleteWorkflowInstance(Long workflowInstanceId)
  227. throws Exception {
  228. HttpInvoker.HttpResponse httpResponse =
  229. deleteWorkflowInstanceHttpResponse(workflowInstanceId);
  230. String content = httpResponse.getContent();
  231. _logger.fine("HTTP response content: " + content);
  232. _logger.fine("HTTP response message: " + httpResponse.getMessage());
  233. _logger.fine(
  234. "HTTP response status code: " + httpResponse.getStatusCode());
  235. try {
  236. return;
  237. }
  238. catch (Exception e) {
  239. _logger.log(
  240. Level.WARNING,
  241. "Unable to process HTTP response: " + content, e);
  242. throw new Problem.ProblemException(Problem.toDTO(content));
  243. }
  244. }
  245. public HttpInvoker.HttpResponse deleteWorkflowInstanceHttpResponse(
  246. Long workflowInstanceId)
  247. throws Exception {
  248. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  249. if (_builder._locale != null) {
  250. httpInvoker.header(
  251. "Accept-Language", _builder._locale.toLanguageTag());
  252. }
  253. for (Map.Entry<String, String> entry :
  254. _builder._headers.entrySet()) {
  255. httpInvoker.header(entry.getKey(), entry.getValue());
  256. }
  257. for (Map.Entry<String, String> entry :
  258. _builder._parameters.entrySet()) {
  259. httpInvoker.parameter(entry.getKey(), entry.getValue());
  260. }
  261. httpInvoker.httpMethod(HttpInvoker.HttpMethod.DELETE);
  262. httpInvoker.path(
  263. _builder._scheme + "://" + _builder._host + ":" +
  264. _builder._port +
  265. "/o/headless-admin-workflow/v1.0/workflow-instances/{workflowInstanceId}",
  266. workflowInstanceId);
  267. httpInvoker.userNameAndPassword(
  268. _builder._login + ":" + _builder._password);
  269. return httpInvoker.invoke();
  270. }
  271. public WorkflowInstance getWorkflowInstance(Long workflowInstanceId)
  272. throws Exception {
  273. HttpInvoker.HttpResponse httpResponse =
  274. getWorkflowInstanceHttpResponse(workflowInstanceId);
  275. String content = httpResponse.getContent();
  276. _logger.fine("HTTP response content: " + content);
  277. _logger.fine("HTTP response message: " + httpResponse.getMessage());
  278. _logger.fine(
  279. "HTTP response status code: " + httpResponse.getStatusCode());
  280. try {
  281. return WorkflowInstanceSerDes.toDTO(content);
  282. }
  283. catch (Exception e) {
  284. _logger.log(
  285. Level.WARNING,
  286. "Unable to process HTTP response: " + content, e);
  287. throw new Problem.ProblemException(Problem.toDTO(content));
  288. }
  289. }
  290. public HttpInvoker.HttpResponse getWorkflowInstanceHttpResponse(
  291. Long workflowInstanceId)
  292. throws Exception {
  293. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  294. if (_builder._locale != null) {
  295. httpInvoker.header(
  296. "Accept-Language", _builder._locale.toLanguageTag());
  297. }
  298. for (Map.Entry<String, String> entry :
  299. _builder._headers.entrySet()) {
  300. httpInvoker.header(entry.getKey(), entry.getValue());
  301. }
  302. for (Map.Entry<String, String> entry :
  303. _builder._parameters.entrySet()) {
  304. httpInvoker.parameter(entry.getKey(), entry.getValue());
  305. }
  306. httpInvoker.httpMethod(HttpInvoker.HttpMethod.GET);
  307. httpInvoker.path(
  308. _builder._scheme + "://" + _builder._host + ":" +
  309. _builder._port +
  310. "/o/headless-admin-workflow/v1.0/workflow-instances/{workflowInstanceId}",
  311. workflowInstanceId);
  312. httpInvoker.userNameAndPassword(
  313. _builder._login + ":" + _builder._password);
  314. return httpInvoker.invoke();
  315. }
  316. public WorkflowInstance postWorkflowInstanceChangeTransition(
  317. Long workflowInstanceId, ChangeTransition changeTransition)
  318. throws Exception {
  319. HttpInvoker.HttpResponse httpResponse =
  320. postWorkflowInstanceChangeTransitionHttpResponse(
  321. workflowInstanceId, changeTransition);
  322. String content = httpResponse.getContent();
  323. _logger.fine("HTTP response content: " + content);
  324. _logger.fine("HTTP response message: " + httpResponse.getMessage());
  325. _logger.fine(
  326. "HTTP response status code: " + httpResponse.getStatusCode());
  327. try {
  328. return WorkflowInstanceSerDes.toDTO(content);
  329. }
  330. catch (Exception e) {
  331. _logger.log(
  332. Level.WARNING,
  333. "Unable to process HTTP response: " + content, e);
  334. throw new Problem.ProblemException(Problem.toDTO(content));
  335. }
  336. }
  337. public HttpInvoker.HttpResponse
  338. postWorkflowInstanceChangeTransitionHttpResponse(
  339. Long workflowInstanceId, ChangeTransition changeTransition)
  340. throws Exception {
  341. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  342. httpInvoker.body(changeTransition.toString(), "application/json");
  343. if (_builder._locale != null) {
  344. httpInvoker.header(
  345. "Accept-Language", _builder._locale.toLanguageTag());
  346. }
  347. for (Map.Entry<String, String> entry :
  348. _builder._headers.entrySet()) {
  349. httpInvoker.header(entry.getKey(), entry.getValue());
  350. }
  351. for (Map.Entry<String, String> entry :
  352. _builder._parameters.entrySet()) {
  353. httpInvoker.parameter(entry.getKey(), entry.getValue());
  354. }
  355. httpInvoker.httpMethod(HttpInvoker.HttpMethod.POST);
  356. httpInvoker.path(
  357. _builder._scheme + "://" + _builder._host + ":" +
  358. _builder._port +
  359. "/o/headless-admin-workflow/v1.0/workflow-instances/{workflowInstanceId}/change-transition",
  360. workflowInstanceId);
  361. httpInvoker.userNameAndPassword(
  362. _builder._login + ":" + _builder._password);
  363. return httpInvoker.invoke();
  364. }
  365. private WorkflowInstanceResourceImpl(Builder builder) {
  366. _builder = builder;
  367. }
  368. private static final Logger _logger = Logger.getLogger(
  369. WorkflowInstanceResource.class.getName());
  370. private Builder _builder;
  371. }
  372. }