Wednesday, October 15, 2008

Linq Expression Tree

Linq Expression : Switching between data Structure and code


             /////////////////////////////////////////////
            /// Linq Expression Tree for (x,y ) => x * y;
            /// Directly build the lambda expression /// 
            /////////////////////////////////////////////

            Expression lambdaExpression1 = (x, y) => x * y;
            Func lamba1Delegate = lambdaExpression1.Compile();
            int resultLamda1 = lamba1Delegate(100, 100);

            Console.WriteLine(resultLamda1.ToString());
   

        /////////////////////////////////////////////
     Or you can do the following 

     Expression Func int int int  AddOperation = (x, y) => x + y; 
       int result = AddOperation.Compile()(10, 10);

            /////////////////////////////////////////////
            ///// Rebuilding and Executing Linq Expression 
            ////  Manually 
            /////////////////////////////////////////////
                
            ParameterExpression p1 = Expression.Parameter(typeof(int), "x");
            ParameterExpression p2 = Expression.Parameter(typeof(int), "y");
            BinaryExpression operation = Expression.Multiply(p1, p2);
            Expression> lambdaExpression2 = Expression.Lambda>(operation, new ParameterExpression[] { p1 , p2});

            Func FunctionForLambda2 = lambdaExpression2.Compile();
            int resultLamda2 = FunctionForLambda2(12, 12);

            Console.WriteLine(resultLamda2.ToString());


Sample Linq Expression 



Another example to  add x + y + z 

/// x + y + z 

            ParameterExpression varx= Expression.Parameter(typeof(int), "x");
            ParameterExpression vary = Expression.Parameter(typeof(int), "y");
            ParameterExpression varz = Expression.Parameter(typeof(int), "z");

            /// x + y 
            Expression operationAdd = Expression.Add(varx, vary);
            
            // (x+y) + z 
            Expression operationAdd2 = Expression.Add(operationAdd, varz);
            LambdaExpression expr1 = Expression.Lambda(operationAdd2, varx, vary, varz);
            
            /// Since we're passing in 3 parameters ..... 
            
            Func FuncCall2 = (Func)expr1.Compile();
            int result4 = FuncCall2(10, 10, 10);

Tuesday, October 14, 2008

SaaS Map

Something i found long long time ago....... Kinda like it. 







Biztalk : Pipeline Stamping Receive Message


This code allows you to stamp your incoming message with a XML element called SequenceNo. This sequence number gives you the ability to sequence without making changes to your existing application.  It is unique in the sense that it does not create a new part for you and there shouldn't be too much modification to your existing codes in order to implement it. 


Creating a pipeline in Biztalk Server is relatively easy. It requires you to

1) Implement specific Interface

2) Create a Biztalk Project that uses a pipeline Item type with extension Btp. Drag the assembly that you have created above into [validate] section.

3) Deploy and move it to the right application. You need to configure properly after this.


