Vector values = new Vector();
for (int i = 1; i < tm.getColumnCount(); i++) {
String tableName = tm.getColumn(i).getTableName();
String columnName = tm.getColumn(i).getColumnName();
Item item = (Item)tm.getValueAt(selectedRowNumber, i);
Cell cell = new Cell(tableName, columnName, item.getFactValue());
values.add(cell);
}
Row selectedRow = new Row(values, container.getCurrentObject().getRowType());
container.getCurrentObject().loadChilds(selectedRow);
Row newRow = ObjectUtil.cloneRow(container.getCurrentObject(), selectedRow);
//set child row need added & set new primary key to childRows
//get new PrimaryKey R.R , RI.R
int newR = 0;
DAO dao = DAO.getInstance();
dao.query(Resources.SELECT_MAX_R_FROM_R_SQL);
ResultSet rs = dao.executeQuery();
try {
if (rs.next())
newR = rs.getInt(1) + 1;
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
Cell oldPrimaryKey1Cell = ObjectUtil.findOldCell(newRow, "R", "R");
oldPrimaryKey1Cell.setColumnValue(newR);
Cell newPrimaryKey1Cell = ObjectUtil.findNewCell(newRow, "R", "R");
newPrimaryKey1Cell.setColumnValue(newR);
Cell oldPrimaryKey2Cell = ObjectUtil.findOldCell(newRow, "RI", "R");
oldPrimaryKey2Cell.setColumnValue(newR);
Cell newPrimaryKey2Cell = ObjectUtil.findNewCell(newRow, "RI", "R");
newPrimaryKey2Cell.setColumnValue(newR);
//set a new childRow equals to newRow
Row newChildRow = null;
Iterator rowIter = newRow.getRowSet().getRows().iterator();
while (rowIter.hasNext()) {
newChildRow = (Row)rowIter.next();
if (newChildRow == newRow) {
break;
}
newChildRow = null;
}
if (newChildRow != null) {
newChildRow = ObjectUtil.cloneRow(getContainer().getCurrentObject(), newRow);
newRow.getRowSet().getRows().add(newChildRow);
}
newRow.getRowSet().getRows().remove(newRow);
Vector needRemoveRows = new Vector();
rowIter = newRow.getRowSet().getRows().iterator();
boolean isLedgerTypeEqualsXVC = false;
boolean isTAmtEqualsZero = false;
while (rowIter.hasNext()) {
Row childRow = (Row)rowIter.next();
String ld = (String)ObjectUtil.findNewCell(childRow, "RI", "LD").getColumnValue();
if (ld != null) {
String lt = LedgerClass.getLTByLD(ld);
if (lt.equals("XVC")) {
needRemoveRows.add(childRow);
isLedgerTypeEqualsXVC = true;
} else {
Double tamt = (Double)ObjectUtil.findNewCell(childRow, "RI", "TAMT").getColumnValue();
if ( (tamt == null) || (tamt == 0.0) ) {
needRemoveRows.add(childRow);
isTAmtEqualsZero = true;
}
}
} else {
needRemoveRows.add(childRow);
}
}
Iterator removedRowsIter = needRemoveRows.iterator();
while (removedRowsIter.hasNext()) {
Row removedRow = (Row)removedRowsIter.next();
newRow.getRowSet().getRows().remove(removedRow);
}
if (isLedgerTypeEqualsXVC) {
DialogManager.showMessageDialog(container, TxtManager.getTxt("INFORMATION.LEDGEREQUALSXVC"));
}
if (isTAmtEqualsZero) {
DialogManager.showMessageDialog(container, TxtManager.getTxt("INFORMATION.TAMTEQUALSZERO"));
}
int i = 1;
rowIter = newRow.getRowSet().getRows().iterator();
while (rowIter.hasNext()) {
Row childRow = (Row)rowIter.next();
oldPrimaryKey1Cell = ObjectUtil.findOldCell(childRow, "R", "R");
oldPrimaryKey1Cell.setColumnValue(newR);
oldPrimaryKey2Cell = ObjectUtil.findOldCell(childRow, "RI", "R");
oldPrimaryKey2Cell.setColumnValue(newR);
Cell oldPrimaryKey3Cell = ObjectUtil.findOldCell(childRow, "RI", "RI");
oldPrimaryKey3Cell.setColumnValue(i);
newPrimaryKey1Cell = ObjectUtil.findNewCell(childRow, "R", "R");
newPrimaryKey1Cell.setColumnValue(newR);
newPrimaryKey2Cell = ObjectUtil.findNewCell(childRow, "RI", "R");
newPrimaryKey2Cell.setColumnValue(newR);
Cell newPrimaryKey3Cell = ObjectUtil.findNewCell(childRow, "RI", "RI");
newPrimaryKey3Cell.setColumnValue(i);
i++;
}
getContainer().getCurrentObject().getRows().add(newRow);
new ObjectEdit(getContainer(), getContainer().getCurrentObject(), newRow, "Add");
getContainer().setEnabled(false);