[15757] | 1 | /**********************************************************************
|
---|
| 2 | *
|
---|
| 3 | * sqlbrowsefilter.cpp --
|
---|
| 4 | * Copyright (C) 2008 DL Consulting Ltd
|
---|
[22050] | 5 | * Copyright (C) 2010 New Zealand Digital Library Project
|
---|
[15757] | 6 | *
|
---|
| 7 | * A component of the Greenstone digital library software
|
---|
| 8 | * from the New Zealand Digital Library Project at the
|
---|
| 9 | * University of Waikato, New Zealand.
|
---|
| 10 | *
|
---|
| 11 | * This program is free software; you can redistribute it and/or modify
|
---|
| 12 | * it under the terms of the GNU General Public License as published by
|
---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 14 | * (at your option) any later version.
|
---|
| 15 | *
|
---|
| 16 | * This program is distributed in the hope that it will be useful,
|
---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | * GNU General Public License for more details.
|
---|
| 20 | *
|
---|
| 21 | * You should have received a copy of the GNU General Public License
|
---|
| 22 | * along with this program; if not, write to the Free Software
|
---|
| 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 24 | *
|
---|
| 25 | *********************************************************************/
|
---|
| 26 |
|
---|
| 27 | #include "sqlbrowsefilter.h"
|
---|
| 28 |
|
---|
| 29 | sqlbrowsefilterclass::sqlbrowsefilterclass ()
|
---|
[22050] | 30 | : sqlfilterclass()
|
---|
| 31 | {}
|
---|
[15757] | 32 |
|
---|
| 33 |
|
---|
| 34 | sqlbrowsefilterclass::~sqlbrowsefilterclass ()
|
---|
[22050] | 35 | {}
|
---|
[15757] | 36 |
|
---|
| 37 | void sqlbrowsefilterclass::filter (const FilterRequest_t &request,
|
---|
| 38 | FilterResponse_t &response,
|
---|
| 39 | comerror_t &err, ostream &logout)
|
---|
| 40 | {
|
---|
| 41 | outconvertclass text_t2ascii;
|
---|
| 42 |
|
---|
[22050] | 43 | if (!connect_to_sqldb(response,err,logout)) {
|
---|
[15757] | 44 | return;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[15764] | 47 | // Request for the metadata values assigned to an element
|
---|
[15802] | 48 | if (request.requestParams == "GetMetadataValues")
|
---|
[15757] | 49 | {
|
---|
[16180] | 50 | text_tarray metadata_element_names;
|
---|
[15941] | 51 | text_t metadata_value_filter = "";
|
---|
[16039] | 52 | text_t metadata_value_grouping_expression = "";
|
---|
[15802] | 53 | OptionValue_tarray::const_iterator options_iterator = request.filterOptions.begin();
|
---|
| 54 | while (options_iterator != request.filterOptions.end())
|
---|
| 55 | {
|
---|
[16180] | 56 | if ((*options_iterator).name == "MetadataElements")
|
---|
[15802] | 57 | {
|
---|
[16180] | 58 | splitchar ((*options_iterator).value.begin(), (*options_iterator).value.end(), ',', metadata_element_names);
|
---|
[15802] | 59 | }
|
---|
[15941] | 60 | if ((*options_iterator).name == "MetadataValueFilter")
|
---|
| 61 | {
|
---|
| 62 | metadata_value_filter = (*options_iterator).value;
|
---|
| 63 | }
|
---|
[16040] | 64 | if ((*options_iterator).name == "MetadataValueGroupingExpression")
|
---|
[16039] | 65 | {
|
---|
| 66 | metadata_value_grouping_expression = (*options_iterator).value;
|
---|
| 67 | }
|
---|
[15802] | 68 | options_iterator++;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[16180] | 71 | text_tarray metadata_values = sql_db_ptr->get_metadata_values (metadata_element_names, metadata_value_filter, metadata_value_grouping_expression);
|
---|
[15764] | 72 |
|
---|
| 73 | // Create a map from metadata value to ResultDocInfo_t, to remove duplicate values and obtain occurrence counts
|
---|
| 74 | map<text_t, ResultDocInfo_t> unique_metadata_values_map;
|
---|
[15757] | 75 | text_tarray::iterator metadata_value_iterator = metadata_values.begin();
|
---|
| 76 | while (metadata_value_iterator != metadata_values.end())
|
---|
| 77 | {
|
---|
[15764] | 78 | text_t metadata_value = *metadata_value_iterator;
|
---|
[15757] | 79 |
|
---|
[15764] | 80 | // If no ResultDocInfo_t has already been created for this metadata value, create one now
|
---|
| 81 | if (unique_metadata_values_map.find(metadata_value) == unique_metadata_values_map.end())
|
---|
| 82 | {
|
---|
| 83 | ResultDocInfo_t metadata_value_result_doc;
|
---|
| 84 | metadata_value_result_doc.OID = metadata_value;
|
---|
| 85 | metadata_value_result_doc.result_num = 1;
|
---|
| 86 | unique_metadata_values_map[metadata_value] = metadata_value_result_doc;
|
---|
| 87 | }
|
---|
| 88 | // Otherwise we've seen this value before, so just update the occurrence count
|
---|
| 89 | else
|
---|
| 90 | {
|
---|
| 91 | unique_metadata_values_map[metadata_value].result_num++;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[15757] | 94 | metadata_value_iterator++;
|
---|
| 95 | }
|
---|
[15764] | 96 |
|
---|
| 97 | // Fill in response.docInfo with the ResultDocInfo_t objects we've created above
|
---|
| 98 | map<text_t, ResultDocInfo_t>::iterator unique_metadata_values_iterator = unique_metadata_values_map.begin();
|
---|
| 99 | while (unique_metadata_values_iterator != unique_metadata_values_map.end())
|
---|
| 100 | {
|
---|
| 101 | response.docInfo.push_back ((*unique_metadata_values_iterator).second);
|
---|
| 102 | unique_metadata_values_iterator++;
|
---|
| 103 | }
|
---|
[15757] | 104 | }
|
---|
| 105 |
|
---|
[15804] | 106 | // Request for the documents with a certain metadata value assigned
|
---|
| 107 | else if (request.requestParams == "GetDocumentsWithMetadataValue")
|
---|
| 108 | {
|
---|
[16180] | 109 | text_tarray metadata_element_names;
|
---|
[15804] | 110 | text_t metadata_value = "";
|
---|
[15836] | 111 | text_t sort_by_metadata_element_name = "";
|
---|
[15804] | 112 | OptionValue_tarray::const_iterator options_iterator = request.filterOptions.begin();
|
---|
| 113 | while (options_iterator != request.filterOptions.end())
|
---|
| 114 | {
|
---|
[16180] | 115 | if ((*options_iterator).name == "MetadataElements")
|
---|
[15804] | 116 | {
|
---|
[16180] | 117 | splitchar ((*options_iterator).value.begin(), (*options_iterator).value.end(), ',', metadata_element_names);
|
---|
[15804] | 118 | }
|
---|
| 119 | if ((*options_iterator).name == "MetadataValue")
|
---|
| 120 | {
|
---|
| 121 | metadata_value = (*options_iterator).value;
|
---|
| 122 | }
|
---|
[15836] | 123 | if ((*options_iterator).name == "SortByMetadataElement")
|
---|
| 124 | {
|
---|
| 125 | sort_by_metadata_element_name = (*options_iterator).value;
|
---|
| 126 | }
|
---|
[15804] | 127 | options_iterator++;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[16180] | 130 | text_tarray document_OIDs = sql_db_ptr->get_documents_with_metadata_value (metadata_element_names, metadata_value, sort_by_metadata_element_name);
|
---|
[15804] | 131 |
|
---|
| 132 | // Fill in response.docInfo with the document OIDs
|
---|
| 133 | text_tarray::iterator document_OID_iterator = document_OIDs.begin();
|
---|
| 134 | while (document_OID_iterator != document_OIDs.end())
|
---|
| 135 | {
|
---|
| 136 | ResultDocInfo_t document_result_doc;
|
---|
| 137 | document_result_doc.OID = *document_OID_iterator;
|
---|
| 138 | response.docInfo.push_back (document_result_doc);
|
---|
| 139 | document_OID_iterator++;
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[22050] | 143 | disconnect_from_sqldb();
|
---|
| 144 |
|
---|
[15757] | 145 | }
|
---|