3D PLM Enterprise Architecture

Data Access - Database

Creating a Document from a Memory Area in the Vault Server

Copying a memory area to a newly created vault document
Use Case

Abstract

This article shows how to create a document in the vault server, copy a memory area into it, and save it.


What You Will Learn With This Use Case

This use case is intended to show you how to create a document in the vault server, copy a memory area into the vault document, and save it. It also shows how to open a vault session and a user session, how to get an URL from the vault document, and shows the two-step save operation.

[Top]

The CAAEvcCreateDocFromMem Use Case

CAAEvcCreateDocFromMem is a use case of the CAAENOVaultClientCPP.edu framework that illustrates ENOVaultClientCPP framework capabilities.

[Top]

What Does CAAEvcCreateDocFromMem Do

CAAEvcCreateDocFromMem begins by opening a vault session and a user session. Then it creates a vault document, copies the contents of a memory area into it, retrieves the vault document URL, and finally save it using a two-step prepare/commit transaction.

On the standard output, the URL of the document it just created is displayed. You can use that URL as an input for the CAAEvcExtractDocIntoMem use case [1] or the CAAEvcExtractDocIntoFile use case [2].

[Top]

How to Launch CAAEvcCreateDocFromMem 

To launch CAAEvcCreateDocFromMem, you will need to set up the build time environment, then compile CAAEvcCreateDocFromMem along with its prerequisites, set up the run time environment, and then execute the use case as follows: [3].

mkrun -c "CAAEvcCreateDocFromMem -v VaultAliasName"

where:

[Top]

Where to Find the CAAEvcCreateDocFromMem Code

The CAAEvcCreateDocFromMem use case is made of the single file CAAEvcCreateDocFromMem.cpp located in the CAAEvcCreateDocFromMem.m module of the CAAENOVaultClientCPP.edu framework:

Windows InstallRootDirectory\CAAENOVaultClientCPP.edu\CAAEvcCreateDocFromMem.m\
Unix InstallRootDirectory/CAAENOVaultClientCPP.edu/CAAEvcCreateDocFromMem.m/

where InstallRootDirectory is the directory where the CAA CD-ROM is installed.

[Top]

Step-by-Step

There are eleven logical steps in CAAEvcCreateDocFromMem:

  1. Creating the Vault Session
  2. Creating a User Session
  3. Creating a Document
  4. Retrieving the Document URL
  5. Retrieving the Default Block Size
  6. Opening the Document in Write Mode
  7. Copying a Memory Area into the Document
  8. Closing the Document
  9. Preparing the Transaction
  10. Committing the Transaction
  11. Ending the Session

[Top]

Creating the Vault Session

...
   ENOVIVaultError     VErr;
   CATBoolean          Master  = TRUE;

   // As the vault session is opened as master, the following arguments are 
   // not taken into account
   CATUnicodeString    Marker("");
   CATUnicodeString    Host("");
   int                 Port    = 0;

   ENOVIVaultSession * pSession = NULL;

   RC = ENOVIVaultSessionFactory::getVaultSession(Marker, Host, Port, Master, &pSession, VErr);
...

The first thing to do is to create a vault session, thanks to the static getVaultSession method of the ENOVIVaultSessionFactory class. The vault component supports distributed transactions (i.e. several processes working in the context of the same transaction) but in this case only one of the sessions is a master one and is able to commit or rollback the transaction. As this use case is a stand alone program, the session must necessarily be opened as master. The useful arguments are:

[Top]

Creating a User Session

...
   ENOVIVaultUserSession * pUserSession = NULL;

   RC = pSession->getUserSession(s_InputVault, &pUserSession, VErr);
...

Once the vault session has be created, a user session must be initialized. A user session corresponds to the connection of a particular client to a given vault server. A user session is initialized using the getUserSession method of the ENOVIVaultSession interface whose arguments are:

If several vault servers were needed to be accessed, a user session per vault server should be created.

[Top]

Creating a Document

...
   ENOVIVaultDocument      * pDoc = NULL;

   // The following parameters are hard coded in this use case 
   // They could have been taken as input
   CATUnicodeString DocName      = "MyDoc";
   CATUnicodeString Description  = "sample document";
   CATUnicodeString MimeType     = "txt";
   CATUnicodeString OriginalName = "";
   CATUnicodeString OriginalPath = "";
   CATUnicodeString OriginalHost = "";
  
   RC = pUserSession->createDocument(DocName,
                                     Description,
                                     MimeType,
                                     OriginalName,
                                     OriginalPath,
                                     OriginalHost,
                                     &pDoc,
                                     VErr);
