Project

General

Profile

« Previous | Next » 

Revision 2741

Added by Duane Costa over 18 years ago

Bug #2207: Implementation of the Metacat Advanced Search engine.

View differences:

src/edu/ucsb/nceas/metacat/advancedsearch/AdvancedSearchServlet.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2005 University of New Mexico and the 
4
 *             Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *   '$Author$'
7
 *     '$Date$'
8
 * '$Revision$'
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
 */
24

  
25
package edu.ucsb.nceas.metacat.advancedsearch;
26

  
27
import java.io.IOException;
28
import java.util.Enumeration;
29

  
30
import javax.servlet.RequestDispatcher;
31
import javax.servlet.ServletContext;
32
import javax.servlet.ServletException;
33
import javax.servlet.http.HttpServlet;
34
import javax.servlet.http.HttpServletRequest;
35
import javax.servlet.http.HttpServletResponse;
36
import javax.servlet.http.HttpSession;
37

  
38
import edu.ucsb.nceas.metacat.client.*;
39

  
40
/** 
41
 * @author dcosta
42
 * 
43
 * The AdvancedSearchServlet executes an advanced search.
44
 */
45
public class AdvancedSearchServlet extends HttpServlet {
46

  
47
  // Instance Variables -- (Not used because they are not thread-safe.)
48

  
49
  // Methods
50
  
51
  /**
52
   * Executes an advanced search.
53
   */
54
  public void doPost(HttpServletRequest request, HttpServletResponse response)
55
          throws IOException, ServletException {
56
    AdvancedSearch advancedSearch;
57
    AdvancedSearchBean advancedSearchBean;
58
    RequestDispatcher dispatcher;
59
    HttpSession httpSession = request.getSession();
60
    Metacat metacat = (Metacat) httpSession.getAttribute("metacat");
61
    String metacatURL;
62
    String result;
63
    final String resultsJSP = "style/skins/default/advancedsearchresults.jsp";
64
    final String resultsXSL = "style/common/resultset.xsl";
65
    ServletContext servletContext = httpSession.getServletContext();
66
    String xslPath = servletContext.getRealPath(resultsXSL);
67

  
68
    // First check whether the metacat URL was set as a context-param.
69
    metacatURL = servletContext.getInitParameter("metacatURL");
70
 
71
    // If no metacat URL was configured, then derive the metacat URL from the
72
    // server name and server port.
73
    if (metacatURL == null || metacatURL.equals("")) {
74
      MetacatHelper metacatHelper = new MetacatHelper();
75
      String serverName = request.getServerName();
76
      int serverPort = request.getServerPort();
77
      metacatURL = metacatHelper.constructMetacatURL(serverName, serverPort);
78
    }
79

  
80
    // Get the advancedSearchBean object that has been loaded up with user
81
    // input. Pass the bean to the advancedSearch object, execute the query,
82
    // and return the search result HTML string.
83
    advancedSearchBean = 
84
                (AdvancedSearchBean) request.getAttribute("advancedSearchBean");
85
    advancedSearch = new AdvancedSearch(advancedSearchBean);
86
    result = advancedSearch.executeAdvancedSearch(metacatURL, metacat, xslPath);
87
    
88
    // Store the result HTML string in the request for retrieval by
89
    // the metacatpathqueryresults.jsp form.
90
    request.setAttribute("result", result);
91
		
92
    dispatcher = request.getRequestDispatcher(resultsJSP);
93
    dispatcher.forward(request, response);
94
  }
95
  
96
}
0 97

  
src/edu/ucsb/nceas/metacat/advancedsearch/AdvancedSearchBean.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2005 University of New Mexico and the 
4
 *             Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *   '$Author$'
7
 *     '$Date$'
8
 * '$Revision$'
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
 */
24

  
25
package edu.ucsb.nceas.metacat.advancedsearch;
26

  
27
import java.io.Serializable;
28

  
29
/**
30
 * @author dcosta
31
 *
32
 * The AdvancedSearchBean class stores query properties and values for
33
 * conducting an advanced Metacat search.
34
 */
