/*���ܼ�飄1�7
*һЩ����realcix20.classes���»���İ����ᅣ1�7
*ʹ�÷�����
*ֱ�ӵ��ã�������Ϊ��̬������
*/
package realcix20.utils;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.Vector;
import realcix20.classes.basic.BaseClass;
import realcix20.classes.basic.Cell;
import realcix20.classes.basic.Column;
import realcix20.classes.basic.Row;
public class ObjectUtil {
//���ָ��object��parentRow,Ϊ�䴴��һ���µ�childRow������childRow��ص�primary keys��ֵ��
public static Row addnewChildRow(BaseClass object, Row parentRow) {
//Create a newRow
Row templateRow = parentRow;
Row newRow = ObjectUtil.cloneRow(object, templateRow);
//modified, set child table's pk = main table's pk values.
String mainTableName = object.getMainTableName();
String childTableName = object.getChildTableName();
Iterator newCellIter = newRow.getNewCells().iterator();
Iterator oldCellIter = newRow.getOldCells().iterator();
try {
Cell newCell = (Cell)newCellIter.next();
Cell oldCell = (Cell)oldCellIter.next();
if (newCell.getTableName().equals(childTableName)) {
newCell.setColumnValue(null);
oldCell.setColumnValue(null);
}
} catch (Exception e) {
e.printStackTrace();
}
Iterator pkColumnIter = object.getPkColumns().iterator();
while (pkColumnIter.hasNext()) {
Column pkColumn = (Column)pkColumnIter.next();
if (pkColumn.getTableName().equals(mainTableName)) {
Cell childPkOldCell = ObjectUtil.findOldCell(newRow, childTableName, pkColumn.getColumnName());
//found
if (childPkOldCell != null) {
Cell childPkNewCell = ObjectUtil.findNewCell(newRow, childTableName, pkColumn.getColumnName());
Cell mainPkNewCell = ObjectUtil.findNewCell(newRow, mainTableName, pkColumn.getColumnName());
childPkOldCell.setColumnValue(mainPkNewCell.getColumnValue());
childPkNewCell.setColumnValue(mainPkNewCell.getColumnValue());
}
}
}
return newRow;
}
//��ʻ����������clsId,tableName,columnName�Լ�ϵͳ���Դӱ�CLSFIELDSTXT�в�����Ӧ���֡�
public static String findColumnTxt(int clsId, String tableName, String columnName) {
String txt = null;
DAO dao = DAO.getInstance();
dao.query(Resources.SELECT_CLS_FIELDS_TXT_SQL);
dao.setObject(1, clsId);
dao.setObject(2, tableName);
dao.setObject(3, columnName);
dao.setObject(4, Resources.getLanguage());
ResultSet rs = dao.executeQuery();
try {
if (rs.next()) {
txt = rs.getString("LNG");
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
return txt;
}
//���ָ��tableName,columnName��ָ��row�в���newCell��
public static Cell findNewCell(Row row, String tableName, String columnName) {
Cell newCell = null;
Iterator cellIter = row.getNewCells().iterator();
while (cellIter.hasNext()) {
Cell cell = (Cell)cellIter.next();
if ( (cell.getColumnName().equals(columnName)) && (cell.getTableName().equals(tableName)) ) {
newCell = cell;
break;
}
}
return newCell;
}
//���ָ��tableName,columnName��ָ��row�в���oldCell��
public static Cell findOldCell(Row row, String tableName, String columnName) {
Cell oldCell = null;
Iterator cellIter = row.getOldCells().iterator();
while (cellIter.hasNext()) {
Cell cell = (Cell)cellIter.next();
if ( (cell.getColumnName().equals(columnName)) && (cell.getTableName().equals(tableName)) ) {
oldCell = cell;
break;
}
}
return oldCell;
}
//���object,tableName,columnName����column��
public static Column findColumn(BaseClass object, String tableName, String columnName) {
Column rColumn = null;
Iterator columnIter = object.getColumns().iterator();
while (columnIter.hasNext()) {
Column column = (Column)columnIter.next();
if ( (column.getTableName().equals(tableName)) && (column.getColumnName().equals(columnName)) ) {
rColumn = column;
break;
}
}
return rColumn;
}
//���ģ��templateRow��¡��һ����row��
public static Row cloneRow(BaseClass object, Row templateRow) {
Row row = null;
//Clone mainRow
Vector values = (Vector)templateRow.getNewCells().clone();
row = new Row(values, object.getRowType());
//Clone childRows
//fix...
row.getRowSet().getRows().clear();
Iterator templateChildRowIter = templateRow.getRowSet().getRows().iterator();
while (templateChildRowIter.hasNext()) {
Row templateChildRow = (Row)templateChildRowIter.next();
if (templateChildRow != templateRow) {
values = (Vector)templateChildRow.getOldCells().clone();
Row childRow = new Row(values, object.getRowType());
row.getRowSet().getRows().add(childRow);
}
}
return row;
}
}