Class: TaskJuggler::Journal

Inherits:
Object show all
Includes:
MessageHandler
Defined in:
lib/taskjuggler/Journal.rb

Overview

A Journal is a list of JournalEntry objects. It provides methods to add JournalEntry objects and retrieve specific entries or other processed information.

Instance Method Summary collapse

Methods included from MessageHandler

#critical, #debug, #error, #fatal, #info, #warning

Constructor Details

#initializeJournal

Create a new Journal object.



350
351
352
353
354
355
# File 'lib/taskjuggler/Journal.rb', line 350

def initialize
  # This list holds all entries.
  @entries = JournalEntryList.new
  # This hash holds a list of entries for each property.
  @propertyToEntries = {}
end

Instance Method Details

#addEntry(entry) ⇒ Object

Add a new JournalEntry to the Journal.



358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/taskjuggler/Journal.rb', line 358

def addEntry(entry)
  return if @entries.include?(entry)
  @entries << entry

  return if entry.property.nil?

  # When we store the property into the @propertyToEntries hash, we need
  # to make sure that we store the PropertyTreeNode object and not a
  # PTNProxy object.
  unless @propertyToEntries.include?(entry.property.ptn)
    @propertyToEntries[entry.property.ptn] = JournalEntryList.new
  end
  @propertyToEntries[entry.property.ptn] << entry
end

#alertEntries(date, property, minLevel, minDate, query) ⇒ Object

Return the list of JournalEntry objects that are dated at or before date, are for property or any of its childs, have at least level alert and are after minDate. We only return those entries with the highest overall alert level.



597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/taskjuggler/Journal.rb', line 597

def alertEntries(date, property, minLevel, minDate, query)
  maxLevel = 0
  entries = []
  # Gather all the current (as of the specified _date_) JournalEntry
  # objects for the property and than find the highest level.
  currentEntriesR(date, property, minLevel, minDate, query).each do |e|
    if maxLevel < e.alertLevel
      maxLevel = e.alertLevel
      entries = [ e ]
    elsif maxLevel == e.alertLevel
      entries << e
    end
  end
  entries
end

#alertLevel(date, property, query) ⇒ Object

Determine the alert level for the given property at the given date. If the property does not have any JournalEntry objects or they are out of date compared to the child properties, the level is computed based on the highest level of the children. Only take the entries that are not filtered by query.hideJournalEntry into account.



583
584
585
586
587
588
589
590
591
# File 'lib/taskjuggler/Journal.rb', line 583

def alertLevel(date, property, query)
  maxLevel = 0
  # Gather all the current (as of the specified _date_) JournalEntry
  # objects for the property and than find the highest level.
  currentEntriesR(date, property, 0, nil, query).each do |e|
    maxLevel = e.alertLevel if maxLevel < e.alertLevel
  end
  maxLevel
end

#currentEntries(date, property, minLevel, minDate, logExp) ⇒ Object

This function returns a list of entries that have all the exact same date and are the last entries before the deadline date. Only messages with at least the required alert level minLevel are returned. Messages with alert level minLevel or higher must be newer than minDate.



617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'lib/taskjuggler/Journal.rb', line 617

def currentEntries(date, property, minLevel, minDate, logExp)
  pEntries = getEntries(property) ?  getEntries(property).last(date) :
             JournalEntryList.new
  # Remove entries below the minium alert level or before the timeout
  # date.
  pEntries.delete_if do |e|
    e.headline.empty? || e.alertLevel < minLevel ||
    (e.alertLevel >= minLevel && minDate && e.date < minDate)
  end

  unless pEntries.empty?
    # Check parents for a more important or more up-to-date message.
    p = property.parent
    while p do
      ppEntries = getEntries(p) ?
                  getEntries(p).last(date) : JournalEntryList.new

      # A parent has a more up-to-date message.
      if !ppEntries.empty? && ppEntries.first.date >= pEntries.first.date
        return JournalEntryList.new
      end

      p = p.parent
    end
  end

  # Remove all entries that are filtered by logExp.
  if logExp
    pEntries.delete_if { |e| hidden(e, logExp) }
  end

  pEntries
end

#currentEntriesR(date, property, minLevel, minDate, query) ⇒ Object

This function recursively traverses a tree of PropertyTreeNode objects from bottom to top. It returns the last entries before date for each property unless there is a property in the sub-tree specified by the root property with more up-to-date entries. The result is a JournalEntryList.



656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# File 'lib/taskjuggler/Journal.rb', line 656

