javax.faces.model.ResultSetDataModel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of myfaces-api Show documentation
Show all versions of myfaces-api Show documentation
The public API classes of the Apache MyFaces CORE JSF-2.2 project
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package javax.faces.model;
import javax.faces.FacesException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.ResultSetMetaData;
import java.util.*;
/**
* see Javadoc of JSF Specification
*
* @author Thomas Spiegl (latest modification by $Author: skitching $)
* @author Martin Marinschek
* @version $Revision: 676298 $ $Date: 2008-07-13 05:31:48 -0500 (Sun, 13 Jul 2008) $
*/
public class ResultSetDataModel extends DataModel
{
// FIELDS
private int _currentIndex = -1;
/**
* The ResultSet being wrapped by this DataModel.
*/
private ResultSet _resultSet = null;
/**
* The MetaData of the ResultSet being wrapped by this DataModel.
*/
private ResultSetMetaData _resultSetMetadata = null;
/**
* Indicator for an updated row at the current position.
*/
private boolean _currentRowUpdated = false;
// CONSTRUCTORS
public ResultSetDataModel()
{
this(null);
}
public ResultSetDataModel(ResultSet resultSet)
{
super();
setWrappedData(resultSet);
}
/** We don't know how many rows the result set has without scrolling
* through the whole thing.
*/
public int getRowCount()
{
return -1;
}
/** Get the actual data of this row
* wrapped into a map.
* The specification is very strict about what has to be
* returned from here, so check the spec before
* modifying anything here.
*/
public Object getRowData()
{
if (_resultSet == null)
{
return null;
}
else if (!isRowAvailable())
{
throw new IllegalArgumentException(
"the requested row is not available in the ResultSet - you have scrolled beyond the end.");
}
try
{
return new WrapResultSetMap(String.CASE_INSENSITIVE_ORDER);
}
catch (SQLException e)
{
throw new FacesException(e);
}
}
public int getRowIndex()
{
return _currentIndex;
}
public Object getWrappedData()
{
return _resultSet;
}
public boolean isRowAvailable()
{
if (_resultSet == null)
{
return false;
}
else if (_currentIndex < 0)
{
return false;
}
try
{
return _resultSet.absolute(_currentIndex + 1);
}
catch (SQLException e)
{
throw new FacesException(e);
}
}
public void setRowIndex(int rowIndex)
{
if (rowIndex < -1)
{
throw new IllegalArgumentException(
"you cannot set the rowIndex to anything less than 0");
}
// Handle the case of an updated row
if (_currentRowUpdated && _resultSet != null)
{
try
{
if (!_resultSet.rowDeleted())
_resultSet.updateRow();
setCurrentRowUpdated(false);
}
catch (SQLException e)
{
throw new FacesException(e);
}
}
int old = _currentIndex;
_currentIndex = rowIndex;
//if no underlying data has been set, the listeners
//need not be notified
if (_resultSet == null)
return;
//Notify all listeners of the upated row
DataModelListener [] listeners = getDataModelListeners();
if ((old != _currentIndex) && (listeners != null))
{
Object rowData = null;
if (isRowAvailable())
{
rowData = getRowData();
}
DataModelEvent event =
new DataModelEvent(this, _currentIndex, rowData);
int n = listeners.length;
for (int i = 0; i < n; i++)
{
if (listeners[i]!=null)
{
listeners[i].rowSelected(event);
}
}
}
}
public void setWrappedData(Object data)
{
if (data == null)
{
_resultSetMetadata = null;
_resultSet = null;
setRowIndex(-1);
}
else
{
_resultSetMetadata = null;
_resultSet = (ResultSet) data;
_currentIndex = -1;
setRowIndex(0);
}
}
private ResultSetMetaData getResultSetMetadata()
{
if (_resultSetMetadata == null)
{
try
{
_resultSetMetadata = _resultSet.getMetaData();
}
catch (SQLException e)
{
throw new FacesException(e);
}
}
return _resultSetMetadata;
}
private void setCurrentRowUpdated(boolean currentRowUpdated)
{
_currentRowUpdated = currentRowUpdated;
}
/* A map wrapping the result set and calling
* the corresponding operations on the result set,
* first setting the correct row index.
*/
private class WrapResultSetMap extends TreeMap
{
private static final long serialVersionUID = -4321143404567038922L;
private int _currentIndex;
public WrapResultSetMap(Comparator comparator) throws SQLException
{
super(comparator);
_currentIndex = ResultSetDataModel.this._currentIndex;
_resultSet.absolute(_currentIndex + 1);
int columnCount = getResultSetMetadata().getColumnCount();
for (int i = 1; i <= columnCount; i++) {
super.put(getResultSetMetadata().getColumnName(i),
getResultSetMetadata().getColumnName(i));
}
}
public void clear()
{
throw new UnsupportedOperationException(
"It is not allowed to remove from this map");
}
public boolean containsValue(Object value)
{
Set
© 2015 - 2025 Weber Informatics LLC | Privacy Policy