db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Helge Weissig <hel...@grajagan.org>
Subject BaseMyTable.getFieldNames() replacement?
Date Wed, 12 Apr 2006 16:53:19 GMT
Hi,

	the version of Torque I am upgrading from (too embarrassed to tell)  
used to put a method getFieldNames() into the generated Base classes.  
I have re-written this as below to be generic, but to my chagrin,  
have come to find out a substantial difference in behaviour: the old  
method would return a list of field names in the order that they were  
present in the schema.xml file. Unfortunately, we have relied on this  
order for our presentation layer, where we want the fields to be in a  
given order.

So here are my questions/comments:
1) is there a better way to do this? For example, do I really need to  
go through the BasePeer class (my table maps came up empty without  
that and try as I might, I only found other posts of people likewise  
frustrated by this problem, but no solution that worked for me).
2) more philosophically, in an MVC approach, should one avoid the  
assumption that was made here? In other words, should the  
presentation layer, in our case the Velocity template, decide on the  
column sort order, rather than the data model layer (the schema file)?

thanks for any input! I hope the code below may be useful to others!

cheers,
h.

	// we are passed a table name and are supposed to return
	// a sorted list of columns. This gets it done, except for the
	// sorting part :(
	public static List getFieldNames(String tableName) {
		List l = new Vector();
		String peerName = toPeerClassName(tableName);
		tableName = camelCaseToUnderBars(tableName);
		log.debug("returning field names for " + tableName);

		DatabaseMap dbMap = null;

		/*
		 * We are using reflection to get the peer for this table. From it, we
		 * use the getMapBuilder method since Torque.getMapBuilder() does not
		 * work in all cases (peers need to have been accessed at least  
once for
		 * it to work)
		 */
		try {
			Class peer = Class.forName(peerName);
			Method m = peer.getMethod("getMapBuilder", (Class[]) null);
			MapBuilder mBuilder = (MapBuilder) m.invoke(peer, (Object[]) null);
			dbMap = mBuilder.getDatabaseMap();
		} catch (ClassNotFoundException e1) {
			log.error("cannot get peer class for " + peerName, e1);
		} catch (SecurityException e) {
			log.error("not allowed to get the method getMapBuilder!", e);
		} catch (NoSuchMethodException e) {
			log.error(peerName + " does not define getMapBuilder", e);

			// none of the next three should ever happen
		} catch (IllegalArgumentException ignored) {
		} catch (IllegalAccessException ignored) {
		} catch (InvocationTargetException ignored) {

			// this is really a TorqueException thrown by getMapBuilder
		} catch (Exception e) {
			log.error("Cannot get a databse map for " + tableName, e);
		}

		log.debug("got db map " + dbMap.getName());

		TableMap tableMap = null;
		if (dbMap.containsTable(tableName)) {
			tableMap = dbMap.getTable(tableName);
		} else {
			log.error("Table map for " + tableName + " does not exist!");
		}

		ColumnMap[] cmap = tableMap.getColumns();

		// field names look like java classes: CapitalizedCamelWords
		for (int i = 0; i < cmap.length; i++) {
			String fname = cmap[i].getColumnName().toLowerCase();
			fname = toJavaFieldName(fname);
			fname = StringUtils.capitalize(fname);
			l.add(fname);
		}
		return l;
	}


---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


Mime
View raw message