Tree Classes Implementation
You will often want to create a new tree. The following procedure discusses this action in more detail.
The TreeMover Application Engine program uses the Tree API (and File Layouts) for importing a tree from a flat file or exporting a tree to a flat file.
To create a new tree:
In this example, you are creating a new tree based on an existing tree structure. The following is the complete code sample: the steps explain each line.
Local ApiObject &Session;
Local ApiObject &TreeList, &MyTree;
Local ApiObject &LvlColl;
&Session = %Session;
&MyTree = &Session.GetTree();
/* create new tree */
If All(&MyTree) Then
&TreeReturn = &MyTree.Create("", "", "PERSONAL_DATA2", "1999-06-01", "PERSONAL_DATA");
If &TreeReturn <> 0 then
/* check PSMessages collection */
End-if;
&MyTree.description = "test tree";
/* add level */
&LvlColl = &MyTree.levels;
&Level = &LvlColl.add("FIRST LVL");
&Level.description = "First Level";
/* add root node */
&RootNode= &MyTree.insertroot("00001");
If ALL(&RootNode) Then
/* insert a leaf */
&NewLeaf = &RootNode.InsertChildLeaf("8000", "8999");
/* save new tree */
&RSLT = &MyTree.Save();
/* Do error checking */
If &RSLT <> 0 Then
/* errors occurred = do error checking */
&ERRORCOL = &Session.PSMessages;
For &I = 1 To &ERRORCOL.count
/* do error processing */
End-For;
Else
/* no errors - saved correctly - do other processing */
End-If;
End-if;
End-if;
Get a session object.
Before you can get a tree, you have to get a session object. The session controls access to the tree, provides error tracing, enables you to set the runtime environment, and so on. Use the %Session system variable to return a reference to the current PeopleSoft session.
&Session = %Session;
Get a tree object.
Use the GetTree method specifying a null value
(" ")
to return a closed tree object.&MyTree = &Session.GetTree();
Create the Tree.
The Create method creates a new tree with the name PERSONAL_DATA2. To ensure that you have a valid tree, use the All built-in function. Description is a required property (if you don’t specify something for Description you cannot save the tree.)
&TreeReturn = &MyTree.Create("", "", "PERSONAL_DATA2", "1999-06-01", "PERSONAL_DATA"); &MyTree.description = "test tree";
Add a level.
To add a level, you have to instantiate a level collection. Although there aren’t any levels in the tree, you can still access this collection. Use the Add method with the level collection to add a new level. Remember, the level name must be 8 characters or less. Description is a required property (if you don’t specify something for Description you cannot save the tree.)
&LvlColl = &MyTree.levels; &Level = &LvlColl.add("FIRST LVL"); &Level.description = "First Level";
Add the root node.
Because this is a new tree, you must first add the root node.
&RootNode = &MyTree.insertroot("00001");
Add a leaf.
To add a new leaf, you must have a reference to the parent node object. Using the All built-in function ensures that there is a root node before you try to insert the leaf with the InsertChildLeaf method.
If ALL(&RootNode) Then &NewLeaf = &RootNode.InsertChildLeaf("8000", "8999");
Save the tree.
When you execute the Save method, the new tree is saved to the database.
&RSLT = &MyTree.Save();
Note: If you’re running the tree API from an Application Engine program, the data won’t actually be committed to the database until the Application Engine program performs a COMMIT.
Check for errors.
You can check if there were any errors using the PSMessages property on the session object.
If All (&RSLT) Then /* errors occurred = do error checking */ &ERRORCOL = &Session.PSMessages; For &I = 1 To &ERRORCOL.count /* do error processing */ End-For; Else /* no errors - saved correctly - do other processing */ End-If;
If there are multiple errors, all errors are logged to the PSMessages collection, not just the first occurrence of an error.
Note: If you’ve called the Tree API from an Application Engine program, all errors are also logged in the application engine error log tables.