Saturday, December 5, 2009

Getting a signed in user in Grails tests

I found this to be helpful. I just needed one more piece of information.

def subject = [
isAuthenticated: true,
principal: "admin"
] as Subject

SecurityUtils.metaClass.static.getSubject = {-> return subject }
Subject.metaClass.getPrincipal = {-> return "admin" }

This way, when I needed the user later on in the code, when I called getPrincipal, I got it. E.g.

JsecUser currentUser = JsecUser.findByUsername(SecurityUtils.getSubject()?.getPrincipal());

in reference to:

"void setUp() {       def subject = [           isAuthenticated: false,           ...       ] as Subject       SecurityUtils.metaClass.static.getSubject = {-> return subject }       ...   }"
- Nabble - grails - user - JSecurity-Plugin and Integration-Tests (view on Google Sidewiki)

Thursday, December 3, 2009

Class Diagram needs class groups selector

I love this plugin, but it really needs a way to graphically select some domain classes and delegate them to a diagram I could select from a menu or pick list or something.

in reference to:

"You may change diagram preferences defaults in your projects configuration file (Config.groovy)."
- Grails Plugin - Class diagram plugin (view on Google Sidewiki)

Seems pretty cool.

I think I like this Side Wiki. Now, if it would only let me highlight stuff for context, etc, I would love that.

in reference to: Google Sidewiki (view on Google Sidewiki)

Monday, October 26, 2009

Game Maker.... pretty cool


Last Weekend, I spent Saturday Morning volunteering my time with students from my son's charter school, Mountainville Academy. I hosted a workshop on the fun and accessible multimedia project/game factory called Scratch.

We went for 3 hours in smaller instructional periods, followed by hands on time. In my opinion, it was all very successful. We covered nearly half of the scratch blocks in the time allotted, and I could begin to see some nice projects coming along.

During that time, one of the students mentioned another game making software package called 'Game Maker', hosted at YoYo Games. I downloaded it and began playing with it a little bit. It had some truly brilliant features that were definitely missing from Scratch. However, it does appear less accessible than Scratch. I.e. I think Scratch hits my 5th graders really well, producing good projects in just a few hours. GameMaker is meant for High Schoolers and beyond, it would seem.

One thing I liked though, were some of the tutorials, including this one: What is a good game. I recommend that if you are in my class, you read it. It will help guide you in your game creation so that after you spend all that time building the game, people will want to spend time playing it.

For those in my class, I want to see all of your scratch projects. I have created a scratch gallery called Mountainville Academy projects located here. Take a moment to take a look at some of the projects there, and to put your own in there as well. I'll see you all on Halloween!

Tuesday, July 7, 2009

Using GraphViz to automatically Diagram your Grails Domain Model



I'm working on a feature that automatically diagrams the Domain model stored in Grails, similar to the method prototyped by the Curious Bunny here. His example works pretty well for a simple model, but begins to break down due to a seeming Grails limitation.

In my model, I have a Party, Organization and Company class. Company extends Organization, Organization extends Party.
Party <--- Organization < --- Company Inside of my Controller that produces the Graph, I iterate through a classes subclasses like so:

domainClass.subClasses.each {subClass ->
out.println """"$domainClass.name" -> "$subClass.name" """
}

This works ok, but when this is run on Company, both Party and Organization are in the set. In a UML diagram, I don't really want both of them, I only want one of them. I.e. it produces a diagram that is more cluttered than it needs to be.

This diagram represents a simplified subset of my domain, based off the Party Pattern Archetype in the book: Enterprise Patterns and MDA: Building Better Software with Archetype Patterns and UML

Notice the redundant inheritance.

Anybody know how to only get direct subclasses, instead of recursive subclasses?

Tuesday, April 21, 2009

Blog host

So, I'm trying to figure out how to host my own blog. Steve Pavlina suggests that I host it with its own domain name, using this domain provider Pair, and ServInt VPS using wordpress. It sounds like a brilliant idea, but I'm not too familiar with PHP.

Since I'm already very familiar with Java and JSP etc, I was wondering if there exists some sort of WordPress for java. Apparently there is, or I can get a free account at WordPress itself, but then they get all of the ad revenue.