35
public class AdvancedSearchBean implements Serializable {
36
  
37
  // Possible values for the various ___QueryType fields. These are translated
38
  // to the various "searchmode" values in the Metacat pathquery.
39
  public static final int CONTAINS = 0;     // LIKE "%word%"
40
  public static final int EXACT_MATCH = 1; 	// LIKE = "word"
41
  public static final int STARTS_WITH = 2;	// LIKE "word%"
42
  public static final int ENDS_WITH = 3;    // LIKE "%word"
43
	
44
  // Possible values for subjectAllAny field
45
  public static final int MATCH_ALL = 0;
46
  public static final int MATCH_ANY = 1;
47
	
48
  private boolean boundaryContained;
49
  private boolean caseSensitive;
50
  private String  creatorOrganization;
51
  private int     creatorOrganizationQueryType;
52
  private String  creatorSurname;
53
  private int     creatorSurnameQueryType;
54
  private String  dateField;      // "PUBLICATION", "COLLECTION", or "ALL"
55
  private String  eastBound;
56
  private String  endDate;
57
  private int     formAllAny;     // MATCH_ALL or MATCH_ANY
58
  private String  locationName;
59
  private String  namedTimescale;
60
  private int     namedTimescaleQueryType;
61
  private String  northBound;
62
  private String  southBound;
63
  private String  siteValue;
64
  private String  startDate;
65
  private int     subjectAllAny;  // MATCH_ALL or MATCH_ANY
66
  private String  subjectField;   // "ALL", "TITLE", "ABSTRACT", or "KEYWORD"
67
  private int     subjectQueryType;
68
  private String  subjectValue;
69
  private String  taxon;
70
  private int     taxonQueryType;
71
  private String  westBound;
72

  
73

  
74
  /**
75
   * @return Returns the creatorOrganization.
76
   */
77
  public String getCreatorOrganization() {
78
    return creatorOrganization;
79
  }
80
  
81
  
82
  /**
83
   * @return Returns the creatorOrganizationQueryType.
84
   */
85
  public int getCreatorOrganizationQueryType() {
86
    return creatorOrganizationQueryType;
87
  }
88
  
89
  
90
  /**
91
   * @return Returns the creatorSurname.
92
   */
93
  public String getCreatorSurname() {
94
    return creatorSurname;
95
  }
96
  
97
  
98
  /**
99
   * @return Returns the creatorSurnameQueryType.
100
   */
101
  public int getCreatorSurnameQueryType() {
102
    return creatorSurnameQueryType;
103
  }
104
  
105
  
106
  /**
107
   * @return Returns the dateField. Possible values: "PUBLICATION", "COLLECTION" 
108
   */
109
  public String getDateField() {
110
    return dateField;
111
  }
112
  
113
  
114
  /**
115
   * @return Returns the eastBound.
116
   */
117
  public String getEastBound() {
118
    return eastBound;
119
  }
120
  
121
  
122
  /**
123
   * @return Returns the endDate.
124
   */
125
  public String getEndDate() {
126
    return endDate;
127
  }
128
  
129
  
130
  /**
131
   * @return Returns the formAllAny value.  Possible values are 0 or 1.      
132
   */
133
  public int getFormAllAny() {
134
    return formAllAny;
135
  }
136
  
137
  
138
  /**
139
   * @return Returns the locationName.
140
   */
141
  public String getLocationName() {
142
    return locationName;
143
  }
144
  
145
  
146
  /**
147
   * @return Returns the namedTimescale.
148
   */
149
  public String getNamedTimescale() {
150
    return namedTimescale;
151
  }
152
  
153
  
154
  /**
155
   * @return Returns the namedTimescaleQueryType.
156
   */
157
  public int getNamedTimescaleQueryType() {
158
    return namedTimescaleQueryType;
159
  }
160
  
161
  
162
  /**
163
   * @return Returns the northBound.
164
   */
165
  public String getNorthBound() {
166
    return northBound;
167
  }
168
  
169
  
170
  /**
171
   * @return Returns the siteValue string.
172
   */
173
  public String getSiteValue() {
174
    return siteValue;
175
  }
176
  
177
  
178
  /**
179
   * @return Returns the southBound.
180
   */
181
  public String getSouthBound() {
182
    return southBound;
183
  }
184
  
185
  
186
  /**
187
   * @return Returns the startDate.
188
   */
189
  public String getStartDate() {
190
    return startDate;
191
  }
192
  
193
  
194
  /**
195
   * @return Returns the subjectAllAny value.  Possible values are 0 or 1.      
196
   */
197
  public int getSubjectAllAny() {
198
    return subjectAllAny;
199
  }
200
  
201
  
202
  /**
203
   * @return Returns the subjectField.  Possible values are:
204
   *         "ALL", "TITLE", "ABSTRACT", "KEYWORD"
205
   */
206
  public String getSubjectField() {
207
    return subjectField;
208
  }
209
  
210
  
211
  /**
212
   * @return Returns the subjectQueryType. Possible values are:
213
   * 0 (contains), 1 (exact match), 2 (starts with), 3 (ends with).
214
   */
215
  public int getSubjectQueryType() {
216
    return subjectQueryType;
217
  }
218
  
219
  
220
  /**
221
   * @return Returns the subjectValue.
222
   */
223
  public String getSubjectValue() {
224
    return subjectValue;
225
  }
226
  
227
  
228
  /**
229
   * @return Returns the taxon.
230
   */
231
  public String getTaxon() {
232
    return taxon;
233
  }
234
  
235
  
236
  /**
237
   * @return Returns the taxonConditonType.
238
   */
239
  public int getTaxonQueryType() {
240
    return taxonQueryType;
241
  }
242
  
243
  
244
  /**
245
   * @return Returns the westBound.
246
   */
247
  public String getWestBound() {
248
    return westBound;
249
  }
250
  
251
  
252
  /**
253
   * @return Returns the boundaryContained value.
254
   */
255
  public boolean isBoundaryContained() {
256
    return boundaryContained;
257
  }
258
  
259

  
260
  /**
261
   * @return Returns the caseSensitive value;
262
   */
263
  public boolean isCaseSensitive() {
264
    return caseSensitive;
265
  }
266
  
267

  
268
  /**
269
   * Boolean to determine whether a string is empty. A string is considered to
270
   * be empty if its value is either null or "".
271
   * 
272
   * @param  s      the string to check
273
   * @return        true if the string is empty, else false.
274
   */
275
  public boolean isEmpty(String s) {
276
    if (s != null && !s.equals(""))
277
      return false;
278
    else
279
      return true;
280
  }
281
  
282
  
283
  /**
284
   * @return Returns the limitedByBoundaries.
285
   */
286
  public boolean isLimitedByBoundaries() {
287
    if (!isEmpty(this.eastBound) && !isEmpty(this.westBound)) {
288
      return true;
289
    }
290
    else if (!isEmpty(this.southBound) && !isEmpty(this.northBound)) {
291
      return true;
292
    }
293
    else {
294
      return false;
295
    }
296
  }
297

  
298
  
299
  /**
300
   * @return Returns the limitedByDate.
301
   */
302
  public boolean isLimitedByDate() {
303
    if (!isEmpty(this.endDate) || !isEmpty(this.startDate)) {
304
      return true;
305
    }
306
    else {
307
      return false;
308
    }
309
  }
310

  
311

  
312
  /**
313
   * @param boundaryContained The boundaryContained value to set.
314
   */
315
  public void setBoundaryContained(final boolean boundaryContained) {
316
    this.boundaryContained = boundaryContained;
317
  }
318
  
319
  
320
  /**
321
   * @param caseSensitive The caseSensitive value to set.
322
   */
323
  public void setCaseSensitive(final boolean caseSensitive) {
324
    this.caseSensitive = caseSensitive;
325
  }
326
  
327
  
328
  /**
329
   * @param creatorOrganization The creatorOrganization to set.
330
   */
331
  public void setCreatorOrganization(final String creatorField) {
332
    this.creatorOrganization = creatorField;
333
  }
334
  
335
  
336
  /**
337
   * @param creatorOrganizationQueryType The creatorOrganizationQueryType to set
338
   */
339
  public void setCreatorOrganizationQueryType(final int creatorConditionType) {
340
    this.creatorOrganizationQueryType = creatorConditionType;
341
  }
342
  
343
  
344
  /**
345
   * @param creatorSurname   The creatorSurname to set.
346
   */
347
  public void setCreatorSurname(final String creatorValue) {
348
    this.creatorSurname = creatorValue;
349
  }
350
  
351
  
352
  /**
353
   * @param creatorSurnameQueryType   The creatorSurnameQueryType to set.
354
   */
355
  public void setCreatorSurnameQueryType(final int creatorSurnameQueryType) {
356
    this.creatorSurnameQueryType = creatorSurnameQueryType;
357
  }
358

  
359

  
360
  /**
361
   * @param dateField The dateField to set.
362
   */
363
  public void setDateField(final String dateField) {
364
    this.dateField = dateField;
365
  }
366
  
367
  
368
  /**
369
   * @param eastBound The eastBound to set.
370
   */
371
  public void setEastBound(final String eastBound) {
372
    this.eastBound = eastBound;
373
  }
374
  
375
  
376
  /**
377
   * @param endDate The endDate to set.
378
   */
379
  public void setEndDate(final String endDate) {
380
    this.endDate = endDate;
381
  }
382
  
383
  
384
  /**
385
   * @return Sets the formAllAny value.  Possible values are
386
   *         MATCH_ALL (0) or MATCH_ANY (1).  
387
   */
388
  public void setFormAllAny(final int allAny) {
389
    this.formAllAny = allAny;
390
  }
391
  
392
  
393
  /**
394
   * @param locationName The locationName to set.
395
   */
396
  public void setLocationName(final String locationName) {
397
    this.locationName = locationName;
398
  }
399
  
400
  
401
  /**
402
   * @param namedTimescale The namedTimescale to set.
403
   */
404
  public void setNamedTimescale(final String namedTimescale) {
405
    this.namedTimescale = namedTimescale;
406
  }
407
  
408
  
409
  /**
410
   * @param namedTimescaleQueryType The namedTimescaleQueryType to set.
411
   */
412
  public void setNamedTimescaleQueryType(final int namedTimescaleQueryType) {
413
    this.namedTimescaleQueryType = namedTimescaleQueryType;
414
  }
415
  
416
  
417
  /**
418
   * @param northBound The northBound to set.
419
   */
420
  public void setNorthBound(final String northBound) {
421
    this.northBound = northBound;
422
  }
423

  
424
  
425
  /**
426
   * @param siteValue    the siteValue to set.
427
   */
428
  public void setSiteValue(final String siteValue) {
429
    this.siteValue = siteValue;
430
  }
431
  
432

  
433
  /**
434
   * @param southBound The southBound to set.
435
   */
436
  public void setSouthBound(final String southBound) {
437
    this.southBound = southBound;
438
  }
439
  
440
  
441
  /**
442
   * @param startDate The startDate to set.
443
   */
444
  public void setStartDate(final String startDate) {
445
    this.startDate = startDate;
446
  }
447
  
448
  
449
  /**
450
   * Sets the subjectAllAny value.  
451
   * 
452
   * @param allAny Possible values are MATCH_ALL (0) or MATCH_ANY (1).  
453
   */
454
  public void setSubjectAllAny(final int allAny) {
455
    this.subjectAllAny = allAny;
456
  }
457
  
458
  
459
  /**
460
   * @param subjectField The subjectField to set.
461
   */
462
  public void setSubjectField(final String subjectField) {
463
    this.subjectField = subjectField;
464
  }
465
  
466
  
467
  /**
468
   * @param subjectQueryType The subjectQueryType to set. Possible values are:
469
   *                         0 (contains), 1 (exact match), 2 (starts with), 
470
   *                         3 (ends with).   
471
   */
472
  public void setSubjectQueryType(final int subjectQueryType) {
473
    this.subjectQueryType = subjectQueryType;
474
  }
475
  
476
  
477
  /**
478
   * @param subjectValue The subjectValue to set.
479
   */
480
  public void setSubjectValue(final String subjectValue) {
481
    this.subjectValue = subjectValue;
482
  }
483
  
484
  
485
  /**
486
   * @param taxon The taxon to set.
487
   */
488
  public void setTaxon(final String taxon) {
489
    this.taxon = taxon;
490
  }
491
  
492
  
493
  /**
494
   * @param taxonQueryType The taxonQueryType to set.
495
   */
496
  public void setTaxonQueryType(final int taxonQueryType) {
497
    this.taxonQueryType = taxonQueryType;
498
  }
499
  
500
  
501
  /**
502
   * @param westBound The westBound to set.
503
   */
504
  public void setWestBound(final String westBound) {
505
    this.westBound = westBound;
506
  }
507

  
508
}
0 509

  
src/edu/ucsb/nceas/metacat/advancedsearch/Stylizer.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2005 University of New Mexico and the 
4
 *             Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *   '$Author$'
