org-taskjuggler.el 23 KB

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