Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

How to add a picture to a requirementDescription using the COM API

How to add a picture to a requirementDescription using the COM API

Problem:

How to add a picture to a requirementDescription using the COM API

Resolution:

How to add a picture to a requirementDescription using the COM API Applicable for:
Product: CaliberRM
Version: Any
OS: Any
Problem Description: How to add a picture to a requirementDescription using the COM API
Problem Resolution: The following Visual Basic Code shows how this can be achieved:

VERSION 5.00

Begin

VB.Form Form1

Caption = "Form1"
ClientHeight = 3510
ClientLeft = 60
ClientTop = 450
ClientWidth = 5250
LinkTopic = "Form1"
ScaleHeight = 3510
ScaleWidth = 5250
StartUpPosition = 3 "Windows Default

End

Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

" The CaliberRM session that will be used in this example.

Dim objSession As Session


Private Sub Form_Load()
Dim Host As String
Dim UserName As String
Dim Password As String
Dim RequirementID As Integer
Dim SampleImagePath As String

" You will need to initialize these variables to something relevant.

Host = "Antonov2k"
UserName = "admin"
Password = "admin"
RequirementID = 1
SampleImagePath = "c:\temp\image.JPG"

" Create a CaliberRM session...

CreateSession(Host, UserName, Password)

" Create a Description with images...

CreateDescriptionWithImages(RequirementID, SampleImagePath)

" Get the requirement"s HTML...

Dim HTML As String

HTML = GetRequirementsHTML(RequirementID)

MsgBox("Here is the HTML: " _
& vbCrLf _
& vbCrLf _
& HTML)

" Get the path of the image...

Dim ImagePath As String

ImagePath = GetImagePath(RequirementID)

MsgBox("Here is the image path: ")

& vbCrLf _
& vbCrLf _
& ImagePath

" At this point the file WILL exist in the image cache...

DisplayImageCacheStatus(ImagePath)

" Clear the cache...

RemoveImageFromCache(ImagePath)


" At this point the file WILL NOT exist in the image cache...

DisplayImageCacheStatus(ImagePath)


" Tell the SDK to download all images referenced in the HTML...

PopulateCacheWithImages(HTML)

" At this point the file WILL exist in the image cache...

DisplayImageCacheStatus(ImagePath)

End Sub

" Displays whether or not the specified file exists.

Private Function DisplayImageCacheStatus(ByVal ImagePath As String)


" Remove the "file:///" from the start of the path...

ImagePath = Strings.Replace(ImagePath, "file:///", "")

" Has the image been copied to this path yet?

Dim FileSystem

FileSystem = CreateObject("Scripting.FileSystemObject")

ImageExists = FileSystem.fileexists(ImagePath)

If ImageExists Then

MsgBox("File exists in the image cache.")

Else

MsgBox("File DOES NOT exist in the image cache.")

End If

End Function

" Removes the specified image from the cache...

Private Function RemoveImageFromCache(ByVal ImagePath As String)

" Remove the "file:///" from the start of the path...

ImagePath = Strings.Replace(ImagePath, "file:///", "")

Dim FileSystem

FileSystem = CreateObject("Scripting.FileSystemObject")

Exists = FileSystem.deletefile(ImagePath)

End Function

" Modifies the description of the specified requirement.

Private Function CreateDescriptionWithImages( _

RequirementID As Integer, _

SampleImagePath As String)


" Get a requirement...

Dim objRequirement As Requirement

objRequirement = objSession.getRequirement(RequirementID)

" Construct a new description...

Dim objDescriptionFactory As New RequirementDescriptionFactory

Dim objDescription As RequirementDescription

" Set the description"s HTML. Note the use of the "new"

" attribute in the image tag.

Set objDescription = objDescriptionFactory.Create( _

"Here is an image " _

& "

& "")

" Modify and save the requirement...

objRequirement.unlock()

objRequirement.lock()

objRequirement.Description = objDescription

objRequirement.save("")

objRequirement.unlock()

End Function

" Uses the SDK"s HTMLHelper object to get the paths of Images

" in the specified requirement"s description. For simplicity,

" this function only returns the first path.

Private Function GetImagePath(ByVal RequirementID As Integer) As String


" Get a requirement...

Dim objRequirement As Requirement

objRequirement = objSession.getRequirement(RequirementID)

" Create an SDK HTMLHelper object...

Dim objHTMLHelperFactory As New HTMLHelperFactory

Dim objHTMLHelper As HTMLHelper

objHTMLHelper = objHTMLHelperFactory.Create()

" Use the HTMLHelper object to extract the image path(s)...

Dim objHTML As String

objHTML = GetRequirementsHTML(RequirementID)

Dim objPathCollection As CaliberRM.Collection

objPathCollection = objHTMLHelper.getImagePaths(objHTML)

" For simplicity we"ll only return the first path...

GetImagePath = objPathCollection.Item(0)

End Function

" Uses the SDK"s ImageManager object to populate the local image

" cache with images.

Private Function PopulateCacheWithImages(ByVal objHTML As String)

objSession.ImageManager.populateCache2(objHTML)

End Function

" Gets the HTML description for the specified Requirement.

Private Function GetRequirementsHTML(ByVal RequirementID As Integer) As String


" Get a requirement...

Dim objRequirement As Requirement

objRequirement = objSession.getRequirement(RequirementID)

GetRequirementsHTML = objRequirement.Description.Text

End Function

" Creates a CaliberRM session.

Private Function CreateSession(ByVal Host As String, ByVal UserID As String, ByVal Password As String)

Dim objServerFactory As CaliberServerFactory
objServerFactory = New CaliberServerFactory

Dim objServer As CaliberServer

objServer = objServerFactory.Create(Host)

objSession = objServer.login(UserID, Password)

End Function

Old KB# 16025

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Version history
Revision #:
1 of 1
Last update:
‎2013-02-15 18:29
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.