[15757] | 1 | /**********************************************************************
|
---|
| 2 | *
|
---|
| 3 | * sqlbrowsefilter.cpp --
|
---|
| 4 | * Copyright (C) 2008 DL Consulting Ltd
|
---|
| 5 | *
|
---|
| 6 | * A component of the Greenstone digital library software
|
---|
| 7 | * from the New Zealand Digital Library Project at the
|
---|
| 8 | * University of Waikato, New Zealand.
|
---|
| 9 | *
|
---|
| 10 | * This program is free software; you can redistribute it and/or modify
|
---|
| 11 | * it under the terms of the GNU General Public License as published by
|
---|
| 12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 13 | * (at your option) any later version.
|
---|
| 14 | *
|
---|
| 15 | * This program is distributed in the hope that it will be useful,
|
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | * GNU General Public License for more details.
|
---|
| 19 | *
|
---|
| 20 | * You should have received a copy of the GNU General Public License
|
---|
| 21 | * along with this program; if not, write to the Free Software
|
---|
| 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | *
|
---|
| 24 | *********************************************************************/
|
---|
| 25 |
|
---|
| 26 | #include "sqlbrowsefilter.h"
|
---|
| 27 | #include "fileutil.h"
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | sqlbrowsefilterclass::sqlbrowsefilterclass ()
|
---|
| 31 | {
|
---|
| 32 | sql_db_ptr = NULL;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 |
|
---|
| 36 | sqlbrowsefilterclass::~sqlbrowsefilterclass ()
|
---|
| 37 | {
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | void sqlbrowsefilterclass::configure (const text_t &key, const text_tarray &cfgline)
|
---|
| 42 | {
|
---|
| 43 | filterclass::configure (key, cfgline);
|
---|
| 44 |
|
---|
| 45 | if (key == "indexstem")
|
---|
| 46 | {
|
---|
| 47 | indexstem = cfgline[0];
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | bool sqlbrowsefilterclass::init (ostream &logout)
|
---|
| 53 | {
|
---|
| 54 | outconvertclass text_t2ascii;
|
---|
| 55 |
|
---|
| 56 | if (!filterclass::init(logout)) return false;
|
---|
| 57 |
|
---|
| 58 | if (sql_db_ptr == NULL)
|
---|
| 59 | {
|
---|
| 60 | // most likely a configuration problem
|
---|
| 61 | logout << text_t2ascii
|
---|
| 62 | << "configuration error: sqlbrowsefilter contains a null sqldbclass\n\n";
|
---|
| 63 | return false;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | if (indexstem.empty())
|
---|
| 67 | {
|
---|
| 68 | indexstem = collection;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | // get the filename for the database and make sure it exists
|
---|
| 72 | sql_db_filename = filename_cat(dbhome, "collect", collection, "index", "text", indexstem);
|
---|
| 73 | sql_db_filename += sql_db_ptr->getfileextension();
|
---|
| 74 | if (!file_exists(sql_db_filename))
|
---|
| 75 | {
|
---|
| 76 | logout << text_t2ascii
|
---|
| 77 | << "warning: database \"" << sql_db_filename << "\" does not exist\n\n";
|
---|
| 78 | return false;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | return true;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 |
|
---|
| 85 | void sqlbrowsefilterclass::filter (const FilterRequest_t &request,
|
---|
| 86 | FilterResponse_t &response,
|
---|
| 87 | comerror_t &err, ostream &logout)
|
---|
| 88 | {
|
---|
| 89 | outconvertclass text_t2ascii;
|
---|
| 90 |
|
---|
| 91 | response.clear();
|
---|
| 92 | err = noError;
|
---|
| 93 |
|
---|
| 94 | if (sql_db_ptr == NULL) {
|
---|
| 95 | // most likely a configuration problem
|
---|
| 96 | logout << text_t2ascii
|
---|
| 97 | << "configuration error: sqlbrowsefilter contains a null sqldbclass\n\n";
|
---|
| 98 | err = configurationError;
|
---|
| 99 | return;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | // open the database
|
---|
| 103 | sql_db_ptr->setlogout (&logout);
|
---|
| 104 | if (!sql_db_ptr->opendatabase (sql_db_filename, DB_READER, 100, false)) {
|
---|
| 105 | // most likely a system problem (we have already checked that the database exists)
|
---|
| 106 | logout << text_t2ascii
|
---|
| 107 | << "system problem: open on database \""
|
---|
| 108 | << sql_db_filename << "\" failed\n\n";
|
---|
| 109 | err = systemProblem;
|
---|
| 110 | return;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | if ((request.filterResultOptions & FRmetadataValues) && !request.fields.empty())
|
---|
| 114 | {
|
---|
| 115 | text_t metadata_element_name = *(request.fields.begin());
|
---|
| 116 | logout << text_t2ascii << "Requesting metadata values for " << metadata_element_name << "...\n";
|
---|
| 117 | text_tarray metadata_values = sql_db_ptr->getmetadatavalues (metadata_element_name);
|
---|
| 118 | text_tarray::iterator metadata_value_iterator = metadata_values.begin();
|
---|
| 119 | while (metadata_value_iterator != metadata_values.end())
|
---|
| 120 | {
|
---|
| 121 | ResultDocInfo_t metadata_value_result_doc;
|
---|
| 122 | metadata_value_result_doc.OID = *metadata_value_iterator;
|
---|
| 123 | response.docInfo.push_back (metadata_value_result_doc);
|
---|
| 124 |
|
---|
| 125 | metadata_value_iterator++;
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | sql_db_ptr->closedatabase(); // Important that local library doesn't leave any files open
|
---|
| 130 | }
|
---|