...

Now a document can be created from the user session. Note that this is just an empty shell to which contents will be associated later on. The parameters to pass to createDocument to create a vault document are:

[Top]

Retrieving the Document URL

...
   SEQUENCE_octet    Url;
   RC = pDoc->getURL(Url);
...

The document's URL is an external link to the document. It contains all the necessary information to retrieve the document without having to know in which vault it is stored, on which machine, and so on.

[Top]

Retrieving the Default Block Size

...
   const CATUnicodeString Pty("VaultClient_BlockSize");
   CATUnicodeString sSize(""); 

   RC = pSession->getProperty(Pty, sSize); 
...
   int BSize = 0; 
   sSize.ConvertToNum(&BSize);

...

The default block size is stored as a property. It can then be retrieved using the generic getProperty method on the ENOVIVaultSession interface

[Top]

Opening the Document in Write Mode

...
   unsigned long DocSize      = 0      ; // The size of the document is unknown
   CATBoolean    WriteFullDoc = CATTrue; // The whole document will be written
      
   RC = pDoc->openWrite(DocSize, WriteFullDoc, VErr);
...

Opening a document in write mode allows you to modify, or create in this case, its contents. This also sets a write lock on the document ensuring that nobody else is modifying it at the same time. That doesn't prevent other people from reading the document but they will only see the last committed image until the current modifications are committed. The parameters to pass to openWrite are:

[Top]

Copying a Memory Area into the Document

...
   int iCLength = BSize*3;
   
   unsigned char * cContent = new unsigned char[iCLength];
   memset(cContent, 'A', iCLength);

   SEQUENCE_octet Buffer(iCLength, iCLength, cContent, TRUE);

   int iOffset = 0; // Offset from the beginning of the buffer

   for (int i = 1; i <= 3; i++)
   {
     RC = pDoc->writeBlock(Buffer, iOffset, BSize, VErr); 
...
     iOffset += BSize; 
   }
...

A memory area is now copied to the vault server document. In this use case, we have chosen to use the writeBlock method. We could have used the write method as well. In fact, in our example, the write method would have been more appropriate since the whole data we want to write into the document is contained into a single memory area. The writeBlock method is useful when data is split among several memory areas.

The parameter to pass to writeBlock are:

[Top]

Closing the Document

...
   RC = pDoc->close(VErr);
...

The document must always be closed. Pay a special attention to the error management. If any kind of error occurs after a document has been opened, make sure it is closed before exiting the method in which you handle the document.

[Top]

Preparing the Transaction

...
   RC = pSession->prepare(VErr);
...

The vault is designed to cooperate with other data servers. So it supports a two-phase commit. That means that the save operation has been split into two steps: prepare and commit. This applies to all the vault documents currently managed by all the user sessions of the vault session. When preparing and committing the transaction, a check is performed to determine whether documents are opened. If an opened document exists, it is considered as "in work" and to ensure that no document is saved in an inconsistent state, the whole save operation is aborted. 

When preparing the transaction, all the necessary checks and most of the save operations are performed, the objective being to make sure that nothing will prevent from committing.

[Top]

Committing the Transaction

...
   RC = pSession->commit(VErr);
...

Committing the transaction is a very light operation validating what has been done when preparing the transaction. Once committed, all the modifications done since the previous commit are saved and become visible to the other users.

[Top]

Ending the Session

...
   RC = ENOVIVaultSessionFactory::endVaultSession(pSession, VErr);
...

This ends and closes all the opened user sessions and closes the vault session.

[Top]


In Short

This use case has demonstrated how to initialize the vault environment, create a document and copy a memory area into it, and finally save it.

[Top]


References

[1] Extracting a Document from the Vault Server to a Memory Area
[2] Extracting a Document from the Vault Server to a File
[3] Building and Launching a CAA V5 Use Case
[Top]

History

Version: 1 [Oct 2001] Document created
[Top]

Copyright © 2001, Dassault Systèmes. All rights reserved.