7
 *     '$Date$'
8
 * '$Revision$'
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
 */
24

  
25
package edu.ucsb.nceas.metacat.advancedsearch;
26

  
27
import java.io.*;
28
import javax.xml.transform.Result;
29
import javax.xml.transform.Source;
30
import javax.xml.transform.Transformer;
31
import javax.xml.transform.TransformerConfigurationException;
32
import javax.xml.transform.TransformerException;
33
import javax.xml.transform.TransformerFactory;
34

  
35
/**
36
 * @author dcosta
37
 * 
38
 * Stylizer class applies the resultset.xsl stylesheet to the pathquery
39
 * results returned by Metacat.
40
 */
41
public class Stylizer {
42

  
43
  /**
44
   * Applies the resultset.xsl stylesheet to the pathquery result string
45
   * returned by Metacat.
46
   * 
47
   * @param resultset       the pathquery result string from Metacat
48
   * @param sessionId       the user's session id
49
   * @param metacatURL      the URL to the Metacat server
50
   * 
51
   * @return htmlString     the result of the transformation from XML to HTML
52
   */
53
  public String resultsetToHTML(final String resultset, 
54
                                final String sessionId,
55
                                final String metacatURL,
56
                                final String xslPath) {
57
    String htmlString = "";
58
    Result result;
59
    StringWriter stringWriter = new StringWriter();
60
    Transformer transformer;
61
    TransformerFactory transformerFactory;
62
    Source xmlSource;
63
    File xsltFile = new File(xslPath);            
64
    Source xsltSource;
65
    StringReader stringReader = new StringReader(resultset);
66
    
67
    xmlSource = new javax.xml.transform.stream.StreamSource(stringReader);
68
    xsltSource = new javax.xml.transform.stream.StreamSource(xsltFile);
69
    result = new javax.xml.transform.stream.StreamResult(stringWriter);
70

  
71
    // create an instance of TransformerFactory
72
    transformerFactory = TransformerFactory.newInstance();
73

  
74
    try {
75
      transformer = transformerFactory.newTransformer(xsltSource);
76
      transformer.setParameter("sessid", sessionId);
77
      transformer.setParameter("metacatURL", metacatURL);
78
      transformer.transform(xmlSource, result);
79
      htmlString = stringWriter.toString();
80
    }
81
    catch (TransformerConfigurationException tce) {
82
      // Error generated by the parser
83
      Throwable x = tce;  // Use the contained exception, if any
84
        
85
      if (tce.getException() != null) {
86
        x = tce.getException();    
87
      }
88

  
89
      x.printStackTrace();   
90
    }
91
    catch (TransformerException te) {
92
      // Error generated by the parser
93
      Throwable x = te;  // Use the contained exception, if any
94
        
95
      if (te.getException() != null) {
96
        x = te.getException();    
97
      }
98

  
99
      x.printStackTrace(); 
100
    }
101
      
102
    return htmlString;
103
  }
104
  
105
}
0 106

  
src/edu/ucsb/nceas/metacat/advancedsearch/BrowseServlet.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2005 University of New Mexico and the 
4
 *             Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *   '$Author$'
