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