/*******************************************************************************
* Copyright 2009, 2010 Innovation Gate GmbH. All Rights Reserved.
*
* This file is part of the OpenWGA server platform.
*
* OpenWGA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, a special exception is granted by the copyright holders
* of OpenWGA called "OpenWGA plugin exception". You should have received
* a copy of this exception along with OpenWGA in file COPYING.
* If not, see <http://www.openwga.com/gpl-plugin-exception>.
*
* OpenWGA 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenWGA in file COPYING.
* If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package de.innovationgate.webgate.api.jdbc;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import de.innovationgate.utils.WGUtils;
import de.innovationgate.webgate.api.WGAPIException;
import de.innovationgate.webgate.api.WGBackendException;
import de.innovationgate.webgate.api.WGDocument;
import de.innovationgate.webgate.api.WGReportingResultSetCore;
public abstract class HibernateResultSet implements WGReportingResultSetCore {
public static void injectQueryParams(Query hibQuery, Map map) throws WGAPIException {
if (map == null || map.size() == 0) {
return;
}
String[] pNames = hibQuery.getNamedParameters();
for (int i = 0; i < pNames.length; i++) {
String pName = pNames[i];
Object value = map.get(pName);
if (value != null) {
if (value instanceof WGDocument) {
value = ((WGDocument) value).getNativeObject();
}
hibQuery.setParameter(pName, value);
}
}
}
private WGDatabaseImpl _parent;
private Query _query;
private long _executionTime;
private Map<String, Object> _parameters;
public HibernateResultSet(WGDatabaseImpl parent, Query query, Map<String, Object> queryParameters) {
_parent = parent;
_query = query;
_parameters = queryParameters;
}
protected abstract Object wrapEntity(Object entity) throws WGAPIException;
protected WGDatabaseImpl getParent() {
return _parent;
}
protected Query getQuery() {
return _query;
}
public int results() throws WGBackendException {
try {
String query = getQuery().getQueryString();
String querySearchString = WGUtils.clearStrings(query, '\'', 'X').toLowerCase();
int startIdx = querySearchString.indexOf(" from ");
// Cannot create count query if we have no regular select/from query
if (startIdx == -1) {
return 0;
}
int orderIdx = querySearchString.indexOf("order by");
int groupIdx = querySearchString.toLowerCase().indexOf("group by");
int endIdx = query.length();
if (orderIdx != -1) {
endIdx = orderIdx;
}
if (groupIdx != -1 && groupIdx < endIdx) {
endIdx = groupIdx;
}
String countQueryString = "select count(*) " + query.substring(startIdx, endIdx);
Query countQuery = getParent().getSession().createQuery(countQueryString);
injectQueryParams(countQuery, _parameters);
return ((Number) countQuery.iterate().next()).intValue();
}
catch (Exception e) {
throw new WGBackendException("Exception retrieving query result size", e);
}
}
public long getExecutionTime() {
return _executionTime;
}
protected void setExecutionTime(long executionTime) {
_executionTime = executionTime;
}
}