7
 *     '$Date$'
8
 * '$Revision$'
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
 */
24

  
25
package edu.ucsb.nceas.metacat.advancedsearch;
26

  
27
import java.io.IOException;
28

  
29
import javax.servlet.RequestDispatcher;
30
import javax.servlet.ServletContext;
31
import javax.servlet.ServletException;
32
import javax.servlet.http.HttpServlet;
33
import javax.servlet.http.HttpServletRequest;
34
import javax.servlet.http.HttpServletResponse;
35
import javax.servlet.http.HttpSession;
36

  
37
import edu.ucsb.nceas.metacat.client.*;
38

  
39
/** 
40
 * @author dcosta
41
 * 
42
 * The BrowseServlet class executes a browse action. This is equivalent to
43
 * a simple search, but the user clicks on a link to determine the search term.
44
 */
45
public class BrowseServlet extends HttpServlet {
46

  
47
  // Instance Variables -- (Not used because they are not thread-safe.)
48

  
49
  // Methods
50
  
51
  /**
52
   * Executes a browse-based simple search.
53
   */
54
  public void doPost(HttpServletRequest request, HttpServletResponse response)
55
      throws IOException, ServletException {
56
    AdvancedSearch advancedSearch;
57
    String browseValue;
58
    RequestDispatcher dispatcher;
59
    HttpSession httpSession = request.getSession();
60
    Metacat metacat = (Metacat) httpSession.getAttribute("metacat");
61
    String metacatURL;
62
    String result = "";
63
    final String resultsJSP = "style/skins/default/advancedsearchresults.jsp";
64
    ServletContext servletContext = httpSession.getServletContext();
65
    String stylesheet = (String) httpSession.getAttribute("stylesheet");
66
    String xslPath = servletContext.getRealPath("style/common/resultset.xsl");
67

  
68
    // First check whether the metacat URL was set as a context-param.
69
    metacatURL = servletContext.getInitParameter("metacatURL");
70

  
71
    // If no metacat URL was configured, then derive the metacat URL from the
72
    // server name and server port.
73
    if (metacatURL == null || metacatURL.equals("")) {
74
      MetacatHelper metacatHelper = new MetacatHelper();
75
      String serverName = request.getServerName();
76
      int serverPort = request.getServerPort();
77
      metacatURL = metacatHelper.constructMetacatURL(serverName, serverPort);
78
    }
79

  
80
    // Tell the web server that the response is HTML
81
    response.setContentType("text/html");
82

  
83
    // Fetch the browseValue parameter from the request and execute a search
84
    advancedSearch = new AdvancedSearch(null);
85
    browseValue = request.getParameter("browseValue");
86
    result = advancedSearch.executeSearch(metacatURL, metacat, xslPath, 
87
                                          browseValue);
88

  
89
    // Store the result HTML string in the request for retrieval by
90
    // the metacatpathqueryresults.jsp form.
91
    request.setAttribute("result", result);
92

  
93
    dispatcher = request.getRequestDispatcher(resultsJSP);
94
    dispatcher.forward(request, response);
95
  }
96
  
97
}
0 98

  
src/edu/ucsb/nceas/metacat/advancedsearch/MetacatLogin.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2005 University of New Mexico and the 
4
 *             Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *   '$Author$'
