jackrabbit-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ard Schrijvers" <a.schrijv...@hippo.nl>
Subject RE: Jackrabbit Best Practices/Design Patterns (an attempt)
Date Wed, 07 Nov 2007 12:44:14 GMT
Not sure wether this is too abstract for you, but you might want to take a look over here:

http://wiki.apache.org/jackrabbit/DavidsModel

Regards Ard



I thought I would augment my question with a (simplified) example of what I'm
trying to do. Starting with a very basic approach:

Node root = session.getRootNode();
Node album = root.getNode("MyAlbum");
Node songs = album.getNode("songs");
Node song = songs.addNode("MySong");
song.setProperty("genre", "funky");
session.save();

I've expanded this into a richer object model:

public class Album{

	public static void main(){
	
		Session session = null;
	
		try{
			session = Repository.login();
			Node root = session.getRootNode();
			
			Album myAlbum = new Album(root.getNode("MyAlbum"));
			Song mySong = album.addSong("MySong");
			mySong.setGenre("funky");
			myAlbum.save();
		}
		catch(Exception e){
			System.err.println(e.getMessage());
		}
		finally{
			if(session != null)
				session.logout();
		}
	}
}


class Album{

	private Node _album;
	
	public Album(Node album){
		_album = album;
	}
	
	public String getName(){
		Property name = _album.getProperty("name");
		return name.getString();
	}
	
	public Song addSong(String name){
		Node songs = _album.getNode("songs");
		Node song = songs.addNode(name);
		return new Song(song);
	}
	
	public void save(){
		_album.save();
	}
}

class Song{
	private Node _song;
	
	public Song(Node song){
		_song = song;
	}
	
	public void setGenre(String genre){
		_song.setProperty("genre", genre);
	}
}

What are your thoughts? Am I doing anything that might cause issues as
things get more complex? I'm new to the concept of JCR, so I'm very
interested in learning how I can use it in my code as naturally as possible. 

Personally, I feel the "addSong" method is a little awkward, but I don't
really see another way to do it. Also, how long should sessions live? With
relational databases, best practice is to open and close the connection as
quickly as possible. However, it seems like JCR sessions can/should stay
open much longer. Any thoughts/tips/examples are very much appreciated!



loproman wrote:
> 
> Hi,
> 
> Does anyone know if there has been any research or documentation developed
> on best practices working with repositories?  Specifically, I'm wondering
> if there are any ideal patterns out there for working with the JCR API.
> I've seen lots of primitive examples where nodes are accessed directly via
> "getNode" and "addNode" etc. However, I think a more scalable solution
> would need to wrap nodes with domain logic (similar to how DTO objects are
> used with a relational database I suspect). Thanks!
> 

-- 
View this message in context: http://www.nabble.com/Jackrabbit-Best-Practices-Design-Patterns-tf4762615.html#a13624765
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.




Mime
  • Unnamed multipart/mixed (inline, None, 0 bytes)
View raw message