Sunday, February 27, 2011

Find a specified requirement in a specified folder: OTA

Work with QC Requirements using OTA API

Public Function GetReqByPath(fullPath$, _
Optional delimChar As String = "\") _
As Req
' This function returns a Req object specified by its
' full path.
' For example:
' Set r = GetReqByPath("SCRATCH\OTA_REQ_DEMO\OTA_S_O_1")
' will return the OTA_S_O_1 object.
' A requirement name is not unique in the project, but it is
' unique as a direct child of another requirement.
' Therefore, these routine works by walking down the
' requirement tree along the fullPath until the requirement
' is found at the end of the path.
' If a backslash is not used as the folder delimiter, any other
' character can be passed in the delimChar argurment.

Dim rFact As reqFactory
Dim theReq As Req, ParentReq As Req
Dim reqList As list
Dim NodeArray() As String, PathArray() As String
Dim WorkingDepth As Integer
On Error GoTo GetReqByPathErr

'Trim the fullPath and strip leading and trailing delimiters

fullPath = Trim(fullPath)
Dim pos%, ln%
pos = InStr(1, fullPath, delimChar)
If pos = 1 Then
fullPath = Mid(fullPath, 2)
End If
ln = Len(fullPath)
pos = InStr(ln - 1, fullPath, delimChar)
If pos > 0 Then
fullPath = Mid(fullPath, 1, ln - 1)
End If

' Get an array of requirements, and the length
' of the path
NodeArray = Split(fullPath, delimChar)
WorkingDepth = LBound(NodeArray)

' Walk down the tree
'tdc is the global TDConnection object.
Set rFact = tdc.reqFactory

For WorkingDepth = LBound(NodeArray) To UBound(NodeArray)
'First time, find under the root (-1)
'After that, under the previous requirement found: ParentReq.ID

If WorkingDepth = LBound(NodeArray) Then
Set reqList = rFact.Find(-1, "RQ_REQ_NAME", _
NodeArray(WorkingDepth), TDREQMODE_FIND_EXACT)
Else
Set reqList = rFact.Find(ParentReq.ID, "RQ_REQ_NAME", _
NodeArray(WorkingDepth), TDREQMODE_FIND_EXACT)
End If
' Delete parent. Each loop has to find it again.
Set ParentReq = Nothing
Dim strItem, reqID&, strID$, thePath$

For Each strItem In reqList
' The List returned from ReqFactory.Find is a List
' of strings of format ID,Name.
' For example "9,Products/Services On Sale"
' Extract the ID from the string by splitting the
' string at the comma.
pos = InStr(strItem, ",")
strID = Mid(strItem, 1, pos - 1)

' Convert the ID to a long, and get the object
reqID = CLng(strID)
Set theReq = rFact.Item(reqID)

'Now check that the object is at the correct depth.
'If so, we've found the requirement. On the next loop,
'we'll look from here down.
thePath = theReq.Path
PathArray = Split(thePath, "\")

' Debug.Print "Number of elements is " & UBound(PathArray)
' Debug.Print theReq.ID, theReq.Name

If UBound(PathArray) = WorkingDepth Then
Set ParentReq = theReq
Exit For
End If
Next strItem
If ParentReq Is Nothing Then Exit For
Next WorkingDepth
Set GetReqByPath = ParentReq
Exit Function

GetReqByPathErr:
ErrHandler err, "GetReqByPath"
Set GetReqByPath = Nothing
End Function

No comments:

Post a Comment