def currentEntriesR(date, property, minLevel, minDate, query)
  DataCache.instance.cached(self, :currentEntriesR, date, property,
                            minLevel, minDate, query) do
    # See if this property has any current JournalEntry objects.
    pEntries = getEntries(property) ? getEntries(property).last(date) :
               JournalEntryList.new
    # Remove entries below the minium alert level or before the timeout
    # date.
    pEntries.delete_if do |e|
      e.headline.empty? || e.alertLevel < minLevel ||
      (e.alertLevel == minLevel && minDate && e.date < minDate)
    end

    # Determine the highest alert level of the pEntries.
    maxPAlertLevel = 0
    pEntries.each do |e|
      maxPAlertLevel = e.alertLevel if e.alertLevel > maxPAlertLevel
    end

    cEntries = JournalEntryList.new
    latestDate = nil
    maxAlertLevel = 0
    # If we have an entry from this property, we only care about child
    # entries that are from a later date.
    minDate = pEntries.first.date + 1 unless pEntries.empty?

    # Now gather all current entries of the child properties and find the
    # date that is closest to and right before the given _date_.
    property.kids.each do |p|
      currentEntriesR(date, p, minLevel, minDate, query).each do |e|
        # Find the date of the most recent entry.
        latestDate = e.date if latestDate.nil? || e.date > latestDate
        # Find the highest alert level.
        maxAlertLevel = e.alertLevel if e.alertLevel > maxAlertLevel
        cEntries << e unless cEntries.include?(e)
      end
    end

    # Only Task properties have dependencies.
    if (query.journalMode == :status_dep ||
        query.journalMode == :alerts_dep) && property.is_a?(Task)
      # Now gather all current entries of the dependency properties and find
      # the date that is closest to and right before the given _date_.
      property['startpreds', query.scenarioIdx].each do |p, onEnd|
        # We only follow end->start dependencies.
        next unless onEnd

        currentEntriesR(date, p, minLevel, minDate, query).each do |e|
          # Find the date of the most recent entry.
          latestDate = e.date if latestDate.nil? || e.date > latestDate
          # Find the highest alert level.
          maxAlertLevel = e.alertLevel if e.alertLevel > maxAlertLevel
          cEntries << e unless cEntries.include?(e)
        end
      end
    end

    if !pEntries.empty? && (maxPAlertLevel > maxAlertLevel ||
                            latestDate.nil? ||
                            pEntries.first.date >= latestDate)
      # If no child property has a more current JournalEntry or one with a
      # higher alert level than this property and this property has
      # JournalEntry objects, than those are taken.
      entries = pEntries
    else
      # Otherwise we take the entries from the kids.
      entries = cEntries
    end

    # Remove all entries that are filtered by query.hideJournalEntry.
    if query.hideJournalEntry
      entries.delete_if { |e| hidden(e, query.hideJournalEntry) }
    end

    # Otherwise return the list provided by the childen.
    entries
  end
end

#delete_ifObject

Delete all entries of the Journal for which the block yields true.



378
379
380
381
382
383
384
# File 'lib/taskjuggler/Journal.rb', line 378

def delete_if
  @entries.delete_if do |e|
    res = yield(e)
    @propertyToEntries[e.property.ptn].delete(e) if res
    res
  end
end

#entries(startDate = nil, endDate = nil, logExp = nil, property = nil, alertLevel = nil) ⇒ Object



562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/taskjuggler/Journal.rb', line 562

def entries(startDate = nil, endDate = nil, logExp = nil, property = nil,
            alertLevel = nil)
  list = JournalEntryList.new
  @entries.each do |entry|
    if (startDate.nil? || startDate <= entry.date) &&
       (endDate.nil? || endDate >= entry.date) &&
       (property.nil? || property.ptn == entry.property ||
                         entry.property.isChildOf?(property.ptn)) &&
       (alertLevel.nil? || alertLevel == entry.alertLevel) &&
       !hidden(entry, logExp)
      list << entry
    end
  end
  list
end

#entriesByResource(resource, startDate = nil, endDate = nil, logExp = nil, task = nil, alertLevel = nil) ⇒ Object

Return a list of all JournalEntry objects for the given resource that are dated between startDate and endDate, are not hidden by their flags matching logExp, are for Task task and have at least the alert level _alertLevel. If an optional parameter is nil, it always matches the entry.



507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/taskjuggler/Journal.rb', line 507

def entriesByResource(resource, startDate = nil, endDate = nil,
                      logExp = nil, task = nil, alertLevel = nil)
  list = JournalEntryList.new
  @entries.each do |entry|
    if entry.author == resource.ptn &&
       (startDate.nil? || entry.date > startDate) &&
       (endDate.nil? || entry.date <= endDate) &&
       (task.nil? || entry.property == task.ptn) &&
       (alertLevel.nil? || entry.alertLevel >= alertLevel) &&
       !entry.headline.empty? && !hidden(entry, logExp)
      list << entry
    end
  end
  list
end

#entriesByTask(task, startDate = nil, endDate = nil, logExp = nil, resource = nil, alertLevel = nil) ⇒ Object

Return a list of all JournalEntry objects for the given task that are dated between startDate and endDate (end date not included), are not hidden by their flags matching logExp are from Author resource and have at least the alert level _alertLevel. If an optional parameter is nil, it always matches the entry.



528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/taskjuggler/Journal.rb', line 528

