Source Code Cross Referenced for BeanDataStore.java in  » J2EE » Sofia » com » salmonllc » sql » 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 » J2EE » Sofia » com.salmonllc.sql 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        //** Copyright Statement ***************************************************
0002:        //The Salmon Open Framework for Internet Applications (SOFIA)
0003:        // Copyright (C) 1999 - 2002, Salmon LLC
0004:        //
0005:        // This program is free software; you can redistribute it and/or
0006:        // modify it under the terms of the GNU General Public License version 2
0007:        // as published by the Free Software Foundation;
0008:        // 
0009:        // This program is distributed in the hope that it will be useful,
0010:        // but WITHOUT ANY WARRANTY; without even the implied warranty of
0011:        // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012:        // GNU General Public License for more details.
0013:        // 
0014:        // You should have received a copy of the GNU General Public License
0015:        // along with this program; if not, write to the Free Software
0016:        // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
0017:        // 
0018:        // For more information please visit http://www.salmonllc.com
0019:        //** End Copyright Statement ***************************************************
0020:        package com.salmonllc.sql;
0021:
0022:        /////////////////////////
0023:        //$Archive: /SOFIA/SourceCode/com/salmonllc/sql/BeanDataStore.java $
0024:        //$Author: Dan $ 
0025:        //$Revision: 21 $ 
0026:        //$Modtime: 11/08/04 10:19a $ 
0027:        /////////////////////////
0028:
0029:        import java.io.Serializable;
0030:        import java.lang.reflect.Constructor;
0031:        import java.lang.reflect.InvocationTargetException;
0032:        import java.lang.reflect.Method;
0033:        import java.math.BigDecimal;
0034:        import java.util.ArrayList;
0035:        import java.util.Collection;
0036:        import java.util.Hashtable;
0037:        import java.util.Iterator;
0038:        import java.util.StringTokenizer;
0039:        import java.util.Vector;
0040:
0041:        import com.salmonllc.util.MessageLog;
0042:        import com.salmonllc.util.Util;
0043:
0044:        /**
0045:         * This class provides an interface between standard java beans and the rest of the framework.<BR>
0046:         * This allows EJBs or other Java Beans to be bound to GUI components so their values can be displayed and manipulated in pages.<BR>
0047:         * This class won't automatically retrieve data as the standard datastore does. This beans must retrieve the data in whatever manner required and passed to one of the insert row methods.<BR>
0048:         * Calling the update method will cause the datastore to copy the values in its internal buffers back to the bean classes instead of the database. The beans themselves must persist the data.<BR>
0049:         * Public bean get methods will automatically be called to move the data from a bean to the datastore buffer when one of the insert methods is called.<BR>
0050:         * public bean set methods will automatically be called to move the data from the datastore buffer to the beans when the update method is called.<BR><BR>
0051:         * Type conversions are as follows:<BR>
0052:         *		String and char and Character bean attributes are converted to DATATYPE_STRING<BR>
0053:         *		Integer and int bean attributes are converted to DATATYPE_INT<BR>
0054:         *		Float and float bean attributes are converted to DATATYPE_FLOAT<BR>
0055:         *		Double and double attributes are converted to DATATYPE_DOUBLE<BR>
0056:         *		Short and short attributes are converted to DATATYPE_SHORT<BR>
0057:         *		Long and long bean attributes are converted to DATATYPE_LONG<BR>
0058:         *		Boolean and boolean bean attributes are converted to DATATYPE_SHORT with 0 being false and non-zero being true<BR>
0059:         *		java.sql.Date and java.util.Date are converted to DATATYPE_DATE<BR>
0060:         *		java.sql.Timestamp is to DATATYPE_DATETIME<BR>
0061:         *		java.sql.Time is to DATATYPE_TIME<BR>
0062:         *		byte[] is converted to DATATYPE_BYTEARRAY<BR>
0063:         *	All other types in the bean (including array types) are ignored.<BR>
0064:         * <BR><BR> 
0065:         * Portions added by<BR>
0066:         * 		Dr Jeffrey J Shifman shifmanjj@superiorcanada.com<BR>
0067:         * 		Superior Electrics Limited http://superiorcanada.com<BR>
0068:         */
0069:        public class BeanDataStore extends DataStoreBuffer {
0070:
0071:            String _extraInfo;
0072:            private int _beanCount = 0;
0073:            public final static int LOAD_ALL = -1;
0074:            private boolean _lowerCaseFirstLetter = false;
0075:
0076:            public static class BeanClassInfo implements  Serializable {
0077:                private Class _beanClass;
0078:                private String _alias;
0079:                private int _index;
0080:
0081:                public String getAlias() {
0082:                    return _alias;
0083:                }
0084:
0085:                public void setAlias(String alias) {
0086:                    _alias = alias;
0087:                }
0088:
0089:                public Class getBeanClass() {
0090:                    return _beanClass;
0091:                }
0092:
0093:                public void setBeanClass(Class beanClass) {
0094:                    _beanClass = beanClass;
0095:                }
0096:
0097:                public int getIndex() {
0098:                    return _index;
0099:                }
0100:
0101:                public void setIndex(int index) {
0102:                    _index = index;
0103:                }
0104:            }
0105:
0106:            public BeanDataStore() {
0107:
0108:            }
0109:
0110:            public BeanDataStore(String beanType) {
0111:                this (beanType, false);
0112:            }
0113:
0114:            public BeanDataStore(String beanType, boolean multiClass) {
0115:                super ();
0116:                try {
0117:                    BeanClassInfo inf[] = getBeanInfo(beanType);
0118:                    for (int i = 0; i < inf.length; i++)
0119:                        addBeanDefinition(inf[i].getBeanClass(),
0120:                                multiClass ? inf[i].getAlias() : null);
0121:                } catch (ClassNotFoundException e) {
0122:                    e.printStackTrace();
0123:                }
0124:            }
0125:
0126:            public BeanDataStore(Class beanType) {
0127:                super ();
0128:                addBeanDefinition(beanType, null);
0129:            }
0130:
0131:            public BeanDataStore(BeanClassInfo inf) {
0132:                super ();
0133:                addBeanDefinition(inf.getBeanClass(), inf.getAlias());
0134:            }
0135:
0136:            public BeanDataStore(BeanClassInfo inf[]) {
0137:                super ();
0138:                for (int i = 0; i < inf.length; i++)
0139:                    addBeanDefinition(inf[i].getBeanClass(), inf[i].getAlias());
0140:            }
0141:
0142:            /**
0143:             * Adds a new bean definition to this components buffer
0144:             * @param beanType The class name for the bean
0145:             * @param alias The name the bean is referenced by
0146:             */
0147:            public void addBeanDefinition(Class beanType, String alias) {
0148:                Method parentGetters[] = new Method[0];
0149:                setUpBuffer(beanType, null, parentGetters, getColumnCount(),
0150:                        alias, _beanCount);
0151:                _beanCount++;
0152:            }
0153:
0154:            /**
0155:             * Classes can override to figure out what type of datatype to map a column to
0156:             * @param beanType
0157:             * @param getMethod
0158:             * @return
0159:             */
0160:            protected int getDataTypeForMethod(Class beanType, Method getMethod) {
0161:                return -1;
0162:            }
0163:
0164:            private int setUpBuffer(Class beanType, String attributeName,
0165:                    Method[] parentGetters, int index, String alias,
0166:                    int beanNumber) {
0167:                DSDataStoreDescriptor desc = getDescriptor();
0168:                Method m[] = beanType.getMethods();
0169:                Class cl[] = new Class[1];
0170:
0171:                outer: for (int i = 0; i < m.length; i++) {
0172:                    String methName = m[i].getName();
0173:                    String name = methName;
0174:                    int dataType = -1;
0175:                    Class returnType = m[i].getReturnType();
0176:
0177:                    if (name.startsWith("get")
0178:                            && m[i].getParameterTypes().length == 0) {
0179:                        if (Util.instanceOf(returnType, Integer.class)
0180:                                || Util.instanceOf(returnType, Integer.TYPE)) {
0181:                            dataType = DATATYPE_INT;
0182:                        } else if (Util.instanceOf(returnType, String.class)) {
0183:                            dataType = DATATYPE_STRING;
0184:                        } else if (Util.instanceOf(returnType, Character.class)
0185:                                || Util.instanceOf(returnType, Character.TYPE)) {
0186:                            dataType = DATATYPE_STRING;
0187:                            returnType = Character.TYPE;
0188:                        } else if (Util.instanceOf(returnType, Float.class)
0189:                                || Util.instanceOf(returnType, Float.TYPE)) {
0190:                            dataType = DATATYPE_FLOAT;
0191:                        } else if (Util.instanceOf(returnType, Double.class)
0192:                                || Util.instanceOf(returnType, Double.TYPE)) {
0193:                            dataType = DATATYPE_DOUBLE;
0194:                        } else if (Util.instanceOf(returnType, Long.class)
0195:                                || Util.instanceOf(returnType, Long.TYPE)) {
0196:                            dataType = DATATYPE_LONG;
0197:                        } else if (Util.instanceOf(returnType, byte[].class)) {
0198:                            dataType = DATATYPE_BYTEARRAY;
0199:                        } else if (Util.instanceOf(returnType, Short.class)
0200:                                || Util.instanceOf(returnType, Short.TYPE)) {
0201:                            dataType = DATATYPE_SHORT;
0202:                        } else if (Util.instanceOf(returnType, Boolean.class)
0203:                                || Util.instanceOf(returnType, Boolean.TYPE)) {
0204:                            dataType = getDescriptor().getBooleanDataType();
0205:                            returnType = Boolean.TYPE;
0206:                        } else if (Util.instanceOf(returnType,
0207:                                java.sql.Timestamp.class)) {
0208:                            dataType = DATATYPE_DATETIME;
0209:                        } else if (Util.instanceOf(returnType,
0210:                                java.sql.Time.class)) {
0211:                            dataType = DATATYPE_TIME;
0212:                        } else if (Util.instanceOf(returnType,
0213:                                java.util.Date.class)) {
0214:                            dataType = DATATYPE_DATE;
0215:                        } else if (Util
0216:                                .instanceOf(returnType, BigDecimal.class)) {
0217:                            dataType = DATATYPE_DOUBLE;
0218:                        } else if (returnType == Object.class) {
0219:                            dataType = getDataTypeForMethod(beanType, m[i]);
0220:                        } else if (!isCollection(returnType)
0221:                                && parentGetters.length < 3
0222:                                && (!name.equals("getClass"))) {
0223:                            Method met = m[i];
0224:                            String nm = name.substring(3);
0225:                            if (attributeName != null) {
0226:                                String test = attributeName;
0227:                                int pos = test.lastIndexOf(".");
0228:                                if (pos > -1)
0229:                                    test = test.substring(pos + 1);
0230:                                if (test.equals(nm))
0231:                                    continue;
0232:                                else
0233:                                    nm = attributeName + "." + nm;
0234:                            }
0235:                            Method newGetters[] = new Method[parentGetters.length + 1];
0236:                            for (int j = 0; j < parentGetters.length; j++) {
0237:                                if (parentGetters[j].equals(met))
0238:                                    continue outer;
0239:                                newGetters[j] = parentGetters[j];
0240:                            }
0241:                            newGetters[newGetters.length - 1] = met;
0242:                            index = setUpBuffer(returnType, nm, newGetters,
0243:                                    index, alias, beanNumber);
0244:                            continue;
0245:                        }
0246:                        name = name.substring(3);
0247:                    } else if (name.startsWith("is")
0248:                            && m[i].getParameterTypes().length == 0
0249:                            && returnType.isAssignableFrom(Boolean.TYPE)) {
0250:                        dataType = getDescriptor().getBooleanDataType();
0251:                        name = name.substring(2);
0252:                    }
0253:
0254:                    if (dataType != -1) {
0255:                        String bucketName = null;
0256:                        if (attributeName != null)
0257:                            bucketName = attributeName + "." + name;
0258:                        else
0259:                            bucketName = name;
0260:                        if (alias != null)
0261:                            bucketName = alias + "." + bucketName;
0262:                        if (_lowerCaseFirstLetter)
0263:                            bucketName = lowerCaseFirstLetter(bucketName);
0264:                        String test = bucketName;
0265:                        int testInc = 0;
0266:                        while (getColumnIndex(test) != -1) {
0267:                            test = bucketName + testInc;
0268:                            testInc++;
0269:                        }
0270:                        bucketName = test;
0271:                        addBucket(bucketName, dataType);
0272:                        DSColumnDescriptor col = desc.getColumn(index);
0273:                        col.setGetMethod(m[i]);
0274:                        col.setPropertyClass(returnType);
0275:                        col.setParentGetMethods(parentGetters);
0276:                        col
0277:                                .setBeanInfo(newBeanInfo(beanType, alias,
0278:                                        beanNumber));
0279:                        cl[0] = returnType;
0280:                        try {
0281:                            Method setMethod = beanType.getMethod("set" + name,
0282:                                    cl);
0283:                            col.setSetMethod(setMethod);
0284:                            col.setUpdateable(true);
0285:                        } catch (Exception e) {
0286:                            col.setUpdateable(false);
0287:                        }
0288:                        index++;
0289:                    }
0290:                }
0291:                return index;
0292:
0293:            }
0294:
0295:            /**
0296:             * Removes the current row from the datastore buffer. 
0297:             * @return True if the row is remove and false if not.
0298:             */
0299:            public synchronized boolean deleteRow() {
0300:                return super .deleteRow();
0301:            }
0302:
0303:            /**
0304:             * Removes the row from the datastore buffer.
0305:             * @return True if the row is remove and false if not.
0306:             */
0307:            public synchronized boolean deleteRow(int row) {
0308:                return super .deleteRow(row);
0309:            }
0310:
0311:            /**
0312:             * Returns the bean at the current row from the standard buffer.
0313:             */
0314:            public Object getBean() throws DataStoreException {
0315:                return getBean(getRow());
0316:            }
0317:
0318:            /**
0319:             * Returns the bean at the specified row from the standard buffer.
0320:             */
0321:            public Object getBean(int row) throws DataStoreException {
0322:                return getBean(row, BUFFER_STANDARD);
0323:
0324:            }
0325:
0326:            /**
0327:             * Returns the bean at the specified row in the specified buffer.
0328:             * Buffer must be BUFFER_STANDARD or BUFFER_FILTERED
0329:             */
0330:            public Object getBean(int row, int buffer)
0331:                    throws DataStoreException {
0332:                Vector v = null;
0333:                if (buffer == BUFFER_STANDARD)
0334:                    v = _rows;
0335:                else if (buffer == BUFFER_FILTERED)
0336:                    v = _filteredRows;
0337:
0338:                if (v == null)
0339:                    throw new DataStoreException(
0340:                            "Buffer must be BUFFER_STANDARD or BUFFER_FILTERED");
0341:
0342:                if (row < 0 || row >= v.size())
0343:                    throw new DataStoreException("Specified row (" + row
0344:                            + ") is out of range.");
0345:
0346:                DSDataRow d = (DSDataRow) v.elementAt(row);
0347:                return d.getBean();
0348:
0349:            }
0350:
0351:            /**
0352:             * Returns all the beans in the stardard buffer as an array of objects.
0353:             */
0354:            public Object[] getBeans() throws DataStoreException {
0355:                return getBeans(BUFFER_STANDARD);
0356:            }
0357:
0358:            /**
0359:             * Returns the beans in the specified buffer as an array of objects.
0360:             * Buffer must be BUFFER_STANDARD or BUFFER_FILTERED
0361:             */
0362:            public Object[] getBeans(int buffer) throws DataStoreException {
0363:                Vector v = null;
0364:                if (buffer == BUFFER_STANDARD)
0365:                    v = _rows;
0366:                else if (buffer == BUFFER_FILTERED)
0367:                    v = _filteredRows;
0368:
0369:                if (v == null)
0370:                    throw new DataStoreException(
0371:                            "Buffer must be BUFFER_STANDARD or BUFFER_FILTERED");
0372:
0373:                Object ret[] = new Object[v.size()];
0374:                for (int i = 0; i < v.size(); i++) {
0375:                    DSDataRow r = (DSDataRow) v.elementAt(i);
0376:                    ret[i] = r.getBean();
0377:                }
0378:
0379:                return ret;
0380:            }
0381:
0382:            /**
0383:             * Returns all the beans in the stardard buffer as a Vector.
0384:             */
0385:            public Vector getBeansAsVector() throws DataStoreException {
0386:                return getBeansAsVector(BUFFER_STANDARD);
0387:            }
0388:
0389:            /**
0390:             * Returns the beans in the specified buffer as a Vector.
0391:             * Buffer must be BUFFER_STANDARD or BUFFER_FILTERED
0392:             */
0393:            public Vector getBeansAsVector(int buffer)
0394:                    throws DataStoreException {
0395:                Object o[] = getBeans(buffer);
0396:                Vector v = new Vector(o.length);
0397:                for (int i = 0; i < o.length; i++)
0398:                    v.addElement(o[i]);
0399:                return v;
0400:            }
0401:
0402:            /**
0403:             * Inserts the bean in the datastore after the last row. If the bean passed is null, this method won't do anything except write an error to the log file.
0404:             */
0405:            public synchronized int insertRow(Object bean) {
0406:                return insertRow(bean, -1);
0407:            }
0408:
0409:            /**
0410:             * Inserts the bean in the datastore after the last row. If the bean passed is null, this method won't do anything except write an error to the log file.
0411:             * @param bean The bean to update
0412:             * @param newBean if the bean represents a new row in the database, set this to true, false for an existing row. This can be used later to in the updateBean method to determine if persisting the bean will require an insert or an update
0413:             */
0414:            public synchronized int insertRow(Object bean, boolean newBean) {
0415:                return insertRow(bean, -1, newBean);
0416:            }
0417:
0418:            /**
0419:             * Inserts the bean in the datastore at the specified row position. If the bean passed is null, this method won't do anything except write an error to the log file.
0420:             */
0421:
0422:            public synchronized int insertRow(Object bean, int pos) {
0423:                return insertRow(bean, pos, false);
0424:            }
0425:
0426:            /**
0427:             * Inserts the bean in the datastore at the specified row position. If the bean passed is null, this method won't do anything except write an error to the log file.
0428:             * @param bean The bean to update
0429:             * @param pos The position in the datastore to place the bean, -1 to place it at the end
0430:             * @param newBean if the bean represents a new row in the database, set this to true, false for an existing row. This can be used later to in the updateBean method to determine if persisting the bean will require an insert or an update
0431:             **/
0432:            public synchronized int insertRow(Object bean, int pos,
0433:                    boolean newBean) {
0434:                if (pos == -1)
0435:                    pos = getRowCount();
0436:                try {
0437:                    if (bean == null)
0438:                        throw new DataStoreException(
0439:                                "Error, the insertRow method cannot accept a null bean");
0440:                } catch (Exception e) {
0441:                    com.salmonllc.util.MessageLog.writeErrorMessage(
0442:                            "insertRow()", e, this );
0443:                }
0444:
0445:                int row = super .insertRow(pos);
0446:                DSDataRow rowObj = getDataRow(row);
0447:
0448:                try {
0449:                    rowObj.populateFromBean(getDescriptor(), bean,
0450:                            newBean ? STATUS_NEW : STATUS_NOT_MODIFIED);
0451:                } catch (Exception e) {
0452:                    com.salmonllc.util.MessageLog.writeErrorMessage(
0453:                            "insertRow()", e, this );
0454:                }
0455:                return row;
0456:            }
0457:
0458:            /**
0459:             * Inserts an array of beans into the datastore at the end of the buffer. Any null beans will be skipped and an error will be written to the log.
0460:             */
0461:            public synchronized void insertRows(Object bean[]) {
0462:                if (bean != null) {
0463:                    for (int i = 0; i < bean.length; i++) {
0464:                        insertRow(bean[i]);
0465:                    }
0466:                }
0467:            }
0468:
0469:            /**
0470:             * Inserts an collection of beans into the datastore at the end of the buffer. All the beans in the collection must be of the same type and any null beans will be skipped and an error will be written to the log.
0471:             */
0472:            public synchronized void insertRows(Collection c) {
0473:                if (c != null)
0474:                    insertRows(c.toArray());
0475:
0476:            }
0477:
0478:            /**
0479:             * Copies all the data changed in the datastore to the respective bean sources
0480:             */
0481:            public synchronized void update() throws Exception {
0482:                update(true);
0483:            }
0484:
0485:            /**
0486:             * Copies all the data changed in the datastore to the respective bean sources. Only resets the DataStoreStatus if you specify
0487:             */
0488:            public void update(boolean resetStatus) throws Exception {
0489:                int count = getRowCount();
0490:                DSDataStoreDescriptor desc = getDescriptor();
0491:                for (int i = 0; i < count; i++) {
0492:                    DSDataRow r = getDataRow(i);
0493:                    int rowStatus = r.getRowStatus();
0494:                    if (rowStatus == STATUS_MODIFIED
0495:                            || rowStatus == STATUS_NEW_MODIFIED) {
0496:                        r.copyDataToBean(desc, resetStatus);
0497:                        beanUpdated(r.getBean(),
0498:                                rowStatus == STATUS_NEW_MODIFIED);
0499:                    }
0500:                    if (!resetStatus)
0501:                        r.setRowStatus(rowStatus);
0502:                }
0503:                for (int i = 0; i < getDeletedCount(); i++) {
0504:                    DSDataRow r = getDataStoreRow(i, BUFFER_DELETED)
0505:                            .getDSDataRow();
0506:                    beanDeleted(r.getBean());
0507:                }
0508:                if (resetStatus)
0509:                    resetStatus();
0510:            }
0511:
0512:            /**
0513:             * An empty method that can be overridden by subclasses to allow for automatic persistance of the data in the bean to the database. Called for new or modified rows from the update method.
0514:             * @param bean The updated data bean
0515:             * @param newBean True for a bean representing an existing row in the database and false for one representing an existing row in the database. Whether or not a bean is considered a new row or not is determined with the newBean argument to the insertRow method.
0516:             **/
0517:            public void beanUpdated(Object bean, boolean newBean)
0518:                    throws Exception {
0519:
0520:            }
0521:
0522:            /**
0523:             * An empty method that can be overridden by subclasses to allow for automatic deletion of the data in the bean to the database. Called for deleted rows from the update method.
0524:             * @param bean The bean deleted from the DataStore
0525:             */
0526:            public void beanDeleted(Object bean) {
0527:
0528:            }
0529:
0530:            /**
0531:             * This method will instantiate the a bean datastore described in the className field. The class must extend BeanDataStore and must have at least one of four constructors: One that takes a class (class name of the bean), one that takes a class name and an extraArgs string parm, one that takes just a String (extraInfo parm), or a no-args constructor.
0532:             * @param className The class name for the BeanDataStore subclass to construct
0533:             * @param beanName The class name of the bean this BeanDataStore will use
0534:             * @param extraInfo a String with arbitrary info passed to the constructor of the BeanDataStore
0535:             **/
0536:            public static BeanDataStore constructBeanDataStore(
0537:                    String className, String beanName, String extraInfo) {
0538:                BeanDataStore ret = null;
0539:                try {
0540:                    Class stringClass = new String().getClass();
0541:                    Class classClass = Class.forName("java.lang.Class");
0542:                    Class beanInfoArrayClass = new BeanClassInfo[0].getClass();
0543:                    Class beanInfoClass = newBeanInfo(null, null, -1)
0544:                            .getClass();
0545:
0546:                    if (className == null && beanName != null)
0547:                        className = beanName;
0548:                    Class cl = Class.forName(className, true, Thread
0549:                            .currentThread().getContextClassLoader());
0550:                    if (!Util.instanceOf(cl, BeanDataStore.class))
0551:                        return null;
0552:                    BeanClassInfo beanInf[] = null;
0553:                    if (beanName != null)
0554:                        beanInf = getBeanInfo(beanName);
0555:
0556:                    Constructor cs[] = cl.getConstructors();
0557:                    Constructor classOnlyCon = null;
0558:                    Constructor classAndExtraInfoCon = null;
0559:                    Constructor noArgsCon = null;
0560:                    Constructor extraInfoOnlyCon = null;
0561:                    Constructor beanInfoArrayCon = null;
0562:                    Constructor beanInfoCon = null;
0563:                    for (int i = 0; i < cs.length; i++) {
0564:                        Class[] args = cs[i].getParameterTypes();
0565:                        if (args.length == 2) {
0566:                            if (args[0] == classClass && args[1] == stringClass)
0567:                                classAndExtraInfoCon = cs[i];
0568:                            else if (args[0] == beanInfoArrayClass
0569:                                    && args[1] == stringClass)
0570:                                beanInfoArrayCon = cs[i];
0571:                            else if (args[0] == beanInfoClass
0572:                                    && args[1] == stringClass)
0573:                                beanInfoCon = cs[i];
0574:                        } else if (args.length == 1) {
0575:                            if (args[0] == classClass)
0576:                                classOnlyCon = cs[i];
0577:                            else if (args[0] == stringClass)
0578:                                extraInfoOnlyCon = cs[i];
0579:
0580:                        } else if (args.length == 0) {
0581:                            noArgsCon = cs[i];
0582:                        }
0583:                    }
0584:
0585:                    if (beanInf == null && extraInfo == null
0586:                            && noArgsCon != null) {
0587:                        Object[] args = new Object[0];
0588:                        ret = (BeanDataStore) noArgsCon.newInstance(args);
0589:                    } else if (beanInf == null && extraInfo != null
0590:                            && extraInfoOnlyCon != null) {
0591:                        Object[] args = new String[1];
0592:                        args[0] = extraInfo;
0593:                        ret = (BeanDataStore) extraInfoOnlyCon
0594:                                .newInstance(args);
0595:                    } else if (beanInf != null && beanInfoArrayCon != null) {
0596:                        Object[] args = new Object[2];
0597:                        args[0] = beanInf;
0598:                        args[1] = extraInfo;
0599:                        ret = (BeanDataStore) beanInfoArrayCon
0600:                                .newInstance(args);
0601:                    } else if (beanInf != null && beanInfoCon != null) {
0602:                        Object[] args = new Object[2];
0603:                        args[0] = beanInf[0];
0604:                        args[1] = extraInfo;
0605:                        ret = (BeanDataStore) beanInfoCon.newInstance(args);
0606:                    } else if (beanInf != null && extraInfo == null
0607:                            && classOnlyCon != null) {
0608:                        Object[] args = new Class[1];
0609:                        args[0] = beanInf[0].getBeanClass();
0610:                        ret = (BeanDataStore) classOnlyCon.newInstance(args);
0611:                    } else {
0612:                        if (classAndExtraInfoCon != null) {
0613:                            Object[] args = new Object[2];
0614:                            args[0] = (beanInf == null ? null : beanInf[0]
0615:                                    .getBeanClass());
0616:                            args[1] = extraInfo;
0617:                            ret = (BeanDataStore) classAndExtraInfoCon
0618:                                    .newInstance(args);
0619:                        } else if (classOnlyCon != null) {
0620:                            Object[] args = new Class[1];
0621:                            args[0] = (beanInf == null ? null : beanInf[0]
0622:                                    .getBeanClass());
0623:                            ret = (BeanDataStore) classOnlyCon
0624:                                    .newInstance(args);
0625:                        } else if (extraInfoOnlyCon != null) {
0626:                            Object[] args = new String[1];
0627:                            args[0] = extraInfo;
0628:                            ret = (BeanDataStore) extraInfoOnlyCon
0629:                                    .newInstance(args);
0630:                        } else if (noArgsCon != null) {
0631:                            Object[] args = new Object[0];
0632:                            ret = (BeanDataStore) noArgsCon.newInstance(args);
0633:                        }
0634:                    }
0635:                } catch (Exception e) {
0636:                    MessageLog.writeErrorMessage(
0637:                            "BeanDataStore.constructDataStore()", e, null);
0638:                } catch (Error e) {
0639:                    MessageLog.writeErrorMessage(
0640:                            "BeanDataStore.constructDataStore()", e, null);
0641:                }
0642:                return ret;
0643:            }
0644:
0645:            /**
0646:             * returns the extra info string. The string can be any arbitrary piece of information used by subclasses to construct themselves.
0647:             */
0648:            public String getExtraInfo() {
0649:                return _extraInfo;
0650:            }
0651:
0652:            /**
0653:             * Sets the extra info string. The string can be any arbitrary piece of information used by subclasses to construct themselves.
0654:             */
0655:            public void setExtraInfo(String string) {
0656:                _extraInfo = string;
0657:            }
0658:
0659:            /**
0660:             * Build a criteria string for the specified row in the datastore. This is an empty method that subclasses can implement to allow custom bean datastores to retrieve data.
0661:             */
0662:            public String buildCriteriaStringForRow(int row) {
0663:                return null;
0664:            }
0665:
0666:            /**
0667:             * Retrieves the data in the DataStore by passing a criteria string built with buildCriteriaStringBuild. This is an empty method that subclasses can implement to allow custom bean datastores to retrieve data based on a selection criteria string.
0668:             */
0669:            public void retrieve(String criteriaString) throws Exception {
0670:                return;
0671:            }
0672:
0673:            /**
0674:             * Refresh the data row from the data in the bean.
0675:             * Data is not retrieved from physical storage unless the bean is an Entity bean.
0676:             * @param dataRow
0677:             * @param bean
0678:             * @throws DataStoreException
0679:             */
0680:            public void refreshInternal(DSDataRow dataRow, Object bean)
0681:                    throws DataStoreException {
0682:                dataRow.populateFromBean(getDescriptor(), bean,
0683:                        DataStoreBuffer.STATUS_NOT_MODIFIED);
0684:            }
0685:
0686:            /**
0687:             * Refresh the given data rows from the data in the given beans.
0688:             * Data is not retrieved from physical storage unless the bean is an Entity bean.
0689:             * @param dataRows
0690:             * @param beans
0691:             * @throws DataStoreException
0692:             */
0693:            public void refreshInternal(Collection dataRows, Collection beans)
0694:                    throws DataStoreException {
0695:                Iterator iterDataRows = dataRows.iterator();
0696:                Iterator iterBeans = beans.iterator();
0697:
0698:                while (iterDataRows.hasNext() && iterBeans.hasNext())
0699:                    refreshInternal((DSDataRow) iterDataRows.next(), iterBeans
0700:                            .next());
0701:            }
0702:
0703:            /**
0704:             * Partially reloads the datastore buffer from the bean. Only columns name starting with prefix will be reloaded.
0705:             * @param row the row to refresh from the bean
0706:             * @param elementPrefix the column prefix to search for
0707:             * @throws DataStoreException
0708:             */
0709:            public void partialRefreshInternal(int row, String elementPrefix)
0710:                    throws DataStoreException {
0711:                DSDataRow dataRow = getDataRow(row);
0712:                dataRow.populateFromBean(getDescriptor(), dataRow.getBean(),
0713:                        getRowStatus(row), elementPrefix);
0714:            }
0715:
0716:            /**
0717:             * Copy changed data in the specified datastore row to its bean source.
0718:             * Data is not copied back to physical storage unless the bean is an Entity bean.
0719:             * Return true if an update was required.
0720:             * @param dataRow
0721:             * @return
0722:             * @throws DataStoreException
0723:             */
0724:            public synchronized boolean updateInternal(DSDataRow dataRow)
0725:                    throws DataStoreException {
0726:                if (isModified(dataRow)) {
0727:                    dataRow.copyDataToBean(getDescriptor());
0728:                    return true;
0729:                } else
0730:                    return false;
0731:            }
0732:
0733:            /**
0734:             * Copy changed data in the specified datastore row to its bean source.
0735:             * Data is not copied back to physical storage unless the bean is an Entity bean.
0736:             * Return true if an update was required.
0737:             * @param row
0738:             * @return
0739:             * @throws DataStoreException
0740:             */
0741:            public synchronized boolean updateInternal(int row)
0742:                    throws DataStoreException {
0743:                return updateInternal(getDataRow(row));
0744:            }
0745:
0746:            /**
0747:             * Copy changed data in the specified datastore row to its bean source.
0748:             * Data is not copied back to physical storage unless the beans are Entity bean.
0749:             * Return true if an update was required.
0750:             * @param dataRows
0751:             * @return
0752:             * @throws DataStoreException
0753:             */
0754:            public synchronized boolean updateInternal(Collection dataRows)
0755:                    throws DataStoreException {
0756:                try {
0757:                    Iterator iter = dataRows.iterator();
0758:                    boolean result = false;
0759:                    while (iter.hasNext())
0760:                        result = updateInternal((DSDataRow) iter.next())
0761:                                || result;
0762:
0763:                    return result;
0764:                } catch (Exception e) {
0765:                    throw new DataStoreException("Unable to update data.", e);
0766:                }
0767:            }
0768:
0769:            /**
0770:             * Insert the given beans into the given dataRows.
0771:             * Data is not copied back to physical storage unless the beans is an Entity bean.
0772:             * @param beans
0773:             * @param dataRows
0774:             * @throws DataStoreException
0775:             */
0776:            public void insertInternal(Collection beans, Collection dataRows)
0777:                    throws DataStoreException {
0778:
0779:                try {
0780:                    DSDataStoreDescriptor d = getDescriptor();
0781:                    Iterator iterDataRows = dataRows.iterator();
0782:                    Iterator iterBeans = beans.iterator();
0783:
0784:                    while (iterDataRows.hasNext() && iterBeans.hasNext()) {
0785:                        DSDataRow dataRow = (DSDataRow) iterDataRows.next();
0786:                        Object bean = iterBeans.next();
0787:                        dataRow.populateFromBean(d, bean,
0788:                                DataStoreBuffer.STATUS_NOT_MODIFIED);
0789:                    }
0790:                } catch (Exception e) {
0791:                    throw new DataStoreException(
0792:                            "Unable to copy data back from physical storage.",
0793:                            e);
0794:                }
0795:            }
0796:
0797:            /**
0798:             * Refresh the data for the given rows. 
0799:             * @param row
0800:             * @param findBean - bean containing the findMethod
0801:             * @param findMethod
0802:             * @param findParams - parameters of the findMethod
0803:             * @throws DataStoreException
0804:             */
0805:            public void refresh(Collection dataRows, Object findBean,
0806:                    Method findMethod) throws DataStoreException {
0807:                Collection beans = getBeans(dataRows);
0808:                try {
0809:                    if (findMethod != null) {
0810:                        Object[] findParams = { beans };
0811:                        Collection newBeans = (Collection) findMethod.invoke(
0812:                                findBean, findParams);
0813:                        refreshInternal(dataRows, newBeans);
0814:                    }
0815:                } catch (Exception e) {
0816:                    throw new DataStoreException(
0817:                            "Unable to refresh the bean data from physical storage.",
0818:                            e);
0819:                }
0820:            }
0821:
0822:            /**
0823:             * Retrieve bean data for the specified data rows.
0824:             * @param dataRow
0825:             * @param findBean - bean containing the findMethod
0826:             * @param findMethod
0827:             * @param findParams - parameters of the findMethod
0828:             * @throws DataStoreException
0829:             */
0830:            public Collection find(Collection dataRows, Object findBean,
0831:                    Method findMethod) throws DataStoreException {
0832:                Collection beans = getBeans(dataRows);
0833:                try {
0834:                    Object[] storeParams = { beans };
0835:                    if (findMethod != null) {
0836:                        Collection result = (Collection) findMethod.invoke(
0837:                                findBean, storeParams);
0838:                        return result;
0839:                    } else
0840:                        return null;
0841:                } catch (Exception e) {
0842:                    throw new DataStoreException(
0843:                            "Unable to retrieve bean data from physical storage.",
0844:                            e);
0845:                }
0846:            }
0847:
0848:            /**
0849:             * Copy changed data in the specified datastore rows to their bean source.
0850:             * Using the bean's store method and parameters, save the changed data back to 
0851:             * physical storage through the EJB.
0852:             * If the store method is null, do not call the store method.
0853:             * 
0854:             * @param dataRow
0855:             * @param storeBean - bean containing the storeMethod
0856:             * @param storeMethod
0857:             * @param storeParams - parameters of the storeMethod
0858:             * @throws DataStoreException
0859:             */
0860:            public void update(Collection dataRows, Object storeBean,
0861:                    Method storeMethod) throws DataStoreException {
0862:                updateInternal(dataRows);
0863:                Collection beans = getBeans(dataRows);
0864:                try {
0865:                    Object[] storeParams = { beans };
0866:                    if (storeMethod != null) {
0867:                        storeMethod.invoke(storeBean, storeParams);
0868:                    }
0869:                } catch (Exception e) {
0870:                    throw new DataStoreException(
0871:                            "Unable to store bean data back to physical storage.",
0872:                            e);
0873:                }
0874:            }
0875:
0876:            /**
0877:             * Update all modified and inserted rows and optionally refresh the data in those rows
0878:             * if the a find method is given.
0879:             * @throws DataStoreException
0880:             */
0881:            public void update(Object bean, Method create, Method store,
0882:                    Method find) throws DataStoreException {
0883:                Vector updated = new Vector();
0884:                Vector inserted = new Vector();
0885:
0886:                // create collections of updated and inserted rows
0887:                for (int i = 0; i < getRowCount(); i++)
0888:                    if (isModified(i))
0889:                        updated.add(getDataRow(i));
0890:                    else if (isInserted(i))
0891:                        inserted.add(getDataRow(i));
0892:
0893:                // do a bulk update and bulk insert
0894:                update(updated, bean, store);
0895:                insert(inserted, bean, create);
0896:
0897:                // do a bulk refresh
0898:                if (find != null) {
0899:                    refresh(updated, bean, find);
0900:                    refresh(inserted, bean, find);
0901:                }
0902:            }
0903:
0904:            /**
0905:             * Insert changed data in the given data rows to their bean sources.
0906:             * Using the bean's create method and parameters, save the changed data back to 
0907:             * physical storage.
0908:             * If the store method is null, do not call the store method.
0909:             * 
0910:             * @param dataRow
0911:             * @param storeBean - bean containing the storeMethod
0912:             * @param storeMethod
0913:             * @param storeParams - parameters of the storeMethod
0914:             * @throws DataStoreException
0915:             */
0916:            public void insert(Collection dataRows, Object createBean,
0917:                    Method createMethod) throws DataStoreException {
0918:                Iterator iter = dataRows.iterator();
0919:                Vector fieldNames = new Vector();
0920:                Vector fieldValues = new Vector();
0921:
0922:                while (iter.hasNext()) {
0923:                    DSDataRow dataRow = (DSDataRow) iter.next();
0924:                    fieldNames.add(getUpdateableFieldNames(dataRow));
0925:                    fieldValues.add(getUpdateableFieldValues(dataRow));
0926:                }
0927:
0928:                try {
0929:                    Object[] createParams = { fieldNames, fieldValues };
0930:                    if (createMethod != null) {
0931:                        Collection beans = (Collection) createMethod.invoke(
0932:                                createBean, createParams);
0933:                        insertInternal(beans, dataRows);
0934:                    }
0935:                } catch (Exception e) {
0936:                    throw new DataStoreException(
0937:                            "Unable to store bean data back to physical storage.",
0938:                            e);
0939:                }
0940:            }
0941:
0942:            /**
0943:             * Delete the specified datastore row in physical storage.
0944:             * If the store method is null, do not call the store method.
0945:             * 
0946:             * @param row
0947:             * @param deleteBean - bean containing the deleteMethod
0948:             * @param deleteMethod
0949:             * @param deleteParams - parameters of the deleteMethod
0950:             * @throws DataStoreException
0951:             */
0952:            public void delete(int row, Object deleteBean, Method deleteMethod)
0953:                    throws DataStoreException {
0954:                try {
0955:                    Collection beans = new Vector();
0956:                    beans.add(getBean(row));
0957:                    Object[] deleteParams = { beans };
0958:                    if (deleteMethod != null) {
0959:                        if (!isInserted(row))
0960:                            deleteMethod.invoke(deleteBean, deleteParams);
0961:                        deleteRow(row);
0962:                    }
0963:                } catch (Exception e) {
0964:                    throw new DataStoreException(
0965:                            "Unable to delete bean in physical storage.", e);
0966:                }
0967:            }
0968:
0969:            /**
0970:             * Delete a row and its associated bean.
0971:             * If the row has no associated been (because it has just been inserted) then
0972:             * simply remove the row.
0973:             * @param row
0974:             */
0975:            public void deleteRowBean(int row) throws DataStoreException {
0976:                try {
0977:                    Object bean = getBean(row);
0978:                    if (bean != null) {
0979:                        Method remove = bean.getClass().getMethod("remove",
0980:                                null);
0981:                        remove.invoke(bean, null);
0982:                    }
0983:                } catch (Exception e) {
0984:                    throw new DataStoreException(getCause(e).getMessage());
0985:                }
0986:                if (!deleteRow(row))
0987:                    throw new DataStoreException(
0988:                            "Row was deleted on database but the presentation row could not be deleted.");
0989:            }
0990:
0991:            /**
0992:             * Load entity or session bean data.  Session beans must have an interface similar to an entity bean's
0993:             * remote interface.
0994:             * 
0995:             * @param	home - home interface to an entity bean (or session bean equivalent)
0996:             * @param 	method - search method name
0997:             * @param	params - array with search method parameters
0998:             * @param	maxRows - Maximum number of rows to be loaded. 
0999:             * 			If maxRows &eq LOADALL or if omitted then there is no limit to the number of rows.
1000:             * 
1001:             * To change he template for this generated type comment go to
1002:             * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
1003:             */
1004:
1005:            public void load(Object bean, String methodName, Object[] params,
1006:                    int maxRows) throws DataStoreException {
1007:                try {
1008:
1009:                    Method method = bean.getClass().getMethod(methodName,
1010:                            toClasses(params));
1011:                    Collection all = (Collection) method.invoke(bean, params);
1012:
1013:                    Iterator iterator = all.iterator();
1014:
1015:                    // Load all beans		
1016:                    int count = 0;
1017:                    while (iterator.hasNext()) {
1018:                        if (maxRows >= 0)
1019:                            if (++count > maxRows)
1020:                                break;
1021:                        insertRow(iterator.next());
1022:                    }
1023:
1024:                } catch (Exception e) {
1025:                    if (getCause(e) == null)
1026:                        throw new DataStoreException("Error loading data", e);
1027:                    else
1028:                        throw new DataStoreException("Error loading data",
1029:                                getCause(e));
1030:                }
1031:
1032:            }
1033:
1034:            public void load(Object bean, int maxRows)
1035:                    throws DataStoreException {
1036:                load(bean, "findAll", null, maxRows);
1037:            }
1038:
1039:            public void load(Object bean) throws DataStoreException {
1040:                load(bean, LOAD_ALL);
1041:            }
1042:
1043:            /**
1044:             * Refresh this datastore (without performing an update).
1045:             * @throws DataStoreException
1046:             * @throws Exception
1047:             */
1048:            public void reload(Object bean) throws DataStoreException,
1049:                    Exception {
1050:                reset();
1051:                load(bean);
1052:            }
1053:
1054:            /**
1055:             * Convert an array of objects into an array of their classes.
1056:             * @param array
1057:             * @return
1058:             */
1059:            public static Class[] toClasses(Object[] array) {
1060:                if (array == null)
1061:                    return null;
1062:
1063:                Class[] result = new Class[array.length];
1064:                for (int i = 0; i < array.length; i++) {
1065:                    if (array[i] == null)
1066:                        result[i] = null;
1067:                    else
1068:                        result[i] = array[i].getClass();
1069:                }
1070:
1071:                return result;
1072:            }
1073:
1074:            /**
1075:             * Return the collection of beans from the collection of data rows.
1076:             * @param dataRows
1077:             * @return
1078:             */
1079:            public Collection getBeans(Collection dataRows)
1080:                    throws DataStoreException {
1081:
1082:                try {
1083:                    Vector result = new Vector();
1084:                    Iterator iter = dataRows.iterator();
1085:
1086:                    while (iter.hasNext()) {
1087:                        DSDataRow dataRow = (DSDataRow) iter.next();
1088:                        result.add(dataRow.getBean());
1089:                    }
1090:                    return result;
1091:
1092:                } catch (Exception e) {
1093:                    throw new DataStoreException("Error collecting beans.", e);
1094:                }
1095:
1096:            }
1097:
1098:            /**
1099:             * Get the number of updateable columns for the given dataRow.
1100:             * @param dataRow
1101:             * @return
1102:             */
1103:            public int getUpdateableColCount(DSDataRow dataRow) {
1104:                DSDataStoreDescriptor d = getDescriptor();
1105:
1106:                int updateableColCount = 0;
1107:                for (int i = 0; i < d.getColumnCount(); i++) {
1108:                    DSColumnDescriptor col = d.getColumn(i);
1109:                    if (col.isUpdateable())
1110:                        updateableColCount++;
1111:                }
1112:
1113:                return updateableColCount;
1114:            }
1115:
1116:            /**
1117:             * Get the updateable field names for the given DSDataRow.
1118:             * 
1119:             * @param dataRow
1120:             * @throws DataStoreException
1121:             */
1122:            public String[] getUpdateableFieldNames(DSDataRow dataRow)
1123:                    throws DataStoreException {
1124:
1125:                DSDataStoreDescriptor d = getDescriptor();
1126:                int updateableColCount = getUpdateableColCount(dataRow);
1127:                String[] fieldNames = new String[updateableColCount];
1128:
1129:                int colIndex = 0;
1130:                for (int i = 0; i < d.getColumnCount(); i++) {
1131:                    DSColumnDescriptor col = d.getColumn(i);
1132:                    if (col.isUpdateable())
1133:                        fieldNames[colIndex++] = getColumnName(i).toLowerCase();
1134:                }
1135:
1136:                return fieldNames;
1137:            }
1138:
1139:            /**
1140:             * Get the updateable field names for the given DSDataRow.
1141:             * 
1142:             * @param dataRow
1143:             * @throws DataStoreException
1144:             */
1145:            public Object[] getUpdateableFieldValues(DSDataRow dataRow)
1146:                    throws DataStoreException {
1147:
1148:                DSDataStoreDescriptor d = getDescriptor();
1149:                int updateableColCount = getUpdateableColCount(dataRow);
1150:                Object[] fieldValues = new Object[updateableColCount];
1151:
1152:                int colIndex = 0;
1153:                for (int i = 0; i < d.getColumnCount(); i++) {
1154:                    DSColumnDescriptor col = d.getColumn(i);
1155:
1156:                    int type = col.getType();
1157:                    Object o = dataRow.getCurrentData()[i]; // Used to get access to _DSDataRow
1158:                    Class propertyClass = col.getPropertyClass();
1159:
1160:                    if (type == DataStoreBuffer.DATATYPE_STRING) {
1161:                        if (propertyClass == Character.TYPE) {
1162:                            if (o == null)
1163:                                o = new Character(' ');
1164:                            else {
1165:                                String s = (String) o;
1166:                                if (s.length() == 0)
1167:                                    o = new Character(' ');
1168:                                else
1169:                                    o = new Character(s.charAt(0));
1170:                            }
1171:                        }
1172:                    } else if (type == DataStoreBuffer.DATATYPE_SHORT) {
1173:                        if (propertyClass == Boolean.TYPE) {
1174:                            boolean val = false;
1175:                            Short s = (Short) o;
1176:                            if (s != null && s.shortValue() != 0)
1177:                                val = true;
1178:                            o = new Boolean(val);
1179:                        } else if (o == null)
1180:                            o = new Short((short) 0);
1181:                    } else if (o == null && propertyClass.isPrimitive()) {
1182:                        if (type == DataStoreBuffer.DATATYPE_INT)
1183:                            o = new Integer(0);
1184:                        else if (type == DataStoreBuffer.DATATYPE_DOUBLE)
1185:                            o = new Double(0);
1186:                        else if (type == DataStoreBuffer.DATATYPE_FLOAT)
1187:                            o = new Float(0);
1188:                        else if (type == DataStoreBuffer.DATATYPE_LONG)
1189:                            o = new Long(0);
1190:                    }
1191:
1192:                    if (col.isUpdateable())
1193:                        fieldValues[colIndex++] = o;
1194:                }
1195:
1196:                return fieldValues;
1197:            }
1198:
1199:            /**
1200:             * Determine if the given row has been modified.
1201:             * @param dataRow
1202:             * @return
1203:             */
1204:            public boolean isModified(DSDataRow dataRow) {
1205:                int status = dataRow.getRowStatus();
1206:                boolean isModified = status == DataStoreBuffer.STATUS_MODIFIED;
1207:                return isModified;
1208:            }
1209:
1210:            /**
1211:             * Determine if the given row has been modified.
1212:             * @param row
1213:             * @return
1214:             */
1215:            public boolean isModified(int row) {
1216:                return isModified(getDataRow(row));
1217:            }
1218:
1219:            /**
1220:             * Determine if the given row has been inserted.
1221:             * @param dataRow
1222:             * @return
1223:             */
1224:            public boolean isInserted(DSDataRow dataRow) {
1225:                int status = dataRow.getRowStatus();
1226:                boolean isInserted = status == DataStoreBuffer.STATUS_NEW
1227:                        || status == DataStoreBuffer.STATUS_NEW_MODIFIED;
1228:                return isInserted;
1229:            }
1230:
1231:            /**
1232:             * Determine if the given row has been inserted.
1233:             * @param row
1234:             * @return
1235:             */
1236:            public boolean isInserted(int row) {
1237:                return isInserted(getDataRow(row));
1238:            }
1239:
1240:            /**
1241:             * Utility method to get the data store descriptor.
1242:             */
1243:            public DSDataStoreDescriptor getDescriptor() {
1244:                return super .getDescriptor();
1245:            }
1246:
1247:            /**
1248:             * Utility method to retrieve a data row.
1249:             */
1250:            public DSDataRow getDataRow(int rowNo) {
1251:                return super .getDataRow(rowNo);
1252:            }
1253:
1254:            /**
1255:             * Utility method to retrieve an error cause.
1256:             * @param e
1257:             * @return
1258:             */
1259:            public static Throwable getCause(Exception e) {
1260:                if (e instanceof  InvocationTargetException)
1261:                    return Util.getCause(e);
1262:                else
1263:                    return e;
1264:            }
1265:
1266:            /**
1267:             * Returns true if the class is a collection, array, vector or hashtable
1268:             */
1269:            public static boolean isCollection(Class c) {
1270:                if (c.isArray())
1271:                    return true;
1272:                else if (Util.instanceOf(Vector.class, c))
1273:                    return true;
1274:                else if (Util.instanceOf(Hashtable.class, c))
1275:                    return true;
1276:                else if (Util.instanceOf(Collection.class, c))
1277:                    return true;
1278:                else
1279:                    return false;
1280:
1281:            }
1282:
1283:            private static BeanClassInfo[] getBeanInfo(String beanString)
1284:                    throws ClassNotFoundException {
1285:                if (beanString == null)
1286:                    return null;
1287:                int pos = beanString.indexOf(",");
1288:                if (pos > -1) {
1289:                    ArrayList l = new ArrayList();
1290:                    StringTokenizer tok = new StringTokenizer(beanString, ",");
1291:                    int index = 0;
1292:                    while (tok.hasMoreTokens()) {
1293:                        String beanEntry = tok.nextToken();
1294:                        l.add(getInfoForBean(beanEntry, index));
1295:                        index++;
1296:                    }
1297:
1298:                    if (l.size() == 0)
1299:                        return null;
1300:                    else {
1301:                        BeanClassInfo ret[] = new BeanClassInfo[l.size()];
1302:                        l.toArray(ret);
1303:                        return ret;
1304:                    }
1305:                } else {
1306:                    BeanClassInfo inf = getInfoForBean(beanString, 0);
1307:                    BeanClassInfo ret[] = { inf };
1308:                    return ret;
1309:                }
1310:
1311:            }
1312:
1313:            private static BeanClassInfo getInfoForBean(String beanString,
1314:                    int index) throws ClassNotFoundException {
1315:                beanString = beanString.trim();
1316:                int pos = beanString.indexOf(" ");
1317:                String bean = null;
1318:                String alias = null;
1319:                if (pos > -1) {
1320:                    bean = beanString.substring(0, pos);
1321:                    alias = beanString.substring(pos + 1);
1322:                } else {
1323:                    bean = beanString;
1324:                    int ndx = beanString.lastIndexOf(".");
1325:                    if (ndx == -1)
1326:                        alias = bean;
1327:                    else
1328:                        alias = bean.substring(ndx + 1);
1329:                }
1330:                Class c = Class.forName(bean);
1331:                BeanClassInfo info = new BeanClassInfo();
1332:                info.setBeanClass(c);
1333:                info.setAlias(alias);
1334:                info.setIndex(index);
1335:                return info;
1336:            }
1337:
1338:            /**
1339:             * Creates a new BeanInfo object, Framework method, do not call directly
1340:             */
1341:            protected static BeanClassInfo newBeanInfo(Class beanClass,
1342:                    String alias, int index) {
1343:                BeanClassInfo inf = new BeanClassInfo();
1344:                inf.setBeanClass(beanClass);
1345:                inf.setAlias(alias);
1346:                inf.setIndex(index);
1347:                return inf;
1348:            }
1349:
1350:            /**
1351:             * Sets the representation of boolean values in this datastore
1352:             * @param type The DataStore DataType to store booleans as
1353:             * @param trueValue An object representing the true value
1354:             * @param falseValue An object representing the false value
1355:             */
1356:            public void setBooleanDataType(int type, Object trueValue,
1357:                    Object falseValue) {
1358:                getDescriptor().setBooleanRepresentation(type, trueValue,
1359:                        falseValue);
1360:            }
1361:
1362:            protected static String lowerCaseFirstLetter(String st) {
1363:                if (st == null)
1364:                    return st;
1365:                boolean lowerNext = false;
1366:                StringBuffer ret = new StringBuffer(st.length());
1367:                for (int i = 0; i < st.length(); i++) {
1368:                    char c = st.charAt(i);
1369:                    if (lowerNext) {
1370:                        c = Character.toLowerCase(c);
1371:                        lowerNext = false;
1372:                    }
1373:                    if (c == '.')
1374:                        lowerNext = true;
1375:                    ret.append(c);
1376:                }
1377:                return ret.toString();
1378:            }
1379:
1380:            /**
1381:             * True to lower case the first letter of each attribute name. Otherwise it is the same case as the get method.
1382:             */
1383:            public boolean getLowerCaseFirstLetter() {
1384:                return _lowerCaseFirstLetter;
1385:            }
1386:
1387:            /**
1388:             * True to lower case the first letter of each attribute name. Otherwise it is the same case as the get method.
1389:             */
1390:
1391:            public void setLowerCaseFirstLetter(boolean lowerCaseFirstLetter) {
1392:                _lowerCaseFirstLetter = lowerCaseFirstLetter;
1393:            }
1394:
1395:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.