|
|
Example CodeThis section shows you some documented examples. Hopefully you can
use them as well for your daily work. All the following procedures can be found in the file To use the downloaded file, extract it in the directory where the TrapEd-executable lies. Afterwards you can simply call the given procedures. You can also provide your examples, if you want to share your code and think it could be usable and useful for others, I would be glad if you email me your code and it will be placed on this page. ExamplesTo get you in touch with some basics here are some simple examples: These are only meant to give you a feeling of how easy it is
to develop your own scripts to work with attr-files.
Print the template-structureThe following function will print the template-structure to the console or stdout:
proc printTemplateStructure {handle projectId {level 0}} {
set projectName [$handle getAttributeValue $projectId Name]
puts "[string repeat " " $level] $projectId Project: $projectName"
foreach id [$handle getIdList Project $projectId] {
# call recursively
printTemplateStructure $handle $id [expr $level +4]
}
foreach id [$handle getIdList Template $projectId] {
set templName [$handle getAttributeValue $id Name]
puts "[string repeat " " [expr $level +4]] $id Template: $templName"
}
}
Open the API-console by opening a DOS-box in the And type the following commands into the console: source example.tcl set th [readAttrFile "d:/example.attr"] printTemplateStructure $th [$th getRootId] With The output will be printed to the file Print statisticsTo gather some information about an attr-file you can take the following procedure as a start. The procedure is included in the
proc printStatistics {handle} {
# count the number of projects/records/templates/files
set rootId [$handle getRootId]
foreach type {Template Project File Record} {
set aId($type) [$handle getIdListRecursive $type $rootId]
puts "Number of ${type}s: [llength $aId($type)]"
}
# find the latest modified template
set lastTime 0
set latestModTemplateId ""
foreach id $aId(Template) {
if {[$handle getAttributeValue $id LastModTime] > $lastTime} {
set lastTime [$handle getAttributeValue $id LastModTime]
set latestModTemplateId $id
}
}
set name [$handle getAttributeValue $latestModTemplateId Name]
# here you can see how the internal time-format (seconds since 1970)
# is formated using the command Create a file: source example.tcl set handle [readAttrFile "example.attr"] printStatistics $handle and place it in the TrapEd-examples-directory. Open a DOS-box, change to the example-directory and call: And the statistics will be stored in the file Synchronize with CVSThe most often problem of Vignette I've heard of is the missing
integration with CVS (Concurrent Versions System,
see CVS-Home).
proc syncCVS {handle dumpDirectory msg} {
set rootId [$handle getRootId]
# work in the given directory
set backupPwd [pwd]
cd $dumpDirectory
foreach id [$handle getIdListRecursive Template $rootId] {
# create the file
set name [$handle getAttributeValue $id Name]
set ext [$handle getAttributeValue $id ext]
set fd [open $name.$ext w+]
puts -nonewline $fd [$handle getAttributeValue $id body]
close $fd
# add or commit to CVS
if {[catch {exec cvs commit -m $msg $name.$ext} result]} {
# error while commiting? maybe we must add this file first
if {[catch {
exec cvs add $name.$ext
exec cvs commit -m $msg $name.$ext
} result]} {
puts "Adding or Commit of $name.$ext failed: $result"
continue
}
}
if {[catch {exec cvs update $name.$ext} result]} {
puts "Updating file failed: $result"
continue
}
# update body in attr-file (to reflect correct $Revision$-Tags ...
set fd [open $name.$ext r+]
$handle setAttributeValue $id body [read $fd]
close $fd
}
cd $backupPwd
}
Having a file called source example.tcl set th [readAttrFile "d:/example.attr"] # Attention: Be sure that the directory "d:/templates" exist. syncCVS $th d:/templates "My CVS commit-message for this release." # overwrite the attr-file to get the updated $Revision$-tags # into Vignette again $th writeToFile "d:/example.attr" And call it like this (in a DOS-box opened in path: " This will write all the template-code into files in the directory
Because TrapEd can be called batch-like you can simply write an
at-job on windows and a cronjob on your server (if you have Unix) to
do a nightly Find a Template, Record, ...To find a template by it's name or a search-pattern you can use the following procedure.
proc findItemByName {handle search_pattern {projectId ""} {type "Template"}} {
set lResult {}
if {$projectId == ""} {
set projectId [$handle getRootId]
}
foreach id [$handle getIdListRecursive $type $projectId] {
set name [$handle getAttributeValue $id Name]
if {[string match -nocase $search_pattern $name]} {
# the name matched the search-pattern:
# append id to result
lappend lResult $id
}
}
return $lResult
}
Call it like this: findItemByName $trapedHandle "*misc*" # will return: example_misc_component example_misc_library Copyright © 2001-2010, Vogel. All rights reserved. Email: Last update: April 25, 2006, at 12:12 PM |