7
 *     '$Date$'
8
 * '$Revision$'
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
 */
24

  
25
package edu.ucsb.nceas.metacat.advancedsearch;
26

  
27
import edu.ucsb.nceas.metacat.client.*;
28

  
29

  
30
/**
31
 * @author dcosta
32
 * 
33
 * MetacatLogin class executes a Metacat login using the Metacat client.
34
 */
35
public class MetacatLogin  {
36

  
37
  /* Object variables */
38
  private LoginBean loginBean = null;
39

  
40
  
41
  /**
42
   * Constructor. Initializes the loginBean object variable.
43
   * 
44
   * @param loginBean  the LoginBean object, holds username and password
45
   */
46
  public MetacatLogin(final LoginBean loginBean) {
47
    this.loginBean = loginBean;
48
  }
49

  
50
  
51
  /**
52
   * Executes the Metacat login.
53
   * 
54
   * @param metacatURL      URL to the metacat servlet
55
   * @param metacat         a Metacat object, possibly null
56
   * 
57
   * @return loginSuccess   true if metacat login was successful, else false
58
   */
59
  public boolean executeLogin(final String metacatURL, final Metacat metacat) {
60
    final String DN;                            // LDAP distinguished name
61
    boolean loginSuccess = false;
62
    MetacatHelper metacatHelper = new MetacatHelper();
63
    String metacatResponse = "";
64
    final String organization = loginBean.getOrganization();
65
    final String password = loginBean.getPassword();
66
    final String username = loginBean.getUsername();
67
    
68
    if (
69
        username == null || 
70
        organization == null || 
71
        password == null || 
72
        username.equals("") || 
73
        organization.equals("") || 
74
        password.equals("")) 
75
    {
76
      return loginSuccess;
77
    }
78
    else {    
79
      System.err.println("Metacat URL: " + metacatURL);
80

  
81
      try {
82
        DN = metacatHelper.constructDN(username, organization);    
83
        metacatResponse = metacat.login(DN, password);
84
        System.err.println("metacatResponse:\n" + metacatResponse);
85
        
86
        if (metacatResponse.indexOf("Authentication successful") > -1) {
87
          loginSuccess = true;
88
        }
89
      } 
90
      catch (MetacatAuthException mae) {
91
        System.err.println("MetacatAuthException:\n" + mae.getMessage());
92
      } 
93
      catch (MetacatInaccessibleException mie) {
94
        System.err.println("Metacat Inaccessible:\n" + mie.getMessage());
95
      }
96
      catch (Exception e) {
97
        System.err.println("General exception:\n" + e.getMessage());
98
        e.printStackTrace();
99
      }
100
    }
101
    
102
    return loginSuccess;
103
  }
104

  
105
}
0 106

  
src/edu/ucsb/nceas/metacat/advancedsearch/AdvancedSearchQueryTerm.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2005 University of New Mexico and the 
4
 *             Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *   '$Author$'