def entriesByTask(task, startDate = nil, endDate = nil, logExp = nil,
                  resource = nil, alertLevel = nil)
  list = JournalEntryList.new
  @entries.each do |entry|
    if entry.property == task.ptn &&
       (startDate.nil? || entry.date >= startDate) &&
       (endDate.nil? || entry.date < endDate) &&
       (resource.nil? || entry.author == resource) &&
       (alertLevel.nil? || entry.alertLevel >= alertLevel) &&
       !entry.headline.empty? && !hidden(entry, logExp)
      list << entry
    end
  end
  list
end

#entriesByTaskR(task, startDate = nil, endDate = nil, logExp = nil, resource = nil, alertLevel = nil) ⇒ Object

Return a list of all JournalEntry objects for the given task or any of its sub tasks that are dated between startDate and endDate, are not hidden by their flags matching logExp, are from Author resource and have at least the alert level _alertLevel. If an optional parameter is nil, it always matches the entry.



549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/taskjuggler/Journal.rb', line 549

def entriesByTaskR(task, startDate = nil, endDate = nil, logExp = nil,
                   resource = nil, alertLevel = nil)
  list = entriesByTask(task, startDate, endDate, logExp, resource,
                       alertLevel)

  task.kids.each do |t|
    list += entriesByTaskR(t, startDate, endDate, logExp, resource,
                           alertLevel)
  end

  list
end

#getEntries(property) ⇒ Object



373
374
375
# File 'lib/taskjuggler/Journal.rb', line 373

def getEntries(property)
  @propertyToEntries[property.ptn]
end

#to_rti(query) ⇒ Object



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/taskjuggler/Journal.rb', line 388

def to_rti(query)
  entries = JournalEntryList.new

  case query.journalMode
  when :journal
    # This is the regular journal. It contains all journal entries that
    # are dated in the query interval. If a property is given, only
    # entries of this property are included.
    if query.property
      if query.property.is_a?(Task)
        entries = entriesByTask(query.property, query.start, query.end,
                                query.hideJournalEntry)
      elsif query.property.is_a?(Resource)
        entries = entriesByResource(query.property, query.start, query.end,
                                    query.hideJournalEntry)
      end
    else
      entries = self.entries(query.start, query.end, query.hideJournalEntry)
    end
  when :journal_sub
    # This mode also contains all journal entries that are dated in the
    # query interval. A property must be given and only entries of this
    # property and all its children are included.
    if query.property.is_a?(Task)
      entries = entriesByTaskR(query.property, query.start, query.end,
                               query.hideJournalEntry)
    end
  when :status_up
    # In this mode only the last entries before the query end date for
    # each task are included. An entry is not included if any of the
    # parent tasks has a more recent entry that is still before the query
    # end date.
    if query.property
      if query.property.is_a?(Task)
        entries += currentEntries(query.end, query.property, 0, query.start,
                                  query.hideJournalEntry)
      end
    else
      query.project.tasks.each do |task|
        # We only care about top-level tasks.
        next if task.parent

        entries += currentEntries(query.end, task, 0, query.start,
                                  query.hideJournalEntry)
        # Eliminate duplicates due to entries from adopted tasks
        entries.uniq!
      end
    end
  when :status_down, :status_dep
    # In this mode only the last entries before the query end date for
    # each task (incl. sub tasks) are included.
    if query.property
      if query.property.is_a?(Task)
        entries += currentEntriesR(query.end, query.property, 0,
                                   query.start, query)
      end
    else
      query.project.tasks.each do |task|
        # We only care about top-level tasks.
        next if task.parent

        entries += currentEntriesR(query.end, task, 0, query.start, query)
        # Eliminate duplicates due to entries from adopted tasks
        entries.uniq!
      end
    end
  when :alerts_down, :alerts_dep
    # In this mode only the last entries before the query end date for
    # each task (incl. sub tasks) and only the ones with the highest alert
    # level are included.
    if query.property
      if query.property.is_a?(Task)
        entries += alertEntries(query.end, query.property, 1, query.start,
                                query)
      end
    else
      query.project.tasks.each do |task|
        # We only care about top-level tasks.
        next if task.parent

        entries += alertEntries(query.end, task, 1, query.start, query)
        # Eliminate duplicates due to entries from adopted tasks
        entries.uniq!
      end
    end
  else
    raise "Unknown jourmal mode: #{query.journalMode}"
  end
  # Sort entries according to the user specified sorting criteria.
  entries.setSorting(query.sortJournalEntries)
  entries.sort!

  # The components of the message are either UTF-8 text or RichText. For
  # the RichText components, we use the originally provided markup since
  # we compose the result as RichText markup first.
  rText = ''
  entries.each do |entry|
    rText += entry.to_rText(query)
  end

  # Now convert the RichText markup String into RichTextIntermediate
  # format.
  unless (rti = RichText.new(rText, RTFHandlers.create(query.project)).
                             generateIntermediateFormat)
    warning('ptn_journal', "Syntax error in journal: #{rText}")
    return nil
  end
  # No section numbers, please!
  rti.sectionNumbers = false
  # We use a special class to allow CSS formating.
  rti.cssClass = 'tj_journal'
  query.rti = rti
end