So first you have to come up with an assembly that overrides the Execute method as shown below. It returns a new IBaseMessage: 

 public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
        {
            int seq = 0;

            /// new message structure ///  
            IBaseMessageFactory msgFactory = pContext.GetMessageFactory(); 
            IBaseMessageContext context = pInMsg.Context;

            IBaseMessagePart OriginalBodyPart = pInMsg.BodyPart;
            Stream MessageStream = OriginalBodyPart.GetOriginalDataStream();
            
            //// Constructing a new message ////

            IBaseMessage msg = msgFactory.CreateMessage();


            
            IPipelineContextEx pcx = pContext as IPipelineContextEx;

            if (OriginalBodyPart != null)
            {
                
                
              #region [Debugging/Logging]
       //XmlDocument xDocOutput = new XmlDocument();
                //xDocOutput.Load(MessageStream);
                //XmlTextWriter writer = new XmlTextWriter("d:\\temp\\message.xml", System.Text.Encoding.UTF8);
                //writer.Formatting = Formatting.Indented;
                //xDocOutput.WriteTo(writer);
                //writer.Flush();
                //writer.Close(); 
       #endregion  
                
                if (pcx != null)
                {
                    ITransaction trans = null;
                    trans = (ITransaction)pcx.GetTransaction();

                    if (trans != null)
                    {
                        //// Get ConnectionString //// 

                        SqlConnection conn = new SqlConnection("Database=SequenceDB;Server=72.15.219.241;connect timeout=60;user id=es14703-dev;password=p0w3rOFdr3ams");
                        SqlCommand cmd = new SqlCommand();
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "GetSequence";
                        
                        SqlParameter parameter = new SqlParameter("@ReceiveLocationName", SqlDbType.NVarChar, 50);
                        parameter.Value = "TestReceiveLocation"; 
                        cmd.Parameters.Add(parameter);


                        ///////////////////////////////////////////
                        /// System.IO.File.AppendAllText("d:\\temp\\mqdata.log", MessageStream.ToString());
                        /////////////////////////////////////////////

                        try
                        {
                            cmd.Connection = conn;
                            conn.Open(); 

                            //// Get the Data /// 
                            seq = Convert.ToInt32(cmd.ExecuteScalar());


                            IBaseMessagePart part = msgFactory.CreateMessagePart();

                            XmlDocument xdoc = new XmlDocument();
                            xdoc.Load(MessageStream);

                            XmlElement newElement = xdoc.CreateElement("SequenceNo");
                            newElement.InnerText = seq.ToString();
                            xdoc.LastChild.AppendChild(newElement);

                            byte[] dataBytes = System.Text.Encoding.UTF8.GetBytes(xdoc.InnerXml);
                            MemoryStream stream = new MemoryStream();
                            stream.Write(dataBytes, 0, dataBytes.Length);

                            stream.Seek(0, SeekOrigin.Begin);

                            part.Data = stream;
                            msg.Context = context;

                            msg.AddPart("DMQMessageContractSend", part, true);

                            
                            IBaseMessagePart ExtendedPart = msgFactory.CreateMessagePart();
                            byte[] extendedContent = System.Text.Encoding.UTF8.GetBytes(string.Format("{0}", seq.ToString()));
                            
                            ExtendedPart.Data = new MemoryStream(extendedContent);
                            ExtendedPart.Charset = "utf-8";
                            ExtendedPart.ContentType = "text/xml";
                            msg.AddPart("Sequence", ExtendedPart, false);


                            #region [OldCodes]

                            ///  Stamp a sequence number to the message /// 

                            //XmlDocument xdoc = new XmlDocument();
                            //xdoc.Load(MessageStream);

                            //XmlElement newElement = xdoc.CreateElement("SequenceNo");
                            //newElement.InnerText = seq.ToString();
                            //xdoc.LastChild.AppendChild(newElement);

                            //byte[] dataBytes = System.Text.Encoding.UTF8.GetBytes(xdoc.InnerXml);
                            //MemoryStream stream = new MemoryStream();

                            //stream.Write(dataBytes, 0, dataBytes.Length);

                            //System.IO.File.AppendAllText("d:\\temp\\msmqlog.log", "somedata gotta be here" +  System.Text.Encoding.UTF8.GetString(dataBytes));

                            //stream.Seek(0, SeekOrigin.Begin);
                            //bodyPart.Data.Seek(0, SeekOrigin.Begin);
                            //OriginalBodyPart.Data = stream;

                            //IBaseMessage outMsg;
                            //outMsg = pContext.GetMessageFactory().CreateMessage();
                            //outMsg.AddPart("Body", pContext.GetMessageFactory().CreateMessagePart(), true);
                            //outMsg.BodyPart.Data = stream;

                            //IBaseMessage outMsg;
                            //pInMsg = pContext.GetMessageFactory().CreateMessage();
                            //pInMsg.AddPart("Sequence", pContext.GetMessageFactory().CreateMessagePart(), true);
                            //pInMsg.BodyPart.Data = stream;

                            //bodyPart.Data.Write(dataBytes, 0, dataBytes.Length);

                            //byte[] outputDataBytes = new byte[bodyPart.Data.Length]; 
                            //int xlength  = System.Convert.ToInt32(bodyPart.Data.Length);
                            //bodyPart.Data.Read(outputDataBytes, 0, xlength);

                            //System.IO.File.AppendAllText("d:\\temp\\scanstream.log", "Output " + System.Text.Encoding.UTF8.GetString(outputDataBytes));

                            //// Increment and Update the data //// 

                            // context.Promote("messageData", "", seq.ToString());
                            // context.Write("messageData", "", seq.ToString());


                            #endregion                            

                            
                            SqlConnection SeqUpdateConn = new SqlConnection("Database=SequenceDB;Server=72.15.219.241;connect timeout=60;user id=es14703-dev;password=p0w3rOFdr3ams");

                            SqlCommand SeqUpdateCommand = new SqlCommand();
                            SeqUpdateCommand.CommandType = CommandType.StoredProcedure;
                            SeqUpdateCommand.CommandText = "UpdateSequence";

                            SqlParameter UpdateParameter = new SqlParameter("@ReceiveLocationName", SqlDbType.NVarChar, 50);
                            UpdateParameter.Value = "TestReceiveLocation";
                            SeqUpdateCommand.Parameters.Add(UpdateParameter);

                            SeqUpdateCommand.Connection = SeqUpdateConn;
                            SeqUpdateConn.Open();

                            SeqUpdateCommand.ExecuteNonQuery(); 


                            SeqUpdateCommand.Dispose();
                            SeqUpdateConn.Close();

                        }
                        catch (SqlException)
                        {
                            throw;
                        }
                        finally
                        {
                            cmd.Dispose();
                            conn.Close();
                        }

                    }
                }

            }


            /// Returning the newly constructed messeage part /// 

            return msg;


        }

