com.datastax.data.dataset.DataRelationTable Maven / Gradle / Ivy
package com.datastax.data.dataset;
import com.datastax.data.dataset.event.DataTableListener;
import com.datastax.data.dataset.event.RowChangeEvent;
import com.datastax.data.dataset.event.TableChangeEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.*;
import java.util.logging.Logger;
public class DataRelationTable extends DataTable {
private static final Logger LOG = Logger.getLogger(DataRelationTable.class.getName());
private DataRelation relation;
private DataSelector parentSelector;
private DataTable parentTable;
private SelectionListener listener = new SelectionListener();
private ParentTableListener pListener = new ParentTableListener();
private ChildTableListener cListener = new ChildTableListener();
public DataRelationTable(DataSet ds) {
super(ds);
}
public DataRelationTable(DataSet ds, String name) {
super(ds, name);
}
public void setRelation(DataRelation relation) {
if (this.relation != relation) {
if (this.relation != null && this.relation.getChildColumn() != null) {
this.relation.getChildColumn().getTable().removeDataTableListener(cListener);
}
this.relation = relation;
addChildListener();
if (relation != null) {
relation.addPropertyChangeListener("childColumn", new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
addChildListener();
}
});
}
}
}
private void addChildListener() {
if (this.relation != null && this.relation.getChildColumn() != null) {
this.relation.getChildColumn().getTable().removeDataTableListener(cListener);
}
if (this.relation != null && this.relation.getChildColumn() != null) {
this.relation.getChildColumn().getTable().addDataTableListener(cListener);
}
}
public DataRelation getRelation() {
return relation;
}
public void setParentSelector(DataSelector selector) {
if (this.parentSelector != null) {
this.parentSelector.removePropertyChangeListener("rowIndices", listener);
}
this.parentSelector = selector;
if (this.parentSelector != null) {
this.parentSelector.addPropertyChangeListener("rowIndices", listener);
}
}
public DataSelector getParentSelector() {
return parentSelector;
}
public void setParentTable(DataTable parent) {
if (this.parentTable != null) {
this.parentTable.removeDataTableListener(pListener);
}
this.parentTable = parent;
if (this.parentTable != null) {
this.parentTable.addDataTableListener(pListener);
}
}
public DataTable getParentTable() {
return parentTable;
}
public void setDataProvider(DataProvider dataProvider) {
LOG.warning("An attempt was made to set the DataProvider for a " +
"DataRelationTable. This is not honored because a " +
"DataRelationTable, by definition, gets its records " +
"by querying a DataSelector from the Parent table, and " +
"the DataRelation leading to the Child table.");
}
public void save() {
LOG.warning("An attempt was made to save a DataRelationTable. " +
"This is not honored because a DataRelationTable, " +
"by definition, gets its records by querying a DataSelector " +
"from the Parent table, and the DataRelation leading to the " +
"Child table. Therefore, to save, the child table should be " +
"asked to save, not the DataRelationTable.");
}
public DataRow appendRow() {
if (relation != null && relation.getChildColumn() != null) {
DataTable t = relation.getChildColumn().getTable();
DataRow row = t.appendRow();
if (parentSelector.getRowIndices().size() == 1) {
row.setValue(relation.getChildColumn().getName(),
relation.getParentColumn().getTable().getValue(
parentSelector.getRowIndices().get(0),
relation.getParentColumn().getName()
));
}
rows.add(row);
return row;
}
return null;
}
public void refresh() {
Map selectorState = new HashMap();
List keyColumns = new ArrayList();
for (DataColumn c : columns.values()) {
if (c.isKeyColumn()) {
keyColumns.add(c);
}
}
for (DataSelector sel : selectors.values()) {
List indices = sel.getRowIndices();
Object[][] values = new Object[indices.size()][keyColumns.size()];
for (int i=0; i list = parentTable.getRows();
super.rows.addAll(relation.getRows(list.toArray(new DataRow[list.size()])));
} else {
super.rows.addAll(relation.getChildColumn().getTable().getRows());
}
fireDataTableChanged(TableChangeEvent.newLoadCompleteEvent(this));
}
for (DataSelector sel : selectors.values()) {
Object[][] values = selectorState.get(sel);
List indices = new ArrayList(values.length);
for (int i=0; i 0) {
if (rows.length == 0) {
sel.setRowIndices(new int[]{0});
} else {
sel.setRowIndices(rows);
}
}
}
}
public DataColumn getColumn(String colName) {
DataColumn col = null;
if (relation != null && relation.getChildColumn() != null && relation.getChildColumn().getTable() != null) {
col = relation.getChildColumn().getTable().getColumn(colName);
}
if (col == null) {
return super.getColumn(colName);
} else {
return col;
}
}
public List getColumns() {
List cols = new ArrayList();
if (relation != null && relation.getChildColumn() != null && relation.getChildColumn().getTable() != null) {
cols.addAll(relation.getChildColumn().getTable().getColumns());
}
cols.addAll(super.getColumns());
return Collections.unmodifiableList(cols);
}
private final class SelectionListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent evt) {
refresh();
}
}
private final class ParentTableListener implements DataTableListener {
public void rowChanged(RowChangeEvent evt) {
}
public void tableChanged(TableChangeEvent evt) {
refresh();
}
}
private final class ChildTableListener implements DataTableListener {
public void rowChanged(RowChangeEvent evt) {
}
public void tableChanged(TableChangeEvent evt) {
refresh();
}
}
}