Tuesday, February 02, 2010

In my last post I provided a small tutorial on how to use the Client-Side Event API within the ASPxGridView component from DevExpress. In this post I would like to cover another snippet that I came up with that may complement my previous tutorial. This snippet assumes that you have multiple selection enabled in the GridView and wish to preserve the selection as the user pages through results.

If Session("pks") IsNot Nothing Then
Dim selectedPKs As List(Of Object) =
CType(Session("pks"), List(Of Object))
For i As Integer = 0 To selectedPKs.Count - 1
Dim selectedIdx As Integer = ASPxGridView1.
FindVisibleIndexByKeyValue(selectedPKs(i))

If selectedIdx >= ASPxGridView1.VisibleStartIndex And
selectedIdx <= ASPxGridView1.VisibleStartIndex +
ASPxGridView1.VisibleRowCount Then

ASPxGridView1.FocusedRowIndex = selectedIdx
Exit For
End If
Next i
End If

This snippet goes in the Data_Bound event so that it executes each time the user moves to a different page within the grid view.

Monday, January 25, 2010

Command Scripts

Recently I had to create a test that simulated a catastrophic failure to ensure the integrity of an embedded database. Development time for the test was limited so I set out for the quickest approach and saved myself the hassle of mocking test cases for another day. The test would initialize the database and then insert a random number of rows. During the test the OS would kill the process while rows were being inserted and then restart the process to ensure the integrity of the database.

I ended up coming up with a command script in Windows XP that I felt was worth sharing due to its relative obscurity. I will save the majority of the details and only highlight the important points.

First, let's look at the script I made to launch the java process responsible for bootstrapping the database:

START java -cp . Sleeper
ping 1.0.0.0 -n 1 -w 5000 >NUL
FOR /F "tokens=1-2" in ('jps') DO (
IF "%%j" == "Sleeper" (
SET PID=%%i
)
)
TASKKILL /PID %PID%

For me, the coolest part of this script was using the jps command to get the PID for the process that I launched in the first step. My second favorite feature (*cough* hack *cough*) was the use of the ping command (not my idea see the link here). It's the only way I could get the script to wait for a few seconds before killing my process - allowing the test running a separate process to reach the portion of the test where the insertion was occurring. It's not the most reliable or sane approach but it worked perfectly for creating a quick simulation.

The second snippet isn't as exciting as the first but think it's a huge timesaver when trying to execute a Java program that has a very large number of dependencies all located in the same directory. This script scans the directory (e.g., the lib/ folder) and adds every filename ending with .jar to the classpath. Again, keep in mind that this isn't optimal - but it's something quick-and-dirty that you can use to get going:

::EnableDelayedExpansion must be turned on in order to
::programmatically append to the classpath
SETLOCAL EnableDelayedExpansion
SET CLASSPATH=.

FOR %%i IN (lib\*.jar) DO SET CLASSPATH=!CLASSPATH!;%%i

%JAVA_HOME%\bin\java -cp %CLASSPATH YourClassGoesHere

