1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
@Name("myLogDataModel")
public class MyLogDataModel extends SerializableDataModel {
@In(create = true,required = true)
MyLogDataProvider dataProvider;
private Long currentPk;
private Map<Long,MyLog> wrappedData = new HashMap<Long,MyLog>();
private ArrayList<Long> wrappedKeys = null;
private static final long serialVersionUID = -1956179896877538628L;
/**
* This method never called from framework.
* (non-Javadoc)
* @see org.ajax4jsf.model.ExtendedDataModel#getRowKey()
*/
@Override
public Object getRowKey() {
return currentPk;
}
/**
* This method normally called by Visitor before request Data Row.
*/
@Override
public void setRowKey(Object key) {
this.currentPk = (Long) key;
}
/**
* This is main part of Visitor pattern. Method called by framework many times during request processing.
*/
@Override
public void walk(FacesContext context, DataVisitor visitor, Range range, Object argument) throws IOException {
int firstRow = ((SequenceRange)range).getFirstRow();
int numberOfRows = ((SequenceRange)range).getRows();
wrappedKeys = new ArrayList<Long>();
for (MyLog item:dataProvider.getItemsByrange(new Integer(firstRow), numberOfRows, null, true)) {
wrappedKeys.add(item.getId());
wrappedData.put(item.getId(), item);
visitor.process(context, item.getId(), argument);
}
}
/**
* This method must return actual data rows count from the Data Provider. It is used by pagination control
* to determine total number of data items.
*/
private Integer rowCount; // better to buffer row count locally
@Override
public int getRowCount() {
if (rowCount==null) {
rowCount = new Integer(getDataProvider().getRowCount());
return rowCount.intValue();
} else {
return rowCount.intValue();
}
}
/**
* This is main way to obtain data row. It is intensively used by framework.
* We strongly recommend use of local cache in that method.
*/
@Override
public Object getRowData() {
if (currentPk==null) {
return null;
} else {
MyLog ret = wrappedData.get(currentPk);
if (ret==null) {
ret = getDataProvider().getMyLogByPk(currentPk);
wrappedData.put(currentPk, ret);
return ret;
} else {
return ret;
}
}
}
/**
* Unused rudiment from old JSF staff.
*/
@Override
public int getRowIndex() {
throw new UnsupportedOperationException();
}
/**
* Unused rudiment from old JSF staff.
*/
@Override
public Object getWrappedData() {
throw new UnsupportedOperationException();
}
/**
* Never called by framework.
*/
@Override
public boolean isRowAvailable() {
if (currentPk==null) {
return false;
} else {
return getDataProvider().hasMyLogByPk(currentPk);
}
}
/**
* Unused rudiment from old JSF staff.
*/
@Override
public void setRowIndex(int rowIndex) {
throw new UnsupportedOperationException();
}
/**
* Unused rudiment from old JSF staff.
*/
@Override
public void setWrappedData(Object data) {
throw new UnsupportedOperationException();
}
/**
* This method suppose to produce SerializableDataModel that will be serialized into View State and used on a post-back.
* In current implementation we just mark current model as serialized. In more complicated cases we may need to
* transform data to actually serialized form.
*/
public SerializableDataModel getSerializableModel(Range range) {
if (wrappedKeys!=null) {
return this;
} else {
return null;
}
}
/**
* This is helper method that is called by framework after model update. In must delegate actual database update to
* Data Provider.
*/
@Override
public void update() {
getDataProvider().update();
}
public MyLogDataProvider getDataProvider() {
return dataProvider;
}
public void setDataProvider(MyLogDataProvider dataProvider) {
this.dataProvider = dataProvider;
} |
Partager