// intitalize
PreparedStatement st = null;
PreparedStatement stCount = null;
HrQueryCriteria criteria = getQueryCriteria();
HrRecords records = getQueryResult().getRecords();
PageCursor pageCursor = getQueryResult().getPageCursor();
try {
// start the SQL expression
StringBuffer sbSql = new StringBuffer();
StringBuffer sbCount = new StringBuffer();
StringBuffer sbFrom = new StringBuffer();
StringBuffer sbWhere = new StringBuffer();
StringBuffer sbJoin = new StringBuffer();
sbSql.append("SELECT A.ID,A.DOCUUID,A.OWNER,A.INPUTDATE,A.UPDATEDATE");
sbSql.append(",A.TITLE,A.HOST_URL,A.FREQUENCY");
sbSql.append(",A.SEND_NOTIFICATION,A.PROTOCOL,H.LAST_HARVEST_DATE");
sbSql.append(",A.FINDABLE,A.SEARCHABLE,A.SYNCHRONIZABLE,A.APPROVALSTATUS,A.LASTSYNCDATE");
sbSql.append(",(SELECT COUNT(*) FROM " + getHarvestingJobTableName() + " HJ");
sbSql.append(" WHERE HJ.HARVEST_ID=A.DOCUUID");
if (getIsDbCaseSensitive(this.getRequestContext())) {
sbSql.append(" AND UPPER(HJ.JOB_STATUS)='" + HjRecord.JobStatus.Submited.name().toUpperCase() + "') ");
} else {
sbSql.append(" AND HJ.JOB_STATUS='" + HjRecord.JobStatus.Submited.name().toUpperCase() + "') ");
}
sbSql.append(",(SELECT COUNT(*) FROM " + getHarvestingJobTableName() + " HJ");
sbSql.append(" WHERE HJ.HARVEST_ID=A.DOCUUID");
if (getIsDbCaseSensitive(this.getRequestContext())) {
sbSql.append(" AND UPPER(HJ.JOB_STATUS)='" + HjRecord.JobStatus.Running.name().toUpperCase() + "') ");
} else {
sbSql.append(" AND HJ.JOB_STATUS='" + HjRecord.JobStatus.Running.name().toUpperCase() + "') ");
}
sbSql.append(",(SELECT COUNT(*) FROM " + getHarvestingJobTableName() + " HJ");
sbSql.append(" WHERE HJ.HARVEST_ID=A.DOCUUID");
if (getIsDbCaseSensitive(this.getRequestContext())) {
sbSql.append(" AND UPPER(HJ.JOB_STATUS)='" + HjRecord.JobStatus.Completed.name().toUpperCase() + "') ");
} else {
sbSql.append(" AND HJ.JOB_STATUS='" + HjRecord.JobStatus.Completed.name().toUpperCase() + "') ");
}
sbSql.append(",(SELECT COUNT(*) FROM " + getHarvestingJobTableName() + " HJ");
sbSql.append(" WHERE HJ.HARVEST_ID=A.DOCUUID");
if (getIsDbCaseSensitive(this.getRequestContext())) {
sbSql.append(" AND UPPER(HJ.JOB_STATUS)='" + HjRecord.JobStatus.Canceled.name().toUpperCase() + "') ");
} else {
sbSql.append(" AND HJ.JOB_STATUS='" + HjRecord.JobStatus.Canceled.name().toUpperCase() + "') ");
}
sbCount.append("SELECT COUNT(A.DOCUUID)");
// append from clause
sbFrom.append(" FROM ").append(getHarvestingTableName()).append(" A");
sbSql.append(sbFrom);
sbCount.append(sbFrom);
// append join clause
sbJoin.append(" LEFT JOIN (SELECT MAX(HH.HARVEST_DATE) AS LAST_HARVEST_DATE");
sbJoin.append(",HH.HARVEST_ID AS UUID FROM GPT_HARVESTING_HISTORY HH ");
sbJoin.append("GROUP BY HH.HARVEST_ID) H ON A.DOCUUID=H.UUID");
sbSql.append(sbJoin);
sbCount.append(sbJoin);
// build the where clause
// TODO remove for the final version after merging
sbWhere.append(" (A.PROTOCOL IS NOT NULL) ");
if (!_ignoreUser) {
Users users = buildSelectablePublishers(getRequestContext());
if (users.size() > 0) {
StringBuilder sb = new StringBuilder();
for (User u : users.values()) {
if (sb.length() > 0) {
sb.append(",");
}
sb.append(Integer.toString(u.getLocalID()));
}
if (sb.length() > 0) {
if (sbWhere.length() > 0) {
sbWhere.append(" and ");
}
sbWhere.append(" A.OWNER in (");
sbWhere.append(sb.toString());
sbWhere.append(") ");
}
}
}
// local harvest id
String sLocalId = getQueryCriteria().getLocalId();
if (sLocalId.length() > 0 && Val.chkInt(sLocalId, 0) > 0) {
sLocalId = appendValueFilter(sbWhere, "A.ID", sLocalId, false);
} else {
sLocalId = "";
}
// harvest UUID
String sHarvestUuid =
UuidUtil.addCurlies(
UuidUtil.removeCurlies(getQueryCriteria().getUuid().toUpperCase()));
if (sHarvestUuid.length() > 0) {
if (getIsDbCaseSensitive(this.getRequestContext())) {
sHarvestUuid = appendValueFilter(sbWhere, "UPPER(A.DOCUUID)", sHarvestUuid, false);
} else {
sHarvestUuid = appendValueFilter(sbWhere, "A.DOCUUID", sHarvestUuid, false);
}
}
// repository name
String sName = criteria.getName().toUpperCase();
if (sName.length() > 0) {
if (getIsDbCaseSensitive(this.getRequestContext())) {
sName = appendValueFilter(sbWhere, "UPPER(A.TITLE)", sName, true);
} else {
sName = appendValueFilter(sbWhere, "A.TITLE", sName, true);
}
}
// host name
String sHostUrl = criteria.getHost().toUpperCase();
if (sHostUrl.length() > 0) {
if (getIsDbCaseSensitive(this.getRequestContext())) {
sHostUrl = appendValueFilter(sbWhere, "UPPER(A.HOST_URL)", sHostUrl, true);
} else {
sHostUrl = appendValueFilter(sbWhere, "A.HOST_URL", sHostUrl, true);
}
}
// protocol type
String sProtocolType = criteria.getProtocolTypeAsString().toUpperCase();
if (criteria.getProtocolType() != HarvestProtocol.ProtocolType.None) {
if (getIsDbCaseSensitive(this.getRequestContext())) {
sProtocolType = appendValueFilter(sbWhere, "UPPER(A.PROTOCOL_TYPE)", sProtocolType, false);
} else {
sProtocolType = appendValueFilter(sbWhere, "A.PROTOCOL_TYPE", sProtocolType, false);
}
}
// update date range
Timestamp tsFrom = criteria.getDateRange().getFromTimestamp();
Timestamp tsTo = criteria.getDateRange().getToTimestamp();
if (tsFrom != null) {
appendExpression(sbWhere, "A.UPDATEDATE >= ?");
}
if (tsTo != null) {
appendExpression(sbWhere, "A.UPDATEDATE <= ?");
}
// harvest date range
Timestamp tsHarvestFrom =
criteria.getLastHarvestDateRange().getFromTimestamp();
Timestamp tsHarvestTo = criteria.getLastHarvestDateRange().getToTimestamp();
if (tsHarvestFrom != null) {
appendExpression(sbWhere, "H.LAST_HARVEST_DATE >= ?");
}
if (tsHarvestTo != null) {
appendExpression(sbWhere, "H.LAST_HARVEST_DATE <= ?");
}
// append the where clause expressions
if (sbWhere.length() > 0) {
sbSql.append(" WHERE ").append(sbWhere.toString());
sbCount.append(" WHERE ").append(sbWhere.toString());
}
// append the order by clause
String sOrderByColumn = criteria.getSortOption().getColumnKey();
String sOrderByDir = criteria.getSortOption().getDirection().name();
if (sOrderByColumn.equalsIgnoreCase("local_id")) {
sOrderByColumn = "A.ID";
} else if (sOrderByColumn.equalsIgnoreCase("harvest_id")) {
sOrderByColumn = "A.DOCUUID";
} else if (sOrderByColumn.equalsIgnoreCase("input_date")) {
sOrderByColumn = "A.INPUTDATE";
} else if (sOrderByColumn.equalsIgnoreCase("update_date")) {
sOrderByColumn = "A.UPDATEDATE";
} else if (sOrderByColumn.equalsIgnoreCase("last_harvest_date")) {
sOrderByColumn = "H.LAST_HARVEST_DATE";
} else if (sOrderByColumn.equalsIgnoreCase("name")) {
sOrderByColumn = "A.TITLE";
} else if (sOrderByColumn.equalsIgnoreCase("host_url")) {
sOrderByColumn = "A.HOST_URL";
} else if (sOrderByColumn.equalsIgnoreCase("protocol_type")) {
sOrderByColumn = "A.PROTOCOL_TYPE";
} else {
sOrderByColumn = "A.INPUTDATE";
sOrderByDir = "DESC";
}
if (sOrderByDir.length() == 0) {
sOrderByDir = "ASC";
}
sbSql.append(" ORDER BY ");
sbSql.append(sOrderByColumn).append(" ").append(sOrderByDir);
// establish the connection
ManagedConnection mc = returnConnection();
Connection con = mc.getJdbcConnection();
// prepare the statements
int n = 0;
st = con.prepareStatement(sbSql.toString());
stCount = con.prepareStatement(sbCount.toString());
// local harvest id
if (sLocalId.length() > 0) {
n++;
st.setInt(n, Val.chkInt(sLocalId, 0));
stCount.setInt(n, Val.chkInt(sLocalId, 0));
}
// harvest UUID
if (sHarvestUuid.length() > 0) {
n++;
st.setString(n, sHarvestUuid);
stCount.setString(n, sHarvestUuid);
}
// repository name
if (sName.length() > 0) {
n++;
st.setString(n, sName);
stCount.setString(n, sName);
}
// host name
if (sHostUrl.length() > 0) {
n++;
st.setString(n, sHostUrl);
stCount.setString(n, sHostUrl);
}
// protocol type
if (criteria.getProtocolType() != HarvestProtocol.ProtocolType.None) {
n++;
st.setString(n, sProtocolType);
stCount.setString(n, sProtocolType);
}
// update date range
if (tsFrom != null) {
n++;
st.setTimestamp(n, tsFrom);
stCount.setTimestamp(n, tsFrom);
}
if (tsTo != null) {
n++;
st.setTimestamp(n, tsTo);
stCount.setTimestamp(n, tsTo);
}
// harvest date range
if (tsHarvestFrom != null) {
n++;
st.setTimestamp(n, tsHarvestFrom);
stCount.setTimestamp(n, tsHarvestFrom);
}
if (tsHarvestTo != null) {
n++;
st.setTimestamp(n, tsHarvestTo);
stCount.setTimestamp(n, tsHarvestTo);
}
// query the count
logExpression(sbCount.toString());
ResultSet rsCount = stCount.executeQuery();
int nTotalRecordCount = 0;
if (rsCount.next()) {
nTotalRecordCount = rsCount.getInt(1);
pageCursor.setTotalRecordCount(nTotalRecordCount);
}
closeStatement(stCount);
stCount = null;
// query records if a count was found
if (nTotalRecordCount > 0) {
// set the start record and the number of records to retrieve
int nCurPage = pageCursor.getCurrentPage();
int nRecsPerPage = getQueryResult().getPageCursor().getRecordsPerPage();
int nStartRecord = ((nCurPage - 1) * nRecsPerPage) + 1;
int nMaxRecsToRetrieve = nCurPage * nRecsPerPage;
if (_ignorePagination || criteria.getDueOnly()) {
st.setMaxRows(nTotalRecordCount);
} else {
st.setMaxRows(nMaxRecsToRetrieve);
}
// execute the query
logExpression(sbSql.toString());
ResultSet rs = st.executeQuery();
// build the record set
int nCounter = 0;
while (rs.next()) {
try {
HrRecord record = null;
if (criteria.getDueOnly()) {
record = readRecord(con, rs);
if (record.getIsHarvestDue()) {
nCounter++;
} else {
nTotalRecordCount = nTotalRecordCount > 0 ? nTotalRecordCount - 1 : 0;
}
} else {
nCounter++;
}
if (nCounter >= nStartRecord) {
if (record == null) {
record = readRecord(con, rs);
}
if (criteria.getDueOnly()) {
if (record.getIsHarvestDue()) {
records.add(record);
}
} else {
records.add(record);
}
// break if we hit the max value for the cursor
if (!_ignorePagination && records.size() >= nRecsPerPage) {
break;
}
}
} catch (ProtocolParseException ex) {
LOGGER.log(Level.WARNING, "Error reading record.", ex);
}
}
}
pageCursor.setTotalRecordCount(nTotalRecordCount);
} finally {
closeStatement(st);
closeStatement(stCount);
}