Archive

Posts Tagged ‘jcr’

Why use Python and Django?

August 9, 2010 7 comments

DISCLAIMER: This could also be called, “Why not use Java and JCR?” But I’m going to “pick on” Java and JCR, simply because the example code was right there and easy to grab. I mean no ill feelings towards Java, the JCR or anyone who uses these. This post is simply a comparison, so that you can see why I emphasize Python and Django for web development.

OK, onward!

I was reading about Jackrabbit today, simply because I like to know what’s out there.  I found this example, for how to create a “PressRelease” object using Jackrabbit:

package org.apache.jackrabbit.ocm.model;

import java.util.Date;

import org.apache.jackrabbit.ocm.mapper.impl.annotation.Field;
import org.apache.jackrabbit.ocm.mapper.impl.annotation.Node;

@Node
public class PressRelease
{
	@Field(path=true) String path;
	@Field String title;
	@Field Date pubDate;
	@Field String content;

	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public Date getPubDate() {
		return pubDate;
	}
	public void setPubDate(Date pubDate) {
		this.pubDate = pubDate;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}

}

That’s 41 lines. Now, you could do something very similar in 9 lines with Django, like this:

from django import models

IDUNNO = 255 #maybe, I'm guessing

class PressRelease(models.Model):
    path = models.CharField(max_length=IDUNNO)
    title = models.CharField(max_length=IDUNNO)
    pub_date = models.DateTimeField(auto_add=True)
    content = models.TextField()

Now, let’s actually do something with this model. Like create an object and save it. In Jackrabbit, you have to do this: (NOTE: I’ve combined two code snippets, because I think that’s what I would have to do to actually make it work.)

// In order to save a PressRelease object, you have to instantiate an Object Content Manager component
List classes = new ArrayList();	
classes.add(PressRelease.class); // Call this method for each persistent class
		
Mapper mapper = new AnnotationMapperImpl(classes);
ObjectContentManager ocm =  new ObjectContentManagerImpl(session, mapper);	

// Insert an object
System.out.println("Insert a press release in the repository");
PressRelease pressRelease = new PressRelease();
pressRelease.setPath("/newtutorial");
pressRelease.setTitle("This is the first tutorial on OCM");
pressRelease.setPubDate(new Date());
pressRelease.setContent("Many Jackrabbit users ask to the dev team to make a tutorial on OCM");
			
ocm.insert(pressRelease);
ocm.save();
			
// Retrieve 
System.out.println("Retrieve a press release from the repository");
pressRelease = (PressRelease) ocm.getObject("/newtutorial");
System.out.println("PressRelease title : " + pressRelease.getTitle());
			
// Delete
System.out.println("Remove a press release from the repository");
ocm.remove(pressRelease);
ocm.save();

(26 Lines?)

Now, let’s try that in Django:

from some_app.models import PressRelease

// Insert an object
print "Insert a press release in the repository"
pressRelease = PressRelease()
pressRelease.path = "/newtutorial"
pressRelease.title = "This is the first tutorial on OCM"
pressRelease.content = "Many Jackrabbit users ask to the dev team to make a tutorial on OCM"
pressRelease.save()
	
// Retrieve 
print "Retrieve a press release from the repository"
pressRelease = PressRelease.objects.get(path="/newtutorial")
print "PressRelease title : " + pressRelease.title
			
// Delete
print "Remove a press release from the repository"
pressRelease.delete()

Now, that’s 18 lines, so it’s not so much difference on the line count only. But look at the readability.

Can you see why I really, really like Django? 🙂