7
 *     '$Date$'
8
 * '$Revision$'
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
 */
24

  
25
package edu.ucsb.nceas.metacat.advancedsearch;
26

  
27
/** 
28
 * @author dcosta
29
 * 
30
 * The AdvancedSearchQueryTerm class holds the data needed to produce a xml 
31
 * string fragment representing a single queryterm in a querygroup.
32
 */
33
public class AdvancedSearchQueryTerm  {
34

  
35
  // Object variables
36
  private final String caseSensitive;//Case sensitive setting, "true" or "false"
37
  private final String indent;       // String of spaces for indenting output
38
  private final int initialLength = 100; // Initial length of the stringBuffer
39
  private final String pathExpr;         // The search field, e.g. "keyword"
40
  private final String searchMode;   // The search mode, e.g. "less-than"
41
  private StringBuffer stringBuffer; // Holds the queryterm xml string
42
  private final String value;        // The search value to match, e.g. "35"
43

  
44

  
45
  /**
46
   * Constructor. Initializes searchMode, caseSensitive, value, and indent.
47
   * 
48
   * @param searchMode       The search mode, e.g. "less-than-equals"
49
   * @param caseSensitive    Case sensitive setting, "true" or "false"
50
   * @param pathExpr         The search field, e.g. "northBoundingCoordinate"
51
   * @param value            The search value to match, e.g. "35"
52
   * @param indent           String of spaces for indenting output
53
   */
54
  public AdvancedSearchQueryTerm(final String searchMode, 
55
                                 final String caseSensitive, 
56
                                 final String pathExpr, 
57
                                 final String value, 
58
                                 final String indent
59
                         ) {
60
    this.searchMode = searchMode;
61
    this.caseSensitive = caseSensitive;
62
    this.pathExpr = pathExpr;
63
    this.value = value;
64
    this.indent = indent;
65
    stringBuffer = new StringBuffer(initialLength);
66
  }
67
  
68

  
69
  /**
70
   * Produce a xml string fragment that represents this queryterm.
71
   * 
72
   * @return    A xml string fragment that represents this queryterm.
73
   */
74
  public String toString() {
75
    stringBuffer.append(indent + 
76
                        "<queryterm searchmode=\"" + 
77
                        searchMode + 
78
                        "\" casesensitive=\"" + 
79
                        caseSensitive + 
80
                        "\">\n"
81
                       );
82

  
83
    stringBuffer.append(indent + "  <value>" + value + "</value>\n");
84

  
85
    // For a simple search or a browse search, the pathExpr string will be "".
86
    if (!pathExpr.equals("")) {
87
      stringBuffer.append(indent + "  <pathexpr>" + pathExpr + "</pathexpr>\n");
88
    }
89
    
90
    stringBuffer.append(indent + "</queryterm>\n");
91

  
92
    return stringBuffer.toString();
93
  }
94

  
95
}
0 96

  
src/edu/ucsb/nceas/metacat/advancedsearch/SearchServlet.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2005 University of New Mexico and the 
4
 *             Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *   '$Author$'
7
 *     '$Date$'
8
 * '$Revision$'
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
 */
24

  
25
package edu.ucsb.nceas.metacat.advancedsearch;
26

  
27
import java.io.IOException;
28
import javax.servlet.RequestDispatcher;
29
import javax.servlet.ServletContext;
30
import javax.servlet.ServletException;
31
import javax.servlet.http.HttpServlet;
32
import javax.servlet.http.HttpServletRequest;
33
import javax.servlet.http.HttpServletResponse;
34
import javax.servlet.http.HttpSession;
35

  
36
import edu.ucsb.nceas.metacat.client.*;
37

  
38
/** 
39
 * @author dcosta
40
 * 
41
 * The SearchServlet class executes a search action. This corresponds to when a
42
 * user fills in a simple search text box and clicks "Search".
43
 */