Once you have this code, next is to create a new biztalk server project and use toolbox to Choose Item which can be added into your pipeline. 

In your orchestration,  reference can be made to this Sequence Number using the following codes:-

varStrSequenceNo = xpath(DMQMessage, "string(/*[local-name()='DMQMessageContractSend' and namespace-uri()='']/*[local-name()='SequenceNo' and namespace-uri()=''])");








Saturday, October 11, 2008

Accelerating Scrollable Flex Control

The general concept of creating this control is using a Timer to control, scroll through your control before slowing down after it is being triggered by Mouse_Wheel.
So you need to listen to two events.




package com.controls.controls
{
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;

import mx.controls.List;

public class CtlList extends List
{

public var sliderSpeed:Number=5;
public var sliderSlowSpeed:Number=1;
private var sliderAlreadyScrolling:Boolean=false;
private var QtyBeforeSlowDown:Number=5;
private var slideTimeCounter:Number=20;
private var currentSlideCounter:Number=slideTimeCounter;

private var DownDirection:Boolean=true;


public function CtlList()
{
this.addEventListener(MouseEvent.MOUSE_WHEEL, MouseWheel_Scrolling);
super();
}

private function MouseWheel_Scrolling(evt:MouseEvent):void
{

if (!sliderAlreadyScrolling)
{
/// do scrolling ///
DoScrolling(evt.delta);

}
else
{
///// Reset the Scrolling Behaviour ///
currentSlideCounter=slideTimeCounter;
}

}

private function DoScrolling(delta:Number):void
{
sliderAlreadyScrolling=true;
var ScrollTimer:Timer = new Timer(50, slideTimeCounter);

if (delta > 0)
{
DownDirection=false;
}
else
{
DownDirection=true;
}


ScrollTimer.addEventListener(TimerEvent.TIMER, ContinueScrolling);
ScrollTimer.addEventListener(TimerEvent.TIMER_COMPLETE, SlowDownScrolling);
ScrollTimer.start();
}

private function ContinueScrolling(evt:TimerEvent):void
{
var currentPosition:Number = this.verticalScrollBar.scrollPosition;
var maxPosition:Number = this.verticalScrollBar.maxScrollPosition;
var minPosition:Number = this.verticalScrollBar.minScrollPosition;
currentSlideCounter--;


if (DownDirection)
{
if ( currentPosition >= minPosition)
{
if (currentSlideCounter <= QtyBeforeSlowDown)
{
this.verticalScrollBar.scrollPosition = (currentPosition + sliderSlowSpeed);
super.scrollToIndex(this.verticalScrollBar.scrollPosition);
}
else
{
this.verticalScrollBar.scrollPosition = (currentPosition + sliderSpeed);
super.scrollToIndex(this.verticalScrollBar.scrollPosition);
}
}
}
else
{
if ( currentPosition <= maxPosition)
{
if (currentSlideCounter <= QtyBeforeSlowDown)
{
this.verticalScrollBar.scrollPosition = (currentPosition - sliderSlowSpeed);
super.scrollToIndex(this.verticalScrollBar.scrollPosition);
}
else
{
this.verticalScrollBar.scrollPosition = (currentPosition - sliderSpeed);
super.scrollToIndex(this.verticalScrollBar.scrollPosition);
}
}

}

}

private function SlowDownScrolling(evt:Event):void
{
this.verticalScrollBar.scrollPosition=0;
sliderAlreadyScrolling=false;
}

private function SetWheelInterval():void {


}


}
}