org-taskjuggler.el 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. ;;; org-taskjuggler.el --- TaskJuggler exporter for org-mode
  2. ;;
  3. ;; Copyright (C) 2007-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Emacs Lisp Archive Entry
  6. ;; Filename: org-taskjuggler.el
  7. ;; Author: Christian Egli
  8. ;; Maintainer: Christian Egli
  9. ;; Keywords: org, taskjuggler, project planning
  10. ;; Description: Converts an org-mode buffer into a taskjuggler project plan
  11. ;; URL:
  12. ;; This file is part of GNU Emacs.
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;; Commentary:
  24. ;;
  25. ;; This library implements a TaskJuggler exporter for org-mode.
  26. ;; TaskJuggler uses a text format to define projects, tasks and
  27. ;; resources, so it is a natural fit for org-mode. It can produce all
  28. ;; sorts of reports for tasks or resources in either HTML, CSV or PDF.
  29. ;; The current version of TaskJuggler requires KDE but the next
  30. ;; version is implemented in Ruby and should therefore run on any
  31. ;; platform.
  32. ;;
  33. ;; The exporter is a bit different from other exporters, such as the
  34. ;; HTML and LaTeX exporters for example, in that it does not export
  35. ;; all the nodes of a document or strictly follow the order of the
  36. ;; nodes in the document.
  37. ;;
  38. ;; Instead the TaskJuggler exporter looks for a tree that defines the
  39. ;; tasks and a optionally tree that defines the resources for this
  40. ;; project. It then creates a TaskJuggler file based on these trees
  41. ;; and the attributes defined in all the nodes.
  42. ;;
  43. ;; * Installation
  44. ;;
  45. ;; Put this file into your load-path and the following line into your
  46. ;; ~/.emacs:
  47. ;;
  48. ;; (require 'org-taskjuggler)
  49. ;;
  50. ;; The interactive functions are similar to those of the HTML and LaTeX
  51. ;; exporters:
  52. ;;
  53. ;; M-x `org-export-as-taskjuggler'
  54. ;; M-x `org-export-as-taskjuggler-and-open'
  55. ;;
  56. ;; * Tasks
  57. ;;
  58. ;; Let's illustrate the usage with a small example. Create your tasks
  59. ;; as you usually do with org-mode. Assign efforts to each task using
  60. ;; properties (it's easiest to do this in the column view). You should
  61. ;; end up with something similar to the example by Peter Jones in
  62. ;; http://www.contextualdevelopment.com/static/artifacts/articles/2008/project-planning/project-planning.org.
  63. ;; Now mark the top node of your tasks with a tag named
  64. ;; "taskjuggler_project" (or whatever you customized
  65. ;; `org-export-taskjuggler-project-tag' to). You are now ready to
  66. ;; export the project plan with `org-export-as-taskjuggler-and-open'
  67. ;; which will export the project plan and open a gant chart in
  68. ;; TaskJugglerUI.
  69. ;;
  70. ;; * Resources
  71. ;;
  72. ;; Next you can define resources and assign those to work on specific
  73. ;; tasks. You can group your resources hierarchically. Tag the top
  74. ;; node of the resources with "taskjuggler_resource" (or whatever you
  75. ;; customized `org-export-taskjuggler-resource-tag' to). You can
  76. ;; optionally assign an identifier (named "resource_id") to the
  77. ;; resources (using the standard org properties commands) or you can
  78. ;; let the exporter generate identifiers automatically (the exporter
  79. ;; picks the first word of the headline as the identifier as long as
  80. ;; it is unique, see the documentation of
  81. ;; `org-taskjuggler-get-unique-id'). Using that identifier you can
  82. ;; then allocate resources to tasks. This is again done with the
  83. ;; "allocate" property on the tasks. Do this in column view or when on
  84. ;; the task type
  85. ;;
  86. ;; C-c C-x p allocate RET <resource_id> RET
  87. ;;
  88. ;; Once the allocations are done you can again export to TaskJuggler
  89. ;; and check in the Resource Allocation Graph which person is working
  90. ;; on what task at what time.
  91. ;;
  92. ;; * Export of properties
  93. ;;
  94. ;; The exporter also takes TODO state information into consideration,
  95. ;; i.e. if a task is marked as done it will have the corresponding
  96. ;; attribute in TaskJuggler ("complete 100"). Also it will export any
  97. ;; property on a task resource or resource node which is known to
  98. ;; TaskJuggler, such as limits, vacation, shift, booking, efficiency,
  99. ;; journalentry, rate for resources or account, start, note, duration,
  100. ;; end, journalentry, milestone, reference, responsible, scheduling,
  101. ;; etc for tasks.
  102. ;;
  103. ;; * Dependencies
  104. ;;
  105. ;; The exporter will handle dependencies that are defined in the tasks
  106. ;; either with the ORDERED attribute (see TODO dependencies in the Org
  107. ;; mode manual) or with the BLOCKER attribute (see org-depend.el) or
  108. ;; alternatively with a depends attribute. Both the BLOCKER and the
  109. ;; depends attribute can be either "previous-sibling" or a reference
  110. ;; to an identifier (named "task_id") which is defined for another
  111. ;; task in the project. BLOCKER and the depends attribute can define
  112. ;; multiple dependencies separated by either space or comma. You can
  113. ;; also specify optional attributes on the dependency by simply
  114. ;; appending it. The following examples should illustrate this:
  115. ;;
  116. ;; * Training material
  117. ;; :PROPERTIES:
  118. ;; :task_id: training_material
  119. ;; :ORDERED: t
  120. ;; :END:
  121. ;; ** Markup Guidelines
  122. ;; :PROPERTIES:
  123. ;; :Effort: 2d
  124. ;; :END:
  125. ;; ** Workflow Guidelines
  126. ;; :PROPERTIES:
  127. ;; :Effort: 2d
  128. ;; :END:
  129. ;; * Presentation
  130. ;; :PROPERTIES:
  131. ;; :Effort: 2d
  132. ;; :BLOCKER: training_material { gapduration 1d } some_other_task
  133. ;; :END:
  134. ;;
  135. ;;;; * TODO
  136. ;; - Use SCHEDULED and DEADLINE information (not just start and end
  137. ;; properties).
  138. ;; - Look at org-file-properties, org-global-properties and
  139. ;; org-global-properties-fixed
  140. ;; - What about property inheritance and org-property-inherit-p?
  141. ;; - Use TYPE_TODO as an way to assign resources
  142. ;; - Make sure multiple dependency definitions (i.e. BLOCKER on
  143. ;; previous-sibling and on a specific task_id) in multiple
  144. ;; attributes are properly exported.
  145. ;;
  146. ;;; Code:
  147. (eval-when-compile
  148. (require 'cl))
  149. (require 'org)
  150. (require 'org-exp)
  151. ;;; User variables:
  152. (defgroup org-export-taskjuggler nil
  153. "Options for exporting Org-mode files to TaskJuggler."
  154. :tag "Org Export TaskJuggler"
  155. :group 'org-export)
  156. (defcustom org-export-taskjuggler-extension ".tjp"
  157. "Extension of TaskJuggler files."
  158. :group 'org-export-taskjuggler
  159. :type 'string)
  160. (defcustom org-export-taskjuggler-project-tag "taskjuggler_project"
  161. "Tag, property or todo used to find the tree containing all
  162. the tasks for the project."
  163. :group 'org-export-taskjuggler
  164. :type 'string)
  165. (defcustom org-export-taskjuggler-resource-tag "taskjuggler_resource"
  166. "Tag, property or todo used to find the tree containing all the
  167. resources for the project."
  168. :group 'org-export-taskjuggler
  169. :type 'string)
  170. (defcustom org-export-taskjuggler-target-version 2.4
  171. "Which version of TaskJuggler the exporter is targeting."
  172. :group 'org-export-taskjuggler
  173. :type 'number)
  174. (defcustom org-export-taskjuggler-default-project-version "1.0"
  175. "Default version string for the project."
  176. :group 'org-export-taskjuggler
  177. :type 'string)
  178. (defcustom org-export-taskjuggler-default-project-duration 280
  179. "Default project duration if no start and end date have been defined
  180. in the root node of the task tree, i.e. the tree that has been marked
  181. with `org-export-taskjuggler-project-tag'"
  182. :group 'org-export-taskjuggler
  183. :type 'integer)
  184. (defcustom org-export-taskjuggler-default-reports
  185. '("taskreport \"Gantt Chart\" {
  186. headline \"Project Gantt Chart\"
  187. columns hierarchindex, name, start, end, effort, duration, completed, chart
  188. timeformat \"%Y-%m-%d\"
  189. hideresource 1
  190. loadunit shortauto
  191. }"
  192. "resourcereport \"Resource Graph\" {
  193. headline \"Resource Allocation Graph\"
  194. columns no, name, utilization, freeload, chart
  195. loadunit shortauto
  196. sorttasks startup
  197. hidetask ~isleaf()
  198. }")
  199. "Default reports for the project."
  200. :group 'org-export-taskjuggler
  201. :type '(repeat (string :tag "Report")))
  202. (defcustom org-export-taskjuggler-default-global-properties
  203. "shift s40 \"Part time shift\" {
  204. workinghours wed, thu, fri off
  205. }
  206. "
  207. "Default global properties for the project. Here you typically
  208. define global properties such as shifts, accounts, rates,
  209. vacation, macros and flags. Any property that is allowed within
  210. the TaskJuggler file can be inserted. You could for example
  211. include another TaskJuggler file.
  212. The global properties are inserted after the project declaration
  213. but before any resource and task declarations."
  214. :group 'org-export-taskjuggler
  215. :type '(string :tag "Preamble"))
  216. ;;; Hooks
  217. (defvar org-export-taskjuggler-final-hook nil
  218. "Hook run at the end of TaskJuggler export, in the new buffer.")
  219. ;;; Autoload functions:
  220. ;; avoid compiler warning about free variable
  221. (defvar org-export-taskjuggler-old-level)
  222. ;;;###autoload
  223. (defun org-export-as-taskjuggler ()
  224. "Export parts of the current buffer as a TaskJuggler file.
  225. The exporter looks for a tree with tag, property or todo that
  226. matches `org-export-taskjuggler-project-tag' and takes this as
  227. the tasks for this project. The first node of this tree defines
  228. the project properties such as project name and project period.
  229. If there is a tree with tag, property or todo that matches
  230. `org-export-taskjuggler-resource-tag' this three is taken as
  231. resources for the project. If no resources are specified, a
  232. default resource is created and allocated to the project. Also
  233. the taskjuggler project will be created with default reports as
  234. defined in `org-export-taskjuggler-default-reports'."
  235. (interactive)
  236. (message "Exporting...")
  237. (setq-default org-done-keywords org-done-keywords)
  238. (let* ((tasks
  239. (org-taskjuggler-resolve-dependencies
  240. (org-taskjuggler-assign-task-ids
  241. (org-taskjuggler-compute-task-leafiness
  242. (org-map-entries
  243. 'org-taskjuggler-components
  244. org-export-taskjuggler-project-tag nil 'archive 'comment)))))
  245. (resources
  246. (org-taskjuggler-assign-resource-ids
  247. (org-map-entries
  248. 'org-taskjuggler-components
  249. org-export-taskjuggler-resource-tag nil 'archive 'comment)))
  250. (filename (expand-file-name
  251. (concat
  252. (file-name-sans-extension
  253. (file-name-nondirectory buffer-file-name))
  254. org-export-taskjuggler-extension)))
  255. (buffer (find-file-noselect filename))
  256. (old-buffer (current-buffer))
  257. (org-export-taskjuggler-old-level 0)
  258. task resource)
  259. (unless tasks
  260. (error "No tasks specified"))
  261. ;; add a default resource
  262. (unless resources
  263. (setq resources
  264. `((("resource_id" . ,(user-login-name))
  265. ("headline" . ,user-full-name)
  266. ("level" . 1)))))
  267. ;; add a default allocation to the first task if none was given
  268. (unless (assoc "allocate" (car tasks))
  269. (let ((task (car tasks))
  270. (resource-id (cdr (assoc "resource_id" (car resources)))))
  271. (setcar tasks (push (cons "allocate" resource-id) task))))
  272. ;; add a default start date to the first task if none was given
  273. (unless (assoc "start" (car tasks))
  274. (let ((task (car tasks))
  275. (time-string (format-time-string "%Y-%m-%d")))
  276. (setcar tasks (push (cons "start" time-string) task))))
  277. ;; add a default version if none was given
  278. (unless (assoc "version" (car tasks))
  279. (let ((task (car tasks))
  280. (version org-export-taskjuggler-default-project-version))
  281. (setcar tasks (push (cons "version" version) task))))
  282. (with-current-buffer buffer
  283. (erase-buffer)
  284. (org-clone-local-variables old-buffer "^org-")
  285. (org-taskjuggler-open-project (car tasks))
  286. (insert org-export-taskjuggler-default-global-properties)
  287. (insert "\n")
  288. (dolist (resource resources)
  289. (let ((level (cdr (assoc "level" resource))))
  290. (org-taskjuggler-close-maybe level)
  291. (org-taskjuggler-open-resource resource)
  292. (setq org-export-taskjuggler-old-level level)))
  293. (org-taskjuggler-close-maybe 1)
  294. (setq org-export-taskjuggler-old-level 0)
  295. (dolist (task tasks)
  296. (let ((level (cdr (assoc "level" task))))
  297. (org-taskjuggler-close-maybe level)
  298. (org-taskjuggler-open-task task)
  299. (setq org-export-taskjuggler-old-level level)))
  300. (org-taskjuggler-close-maybe 1)
  301. (org-taskjuggler-insert-reports)
  302. (save-buffer)
  303. (or (org-export-push-to-kill-ring "TaskJuggler")
  304. (message "Exporting... done"))
  305. (current-buffer))))
  306. ;;;###autoload
  307. (defun org-export-as-taskjuggler-and-open ()
  308. "Export the current buffer as a TaskJuggler file and open it
  309. with the TaskJuggler GUI."
  310. (interactive)
  311. (let* ((file-name (buffer-file-name (org-export-as-taskjuggler)))
  312. (process-name "TaskJugglerUI")
  313. (command (concat process-name " " file-name)))
  314. (start-process-shell-command process-name nil command)))
  315. (defun org-taskjuggler-targeting-tj3-p ()
  316. "Return true if we are targeting TaskJuggler III."
  317. (>= org-export-taskjuggler-target-version 3.0))
  318. (defun org-taskjuggler-parent-is-ordered-p ()
  319. "Return true if the parent of the current node has a property
  320. \"ORDERED\". Return nil otherwise."
  321. (save-excursion
  322. (and (org-up-heading-safe) (org-entry-get (point) "ORDERED"))))
  323. (defun org-taskjuggler-components ()
  324. "Return an alist containing all the pertinent information for
  325. the current node such as the headline, the level, todo state
  326. information, all the properties, etc."
  327. (let* ((props (org-entry-properties))
  328. (components (org-heading-components))
  329. (level (nth 1 components))
  330. (headline
  331. (replace-regexp-in-string
  332. "\"" "\\\"" (nth 4 components) t t)) ; quote double quotes in headlines
  333. (parent-ordered (org-taskjuggler-parent-is-ordered-p)))
  334. (push (cons "level" level) props)
  335. (push (cons "headline" headline) props)
  336. (push (cons "parent-ordered" parent-ordered) props)))
  337. (defun org-taskjuggler-assign-task-ids (tasks)
  338. "Given a list of tasks return the same list assigning a unique id
  339. and the full path to each task. Taskjuggler takes hierarchical ids.
  340. For that reason we have to make ids locally unique and we have to keep
  341. a path to the current task."
  342. (let ((previous-level 0)
  343. unique-ids unique-id
  344. path
  345. task resolved-tasks tmp)
  346. (dolist (task tasks resolved-tasks)
  347. (let ((level (cdr (assoc "level" task))))
  348. (cond
  349. ((< previous-level level)
  350. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  351. (dotimes (tmp (- level previous-level))
  352. (push (list unique-id) unique-ids)
  353. (push unique-id path)))
  354. ((= previous-level level)
  355. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  356. (push unique-id (car unique-ids))
  357. (setcar path unique-id))
  358. ((> previous-level level)
  359. (dotimes (tmp (- previous-level level))
  360. (pop unique-ids)
  361. (pop path))
  362. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  363. (push unique-id (car unique-ids))
  364. (setcar path unique-id)))
  365. (push (cons "unique-id" unique-id) task)
  366. (push (cons "path" (mapconcat 'identity (reverse path) ".")) task)
  367. (setq previous-level level)
  368. (setq resolved-tasks (append resolved-tasks (list task)))))))
  369. (defun org-taskjuggler-compute-task-leafiness (tasks)
  370. "Figure out if each task is a leaf by looking at it's level,
  371. and the level of its successor. If the successor is higher (ie
  372. deeper), then it's not a leaf."
  373. (let (new-list)
  374. (while (car tasks)
  375. (let ((task (car tasks))
  376. (successor (car (cdr tasks))))
  377. (cond
  378. ;; if a task has no successors it is a leaf
  379. ((null successor)
  380. (push (cons (cons "leaf-node" t) task) new-list))
  381. ;; if the successor has a lower level than task it is a leaf
  382. ((<= (cdr (assoc "level" successor)) (cdr (assoc "level" task)))
  383. (push (cons (cons "leaf-node" t) task) new-list))
  384. ;; otherwise examine the rest of the tasks
  385. (t (push task new-list))))
  386. (setq tasks (cdr tasks)))
  387. (nreverse new-list)))
  388. (defun org-taskjuggler-assign-resource-ids (resources)
  389. "Given a list of resources return the same list, assigning a
  390. unique id to each resource."
  391. (let (unique-ids new-list)
  392. (dolist (resource resources new-list)
  393. (let ((unique-id (org-taskjuggler-get-unique-id resource unique-ids)))
  394. (push (cons "unique-id" unique-id) resource)
  395. (push unique-id unique-ids)
  396. (push resource new-list)))
  397. (nreverse new-list)))
  398. (defun org-taskjuggler-resolve-dependencies (tasks)
  399. (let ((previous-level 0)
  400. siblings
  401. task resolved-tasks)
  402. (dolist (task tasks resolved-tasks)
  403. (let* ((level (cdr (assoc "level" task)))
  404. (depends (cdr (assoc "depends" task)))
  405. (parent-ordered (cdr (assoc "parent-ordered" task)))
  406. (blocker (cdr (assoc "BLOCKER" task)))
  407. (blocked-on-previous
  408. (and blocker (string-match "previous-sibling" blocker)))
  409. (dependencies
  410. (org-taskjuggler-resolve-explicit-dependencies
  411. (append
  412. (and depends (org-taskjuggler-tokenize-dependencies depends))
  413. (and blocker (org-taskjuggler-tokenize-dependencies blocker)))
  414. tasks))
  415. previous-sibling)
  416. ; update previous sibling info
  417. (cond
  418. ((< previous-level level)
  419. (dotimes (tmp (- level previous-level))
  420. (push task siblings)))
  421. ((= previous-level level)
  422. (setq previous-sibling (car siblings))
  423. (setcar siblings task))
  424. ((> previous-level level)
  425. (dotimes (tmp (- previous-level level))
  426. (pop siblings))
  427. (setq previous-sibling (car siblings))
  428. (setcar siblings task)))
  429. ; insert a dependency on previous sibling if the parent is
  430. ; ordered or if the tasks has a BLOCKER attribute with value "previous-sibling"
  431. (when (or (and previous-sibling parent-ordered) blocked-on-previous)
  432. (push (format "!%s" (cdr (assoc "unique-id" previous-sibling))) dependencies))
  433. ; store dependency information
  434. (when dependencies
  435. (push (cons "depends" (mapconcat 'identity dependencies ", ")) task))
  436. (setq previous-level level)
  437. (setq resolved-tasks (append resolved-tasks (list task)))))))
  438. (defun org-taskjuggler-tokenize-dependencies (dependencies)
  439. "Split a dependency property value DEPENDENCIES into the
  440. individual dependencies and return them as a list while keeping
  441. the optional arguments (such as gapduration) for the
  442. dependencies. A dependency will have to match `[-a-zA-Z0-9_]+'."
  443. (cond
  444. ((string-match "^ *$" dependencies) nil)
  445. ((string-match "^[ \t]*\\([-a-zA-Z0-9_]+\\([ \t]*{[^}]+}\\)?\\)[ \t,]*" dependencies)
  446. (cons
  447. (substring dependencies (match-beginning 1) (match-end 1))
  448. (org-taskjuggler-tokenize-dependencies (substring dependencies (match-end 0)))))
  449. (t (error (format "invalid dependency id %s" dependencies)))))
  450. (defun org-taskjuggler-resolve-explicit-dependencies (dependencies tasks)
  451. "For each dependency in DEPENDENCIES try to find a
  452. corresponding task with a matching property \"task_id\" in TASKS.
  453. Return a list containing the resolved links for all DEPENDENCIES
  454. where a matching tasks was found. If the dependency is
  455. \"previous-sibling\" it is ignored (as this is dealt with in
  456. `org-taskjuggler-resolve-dependencies'). If there is no matching
  457. task the dependency is ignored and a warning is displayed ."
  458. (unless (null dependencies)
  459. (let*
  460. ;; the dependency might have optional attributes such as "{
  461. ;; gapduration 5d }", so only use the first string as id for the
  462. ;; dependency
  463. ((dependency (car dependencies))
  464. (id (car (split-string dependency)))
  465. (optional-attributes
  466. (mapconcat 'identity (cdr (split-string dependency)) " "))
  467. (path (org-taskjuggler-find-task-with-id id tasks)))
  468. (cond
  469. ;; ignore previous sibling dependencies
  470. ((equal (car dependencies) "previous-sibling")
  471. (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks))
  472. ;; if the id is found in another task use its path
  473. ((not (null path))
  474. (cons (mapconcat 'identity (list path optional-attributes) " ")
  475. (org-taskjuggler-resolve-explicit-dependencies
  476. (cdr dependencies) tasks)))
  477. ;; warn about dangling dependency but otherwise ignore it
  478. (t (display-warning
  479. 'org-export-taskjuggler
  480. (format "No task with matching property \"task_id\" found for id %s" id))
  481. (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks))))))
  482. (defun org-taskjuggler-find-task-with-id (id tasks)
  483. "Find ID in tasks. If found return the path of task. Otherwise
  484. return nil."
  485. (let ((task-id (cdr (assoc "task_id" (car tasks))))
  486. (path (cdr (assoc "path" (car tasks)))))
  487. (cond
  488. ((null tasks) nil)
  489. ((equal task-id id) path)
  490. (t (org-taskjuggler-find-task-with-id id (cdr tasks))))))
  491. (defun org-taskjuggler-get-unique-id (item unique-ids)
  492. "Return a unique id for an ITEM which can be a task or a resource.
  493. The id is derived from the headline and made unique against
  494. UNIQUE-IDS. If the (downcased) first token of the headline is not
  495. unique try to add more (downcased) tokens of the headline or
  496. finally add more underscore characters (\"_\")."
  497. (let* ((headline (cdr (assoc "headline" item)))
  498. (parts (split-string headline))
  499. (id (org-taskjuggler-clean-id (downcase (pop parts)))))
  500. ; try to add more parts of the headline to make it unique
  501. (while (and (member id unique-ids) (car parts))
  502. (setq id (concat id "_" (org-taskjuggler-clean-id (downcase (pop parts))))))
  503. ; if its still not unique add "_"
  504. (while (member id unique-ids)
  505. (setq id (concat id "_")))
  506. id))
  507. (defun org-taskjuggler-clean-id (id)
  508. "Clean and return ID to make it acceptable for taskjuggler."
  509. (and id
  510. ;; replace non-ascii by _
  511. (replace-regexp-in-string
  512. "[^a-zA-Z0-9_]" "_"
  513. ;; make sure id doesn't start with a number
  514. (replace-regexp-in-string "^\\([0-9]\\)" "_\\1" id))))
  515. (defun org-taskjuggler-open-project (project)
  516. "Insert the beginning of a project declaration. All valid
  517. attributes from the PROJECT alist are inserted. If no end date is
  518. specified it is calculated
  519. `org-export-taskjuggler-default-project-duration' days from now."
  520. (let* ((unique-id (cdr (assoc "unique-id" project)))
  521. (headline (cdr (assoc "headline" project)))
  522. (version (cdr (assoc "version" project)))
  523. (start (cdr (assoc "start" project)))
  524. (end (cdr (assoc "end" project))))
  525. (insert
  526. (format "project %s \"%s\" \"%s\" %s +%sd {\n }\n"
  527. unique-id headline version start
  528. org-export-taskjuggler-default-project-duration))))
  529. (defun org-taskjuggler-filter-and-join (items)
  530. "Filter all nil elements from ITEMS and join the remaining ones
  531. with separator \"\n\"."
  532. (let ((filtered-items (remq nil items)))
  533. (and filtered-items (mapconcat 'identity filtered-items "\n"))))
  534. (defun org-taskjuggler-get-attributes (item attributes)
  535. "Return all attribute as a single formated string. ITEM is an
  536. alist representing either a resource or a task. ATTRIBUTES is a
  537. list of symbols. Only entries from ITEM are considered that are
  538. listed in ATTRIBUTES."
  539. (org-taskjuggler-filter-and-join
  540. (mapcar
  541. (lambda (attribute)
  542. (org-taskjuggler-filter-and-join
  543. (org-taskjuggler-get-attribute item attribute)))
  544. attributes)))
  545. (defun org-taskjuggler-get-attribute (item attribute)
  546. "Return a list of strings containing the properly formatted
  547. taskjuggler declaration for a given ATTRIBUTE in ITEM (an alist).
  548. If the ATTRIBUTE is not in ITEM return nil."
  549. (cond
  550. ((null item) nil)
  551. ((equal (symbol-name attribute) (car (car item)))
  552. (cons (format "%s %s" (symbol-name attribute) (cdr (car item)))
  553. (org-taskjuggler-get-attribute (cdr item) attribute)))
  554. (t (org-taskjuggler-get-attribute (cdr item) attribute))))
  555. (defun org-taskjuggler-open-resource (resource)
  556. "Insert the beginning of a resource declaration. All valid
  557. attributes from the RESOURCE alist are inserted. If the RESOURCE
  558. defines a property \"resource_id\" it will be used as the id for
  559. this resource. Otherwise it will use the ID property. If neither
  560. is defined it will calculate a unique id for the resource using
  561. `org-taskjuggler-get-unique-id'."
  562. (let ((id (org-taskjuggler-clean-id
  563. (or (cdr (assoc "resource_id" resource))
  564. (cdr (assoc "ID" resource))
  565. (cdr (assoc "unique-id" resource)))))
  566. (headline (cdr (assoc "headline" resource)))
  567. (attributes '(limits vacation shift booking efficiency journalentry rate)))
  568. (insert
  569. (concat
  570. "resource " id " \"" headline "\" {\n "
  571. (org-taskjuggler-get-attributes resource attributes) "\n"))))
  572. (defun org-taskjuggler-clean-effort (effort)
  573. "Translate effort strings into a format acceptable to taskjuggler,
  574. i.e. REAL UNIT. A valid effort string can be anything that is
  575. accepted by `org-duration-string-to-minutes´."
  576. (cond
  577. ((null effort) effort)
  578. (t (let* ((minutes (org-duration-string-to-minutes effort))
  579. (hours (/ minutes 60.0)))
  580. (format "%.1fh" hours)))))
  581. (defun org-taskjuggler-get-priority (priority)
  582. "Return a priority between 1 and 1000 based on PRIORITY, an
  583. org-mode priority string."
  584. (max 1 (/ (* 1000 (- org-lowest-priority (string-to-char priority)))
  585. (- org-lowest-priority org-highest-priority))))
  586. (defun org-taskjuggler-open-task (task)
  587. (let* ((unique-id (cdr (assoc "unique-id" task)))
  588. (headline (cdr (assoc "headline" task)))
  589. (effort (org-taskjuggler-clean-effort (cdr (assoc org-effort-property task))))
  590. (depends (cdr (assoc "depends" task)))
  591. (allocate (cdr (assoc "allocate" task)))
  592. (priority-raw (cdr (assoc "PRIORITY" task)))
  593. (priority (and priority-raw (org-taskjuggler-get-priority priority-raw)))
  594. (state (cdr (assoc "TODO" task)))
  595. (complete (or (and (member state org-done-keywords) "100")
  596. (cdr (assoc "complete" task))))
  597. (parent-ordered (cdr (assoc "parent-ordered" task)))
  598. (previous-sibling (cdr (assoc "previous-sibling" task)))
  599. (milestone (or (cdr (assoc "milestone" task))
  600. (and (assoc "leaf-node" task)
  601. (not (or effort
  602. (cdr (assoc "duration" task))
  603. (cdr (assoc "end" task))
  604. (cdr (assoc "period" task)))))))
  605. (attributes
  606. '(account start note duration endbuffer endcredit end
  607. flags journalentry length maxend maxstart minend
  608. minstart period reference responsible scheduling
  609. startbuffer startcredit statusnote)))
  610. (insert
  611. (concat
  612. "task " unique-id " \"" headline "\" {\n"
  613. (if (and parent-ordered previous-sibling)
  614. (format " depends %s\n" previous-sibling)
  615. (and depends (format " depends %s\n" depends)))
  616. (and allocate (format " purge %s\n allocate %s\n"
  617. (or (and (org-taskjuggler-targeting-tj3-p) "allocate")
  618. "allocations")
  619. allocate))
  620. (and complete (format " complete %s\n" complete))
  621. (and effort (format " effort %s\n" effort))
  622. (and priority (format " priority %s\n" priority))
  623. (and milestone (format " milestone\n"))
  624. (org-taskjuggler-get-attributes task attributes)
  625. "\n"))))
  626. (defun org-taskjuggler-close-maybe (level)
  627. (while (> org-export-taskjuggler-old-level level)
  628. (insert "}\n")
  629. (setq org-export-taskjuggler-old-level (1- org-export-taskjuggler-old-level)))
  630. (when (= org-export-taskjuggler-old-level level)
  631. (insert "}\n")))
  632. (defun org-taskjuggler-insert-reports ()
  633. (let (report)
  634. (dolist (report org-export-taskjuggler-default-reports)
  635. (insert report "\n"))))
  636. (provide 'org-taskjuggler)
  637. ;;; org-taskjuggler.el ends here