⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 domainobjectfactory.java

📁 利用java反射机制
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     */    public boolean isDiscardUnmodifiedObjects() {        return discardUnmodifiedObjects;    }        /** Setter for property discardUnmodifiedObjects.     * @param discardUnmodifiedObjects New value of property discardUnmodifiedObjects.     *     */    public void setDiscardUnmodifiedObjects(boolean discardUnmodifiedObjects) {        ListIterator li = this.objectList.listIterator();        while (li.hasNext()){            DomainObject domainObject = (DomainObject) li.next();            if (domainObject.getSqlStatus() == domainObject.ORIGINAL){                li.remove();            }        }        this.discardUnmodifiedObjects = discardUnmodifiedObjects;    }        /** Getter for property optimisticLock.     * @return Value of property optimisticLock.     *     */    public char getOptimisticLock() {        return optimisticLock;    }        /** Setter for property optimisticLock.     * @param optimisticLock New value of property optimisticLock.     *     */    public void setOptimisticLock(char optimisticLock) {        this.optimisticLock = optimisticLock;    }        /** Getter for property readOnly.     * @return Value of property readOnly.     *     */    public boolean isReadOnly() {        return readOnly;    }        /** Setter for property readOnly.     * @param readOnly New value of property readOnly.     *     */    public void setReadOnly(boolean readOnly) {        this.readOnly = readOnly;    }        public void setOriginal() {        if (isReadOnly()){            throw new IllegalStateException("Cannot setOriginal() for readOnly object");        }        objectList.add(this.removedObjects);        ListIterator li = objectList.listIterator();        while (li.hasNext()){            DomainObject domainObject = (DomainObject) li.next();            if (domainObject.getSqlStatus() == domainObject.DELETED &&                domainObject.getOriginalValues() != null &&                domainObject.getOriginalValues().getFieldCount() > 0){                domainObject.setSqlStatus(domainObject.ORIGINAL);            }else if (domainObject.getSqlStatus() == domainObject.INSERTED &&                domainObject.getOriginalValues() == null ||                domainObject.getOriginalValues().getFieldCount() < 1){                li.remove();            }else if (domainObject.getSqlStatus() == domainObject.UPDATED &&                domainObject.getOriginalValues() != null &&                domainObject.getOriginalValues().getFieldCount() > 0){                domainObject.setSqlStatus(domainObject.ORIGINAL);            }else{                throw new IllegalArgumentException("Invalid SQL Status");            }        }        removedObjects.clear();    }        /** Getter for property schema.     * @return Value of property schema.     *     */    public java.lang.String getSchema() {        return schema;    }        /** Setter for property schema.     * @param schema New value of property schema.     *     */    public void setSchema(java.lang.String schema) {        this.schema = schema;    }        /** Getter for property table.     * @return Value of property table.     *     */    public java.lang.String getTable() {        return table;    }        /** Setter for property table.     * @param table New value of property table.     *     */    public void setTable(java.lang.String table) {        this.table = table;    }        public boolean writeData() throws Exception{        if (!beforeWriteData()){            return false;        }        if (dataWriter == null){            dataWriter = new DBDataWriter();        }        boolean success = dataWriter.writeData(this);        if (!afterWriteData()){            return false;        }        return success;    }        public boolean beforeWriteData() throws Exception{        return true;    }        public boolean afterWriteData() throws Exception{        return true;    }        /** Getter for property metaData.     * @return Value of property metaData.     *     */        public MetaData getMetaData() {        return this.metaData;    }        public void populateMetaData() {        try{            this.metaData = new MetaData();            this.metaData.populate(getMapping(), this.requestor);            Collection tables = (this.metaData.getTables()).values();            if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::metaData.getTables(): " + tables);            /* Get all distinct Table names for requestor (DomainObject) */            Iterator iter = tables.iterator();            while (iter.hasNext()){                String[] tableId = (String[]) iter.next();                //if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::fullName: " + fullName);                if (this.dbServices.isUseDataSource()){                    PKCache.getInstance().setUseDataSource(true);                    PKCache.getInstance().setDataSourceName(this.dbServices.getDataSourceName());                }else{                    PKCache.getInstance().setUseDataSource(false);                    PKCache.getInstance().setDriverName(this.dbServices.getDriverName());                    PKCache.getInstance().setDbUrl(this.dbServices.getDbUrl());                    PKCache.getInstance().setUserName(this.dbServices.getUserName());                    PKCache.getInstance().setPassword(this.dbServices.getPassword());                }                List pk = PKCache.getInstance().getPrimaryKey(tableId[0], tableId[1], tableId[2]);                if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::pk: " + pk);                            }            if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::tables: " + tables);            if (tables.size() == 1){                //there is only one table for this object, otherwise you must set schema and table to update/delete/insert                setCatalog(this.metaData.getCatalogName(1));                setSchema(this.metaData.getSchemaName(1));                setTable(this.metaData.getTableName(1));                if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::schema: " + getSchema());                if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::tables: " + getTable());            }        }catch (Exception e){            e.printStackTrace();            throw new RuntimeException(e);        }    }        /** Setter for property metaData.     * @param metaData New value of property metaData.     *     */    public void setMetaData(MetaData metaData) {        this.metaData = metaData;    }        /** Getter for property dbServices.     * @return Value of property dbServices.     *     */    public DBServices getDBServices() {        return dbServices;    }        /** Setter for property dbServices.     * @param dbServices New value of property dbServices.     *     */    public void setDBServices(DBServices dbServices) {        this.dbServices = dbServices;    }        /** Getter for property throwOptimisticLockDeleteException.     * @return Value of property throwOptimisticLockDeleteException.     *     */    public boolean isThrowOptimisticLockDeleteException() {        return throwOptimisticLockDeleteException;    }        /** Setter for property throwOptimisticLockDeleteException.     * @param throwOptimisticLockDeleteException New value of property throwOptimisticLockDeleteException.     *     */    public void setThrowOptimisticLockDeleteException(boolean throwOptimisticLockDeleteException) {        this.throwOptimisticLockDeleteException = throwOptimisticLockDeleteException;    }        /** Getter for property throwOptimisticLockUpdateException.     * @return Value of property throwOptimisticLockUpdateException.     *     */    public boolean isThrowOptimisticLockUpdateException() {        return throwOptimisticLockUpdateException;    }        /** Setter for property throwOptimisticLockUpdateException.     * @param throwOptimisticLockUpdateException New value of property throwOptimisticLockUpdateException.     *     */    public void setThrowOptimisticLockUpdateException(boolean throwOptimisticLockUpdateException) {        this.throwOptimisticLockUpdateException = throwOptimisticLockUpdateException;    }        /* IMPORTANT! *************************************************************       Call this method after successeful COMMIT to cync original and current       values: copy current values to original       and set sqlStatus == ORIGINAL and discard Objects deleted from DB     */        public void syncSqlStatus(){        if (isReadOnly()){            throw new IllegalStateException("Cannot syncSqlStatus() for readOnly object");        }        try{            removedObjects.clear();            ListIterator li = objectList.listIterator();            while (li.hasNext()){                boolean copyValues = true;                boolean created = false;                DomainObject domainObject = (DomainObject) li.next();                if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::syncSqlStatus()::DomainObject: " + domainObject + " \n");                if (domainObject.getSqlStatus() == domainObject.DELETED &&                domainObject.getOriginalValues() != null &&                domainObject.getOriginalValues().getFieldCount() > 0){                    li.remove();                    copyValues = false;                }else if (domainObject.getSqlStatus() == domainObject.INSERTED &&                (domainObject.getOriginalValues() == null ||                domainObject.getOriginalValues().getFieldCount() < 1)){                    domainObject.setSqlStatus(domainObject.ORIGINAL);                    created = true;                }else if (domainObject.getSqlStatus() == domainObject.UPDATED &&                domainObject.getOriginalValues() != null &&                domainObject.getOriginalValues().getFieldCount() > 0){                    domainObject.setSqlStatus(domainObject.ORIGINAL);                }else if (domainObject.getSqlStatus() == domainObject.ORIGINAL){                    // do nothing                    continue;                }else{                    throw new IllegalArgumentException("Invalid SQL Status");                }                if (copyValues){ // Sync values in Object and it's originalValues                    DataHolder originalValues = null;                    if (created){                        originalValues = new DataHolder(mapping.size());                    }else{                        originalValues = domainObject.getOriginalValues();                    }                    Iterator iter = mapping.values().iterator();                    int fieldIndex = 0;                    if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::syncSqlStatus()::created: " + created + " \n");                    while(iter.hasNext()){                        fieldIndex ++;                        String fieldName  = (String) iter.next();                                                if (created){                            Method readMethod = this.metaData.getReadMethod(this.metaData.getColumnIndexByFieldName(fieldName));                            //Method readMethod = ((PropertyDescriptor) this.getObjectProperties().get(fieldName)).getReadMethod();                            Object value = readMethod.invoke(domainObject, readArg);                            originalValues.setFieldNameAndValue(fieldIndex, fieldName, value);                        }else{                            if (originalValues.findFieldIndex(fieldName) != -1){                                Method readMethod = this.metaData.getReadMethod(this.metaData.getColumnIndexByFieldName(fieldName));                                                                Object value = readMethod.invoke(domainObject, readArg);                                domainObject.setOriginalValue(fieldName, value);                            }                        }                    }                    if (created){                        domainObject.setOriginalValues(originalValues);                        created = false;                    }                }            }            if (this.dataWriter != null){                this.dataWriter.reset();            }        }catch (Exception e){            throw new RuntimeException(e);        }    }    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -