ScrollingResults.java
/*
** Module : ScrollingResults.java
** Abstract : Implementation of Results which wrappers ScrollableResults
**
** Copyright (c) 2004-2024, Golden Code Development Corporation.
**
** -#- -I- --Date-- --JPRM-- -----------------------------------Description-----------------------------------
** 001 ECF 20070711 @34451 Created initial version. Extracted inner class from PreselectQuery.
** 002 ECF 20080709 @39089 Modified sessionClosing(). We now clean up resources when the session
** is closing, since the results cannot be accessed afterward.
** 003 CA 20080815 @39467 Support API change in DBUtils.
** 004 CA 20090707 @43070 On cleanup, do not re-close the results if they are already closed.
** 005 ECF 20090711 @43190 Added implicit transaction support. Call {push/pop}
** ImplicitTransaction() in Persistence in c'tor and cleanup().
** 006 ECF 20150715 Replaced Apache commons logging with J2SE logging.
** 007 OM 20160720 Prevented ContextLocalCleanupException to be thrown when client
** disconnects and the context is not accessible.
** 008 ECF 20200419 Removed Hibernate dependencies.
** 008 RFB 20200627 Changes to scroll per CA related to 4GL checking if we are on (or before) the
** first record (and we are actually on/before the first row). But, in postgresql
** JDBC driver, doing a -1 scroll will position us on the last row (it circles
** back from the last row if the value is negative!).
** 009 GBB 20230512 Logging methods replaced by CentralLogger/ConversionStatus.
** 010 AD 20231110 Implemented method get(boolean) which calls get(boolean) from ScrollableResults.
** AD 20231113 Refactored get() to use get(boolean).
** 011 OM 20241128 Multi-tenant runtime support: selected the proper persistence context, eventually
** based on [sharedDb] parameter.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
** Under Section 7 of the GNU Affero GPL version 3, the following additional
** terms apply to the works covered under the License. These additional terms
** are non-permissive additional terms allowed under Section 7 of the GNU
** Affero GPL version 3 and may not be removed by you.
**
** 0. Attribution Requirement.
**
** You must preserve all legal notices or author attributions in the covered
** work or Appropriate Legal Notices displayed by works containing the covered
** work. You may not remove from the covered work any author or developer
** credit already included within the covered work.
**
** 1. No License To Use Trademarks.
**
** This license does not grant any license or rights to use the trademarks
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
** of Golden Code Development Corporation. You are not authorized to use the
** name Golden Code, FWD, or the names of any author or contributor, for
** publicity purposes without written authorization.
**
** 2. No Misrepresentation of Affiliation.
**
** You may not represent yourself as Golden Code Development Corporation or FWD.
**
** You may not represent yourself for publicity purposes as associated with
** Golden Code Development Corporation, FWD, or any author or contributor to
** the covered work, without written authorization.
**
** 3. No Misrepresentation of Source or Origin.
**
** You may not represent the covered work as solely your work. All modified
** versions of the covered work must be marked in a reasonable way to make it
** clear that the modified work is not originating from Golden Code Development
** Corporation or FWD. All modified versions must contain the notices of
** attribution required in this license.
*/
package com.goldencode.p2j.persist;
import java.util.logging.*;
import com.goldencode.p2j.persist.orm.*;
import com.goldencode.p2j.util.logging.*;
/**
* A simple, scrolling implementation of the {@code Results} interface, which offers a thin layer
* around the internal, {@code ScrollableResults} object provided by the query.
*/
final class ScrollingResults
implements Results
{
/** Logger */
private static final CentralLogger LOG = CentralLogger.get(ScrollingResults.class.getName());
/** Scrollable result set; primary keys or {@code Record}s */
private final ScrollableResults<Object> results;
/** Persistence object which provided the underlying ScrollableResults */
private final Persistence persistence;
/**
* The {@link Persistence} context. Only used at cleanup time when the getter is not accessible
* any more.
*/
private final Persistence.Context[] persistenceContext = new Persistence.Context[2];
/** Track closed state of the {@link #results}. */
private boolean closed = false;
/**
* Construct an instance of this object from a scrollable result set
* provided by query execution.
*
* @param persistence
* The associated persistence service object.
* @param results
* Scrollable result set generated by the query.
*/
ScrollingResults(Persistence persistence, ScrollableResults<Object> results)
throws PersistenceException
{
this.results = results;
this.persistence = persistence;
this.persistenceContext[0] = persistence.getContext(true);
this.persistenceContext[1] = persistence.getContext(false);
persistenceContext[0].useSession();
if (persistenceContext[0] != persistenceContext[1])
{
persistenceContext[1].useSession();
}
}
/**
* Move cursor to the first result row.
*
* @return <code>true</code> if there are any results.
*/
public boolean first()
{
return results.first();
}
/**
* Move cursor to the last result row.
*
* @return <code>true</code> if there are any results.
*/
public boolean last()
{
return results.last();
}
/**
* Move cursor to the next result row.
*
* @return <code>true</code> if there is a result under the cursor
* under the move.
*/
public boolean next()
{
return results.next();
}
/**
* Move cursor to the previous result row.
*
* @return <code>true</code> if there is a result under the cursor
* under the move.
*/
public boolean previous()
{
return results.previous();
}
/**
* Is the cursor on the first row in the result set?
*
* @return <code>true</code> if the cursor is on the first row.
*/
public boolean isFirst()
{
return results.isFirst();
}
/**
* Is the cursor on the last row in the result set?
*
* @return <code>true</code> if the cursor is on the first row.
*/
public boolean isLast()
{
return results.isLast();
}
/**
* Get the array of objects at the current result row.
*
* @return Object array or {@code null}.
*/
public Object[] get()
{
return get(false);
}
/**
* Get the array of objects at the current result row
* when forceOnlyId is false.
* Get the array of primary keys of the objects
* at the current result row when forceOnlyId is true.
*
* @param forceOnlyId
* Indicates whether it's only the primary keys
* that must be returned, or all data found is returned.
*
* @return Object array or {@code null}.
*/
public Object[] get(boolean forceOnlyId)
{
return results.get(forceOnlyId);
}
/**
* Get the object at the current result row, at the specified column.
*
* @param column
* Zero-based index column of the desired object.
*
* @return Object at <code>column</code> or <code>null</code>.
*/
public Object get(int column)
{
Object[] row = get();
return (row != null) ? row[column] : null;
}
/**
* Get the primary key ID at the current result row, at the specified
* column.
*
* @param column
* Zero-based index column of the desired ID.
*
* @return ID of the record or <code>null</code>.
*/
public Long getID(int column)
{
Object result = get(column);
if (result instanceof Record)
{
return ((Record) result).primaryKey();
}
return (Long) result;
}
/**
* Get the row number currently under the cursor.
*
* @return Zero-based index of the current row, or <code>-1</code>
* if the cursor is not currently on a result.
*/
public int getRowNumber()
{
return results.getRowNumber();
}
/**
* Set the row number currently under the cursor.
*
* @param row
* Zero-based index of the row to be set as the current row.
*
* @return <code>true</code> if there is a row at the specified row
* number; else <code>false</code>.
*/
public boolean setRowNumber(int row)
{
return results.setRowNumber(row);
}
/**
* Scroll the cursor ahead by the specified number of rows. If the
* number is negative, the cursor is moved backward. This may put the
* cursor off the end (either end) of the query's results.
*
* @param rows
* Number of rows to jump ahead or back.
*
* @return <code>true</code> if there is a row at the new location;
* else <code>false</code>.
*/
public boolean scroll(int rows)
{
if (results.getRowNumber() + rows < 0)
{
return false;
}
return results.scroll(rows);
}
/**
* Reset the cursor to its natural starting position, before the first
* result row.
*/
public void reset()
{
results.beforeFirst();
}
/**
* Invoked when the current Hibernate session is about to close. Gives
* the implementation an opportunity to perform appropriate processing in
* response to this event.
*/
public void sessionClosing()
{
cleanup();
}
/**
* Close the wrapped {@code ScrollableResults} object.
*/
public void cleanup()
{
if (closed)
{
return;
}
try
{
results.close();
}
catch (PersistenceException exc)
{
DBUtils.handleException(persistence.getDatabase(Persistence.PRIVATE_CTX), exc);
// Non-critical operation; logging the error is sufficient.
if (LOG.isLoggable(Level.FINE))
{
LOG.log(Level.FINE, exc.getMessage(), exc);
}
else if (LOG.isLoggable(Level.WARNING))
{
LOG.log(Level.WARNING, exc.getMessage());
}
}
finally
{
closed = true;
persistenceContext[0].releaseSession();
if (persistenceContext[0] != persistenceContext[1])
{
persistenceContext[1].releaseSession();
}
}
}
}