44
public class SearchServlet extends HttpServlet {
45

  
46
  // Instance Variables -- (Not used because they are not thread-safe.)
47

  
48
  // Methods
49
  
50
  /**
51
   * Executes a simple search.
52
   */
53
  public void doPost(HttpServletRequest request, HttpServletResponse response)
54
      throws IOException, ServletException {
55
    AdvancedSearch advancedSearch;
56
    RequestDispatcher dispatcher;
57
    HttpSession httpSession = request.getSession();
58
    Metacat metacat = (Metacat) httpSession.getAttribute("metacat");
59
    String metacatURL;
60
    String result = "";
61
    final String resultsJSP = "style/skins/default/advancedsearchresults.jsp";
62
    String searchValue;
63
    ServletContext servletContext = httpSession.getServletContext();
64
    String stylesheet = (String) httpSession.getAttribute("stylesheet");
65
    String xslPath = servletContext.getRealPath("style/common/resultset.xsl");
66

  
67
    // First check whether the metacat URL was set as a context-param.
68
    metacatURL = servletContext.getInitParameter("metacatURL");
69

  
70
    // If no metacat URL was configured, then derive the metacat URL from the
71
    // server name and server port.
72
    if (metacatURL == null || metacatURL.equals("")) {
73
      MetacatHelper metacatHelper = new MetacatHelper();
74
      String serverName = request.getServerName();
75
      int serverPort = request.getServerPort();
76
      metacatURL = metacatHelper.constructMetacatURL(serverName, serverPort);
77
    }
78

  
79
    // Tell the web server that the response is HTML
80
    response.setContentType("text/html");
81

  
82
    // Fetch the searchValue parameter from the request and execute a search
83
    advancedSearch = new AdvancedSearch(null);
84
    searchValue = request.getParameter("searchValue");
85
    System.err.println("Search Servlet: " + searchValue);
86
    result = advancedSearch.executeSearch(metacatURL, metacat, xslPath, searchValue);
87

  
88
    // Store the result HTML string in the request for retrieval by
89
    // the metacatpathqueryresults.jsp form.
90
    request.setAttribute("result", result);
91

  
92
    dispatcher = request.getRequestDispatcher(resultsJSP);
93
    dispatcher.forward(request, response);
94
  }
95
  
96
}
0 97

  
src/edu/ucsb/nceas/metacat/advancedsearch/AdvancedSearch.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2005 University of New Mexico and the 
4
 *             Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *
7
 *   '$Author$'
8
 *     '$Date$'
9
 * '$Revision$'
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
 */
25

  
26
package edu.ucsb.nceas.metacat.advancedsearch;
27

  
28
import java.io.Reader;
29
import java.io.StringReader;
30
import java.util.ArrayList;
31
import java.util.StringTokenizer;
32

  
33
import edu.ucsb.nceas.metacat.client.*;
34
import edu.ucsb.nceas.utilities.*;
35

  
36
/**
37
 * @author dcosta
38
 * 
39
 * AdvancedSearch class constructs queries that use the pathquery feature of 
40
 * Metacat. It can execute either an advanced search, where the user fills in
41
 * fields in a web form, or a simple search on a string.
42
 */
