Source Code Cross Referenced for WRowCrudPage.java in  » Database-ORM » SimpleORM » simpleorm » simplewebapp » eg » token » 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 » Database ORM » SimpleORM » simpleorm.simplewebapp.eg.token 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package simpleorm.simplewebapp.eg.token;
002:
003:        import simpleorm.simplewebapp.core.*;
004:        import simpleorm.simplewebapp.eg.dbute.WCreateDB;
005:        import simpleorm.simplewebapp.scalarFields.WFieldString;
006:
007:        import java.sql.Connection;
008:        import java.sql.DatabaseMetaData;
009:        import java.sql.ResultSet;
010:        import java.sql.PreparedStatement;
011:        import java.util.LinkedHashMap;
012:
013:        public class WRowCrudPage extends WPage {
014:
015:            public final WRowCrudPage.WCrudPagelet pagelet = new WRowCrudPage.WCrudPagelet(
016:                    this );
017:
018:            Connection con;
019:
020:            protected void onInitialize() throws Exception {
021:                con = WCreateDB.openJdbc();
022:            }
023:
024:            protected void onFinalize() throws Exception {
025:                if (con != null)
026:                    con.close();
027:            }
028:
029:            public static class WCrudPagelet extends WPagelet {
030:
031:                final WButton update = addNewButton("Update");
032:                final WButton delete = addNewButton("Delete");
033:                final WButton add = addNewButton("Add");
034:
035:                String table_name = "RSA_USER";
036:
037:                WFieldGroup identity = addGroup(this , new WFieldGroup(
038:                        "user.identity"));
039:
040:                WFieldGroup details = addGroup(this , new WFieldGroup(
041:                        "user.details"));
042:                LinkedHashMap<String, WField> colFields = new LinkedHashMap();
043:                WFieldString keyField = null;
044:
045:                LinkedHashMap<String, String> row = new LinkedHashMap(); // fieldname --> value
046:
047:                public WCrudPagelet(WPage wpage) {
048:                    super (wpage, "crud");
049:                }
050:
051:                Connection getCon() {
052:                    return ((WRowCrudPage) getPage()).con;
053:                }
054:
055:                protected void onInitialize() throws Exception {
056:                    createFields();
057:                }
058:
059:                /**
060:                 * Disables buttons before fields are validated.
061:                 * Needs to retrieve the row here before field validators in order
062:                 * to be able to set the buttons correctly.
063:                 * (The messy case is when a user attempts to add a row, enters
064:                 * an id, but has validation errors.)
065:                 */
066:                protected void onPreValidate() throws Exception {
067:                    String theKey = keyField.getText(); // getValue() not available yet.
068:                    if (theKey != null)
069:                        row = retrieveRow(theKey);
070:                    if (row == null) {
071:                        update.setDisabled(true);
072:                        delete.setDisabled(true);
073:                    } else
074:                        add.setDisabled(true);
075:                }
076:
077:                public void onNotSubmitted() { // ie. initial display of form
078:                    // Fetch data here
079:                    String theId = keyField.getValue();
080:                    if (theId != null) {
081:                        for (String col : colFields.keySet()) {
082:                            colFields.get(col).setValue(row.get(col));
083:                        }
084:                    }
085:                }
086:
087:                public void onWasSubmitted() throws Exception { // ie. after a post back
088:                    String key = keyField.getValue();
089:                    if (getSubmitButton() == update || getSubmitButton() == add) {
090:                        boolean add = row == null;
091:                        if (row == null)
092:                            row = new LinkedHashMap();
093:                        for (String colField : colFields.keySet()) {
094:                            row.put(colField, (String) colFields.get(colField)
095:                                    .getValue());
096:                        }
097:                        if (add)
098:                            addRow(row);
099:                        else
100:                            updateRow(key, row);
101:                    } else if (getSubmitButton() == delete) {
102:                        if (row != null)
103:                            deleteRow(key);
104:                    }
105:                }
106:
107:                void createFields() throws Exception {
108:                    /// Read meta data to define WFields dynamically
109:                    DatabaseMetaData dbmd = getCon().getMetaData();
110:                    ResultSet cols = dbmd.getColumns(null, // ## allTables.getString("TABLE_CAT"),
111:                            null, // ##allTables.getString("TABLE_SCHEM"),
112:                            table_name, null);
113:                    while (cols.next()) {
114:                        String colName = cols.getString("COLUMN_NAME");
115:                        WFieldGroup fgroup = keyField == null ? identity
116:                                : details;
117:                        WFieldString fld = new WFieldString(colName);
118:                        if (keyField == null)
119:                            keyField = fld;
120:                        colFields.put(colName, addField(fgroup, fld));
121:                    }
122:                    cols.close();
123:                    if (keyField == null)
124:                        throw new WValidationException(
125:                                "No columns, maybe need to initialize database "
126:                                        + table_name, null);
127:                }
128:
129:                LinkedHashMap retrieveRow(String keyValue) throws Exception {
130:                    PreparedStatement ps = getCon().prepareStatement(
131:                            "SELECT * FROM " + table_name + " WHERE \""
132:                                    + keyField.getDataName() + "\" = ?");
133:                    ps.setString(1, keyValue);
134:                    ResultSet rs = ps.executeQuery();
135:
136:                    if (!rs.next())
137:                        return null;
138:
139:                    LinkedHashMap row = new LinkedHashMap();
140:                    for (String colField : colFields.keySet()) {
141:                        row.put(colField, rs.getString(colField));
142:                    }
143:
144:                    if (rs.next())
145:                        throw new WValidationException(
146:                                "Primary key not unique " + table_name + " : "
147:                                        + keyValue, keyField);
148:
149:                    rs.close();
150:                    ps.close();
151:                    return row;
152:                }
153:
154:                void updateRow(String keyValue,
155:                        LinkedHashMap<String, String> row) throws Exception {
156:                    String set = "";
157:                    for (String col1 : row.keySet()) {
158:                        if (!"".equals(set))
159:                            set += ", ";
160:                        set += "\"" + col1 + "\" = ?";
161:                    }
162:                    PreparedStatement ps = getCon().prepareStatement(
163:                            "UPDATE " + table_name + " SET " + set
164:                                    + " WHERE \"" + keyField.getDataName()
165:                                    + "\" = ?");
166:                    int fld = 1;
167:                    for (String col2 : row.keySet()) {
168:                        ps.setString(fld++, row.get(col2));
169:                    }
170:                    ps.setString(fld++, keyValue);
171:
172:                    ps.executeUpdate();
173:                    ps.close();
174:                }
175:
176:                void addRow(LinkedHashMap<String, String> row) throws Exception {
177:                    String insert = "", values = "";
178:                    for (String col1 : row.keySet()) {
179:                        if (!"".equals(insert)) {
180:                            insert += ", ";
181:                            values += ", ";
182:                        }
183:                        insert += "\"" + col1 + "\"";
184:                        values += "?";
185:                    }
186:                    PreparedStatement ps = getCon().prepareStatement(
187:                            "INSERT INTO " + table_name + " (" + insert
188:                                    + ") VALUES (" + values + ")");
189:                    int fld = 1;
190:                    for (String col3 : row.keySet()) {
191:                        ps.setString(fld++, row.get(col3));
192:                    }
193:                    ps.executeUpdate();
194:                    ps.close();
195:                }
196:
197:                void deleteRow(String keyValue) throws Exception {
198:                    PreparedStatement ps = getCon().prepareStatement(
199:                            "DELETE FROM " + table_name + " WHERE \""
200:                                    + keyField.getDataName() + "\" = ?");
201:                    ps.setString(1, keyValue);
202:
203:                    ps.executeUpdate();
204:                    ps.close();
205:                }
206:
207:            }
208:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.