Be sure to include the SETLOCAL (http://ss64.com/nt/setlocal.html) directive and to use the ! operator instead of the % operator when doing the assignment.

Sunday, January 24, 2010

ASPxGridView Client-Side API

When it comes to managing tabular data within an ASP.NET application I have found that the ASPxGridView control from DevExpress delivers a nice upgrade from the standard ASP.NET GridView control. In addition to the UI, the ASPxGridView control provides you with a seemingly endless number of customization options and provides great support for CRUD operations, exporting data, and filtering data with as little of ceremony (ok so maybe the ceremony is still there, it's just hidden inside of all of the design time configurations) that is possible with respect to an ASP.NET application.

Overall, I'm 85% satisified with how DevExpress implemented this control. The 15% of dissatisfaction that remains is distributed across missing features, method semantics, and the lack of documentation for the Client-Side API. Since my first two complaints are based on my opinion and I have no influence on how DevExpress conducts its business - I will use this post to cover the absolute basics for exercising the JavaScript API provided by the ASPxGridView control.

Accessing the Client-Side events is done by clicking on the smart dag on a ASPxGridView control while in design mode. With the "Tasks" panel open:

  1. Click the link for "Client-Side Events..."
  2. Select the event that you wish to customize.
  3. You'll see an empty function:

  4. function(s, e) {

    }

  5. The two parameters passed into the function are the sender object (that's the 's' parameter which is the ASPxGridView in our example) and the event object.
  6. For this tutorial let's detect a selection change within the ASPxGridView and perform a simple validation on selection to enable or disable a button that's on the same form. After selecting the SelectionChanged event I provide the following JavaScript:

    function(s, e) {
    s.GetSelectedFieldValues("ID",
    function onGetValues(result){
    var button = document.getElementById("btnCreateID");
    if (s.GetSelectedRowCount() != 1) {
    button.disabled = true;
    } else {
    if (result[0] = null)
    button.disabled = false;
    else
    button.disabled = true;
    }});
    }

    The above code invokes the GetSelectedFieldValues method from the Client-Side API and supplies the column value we wish to retrieve (a column named "ID" in this case) and a callback function that's used to process the result of getting the selected field values. In our example if we've selected one column and if that column doesn't already have an ID assigned to it, the create ID button is enabled.

That's all there is to it. The example is basic and somewhat contrived but hopefully it fills the void for your typical 'Getting Started' documentation that I'd like to see DevExpress provide.

Wednesday, January 20, 2010

My Top 10 Commonly Used Eclipse Shortcuts

Since getting reaquainted with the Eclipse IDE in the past few months I thought I'd share my top 10 most commonly used keyboard shortcuts (along with some commentary of course):

  1. Alt + Up or Down Arrow: This shortcut moves a line of code up or down based on the directional arrow you choose. Identation is respected so if you move a line of code inside of something like a loop or conditional you maintain formatting.

  2. CTRL + Shift + F: Formats the code. This is a real time saver especially if you have a custom formatter defined for your workspace.

  3. CTRL + Alt + Up or Down Arrow: Duplicates a line of code. I find myself using this shortcut when making variable declarations.

  4. CTRL + Shift + T: Opens a Type. Simliar to CTRL + Shift + R (open a resource) but this is more precise when you're truly looking to find a type and not a file in your workspace.

  5. CTRL + Shift + O: Organizes imports. My code always feels amateur until I remove all of the unnecessary imports along with removing the blanket import statements (e.g., some.package.*).

  6. Alt + Shift + R: Refactor. I'm not sure when Eclipse added the UI-sugar that live updates your code during a refactor operation but I like it!

  7. CTRL + H: Search, not find. Not a huge fan of the search utility but sometimes it can't be avoided.

  8. CTRL + /: Toggle Comment. Another huge time saver especially when you're trying to diagnose an offending block of code. For the purists at heart, the CTRL+Shift+\ and CTRL+Shift+/ add and remove (respectively) block comments.

  9. CTRL + W: Close window. Definitely not revolutionary but I feel this one is often overlooked.

  10. CTRL + L: Go to line. For the moments when your stack traces appear in a separate log file and not in a console inside of eclipse (where a hyperlink is usually provided if the source is available).

Tuesday, January 12, 2010

Content Repositories

Managing content vs. data is something that is becoming more clear to me in recent months. Over the last 2 years I’ve had the opportunity to get familiar with XML repositories (mainly MarkLogic Server) but did not really see the benefit of using such a technology outside of an enterprise-search-like application. MarkLogic provides a rich search API that includes content processing and enrichment along with some other pretty powerful features. All of this functionality is great but what if your project budget can’t accommodate the steep price? Are lesser known or open source content repositories still worth it?

I think so. First - what does it really mean when someone says their application manages content vs. data? In most cases, content and data can describe the same type of information but vary significantly in terms of extensibility. Data has a rigid structure that can be difficult to change over time while content tends to have a less rigid structure that can absorb additions and transformations more gracefully over the life of an application or solution. With that loose distinction between content and data in place picture using an XML repository to store messages exchanged in a SOA publish / subscribe paradigm. The messages will most likely evolve over time requiring developers to update the storage and retrieval mechanisms traditionally involving the update of relational queries in the application along with the structure of the database. Since XML repositories don’t have a set structure – developers are only concerned with how to retrieve the information and don’t need to concern themselves with the semantics behind storing the document (message validation isn’t included in this discussion).

This use case is fundamental and primitive but hopefully illustrates a key benefit for those looking to distinguish managing content from data.

Monday, January 04, 2010

The window.open Saga

A while back I encountered a strange problem with window.open() Javascript function. The offending Javascript looked like this:


window.open('someurl.aspx', 'My Window');


The code seemed to work fine in Firefox but I got an error when trying to evaluate the expression in Internet Explorer 7. I went crazy trying to figure out what was wrong with my code and soon found the space ' ' in the second parameter ('My Window') was causing the problem. If replaced the space with an underscore (i.e., 'My Window' -> 'My_Window') the error disappeared. I noticed that if I would've read the Mozilla JS documentation (https://developer.mozilla.org/En/DOM/Window.open) I would've seen the warning about not putting a space in the window name but I didn't. However the issue was related to IE7 I turned to Microsoft's documentation (http://msdn.microsoft.com/en-us/library/ms536651%28VS.85%29.aspx) and it should be no surprise that the no-space-in-the-window-name isn't mentioned anywhere.