root/main/trunk/greenstone2/runtime-src/src/colservr/sqlbrowsefilter.cpp @ 22050

Revision 22050, 5.3 KB (checked in by davidb, 10 years ago)

Updating of code to support sql-query filter

Line 
1/**********************************************************************
2 *
3 * sqlbrowsefilter.cpp --
4 * Copyright (C) 2008  DL Consulting Ltd
5 * Copyright (C) 2010  New Zealand Digital Library Project
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
29sqlbrowsefilterclass::sqlbrowsefilterclass ()
30  : sqlfilterclass()
31{}
32
33
34sqlbrowsefilterclass::~sqlbrowsefilterclass ()
35{}
36
37void sqlbrowsefilterclass::filter (const FilterRequest_t &request,
38                   FilterResponse_t &response,
39                   comerror_t &err, ostream &logout)
40{
41  outconvertclass text_t2ascii;
42
43  if (!connect_to_sqldb(response,err,logout)) {
44    return;
45  }
46
47  // Request for the metadata values assigned to an element
48  if (request.requestParams == "GetMetadataValues")
49  {
50    text_tarray metadata_element_names;
51    text_t metadata_value_filter = "";
52    text_t metadata_value_grouping_expression = "";
53    OptionValue_tarray::const_iterator options_iterator = request.filterOptions.begin();
54    while (options_iterator != request.filterOptions.end())
55    {
56      if ((*options_iterator).name == "MetadataElements")
57      {
58    splitchar ((*options_iterator).value.begin(), (*options_iterator).value.end(), ',', metadata_element_names);
59      }
60      if ((*options_iterator).name == "MetadataValueFilter")
61      {
62    metadata_value_filter = (*options_iterator).value;
63      }
64      if ((*options_iterator).name == "MetadataValueGroupingExpression")
65      {
66    metadata_value_grouping_expression = (*options_iterator).value;
67      }
68      options_iterator++;
69    }
70
71    text_tarray metadata_values = sql_db_ptr->get_metadata_values (metadata_element_names, metadata_value_filter, metadata_value_grouping_expression);
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;
75    text_tarray::iterator metadata_value_iterator = metadata_values.begin();
76    while (metadata_value_iterator != metadata_values.end())
77    {
78      text_t metadata_value = *metadata_value_iterator;
79
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
94      metadata_value_iterator++;
95    }
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    }
104  }
105
106  // Request for the documents with a certain metadata value assigned
107  else if (request.requestParams == "GetDocumentsWithMetadataValue")
108  {
109    text_tarray metadata_element_names;
110    text_t metadata_value = "";
111    text_t sort_by_metadata_element_name = "";
112    OptionValue_tarray::const_iterator options_iterator = request.filterOptions.begin();
113    while (options_iterator != request.filterOptions.end())
114    {
115      if ((*options_iterator).name == "MetadataElements")
116      {
117    splitchar ((*options_iterator).value.begin(), (*options_iterator).value.end(), ',', metadata_element_names);
118      }
119      if ((*options_iterator).name == "MetadataValue")
120      {
121    metadata_value = (*options_iterator).value;
122      }
123      if ((*options_iterator).name == "SortByMetadataElement")
124      {
125    sort_by_metadata_element_name = (*options_iterator).value;
126      }
127      options_iterator++;
128    }
129
130    text_tarray document_OIDs = sql_db_ptr->get_documents_with_metadata_value (metadata_element_names, metadata_value, sort_by_metadata_element_name);
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
143  disconnect_from_sqldb();
144
145}
Note: See TracBrowser for help on using the browser.