001: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
002:
003: package jodd.db.orm;
004:
005: import jodd.util.StringUtil;
006: import jodd.util.collection.IntArrayList;
007: import jodd.bean.BeanUtil;
008:
009: import java.util.ArrayList;
010: import java.util.Collection;
011: import java.util.Iterator;
012: import java.util.List;
013:
014: /**
015: * Relation between mapped entities of one row in result set.
016: */
017: public class DbOrmRelation {
018:
019: /**
020: * Relation definition.
021: */
022: protected static class Relation {
023: protected int left;
024: protected int right;
025: protected String onName; // left entity property name for injecting the right entity
026: }
027:
028: /**
029: * All relations.
030: */
031: protected List<Relation> relations = new ArrayList<Relation>();
032: /**
033: * All mapped entities.
034: */
035: protected List<Class> mappedEntities = new ArrayList<Class>();
036: /**
037: * Only root entities.
038: */
039: protected IntArrayList rootEntityNdxs = new IntArrayList();
040:
041: /**
042: * Single mapped entity.
043: */
044: public class MappedEntity {
045:
046: MappedEntity(Class target) {
047: this .target = target;
048: index = mappedEntities.size();
049: mappedEntities.add(target);
050: }
051:
052: protected int index;
053: protected Class target;
054:
055: private String classToPropertyName(Class clazz) {
056: return StringUtil.uncapitalize(clazz.getSimpleName());
057: }
058:
059: public MappedEntity join(Class destination) {
060: return join(destination, classToPropertyName(destination));
061: }
062:
063: /**
064: * Joins a new entity to the current mapped entity.
065: * @see #joinAndMap(Class)
066: */
067: public MappedEntity join(Class destination,
068: String onPropertyName) {
069: joinAndMap(destination, onPropertyName);
070: return this ;
071: }
072:
073: public MappedEntity joinAndMap(Class destination) {
074: return joinAndMap(destination,
075: classToPropertyName(destination));
076: }
077:
078: /**
079: * Joins a new entity and returns it.
080: * @see #join(Class)
081: */
082: public MappedEntity joinAndMap(Class destination,
083: String onPropertyName) {
084: Relation r = new Relation();
085: r.left = index;
086: r.right = mappedEntities.size();
087: r.onName = onPropertyName;
088: relations.add(r);
089: return new MappedEntity(destination);
090: }
091: }
092:
093: /**
094: * Defines root entities.
095: */
096: public MappedEntity map(Class clazz) {
097: MappedEntity me = new MappedEntity(clazz);
098: rootEntityNdxs.add(me.index);
099: return me;
100: }
101:
102: // ---------------------------------------------------------------- pack one
103:
104: @SuppressWarnings({"unchecked"})
105: protected <T> void packCollectionOne(DbOrmQuery query,
106: Collection<T> result) {
107: Class[] entities = mappedEntities
108: .toArray(new Class[mappedEntities.size()]);
109: Iterator<Object[]> it = query.iterate(entities);
110: while (it.hasNext()) {
111: Object[] objs = it.next();
112: result.add((T) joinOne(objs));
113: }
114: }
115:
116: @SuppressWarnings({"unchecked"})
117: protected Object packOne(DbOrmQuery query) {
118: Class[] entities = mappedEntities
119: .toArray(new Class[mappedEntities.size()]);
120: Object[] objs = query.find(entities);
121: return joinOne(objs);
122: }
123:
124: private Object joinOne(Object[] objs) {
125: for (Relation r : relations) {
126: Object left = objs[r.left];
127: if (left != null) {
128: BeanUtil.setProperty(objs[r.left], r.onName,
129: objs[r.right]);
130: }
131: }
132: return objs[rootEntityNdxs.get(0)];
133: }
134:
135: // ---------------------------------------------------------------- pack all
136:
137: protected void packCollectionAll(DbOrmQuery query,
138: Collection<Object[]> result) {
139: Class[] entities = mappedEntities
140: .toArray(new Class[mappedEntities.size()]);
141: Iterator<Object[]> it = query.iterate(entities);
142: while (it.hasNext()) {
143: Object[] objs = it.next();
144: result.add(joinAll(objs));
145: }
146: }
147:
148: protected Object[] packAll(DbOrmQuery query) {
149: Class[] entities = mappedEntities
150: .toArray(new Class[mappedEntities.size()]);
151: Object[] objs = query.find(entities);
152: return joinAll(objs);
153: }
154:
155: private Object[] joinAll(Object[] objs) {
156: for (Relation r : relations) {
157: BeanUtil.setProperty(objs[r.left], r.onName, objs[r.right]);
158: }
159: int rootEntityNdxsSize = rootEntityNdxs.size();
160: Object[] row = new Object[rootEntityNdxsSize];
161: for (int i = 0; i < rootEntityNdxsSize; i++) {
162: row[i] = objs[rootEntityNdxs.get(i)];
163: }
164: return row;
165:
166: }
167:
168: }
|