Skip navigation

Once again, I’m apparently working in uncharted territory for the Dojo framework. I am working on a rich application that uses a Tree View to navigate certain data. When a user makes changes, the tree needs to update itself. In previous applications I’ve resorted to refreshing the entire tree due to the fact that the dijit.Tree is not very developer friendly. It is difficult to set up in the first place due to lack of documentation. This time however, I decided to dive into the code and figure out how to refresh only certain nodes in the tree. Here is what I found, it is pretty straight forward:

dojo.provide("my.ContentTreeNode");

dojo.require("dijit.Tree");

dojo.declare("my.ContentTreeNode", [dijit._TreeNode], {

	_setIconClassAttr: function(iconClass) {
		dojo.query("> .dijitTreeRow > .dijitTreeContent > .dijitIcon", this.domNode).attr("class", "dijitIcon dijitTreeIcon " + iconClass);
	},

	updateChildItems: function(items) {
		this.clearChildren();
		// set the child items of the node
		this.setChildItems(items);

		this.tree._expandNode(this, true);
	},

	clearChildren: function() {
		var childNodes = this.getChildren();

		dojo.forEach(childNodes, function(childNode) {
			// remove the node from the tree's item node map
			delete this.tree._itemNodesMap[this.tree.model.getIdentity(childNode.item)];
			// remove each node
			this.removeChild(childNode);
		}, this);
	}

});

As you can see, I extended dijit._TreeNode in order to add a few methods.
In order to use this tree node, you’ll also have to extend the dijit.Tree:

dojo.provide("MyContentTree");

dojo.require("dijit.Tree");
dojo.require("my.ContentTreeNode");

dojo.declare("my.ContentTree", [ dijit.Tree ], {
	_createTreeNode: function(args) {
		return new my.ContentTreeNode(args);
	}
});

I just thought I’d share this since Dojo’s documentation is lacking useful information. It seems like no matter how many documentation websites the guys at Dojo create, they never fully document everything. Here is a perfect example. I couldn’t for the life of me find out how to programatically create a dijit.PopupMenuItem. Through trial and error I figured this out:

var menuItem = new dijit.PopupMenuItem({
	label: "My Item",
	iconClass: "myIconClass",
	popup: new dijit.Menu()
});

The key being the popup property. If you don’t initialize the popup property with an instance of a new dijit.Menu, the whole thing doesn’t work. You can then use menuItem.popup.addChild(…) to add new menu items to the popup menu item.

I found a way to disable the infinitely annoying web development server that pops up for those of us who use IIS to develop ASP.NET applications. I often attach to the w3wp.exe process in order to debug my MVC controllers and whatnot and I always get a million little tray icons for the stupid web development server.

Now, I’m sure many people know you can click into the properties of each individual web application and set the project to run in IIS, or a custom web server. If you configure a project so that visual studio knows it’s an IIS application, visual studio becomes even more annoying. If you open a project that hasn’t been mapped in IIS it will bug you until you do it, and if you don’t the project will be unloaded. I say, screw you Microsoft, let me do my job. I know what I’m doing.

Now that I’ve gotten the rant out of the way, on to the solution. All you have to do is:

  1. Select all of the web projects in your solution.
  2. Open the properties pane if you don’t already have it open.
  3. Change the option “Always Start When Debugging” to false.

That’s it, say goodbye to 15 million development server icons in your tray.

I just wanted to quickly throw this out there because I had a really hard time finding any information out there on this issue.  The scenario I have is a base abstract class which contains a handful of properties.  There are three types which extend this class and add their own relationships.

The problem I ran into is that by default the auto mapper for Fluent automatically uses the type names as the discriminator to determine which type should be loaded.  What this means is the column in the database must be a varchar.  I think it is pretty obnoxious to have the full type of your class in every single row.  What I want is to use an enum to determine which type is used, and have it stored as an integer in the database.  You’d think this would be a pretty simple and well documented use case; it is not.

First off, you’ll need to create an auto mapping override for the parent class:

public class ParentMap : IAutoMappingOverride {
    public void Override(AutoMapping mapping) {
        mapping.DiscriminateSubClassesOnColumn("Type", 0);
    }
}

Regardless, I figured out how to do what I wanted to do.  In your auto mapping configuration class, you’ll need to tell the auto mapper to ignore the types which extend the abstract base class, like so:

public override bool ShouldMap(System.Type type) {
	return type != typeof(ChildType1) && type != typeof(ChildType2) && type != typeof(ChildType3);
}

Next, you need to create your own SubclassMap which manually maps all of the properties specific to the child class. Just to be clear, you do not have to map any of the properties defined in the parent class. These properties should be handled by the auto mapper (and if you need to modify the mappings you should create an Auto Mapping Override.

class ChildType1Map : SubclassMap {
    public ChildType1Map() {
        this.DiscriminatorValue((int)MyEnum.ChildType1);
        this.References(x => x.AnotherThing);
        this.Map(x => x.Name);
    }
}

The important part here is the call to DiscriminatorValue. This is where you instruct NHibernate what the value of the discriminator column will be for this type. Do the same for your other entities that extend the parent class. In order to get Fluent to pick up on these mapping files you’ll need to register them in your AutoPersistenceModel:

model.Add(typeof(ResourceFolderMap));

That should be all it takes. Hopefully this saves someone the hours I spent researching this.

For whatever reason…the GNOME people decided that menu / button icons were a usability problem.  I feel the opposite way.  I think the menu / button icons are absolutely a must have for usability. Icons make certain menu items stand out immediately.  The interface tab has now disappeared from the Appearance app in GNOME which means you have to manually enable the options to display icons.  This is easily done, just run gconf-editor in either the terminal or if you’re smart you have Gnome Do.

Once you’re there, navigate to Desktop -> Gnome -> Interface.  Check off buttons_have_icons and menus_have_icons.  These settings take effect as soon as you check off the options.  Enjoy.

I have been working with LINQ quite a bit for a contract I am working on.  And I have to say I’m not impressed.  I think Microsoft has continued to make poor decisions with the .NET framework.  Although I like the idea of a simple to use query language / ORM (Entity Framework), there are a number of problems.

  • The documentation is absolutely horrible.
  • It can be rather difficult to get the query language to do what you want it to do.
  • Exception messages are misleading and not informative.

I am originally a .NET guy.  I started my professional career working with C# and I really liked it.  Recently, I made the switch over to Java.  I liked the idea of Java because there is such a great open source community that has turned out a lot of really excellent and re-usable code.  In the open source community, the documentation is, in general, excellent.  Microsoft’s documentation has always been piss poor because how else would the publishers sell books?  In open source world, there are books available, but the documentation is usually sufficient.

I have run into a number of situations where the query language is very difficult to work with.  Specifically with reporting type queries where you’re getting into sub-selects.  Again, if the documentation were better I probably would have found a decent solution to my problem quickly.

My biggest trouble with the whole thing is when there is a problem and an exception is thrown, the messages are horrible.  For instance, I created a quick class to import some data from tab delimited files.  I set up some new data models and created my tables.  I gave the code a shot and I received an exception with this vague message: “System.Data.Linq.ChangeConflictException: Row not found or changed”.  All I can think of is “PC Load Letter, WTF does that mean?”  It turned out that one of my columns in the model design didn’t have the same data type as the column in the database.  Come on M$, how does that message make sense for that problem?  If it weren’t for someone else having that same problem in a forum, I could have lost HOURS hunting around trying to figure that out.

Again, I am a Java guy.  I am used to Hibernate which has been around for a while and is rock solid.  I feel like the open source community comes up with much better solutions compared to Microsoft.  Everything they do is always very specific, always a black box with very little insight, and generally has all kinds of annoyances that make the thing a pain to work with.

EDIT: Another thing…there is no way to batch insert.  Inserting seems to take far longer than it should…something that would take mere minutes with Hibernate / MySQL is going on an hour with LINQ / SQL 2005.

System.Data.Linq.ChangeConflictException: Row not found or changed

While we’re waiting for Firefox 4.0, which will bring significantly better compatibility with Windows 7, the Strata40 theme combined with StrataBuddy will make your Firefox 3.5/3.6 look pretty with Aero.  It is the best theme available for the current version of Firefox in my opinion.  StrataBuddy lets you customize the theme to your liking.  Check out the screenshot below.  Links are posted below the image.

The Strata40 theme makes Firefox look pretty on Windows 7!

Strata40 on Windows 7

Get the Strata40 theme

Get the StrataBuddy plugin

P.S. I didn’t mention Vista because Vista is CRAP.

I recently added functionality to the CMS I’ve been working on for my company.  It allows us to enter GSP tags into the content that is being posted to our Grails website.  These tags would then be properly rendered through Grails.

After searching around for hours, I thought I had found a solution to pass a string through the grails template engine:

new GroovyPagesTemplateEngine().createTemplate(attrs.templateString, “somepage.gsp”).make(attrs.model).writeTo(out)

This works great for our ${} style tags, however it does not work for anything in a tag library.  Again, I went to google and I could not find anything!  Finally, I found an obscure post describing how to build e-mails using GSP.  There isn’t any explanation as to how the template engine is acquired, but it seems to work.  In your tag lib, or controller create a member variable:

GroovyPagesTemplateEngine groovyPagesTemplateEngine

It seams as though you have to name it exactly as it is above.  You can then call the same method as before:

groovyPagesTemplateEngine.createTemplate(attrs.templateString, “somepage.gsp”).make(attrs.model).writeTo(out)

And bam, all of your tag libs are referenced properly!  Also, you can put whatever you want in “somepage.gsp”.  It doesn’t seem to matter.  However, I would assume for performance purposes, if the template is expected to be the same, make sure the page name is the same (so that grails can do its internal caching magic).

I’ve been working recently on a project that heavily relies on Hibernate.  I’ve learned a ton about Hibernate in the past few months.  Hibernate is excellent, it really saves a ton of time when it comes to dealing with data object persistence.  However, it can be difficult sometimes to get it to work exactly the way you want it to.

Some of the more advanced mappings are not quite as simple as the documentation, and even the excellent book Java Persistence with Hibernate will have you believe.  For instance, mapping Map collections properly.  The cleanest way to manage a collection of child objects is like this:

Parent parent = new Parent();

Child child = new Child();

parent.getChildren().put(“someChild”, child);

HibernateUtil.getSession().save(parent);

This works very well (with cascade = all) by default.  In order for the above example to work, the collection has to be mapped with the “inverse = false”.  The inverse flag is a matter of some confusion.  It simply tells Hibernate who will control the relationship between the parent and the child.  If inverse = false, the parent is the master, if it is true the child is the master.  This means, if the child is the master, the parent property MUST be set or the relationship will not be persisted, even if you add the child to the parent’s children collection.  If the parent is the master, the object must be added to its children collection, or the relationship will not be persisted.  Underneath it all, the inverse flag manages how the queries during inserts and updates are written.

The problem occurs when deletion of children needs to be managed as well.  Add this bit of code to the above example:

parent.getChildren().remove(“someChild”);

HibernateUtil.getSession().save(parent);

In order for this to work, the cascade option needs to be set to “all-delete-orphan.”  This instructs Hibernate to delete the children if they no longer belong to the parent.  An important thing to note is: if the collection is mapped with inverse=”true” all-delete-orphan DOES NOT DO ANYTHING.  This is something that I did not see in my reading (although I’m sure it’s there somewhere).  When the collection is mapped with inverse = true, you need to set the child’s parent property to null, or manually delete the object.

If you are using inverse=false, you may run into a weird issue with all-delete-orphan.  If your child’s parent foreign key relationship is set to not allow null values in the database, you will receive errors like “ParentFK cannot be null.”  This is because prior to deleting the child object Hibernate executes a query like this: “update Children set ParentFK = null where ChildID = 99;” .  Why it does this is beyond me.  However I do know how to fix the issue.  What you have to do is set the map’s key column to not null, and set update = true.  Then, in the child mapping, set the parent many to one to not insert or update.  You will need to do the same for the index of the map mapping.  Here’s what the mappings should look like:

<map name=”children” cascade=”all-delete-orphan” inverse=”false”>

<key column=”ParentFK” not-null=”true” update=”true” />

<index column=”Name” type=”string” update=”true” />

<element type=”Child” />

</map>

The child’s parent property should look like this:

<many-to-one name=”parent” column=”ParentFK” class=”Parent” update=”false” insert=”false” />

<property name=”name” column=”Name” updated=”false” insert=”false” />

This will get rid of the nasty issue of Hibernate trying to update the foreign key to null before it deletes children.  Another problem I ran into with map collections is when the index property for the child object did not exist on the child iteself, but on an object that the child references.  To fix this, all you need to do is change the index to a map-key, and use a formula like so:

<map name=”children” cascade=”all-delete-orphan” inverse=”false”>

<key column=”ParentFK” not-null=”true” update=”true” />

<map-key column=”o.Name” type=”string” formula=”(select o.Name from Catalog.OtherObjects o where o.OtherObjectID = OtherObjectFK)” />

<element type=”Child” />

</map>

Notice I included the catalog name before the table name, this is required if you do not select a default database in your hibernate configuration (I do this because we have multiple databases which our objects are stored in).

Well, hopefully these findings are helpful to others.  I’m sure they are not original, but I had a hard time digging this information up online.  As a matter of fact I did not find this information anywhere out there surprisingly.  I had to piece it together by going through the documentation and running unit tests.

If you are looking for a simple, easy to use and configure web framework for Java, Grails is the way to go.  I spent a ton of time looking around for Java web frameworks and I landed on Grails.  I am in the process of rebuilding my company’s public facing website as well as their intranet.  Because the two sites need access to the same data, I decided to build a common class library which handles all data access.  Although Grails is meant to handle all mapping and data acess for you, it was pretty easy to work in my existing data access project.

The first thing I would like to note about Grails is that there is virtually no configuration.  You will have to edit one or two configuration files if you want to customize certain aspects of the application, but unlike other frameworks, there is ZERO xml configuration.  I have run into a number of challenges as my website, like all websites, has a bunch of unique features.  Due to the excellent documentation for Grails I have had little to no trouble overcoming these challenges.

Because my application uses it’s own set of class factories, I was not able to simply drop my hibernate.xml.cfg and mapping files into grails.  I need to have complete control over how the objects are loaded and saved to the database.  This meant I would need something similar to the OpenSessionInViewFilter.  The problem with adding a fitler is that I didn’t want to open up sessions for every request, just requests that needed acess to the data.  After reading through the documentation I found that Grails has support for controller filters, here is a simple way to open up and close sessions for Hibernate in a grails filter.  Drop a file named HibernateFilters.groovy into the grails-app/conf directory:

public class HibernateFilters {
def filters = {
ensureSession(controller:’*', action:’*') {
before = {
HibernateUtil.getCurrentSession().beginTransaction();
}

afterView = {
HibernateUtil.getCurrentSession().getTransaction().commit();
}
}
}
}

Between tag libraries, templates and layouts, building UI in a Grails application is a breeze.  The UI code is so clean because of the way the rendering engine works.  There is no need for server side code in the views (although it is possible to put it there).

AJAX support is excellent in Grails.  It solves the common problem of rendering of UI on the server side as well as the client side.  It is simple to render a template or view and return it in an AJAX call.  This is huge, becuase you won’t find yourself writing the same UI code twice.  In my experience with .NET this was one of the most frustrating parts of building AJAX applications.  Also, Grails makes it really simple to render JSON and XML.  You can simply use “as JSON” or “as XML” in your render calls.

All in all, Grails is a quick and easy to use framework that provides all the tools a web developer needs to build a rich and dynamic web application with minimal headaches.  Out of the frameworks I’ve used (ASP.NET, CakePHP, Wicket, Tapestry), Grails is definitely at the top of the list (followed closely by CakePHP).

Check out the official grails website.

Follow

Get every new post delivered to your Inbox.