43
public class AdvancedSearch  {
44

  
45
  // Object variables
46
  private AdvancedSearchBean advancedSearchBean = null;
47
  private String caseSensitive;
48
  private final String globalOperator;
49
  private boolean hasSubjectSearch = false;
50
  private boolean hasAuthorSearch = false;
51
  private boolean hasSpatialSearch = false;
52
  private boolean hasTaxonomicSearch = false;
53
  private boolean hasTemporalSearch = false;
54
  private boolean hasSiteFilter = false;
55
  private int indentLevel = 2;
56
  private boolean isCaseSensitive = false;
57
  private AdvancedSearchPathQuery pathQuery;
58
  private AdvancedSearchQueryGroup queryGroup;
59
  private String queryString;
60
  private String site;
61
  private final String title = "Advanced Search";
62

  
63

  
64
  /**
65
   * Constructor. Used when the user has filled in an Advanced Search form.
66
   * The property values are contained in the advancedSearchBean object.
67
   * For a simple search, the advancedSearchBean object is passed as null.
68
   * 
69
   * @param advancedSearchBean   An AdvancedSearch bean.
70
   */
71
  public AdvancedSearch(final AdvancedSearchBean advancedSearchBean) {
72
    int allAny = AdvancedSearchBean.MATCH_ALL;
73
    String indent = getIndent(indentLevel * 1);
74
    final String operator;
75
    
76
    if (advancedSearchBean != null) {
77
      allAny = advancedSearchBean.getFormAllAny();
78
      this.isCaseSensitive = advancedSearchBean.isCaseSensitive();
79
      site = advancedSearchBean.getSiteValue();
80
    }
81

  
82
    if (allAny == AdvancedSearchBean.MATCH_ALL) {
83
      globalOperator = "INTERSECT";
84
    }
85
    else {
86
      globalOperator = "UNION";
87
    }
88
    
89
    if (isCaseSensitive == true) {
90
      this.caseSensitive = "true";
91
    }
92
    else {
93
      this.caseSensitive = "false";
94
    }
95

  
96
    this.queryGroup = new AdvancedSearchQueryGroup(globalOperator, indent);
97
    this.pathQuery = new AdvancedSearchPathQuery(title, queryGroup, indent);
98
    this.advancedSearchBean = advancedSearchBean;
99
  }
100
  
101

  
102
  /**
103
   * Adds a string to an ArrayList of terms. An auxiliary method to the
104
   * parseTermsAdvanced() method.
105
   * 
106
   * @param terms      ArrayList of strings.
107
   * @param term       the new string to add to the ArrayList, but only if
108
   *                   it isn't an empty string.
109
   */
110
  private void addTerm(ArrayList terms, final StringBuffer term) {
111
    final String s = term.toString().trim();
112
      
113
    if (s.length() > 0) {
114
      terms.add(s);
115
    }
116
  }
117

  
118

  
119
  /**
120
   * A full subject query searches the title, abstract, and keyword sections of
121
   * the document. Individual searches on these sections is also supported.
122
   */
123
  private void buildQuerySubject() {
124
    int allAny = advancedSearchBean.getSubjectAllAny();
125
    String emlField;
126
    String indent;
127
    final String innerOperator = "UNION";
128
    AdvancedSearchQueryGroup innerQuery = null;
129
    final String outerOperator;
130
    AdvancedSearchQueryGroup outerQuery;
131
    AdvancedSearchQueryTerm qt;
132
    String searchMode;
133
    final String subjectField = advancedSearchBean.getSubjectField();
134
    final int subjectQueryType = advancedSearchBean.getSubjectQueryType();
135
    String term;
136
    ArrayList terms;
137
    String value = advancedSearchBean.getSubjectValue();
138
 
139
    if ((value != null) && (!(value.equals("")))) {
140
      hasSubjectSearch = true;
141

  
142
      if (allAny == AdvancedSearchBean.MATCH_ALL) {
143
        outerOperator = "INTERSECT";
144
      }
145
      else {
146
        outerOperator = "UNION";
147
      }
148

  
149
      indent = getIndent(indentLevel * 2);
150
      outerQuery = new AdvancedSearchQueryGroup(outerOperator, indent);
151
      terms = parseTermsAdvanced(value);
152
      searchMode = metacatSearchMode(subjectQueryType);
153
      
154
      for (int i = 0; i < terms.size(); i++) {
155
        term = (String) terms.get(i);
156
        indent = getIndent(indentLevel * 3);
157
        innerQuery = new AdvancedSearchQueryGroup(innerOperator, indent);
158
        indent = getIndent(indentLevel * 4);
159
            
160
        if (subjectField.equals("ALL") || subjectField.equals("TITLE")) {
161
          emlField = "dataset/title";
162
          qt = new AdvancedSearchQueryTerm(searchMode, caseSensitive, emlField, 
163
                                           term, indent);
164
          innerQuery.addQueryTerm(qt);
165
        }
166

  
167
        if (subjectField.equals("ALL") || subjectField.equals("ABSTRACT")) {
168
          emlField = "dataset/abstract/para";
169
          qt = new AdvancedSearchQueryTerm(searchMode, caseSensitive, emlField, 
170
                                           term, indent);
171
          innerQuery.addQueryTerm(qt);
172

  
173
          emlField = "dataset/abstract/section/para";
174
          qt = new AdvancedSearchQueryTerm(searchMode, caseSensitive, emlField, 
175
                                           term, indent);
176
          innerQuery.addQueryTerm(qt);
177
        }
178

  
179
        if (subjectField.equals("ALL") || subjectField.equals("KEYWORDS")) {
180
          emlField = "keywordSet/keyword";
181
          qt = new AdvancedSearchQueryTerm(searchMode, caseSensitive, emlField, 
182
                                           term, indent);
183
          innerQuery.addQueryTerm(qt);
184
        }
185
      
186
        outerQuery.addQueryGroup(innerQuery);
187
      }
188

  
189
      // Minimize the number of query groups that get created, depending on
190
      // which criteria the user specified.
191
      //
192
      if (terms.size() > 1) {
193
        queryGroup.addQueryGroup(outerQuery);
194
      }
195
      else if (terms.size() == 1){
196
        queryGroup.addQueryGroup(innerQuery);
197
      }
198
    }
199

  
200
  }
201
  
202

  
203
  /**
204
   * An author query will search the creator/individualName/surName field, the
205
   * creator/organizationName field, or an intersection of both fields.
206
   */
207
  private void buildQueryAuthor() {
208
    boolean addQueryGroup = false;
209
    final int creatorSurnameQueryType = 
210
                                advancedSearchBean.getCreatorSurnameQueryType();
211
    final int creatorOrganizationQueryType = 
212
                           advancedSearchBean.getCreatorOrganizationQueryType();
213
    String emlField;
214
    String indent = getIndent(indentLevel * 2);
215
    AdvancedSearchQueryGroup qg = 
216
                           new AdvancedSearchQueryGroup(globalOperator, indent);
217
    AdvancedSearchQueryTerm qt;
218
    String searchMode;
219
    String value = advancedSearchBean.getCreatorSurname();
220

  
221
    indent = getIndent(indentLevel * 3);
222
    if ((value != null) && (!(value.equals("")))) {
223
      emlField = "dataset/creator/individualName/surName";
224
      searchMode = metacatSearchMode(creatorSurnameQueryType);
225
      qt = new AdvancedSearchQueryTerm(searchMode, caseSensitive, emlField, 
226
                                       value, indent);
227
      qg.addQueryTerm(qt);        
228
      addQueryGroup = true;
229
    }
230

  
231
    value = advancedSearchBean.getCreatorOrganization();
232
      
233
    if ((value != null) && (!(value.equals("")))) {
234
      emlField = "creator/organizationName";
235
      searchMode = metacatSearchMode(creatorOrganizationQueryType);
236
      qt = new AdvancedSearchQueryTerm(searchMode, caseSensitive, emlField, 
237
                                       value, indent);
238
      qg.addQueryTerm(qt);        
239
      addQueryGroup = true;
240
    }
241
    
242
    if (addQueryGroup) {      
243
      hasAuthorSearch = true;
244
      queryGroup.addQueryGroup(qg);
245
    }
246
  }
247
  
248

  
249
  /**
250
   * Two kinds of spatial searches are supported. The first is on a specific
251
   * named location. The second is on north/south/east/west bounding
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff