developer.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. Developer's Guide
  2. =================
  3. - `Task Types <#task-types>`__ - write your own operations
  4. - `Actions <#actions>`__ - extend the GUI for your tasks
  5. - `Reporting <#reporting>`__ - choose the content and destination of
  6. your batch reports
  7. Task Types
  8. ----------
  9. Task manager can be extended with custom made task types. Make your own
  10. implementation of the interface ``TaskType`` and let it be a spring
  11. bean. The name provided via the ``Named`` interface is used as a
  12. reference.
  13. .. code:: java
  14. /**
  15. * A Task Type.
  16. *
  17. */
  18. public interface TaskType extends Named {
  19. /**
  20. * Return parameter info for this task type.
  21. * It is recommended to use a LinkedHashMap and add the parameters in a intuitive order.
  22. * This order will be preserved to present parameters to the user.
  23. *
  24. * @return the parameter info
  25. */
  26. Map<String, ParameterInfo> getParameterInfo();
  27. /**
  28. * Run a task, based on these parameter values.
  29. * @param ctx task context
  30. * @return the task result
  31. */
  32. TaskResult run(TaskContext ctx) throws TaskException;
  33. /**
  34. * Do a clean-up for this task (for example, if this task publishes something, remove it).
  35. * @param ctx task context
  36. * @throws TaskException
  37. */
  38. void cleanup(TaskContext ctx) throws TaskException;
  39. /**
  40. * task type can specify whether it supports clean-up or not
  41. *
  42. * @return true if clean-up is supported
  43. */
  44. default boolean supportsCleanup() {
  45. return true;
  46. }
  47. }
  48. A ParameterInfo object contains a name, a `type <#parameter-type>`__,
  49. whether they are required, and which other parameters they depend on
  50. (for example, a database table depends on a database).
  51. The Task context looks as follows:
  52. .. code:: java
  53. /**
  54. * Task Context used during batch run or task clean-up.
  55. *
  56. */
  57. public interface TaskContext {
  58. /**
  59. * @return the task
  60. */
  61. Task getTask();
  62. /**
  63. * @return the batch context, null if this is a clean-up
  64. */
  65. BatchContext getBatchContext();
  66. /**
  67. *
  68. * @return the parameter values, lazy loaded from task and configuration.
  69. *
  70. * @throws TaskException
  71. */
  72. Map<String, Object> getParameterValues() throws TaskException;
  73. /**
  74. * Tasks can call this function to check if the user wants to interrupt the batch
  75. * and interrupt themselves.
  76. * If they do, they should still return a TaskResult that implements a roll back
  77. * of what was already done.
  78. *
  79. * @return whether the batch run should be interrupted, false if this is a clean-up
  80. */
  81. boolean isInterruptMe();
  82. }
  83. The batch context looks as follows:
  84. .. code:: java
  85. /**
  86. * During run, tasks create temporary objects that are committed to real objects during
  87. * the commit phase (such as a table name) This maps real objects
  88. * to temporary objects during a single batch run. Tasks should save and look up temporary
  89. * objects so that tasks within a batch can work together.
  90. *
  91. */
  92. public interface BatchContext {
  93. public static interface Dependency {
  94. public void revert() throws TaskException;
  95. }
  96. Object get(Object original);
  97. Object get(Object original, Dependency dependency);
  98. /**
  99. * Whatever is put here in the task, must be removed in the commit!
  100. *
  101. * @param original
  102. * @param temp
  103. */
  104. void put(Object original, Object temp);
  105. void delete(Object original) throws TaskException;
  106. BatchRun getBatchRun();
  107. }
  108. The task result looks as follows:
  109. .. code:: java
  110. /**
  111. * A handle of a task that was run but must still be committed or rolled back.
  112. *
  113. *
  114. */
  115. public interface TaskResult {
  116. /**
  117. * finalize and clean-up resources any roll-back data
  118. */
  119. void commit() throws TaskException;
  120. /**
  121. * batch has failed - cancel all changes
  122. */
  123. void rollback() throws TaskException;
  124. }
  125. This is an example of how a task type can create temporary object:
  126. .. code:: java
  127. //inside TaskType.run method
  128. ctx.getBatchContext().put(originalObject, tempObject)
  129. ...
  130. return new TaskResult() {
  131. @Override
  132. public void commit() throws TaskException {
  133. //this MUST be done!!!
  134. ctx.getBatchContext.delete(originalObject)
  135. }
  136. ...
  137. }
  138. Another task type would use this temporary object as follows:
  139. .. code:: java
  140. //inside TaskType.run method
  141. Object tempObject = ctx.getBatchContext().get(originalObject, new Dependency() {
  142. @Override
  143. public void revert() {
  144. Object object = ctx.getBatchContext().get(originalObject);
  145. mySomething.setMyProperty(object);
  146. mySomething.save();
  147. }
  148. });
  149. mySomething.setMyProperty(tempObject);
  150. mySomething.save();
  151. Parameter Types
  152. ~~~~~~~~~~~~~~~
  153. Custom task types may use existing or define new parameter types. They
  154. handle parameter validation, parsing parameter Strings into other object
  155. types, and provide information to the GUI about the parameters.
  156. Existing regular Parameter Types (static members of ``ParameterType``
  157. interface):
  158. - ``STRING``
  159. - ``INTEGER``
  160. - ``BOOLEAN``
  161. - ``URI``
  162. - ``SQL`` (protects against ';' hacking)
  163. External Parameter Types (members of ``ExtTypes`` spring bean): \*
  164. ``dbName``: :ref:`database name <taskmanager_user_databases>` \* ``tableName``:
  165. table name (parameter must depend on parameter of ``dbName`` type) \*
  166. ``extGeoserver``: :ref:`external geoserver <taskmanager_user_external_geoserver>`
  167. \* ``internalLayer``: layer from geoserver catalog \* ``name``: name
  168. qualified with namespace from geoserver catalog \* ``fileService``:
  169. :ref:`file service <taskmanager_user_file_services>` \* ``file``: reference to file
  170. (parameter must dpend of parameter of ``fileService`` type)
  171. Defining a new Parameter Type:
  172. .. code:: java
  173. /**
  174. *
  175. * A Parameter Type For a Task
  176. *
  177. */
  178. public interface ParameterType {
  179. /**
  180. * List possible values for this parameter (when applicable).
  181. * Include an empty string if custom value is also allowed.
  182. *
  183. * @param dependsOnRawValues raw values of depending parameters.
  184. * @return list of possible values, null if not applicable.
  185. */
  186. public List<String> getDomain(List<String> dependsOnRawValues);
  187. /**
  188. * Validate and parse a parameter value for this parameter (at run time).
  189. *
  190. * @param value the raw value.
  191. * @param dependsOnRawValues raw values of depending parameters.
  192. * @return the parsed value, NULL if the value is invalid.
  193. */
  194. public Object parse(String value, List<String> dependsOnRawValues);
  195. /**
  196. * Validate a parameter value (at configuration time).
  197. *
  198. * @param value the raw value.
  199. * @param dependsOnRawValues raw values of depending parameters.
  200. * @return true if the value is considered valid at configuration time (may still be considered
  201. * invalid at parse time)
  202. */
  203. public default boolean validate(String value, List<String> dependsOnRawValues) {
  204. return parse(value, dependsOnRawValues) != null;
  205. }
  206. /**
  207. * Returns a list of web actions related to this type
  208. *
  209. * @return list of web actions
  210. */
  211. public default List<String> getActions() {
  212. return Collections.emptyList();
  213. }
  214. }
  215. Actions
  216. -------
  217. Actions are extensions to the taskmanager webGUI attached to particular
  218. parameter types.
  219. .. code:: java
  220. public interface Action extends Named, Serializable {
  221. /**
  222. * Execute this action.
  223. *
  224. * @param onPage the configuration page.
  225. * @param target the target of the ajax request that executed this action.
  226. * @param valueModel the value of the attribute, for reading and writing.
  227. * @param dependsOnRawValues raw values of depending attributes.
  228. */
  229. void execute(ConfigurationPage onPage, AjaxRequestTarget target, IModel<String> valueModel, List<String> dependsOnRawValues);
  230. /**
  231. * Check whether this action can be executed with current values.
  232. * \
  233. * @param value the value of the attribute.
  234. * @param dependsOnRawValues raw values of depending attributes.
  235. * @return whether this action accepts these values.
  236. */
  237. default boolean accept(String value, List<String> dependsOnRawValues) {
  238. return true;
  239. }
  240. }
  241. In order to be linked to parameter types, an action must be spring bean.
  242. The name provided via the ``Named`` interface is used as a reference.
  243. Reporting
  244. ---------
  245. Report builders
  246. ~~~~~~~~~~~~~~~
  247. Reports are user friendly representations of finished batch runs, that
  248. are sent to some destination right after the batch run has finished. A
  249. report has a type (``FAILED``, ``CANCELLED`` or ``SUCCESS``), a title
  250. and a content. Use spring to configure a single report builder.
  251. .. code:: java
  252. /**
  253. * A report builder generates a report from a batch.
  254. * One could write a custom one.
  255. *
  256. */
  257. public interface ReportBuilder {
  258. Report buildBatchRunReport(BatchRun batchRun);
  259. }
  260. Report services.
  261. ~~~~~~~~~~~~~~~~
  262. Use spring to configure any number of report services.
  263. .. code:: java
  264. /**
  265. * A report service sends a report to a particular destination.
  266. * One can add an unlimited amount of report services which will all be used.
  267. *
  268. */
  269. public interface ReportService {
  270. /**
  271. * Enumeration for filter.
  272. *
  273. */
  274. public enum Filter {
  275. /** All batch runs are reported **/
  276. ALL (Report.Type.FAILED, Report.Type.CANCELLED, Report.Type.SUCCESS),
  277. /** Only failed and cancelled batch runs are reported **/
  278. FAILED_AND_CANCELLED (Report.Type.FAILED, Report.Type.CANCELLED),
  279. /** Only failed batch runs are reported **/
  280. FAILED_ONLY (Report.Type.FAILED);
  281. Report.Type[] types;
  282. private Filter(Report.Type... types) {
  283. this.types = types;
  284. }
  285. public boolean matches(Report.Type type) {
  286. return ArrayUtils.contains(types, type);
  287. }
  288. }
  289. /**
  290. * Return the filter of the report.
  291. *
  292. * @return the filter of the report.
  293. */
  294. public Filter getFilter();
  295. /**
  296. * Send a report.
  297. *
  298. * @param report the report.
  299. */
  300. public void sendReport(Report report);
  301. }