My sister-in-law has her own blog called theliteratemother.org that is hosted on wordpress as well. It seems to be pretty cool, and maybe more like what I'm looking for.

Otherwise, I'm looking at spending at least $50/month to host it where Steve Pavlina suggests. I.e. ServInt.

Seems like I should at least as Bridget where she hosts hers, I suppose.

Hibernate Caching

I've been working on getting Hibernate caching to work at work to implement a new feature from Product Management. The original requirements suggested rolling our own caching mechanism, but since our application already has hibernate, it doesn't seem to make sense to roll our own.

Early investigation lead me here:

A wonderful article about hibernate caching. Since our application uses xdoclet for defining hibernate mapping, it wasn't quite complete.

But I really like the analysis because it displays rather clearly how much faster it runs with the caching thrown in.

I'm tempted as well to perform a similar analysis. The article talks about caching the object, and the relationships to the object, which are essential for my problem as well because I have a hierarchy in my objects of Parent -> child. My object is called the Domain Object.

the hbm_xml would be this:



The xdoclet then would be (found here):
/**
* @author petersmiley
*
* @hibernate.class table="Domains" dynamic-insert="true" dynamic-update="true" optimistic-lock="version"
* @hibernate.cache usage="read-only"
* @struts.form "DomainForm"
*
* @auditable.property entity-name="audit.entity.domain"
*/
public class Domain extends BaseObject implements Auditable, Comparable {
.... and the link back to the parent:
/**
* @struts.form-field form-name="parentDomain"
* @hibernate.cache usage="read-write"
* @hibernate.many-to-one
* column="PARENT_ID"
* class="com.norkom.base.resources.model.Domain"
* not-null="false"
* unique="false"
* --cascade="none"
* --outer-join="false""
*
* @auditable.property identified-by="parent.domain"
*/
public Domain getParentDomain() {
return parentDomain;
}
public void setParentDomain(Domain parentDomain) {
this.parentDomain = parentDomain;
}

Note: if you use "read-only" then don't expect to be able to put any objects into the database. When it says read-only, it means it! (I tried it. I had to change to read-write)

Here is my unit test:
public void testCachedCrud() {
List list = domainDao.getAllDomains();
assertEquals(0, list.size());
TestTimer createDomainsTimer = new TestTimer("test Create Domains");
int size = 10;
for (int i=0;i
Domain parentDomain = createBasicDomain(PARENT_DOMAIN_NAME + i);
for(int j=0;j
Domain childDomain = createBasicDomain(CHILD_DOMAIN_DOMAIN + i + ":" + j);
childDomain.setParentDomain(parentDomain);
domainDao.saveDomain(childDomain);
}
domainDao.saveDomain(parentDomain);
}
createDomainsTimer.done();

for (int i=0;i
TestTimer timer = new TestTimer("test Get Domains" + i);
Domain parentDomain = domainDao.getDomainByName(PARENT_DOMAIN_NAME + i);
timer.done();
}
}

Results, with cacheing:
INFO [DomainDaoImplTest] -> Loading config for: /com/norkom/base/business/dao/impl/test-context.xml
EMMA: collecting runtime coverage data ...
WARN [Configurator] -> No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/work/trunk/SUBVERSION/Libs/hibernate/2.1.8/ehcache-0.9.jar!/ehcache-failsafe.xml
WARN [EhCacheProvider] -> Could not find configuration [com.norkom.base.resources.model.Domain]; using defaults.
DEBUG [TestTimer] -> test Create Domains: 140 ms
DEBUG [TestTimer] -> test Get Domains0: 0 ms
DEBUG [TestTimer] -> test Get Domains1: 0 ms
DEBUG [TestTimer] -> test Get Domains2: 0 ms
DEBUG [TestTimer] -> test Get Domains3: 0 ms
DEBUG [TestTimer] -> test Get Domains4: 0 ms
DEBUG [TestTimer] -> test Get Domains5: 0 ms
DEBUG [TestTimer] -> test Get Domains6: 0 ms
DEBUG [TestTimer] -> test Get Domains7: 0 ms
DEBUG [TestTimer] -> test Get Domains8: 0 ms

Now, lets try without caching...
DEBUG [TestTimer] -> test Get Domains9: 0 ms