May 2008 - Posts

Detecting If a Number has a Decimal Fraction

emoticon

This seems like pretty basic functionality of a number type, but I can't seem to be able to find any  built-in function in any of .Net number type that support decimal (decimal, double) or the System.Math class to do this.

In any case, if you find yourself in need to find out if a number has a decimal fraction, here are a couple of ways to do it:

You can convert the number to string and if the string contains a decimal delimiter (usually it's a period if you are using English locale, but it might be comma in some locale), then you can be sure that it has a decimal fraction.

double number = 99.99f;
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.GetCultureInfo("ID-id");
bool hasDecimalFraction = number.ToString(ci).Contains(ci.NumberFormat.NumberDecimalSeparator);
Console.WriteLine(hasDecimalFraction);  // should return true

 

Another way is to subtract the number by its floor and detect if what's left is greater than 0.

decimal number1 = 99.99m;
decimal number2 = 99m;
bool hasDecimalFraction = (number1 - Math.Floor(number1)) > 0;
Console.WriteLine(hasDecimalFraction);  // should return true
 
hasDecimalFraction = (number2 - Math.Floor(number2)) > 0;
Console.WriteLine(hasDecimalFraction);  // should return false

 

In any case, I really think this should be native to the number (decimal, double) / Math class.  Or is there such a function somewhere and I'm just not aware of it...  If you know otherwise, let me know.

Share this post: | | | |
Posted by Jimmy Chandra | 5 comment(s)
Filed under: ,

Quick Glances at Dependency Injection Frameworks Documentations

I've just take a look at a couple of Dependency Injection frameworks that are available for .Net today, namely Unity from Enterprise Library and Ninject.

To try school myself further on these frameworks, I decided to look at the respective documentations.

emoticon
First, I went to Ninject site.  The site itself is very attractive (THEY HAVE NINJA!!) and clean.  When I went to browse the dojo section, I was rewarded with a fun, clear and easy to follow explanation on the framework and how to do stuffs using it.  Kudo to whoever wrote that documentation.  The format works for me and it was very enjoyable to read.

emoticon

Then I decided to download Unity documentation from Microsoft site.  After running the installation, I launched MSDN Document Explorer and started reading.  What I got from it is a very confusing, dry and require a whole lot more brainpower to just wrap my head around the subject. 

I think I'll lay Unity down for a bit and go play with Ninject.  It seems more fun, and hey, they got ninjas, swords, shurikens and warlords.  If you are new to dependency injection framework and wish to get to know it better, I strongly suggest taking look at Ninject and its documentation.

On a side note, they are other frameworks too if you are interested.  Take a look at Spring.NET, and Castle Windsor (which I think Ninject use internally, not sure, but I saw some stuffs that look like assembly from Castle project in the source code).

emoticon
Have fun injecting!
Share this post: | | | |
Posted by Jimmy Chandra | with no comments
Filed under: , ,

Onionhead Fun...

I'm currently working on a plugin for Windows Live Writer that involve Onionheads on my spare time.

If you do not already know what's an onionhead, check it out at Onion Club.

I'm almost close to completion.  Actually the onionhead underneath is generated using the plugin prototype.

Check back in a couple of days...

emoticon
emoticon
Share this post: | | | |
Posted by Jimmy Chandra | with no comments
Filed under: ,

Hacking K2 [blackpearl] Project .kprx File

If you don't already know, .kprx file is just an XML file.  Which mean you can just open it inside Visual Studio or Notepad or any other text editor.

One occassion I find this useful is when you want to quickly do a find and replace operation on the workflow activity level custom .NET code since these code are stored as CDATA section inside the XML.  A bit of caviat... make sure you know what you are doing (naturally).

Most of the time I found that drilling down into the workflow code level is just a bit too slow to do inside K2 Designer (at least on my machine), that's why I prefer to do it this way since it's way faster when I need to do mass find and replace on multiple workflow event that require me to do View Code from the context menu.

Share this post: | | | |
Posted by Jimmy Chandra | with no comments
Filed under:

K2 [blackpearl] Lesson Learnt Part Deux

Apparently, adding a hyperlink into a K2 mail event from the K2 Designer is next to impossible (if you wish to do it from inside the designer itself).   I wish they'll add this functionality as a patch in the near future. *AHEM* SourceCode, are you listening?

image

Fig 1. There is button to add hyperlink anywhere in this email body dialog Sad

After fiddling around with it for a while I finally have to resort back to coding.  Basically, what you need to do is View Code on the K2 Mail Event that you wish to add the hyperlink to.  This will open up the Windows Workflow diagram.

image

Fig 2. Windows Workflow Foundation diagram of a K2 [blackpearl] Mail Event

Afterward, choose View Code again from the context menu of the SendMail activity in WF diagram above.  When the code screen comes up, just look for this.EmailMessage.Body = K2.Configuration.Body; line in the SetProperties_ExecuteCode method and change it to include your email HTML equivalent.

Here is an example:

   1:  //Url ... something like http://localhost/ChangeManagement/ViewCMF.aspx?d=34310
   2:  string url = (string)K2.ProcessInstance.DataFields["WebUrl"].Value +
   3:      "/ViewCMF.aspx?d=" + (string)K2.ProcessInstance.DataFields["DocumentNumber"].Value;
   4:   
   5:  this.EmailMessage.Body = 
   6:    "<html><head><title>Test Email</title></head><body>" +
   7:    "<p>Click <a href=\"" + url + "\">here</a>.</p>" +
   8:    "</body></html>";

Trying to add the <a href=....>here</a> in the designer itself will not work and will cause grief when you try to click the link in Outlook / Outlook Web Access.

image

Fig 3. DO NOT do this.  It will NOT work!

image

Fig 4. Pain and suffering awaits those who didn't obey what I said in Fig. 3. (Your link will be blocked since it will be malformed)

image

Fig 5. Hovering over the link in Outlook show the malformed URL of the link in Fig 4.

I guess another way you could do this is to set the mail type to just plain text, type the long url in without the <a href...> tag and hope your mail client is smart enough to convert the http://.... line to a real link (sucks, but it work with Outlook at least).

image

Fig 6. This will work on Outlook (Plain Text and no hardcoded HTML link)
Share this post: | | | |
Posted by Jimmy Chandra | 2 comment(s)
Filed under: ,

Custom CSS update for the blog

Phew, created my own CSS overrides for the my blog.  It was harder than I thought.  Lots of stuffs to overrides to make it appear like I want. 

Default themes sort of irritating in that some of the code posted always got cut off.  Not that the new one completely eliminate those cut off, but it should minimize them better since the screen was set to full width on this particular theme. 

Most of the features are implemented.  I still need to do some touch ups on Comments and Comments form and maybe add some graphics to finish things off later..

Share this post: | | | |
Posted by Jimmy Chandra | with no comments

K2 [blackpearl] Lesson Learnt...Uhm...so far...

Lesson 1

Never ever use XML Field if you wish to include data from it in the Workspace Notification Event Designer.  Put the data you want to expose in Data Field instead.  See Lesson 3 for when to use XMLField.

image

Fig 1 Can't drill into the XMLField further...

Lesson 2

In the case you need to create your own notification (Mail Event) inside your workflow process, you will need to craft the WorkItem Serial Number yourself by combining Process Instance ID + _ + Activity Instance Destination ID from the Context Browser like so:

clip_image001

Fig 2 Process Instance ID and ActivityInstance Destination ID combined to create the WorkItem Serial Number

Lesson 3

The best usage of XMLField that I found so far is to hold User Account to be fed to the Destination User when you have multiple Destination Users for a particular event.  Say you need approval from n number of people for a particular step in your workflow.  Just create an XMLField and stash those users in there.  The XML fragment should look like so (you can replace the tag with a more approriate tags):

   1:  <Approvers>
   2:    <Approver>K2:DOMAIN\User1</Approver>
   3:    <Approver>K2:DOMAIN\User2</Approver>
   4:    <Approver>K2:DOMAIN\User3</Approver>
   5:  </Approvers>

And then just add the repeating field (see below) as your Destination User.

image

Fig 3 Adding Document/ChangeRequest/Approvers/Approver repeating node as Destination Users.
Share this post: | | | |
Posted by Jimmy Chandra | with no comments
Filed under: