Source Code Cross Referenced for DbOrmRelation.java in  » Development » jodd » jodd » db » orm » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » jodd » jodd.db.orm 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.