cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeremy Quinn <jer...@media.demon.co.uk>
Subject Re: Flow Database stuff ( The new FOM? )
Date Mon, 14 Jul 2003 11:53:18 GMT

On Sunday, July 13, 2003, at 10:40 AM, Reinhard Pötz wrote:

> To understand this correctly: Does e.g. Hibernate give you this
> typed "object repository"? You only have to deal with beans and
> all the persistence stuff is done for you "by magic".
>

It is not magic, but it does feel a bit like it sometimes ;)

Anyway, because I think you would really like to see how Hibernate can 
be used, here is some (non-brilliant) code for adding a child bean 
(Coverage represents a hierarchy of geographical locations).

'Edit' uses a (non-continuation) screen that shows lots of 
relationships around the individual Bean providing links to function 
calls to change complex properties and a form to edit simple properties 
(submitted to self), 'Add' uses a subset of the 'Edit' screen which 
just has the editable fields for simple properties and is a 
continuation.

HTH

regards Jeremy

/*
  * edit a bean defined by the ID (args.id) and TYPE (args.type)
  * externally called function "edit/[type]/[id]"
  * if args.id is empty, get the user to choose a bean to work on
  * currently throws exceptions to the top
  */
function edit(args){
	var bean = null;
	if ("".equals(args.id)) {
		// chooseBean is a continuation to navigate and choose a Bean
		bean = chooseBean(args, "Please choose a " + args.type + " to start 
work on.");
		if (bean == null) {
			redirect("../../start");
			return;
		} else {
			args.id = bean.id;
			bean.userMessage = "Ready to Edit " + bean.name + ".";
		}
	} else {
		bean = getBean(args);
		bean.userMessage = "Ready to Edit " + bean.name + ".";
		if (cocoon.request.getParameter(Constants.SAVEACTION)) {
			saveThisBean(bean, args);
		}
	}
	editThisBean(bean, args);
}	

/*
  * present an editing screen
  * currently throws exceptions to the top
  * part of the screen is a form to save changes to the local 
properties, submitted to self
  */
function editThisBean(bean, args) {
	fetchSession().refresh(bean); // function used in situations where the 
HSession may be closed
	var editable = {
		bean : bean,
		formaction: "edit/" + args.type + "/" + args.id,
		constants : Constants
	};
	sendPage("screen/" + args.type + "/edit", editable);
}

/*
  * gives you a form to add a new coverage
  * currently throws exceptions to the top
  */
function addCoverage(args) {
	var bean = getBean(args);
	var editable = {
		bean : { name : "", description : "", userMessage : "Ready to Add a 
child to " + bean.name},
		errors : { name : "", description : "", count: 0 },
		formaction : "edit/" + args.type + "/" + args.id,    // my 
continuation is in a ReqParam
		constants : Constants
	};
	while (true) {
		sendPageAndWait("screen/coverage/details", editable);
		editable.bean.name = cocoon.request.getParameter(Constants.NAMEFIELD);
		editable.bean.description = 
cocoon.request.getParameter(Constants.DESCFIELD);
		if (cocoon.request.getParameter(Constants.CANCELACTION)) {
			bean.setUserMessage("Addition cancelled by User");
			break;
		}
		// this validation should be elsewhere ideally
		if ("".equals(editable.bean.name)) {
			editable.errors.name = "Please fill in the name properly";
			editable.errors.count++;
		}
		if ("".equals(editable.bean.description)) {
			editable.errors.description = "Please fill in the description 
properly";
			editable.errors.count++;
		}
		if (editable.errors.count > 0) {
			editable.bean.userMessage = "There were problems [" + 
editable.errors.count + "], please try again";
			editable.errors.count = 0;
		} else { // no errors
			// chooseType is a continuation to navigate and choose a Type Bean
			var type = chooseType(args, "Choose a Type for: " + 
editable.bean.name);
			if (type == null) {
				bean.setUserMessage("Addition cancelled by User");
				break;
			} else {
				fetchSession().refresh(bean); // the Session had been closed
				var child = new Packages.org.iniva.archive.Coverage();
				// this property setting should be moved elsewhere too .....
				child.setName(editable.bean.name);
				child.setDescription(editable.bean.description);
				child.setType(type);
				child.setParent(bean);
				bean.addChild(child);
				saveBean(child);
				bean.userMessage = child.name + " successfully saved.";
				break;
			}
		}
	} // end while
	editThisBean(bean, args);
}

/*
  * Return a Bean persisted by Hibernate, using the ID and class
  * Throws Hibernate Exceptions
  */
function getBean (args) {
	var sess = fetchSession(); // calls a static method in the Persistence 
Filter
	var bean = new Packages[args.klass]();
	var transaction = null;
	try {
		transaction = sess.beginTransaction();
		sess.load(bean, new java.lang.Long(args.id));
		transaction.commit();
		return bean;
	} catch (e) {
		rollBack(transaction);
		throw e;
	}
}

/*
  * Persist a Bean using Hibernate
  * Throws Hibernate Exceptions
  */
function saveBean(bean) {
	var sess = fetchSession();
	var transaction = null;
	try {
		transaction = sess.beginTransaction();
		sess.saveOrUpdate(bean);
		transaction.commit();
	} catch (e) {
		rollBack(transaction);
		throw e;
	}
}


Mime
View raw message