/********************************************************************** * * sqlbrowsefilter.cpp -- * Copyright (C) 2008 DL Consulting Ltd * * A component of the Greenstone digital library software * from the New Zealand Digital Library Project at the * University of Waikato, New Zealand. * * This program 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 2 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * *********************************************************************/ #include "sqlbrowsefilter.h" #include "fileutil.h" sqlbrowsefilterclass::sqlbrowsefilterclass () { sql_db_ptr = NULL; } sqlbrowsefilterclass::~sqlbrowsefilterclass () { } void sqlbrowsefilterclass::configure (const text_t &key, const text_tarray &cfgline) { filterclass::configure (key, cfgline); if (key == "indexstem") { indexstem = cfgline[0]; } } bool sqlbrowsefilterclass::init (ostream &logout) { outconvertclass text_t2ascii; if (!filterclass::init(logout)) return false; if (sql_db_ptr == NULL) { // most likely a configuration problem logout << text_t2ascii << "configuration error: sqlbrowsefilter contains a null sqldbclass\n\n"; return false; } if (indexstem.empty()) { indexstem = collection; } // get the filename for the database and make sure it exists sql_db_filename = filename_cat(dbhome, "collect", collection, "index", "text", indexstem); sql_db_filename += sql_db_ptr->getfileextension(); if (!file_exists(sql_db_filename)) { logout << text_t2ascii << "warning: database \"" << sql_db_filename << "\" does not exist\n\n"; return false; } return true; } void sqlbrowsefilterclass::filter (const FilterRequest_t &request, FilterResponse_t &response, comerror_t &err, ostream &logout) { outconvertclass text_t2ascii; response.clear(); err = noError; if (sql_db_ptr == NULL) { // most likely a configuration problem logout << text_t2ascii << "configuration error: sqlbrowsefilter contains a null sqldbclass\n\n"; err = configurationError; return; } // open the database sql_db_ptr->setlogout (&logout); if (!sql_db_ptr->opendatabase (sql_db_filename, DB_READER, 100, false)) { // most likely a system problem (we have already checked that the database exists) logout << text_t2ascii << "system problem: open on database \"" << sql_db_filename << "\" failed\n\n"; err = systemProblem; return; } // Request for the metadata values assigned to an element if ((request.filterResultOptions & FRmetadataValues) && !request.fields.empty()) { text_t metadata_element_name = *(request.fields.begin()); text_tarray metadata_values = sql_db_ptr->getmetadatavalues (metadata_element_name); // Create a map from metadata value to ResultDocInfo_t, to remove duplicate values and obtain occurrence counts map unique_metadata_values_map; text_tarray::iterator metadata_value_iterator = metadata_values.begin(); while (metadata_value_iterator != metadata_values.end()) { text_t metadata_value = *metadata_value_iterator; // If no ResultDocInfo_t has already been created for this metadata value, create one now if (unique_metadata_values_map.find(metadata_value) == unique_metadata_values_map.end()) { ResultDocInfo_t metadata_value_result_doc; metadata_value_result_doc.OID = metadata_value; metadata_value_result_doc.result_num = 1; unique_metadata_values_map[metadata_value] = metadata_value_result_doc; } // Otherwise we've seen this value before, so just update the occurrence count else { unique_metadata_values_map[metadata_value].result_num++; } metadata_value_iterator++; } // Fill in response.docInfo with the ResultDocInfo_t objects we've created above map::iterator unique_metadata_values_iterator = unique_metadata_values_map.begin(); while (unique_metadata_values_iterator != unique_metadata_values_map.end()) { response.docInfo.push_back ((*unique_metadata_values_iterator).second); unique_metadata_values_iterator++; } } sql_db_ptr->closedatabase(); // Important that local library doesn't leave any files open }