One of the issues in a persistent environment is to know if the code has already been added to the system. Typically Jetfire code only needs to added on initialization or when changes to the Jetfire code occurs.

The C# code below shows how to add Jetfire code and how to check if it has already been added to the system.

using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TrackerRealm.Jetfire.Base;
using TrackerRealm.Jetfire;
using TrackerRealm.Jetfire.TransportCommon;
using TrackerRealm.Jetfire.NexusLinqStorage;

namespace JetfireCoreExampleTests.Startup
{
    /// <summary>
    /// AddingCodeOnceTest
    /// </summary>
    [TestClass]
    public class AddingCodeOnceTest
    {
        /// <summary>
        /// One of the issues in a persistent environment is to know
        /// if the code has already been added to the system.
        /// Typically Jetfire code only needs to added on initialization
        /// or when changes to the Jetfire code occurs.
        /// 
        /// Below shows how to add code and how to check if it has
        /// already been added to the system.
        /// </summary>
        [TestMethod]
        public void AddCodeOnce()
        {
            string jetfireCode =
                @"
            namespace test
            {
               public workflow Data
               {
                  // Constructor
                  public Data(string name, string details)
                  {
                     // use the built in description properties to store extra information.
                     this.Subject = name;
                     this.ToolTip = details;
                     
                  }
                  DateTime creationTime = DateTime.Now;
                  public string Created
                  {
                     get{return ""This workflow was created at:"" + creationTime.ToString();}
                  }
               }
            }
            ";
            LinqStartObjects start = JetfireTest.NewNexusServer(
                JetfireTest.CDRIVE_JETFIRETEST,     // directory name - where the objects are stored
                TjServerStorageType.FileLinq, 
                TjStorageCommand.ClearStorage       // deletes any existing files in the directory
                );
            TjNexus nexus = start.ClientNexus;
            //
            //  Parse source code and create the Jetfire executable code expressions, 
            //  get the workflow class 'Data',
            //  then instantiate a new workflow object.
            //
            nexus.ParseServer(jetfireCode);
            TjWorkflowClass workflowClass = nexus.FindClass<TjWorkflowClass>("test", "Data");
            TjParameter[] parameters = TjParameter.ConvertToArray(nexus, new object[]
                        {
                            "My First Data",
                            "A detailed description of my first object"
                        });
            TjWorkflow data = workflowClass.NewWorkflow(parameters);
            //
            // Check to see if the workflow was created properly.
            //
            Assert.AreEqual("My First Data", data.Get<string>("Subject"));
            Assert.AreEqual("A detailed description of my first object", data.Get<string>("ToolTip"));
            //
            //  Shut down the client nexus.
            //
            JetfireTest.AssertNoIncidents(nexus);
            start.Transport.Dispose();
            
            //
            //   Create a new client nexus - This will restore old objects.
            //
            LinqStartObjects start2 = JetfireTest.NewNexusServer(
                JetfireTest.CDRIVE_JETFIRETEST, 
                TjServerStorageType.FileLinq, 
                TjStorageCommand.ReUseExisingStorage
                );
            TjNexus nexus2 = start2.ClientNexus;
            //
            //  Now try to find the code.
            //
            TjSecureHash codeHash = new TjSecureHash(jetfireCode);
            bool isFound = nexus2.CodeCollection.ContainsKey(codeHash);
            Assert.IsTrue(isFound);
            // 
            // Since the code was found, there is no need to add it again.
            // It can just be used as above.
            //

            JetfireTest.AssertNoIncidents(nexus2);
            start2.Transport.Dispose();
        }
    }
}

Last edited Feb 8, 2009 at 7:17 PM by JohnHansen, version 3

Comments

No comments yet.