Navigation Menu

Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/stable-develop' into stable-develop
Browse files Browse the repository at this point in the history
  • Loading branch information
François Prunayre committed Jun 30, 2014
2 parents 9ceb7a1 + ab360ce commit aebda42
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 12 deletions.
Expand Up @@ -153,7 +153,7 @@ public Query build(LuceneQueryInput luceneQueryInput) {
// search criteria fields may contain zero or more _OR_ in their name, in which case the search will be a
// disjunction of searches for fieldnames separated by that.
//
// here such _OR_ fields are parsed, an OR searchCriteria map is created, and theyre removed from vanilla
// here such _OR_ fields are parsed, an OR searchCriteria map is created, and they're removed from vanilla
// searchCriteria map.
//
Map<String, Set<String>> searchCriteriaOR = new HashMap<String, Set<String>>();
Expand Down
Expand Up @@ -709,6 +709,37 @@ private void computeQuery(ServiceContext srvContext, Element request, ServiceCon
if(Log.isDebugEnabled(Geonet.LUCENE))
Log.debug(Geonet.LUCENE, "requestedLanguageOnly: " + requestedLanguageOnly);

// --- operation parameter
List<Content> operations = new ArrayList<Content>(request.getChildren("operation"));
// removes the parameter from the request
request.removeChildren("operation");

// Handles operation (filter by download / dynamic visible to the
// current user)
if (operations.size() > 0) {
StringBuilder grpList = new StringBuilder();
for (Integer g : userGroups) {
if (grpList.length() > 0)
grpList.append(" or ");
grpList.append(g);
}
String grps = grpList.toString();
for (Content elem : operations) {
if (elem.getValue().equalsIgnoreCase("view")) {
request.addContent(new Element("_operation0").addContent(grps));
} else if (elem.getValue().equalsIgnoreCase("download")) {
request.addContent(new Element("_operation1").addContent(grps));
} else if (elem.getValue().equalsIgnoreCase("editing")) {
request.addContent(new Element("_operation2").addContent(grps));
} else if (elem.getValue().equalsIgnoreCase("notify")) {
request.addContent(new Element("_operation3").addContent(grps));
} else if (elem.getValue().equalsIgnoreCase("dynamic")) {
request.addContent(new Element("_operation5").addContent(grps));
} else if (elem.getValue().equalsIgnoreCase("featured")) {
request.addContent(new Element("_operation6").addContent(grps));
}
}
}

if (_styleSheetName.equals(Geonet.File.SEARCH_Z3950_SERVER)) {
// Construct Lucene query by XSLT, not Java, for Z3950 anyway :-)
Expand Down Expand Up @@ -739,7 +770,7 @@ private void computeQuery(ServiceContext srvContext, Element request, ServiceCon
//System.out.println("** error rewriting query: "+x.getMessage());
}
}

// Boosting query
if (_boostQueryClass != null) {
try {
Expand Down
Expand Up @@ -94,4 +94,12 @@ public class SearchParameter {
public static final String VALID = "valid";
public static final String HASFEATURECAT="hasfeaturecat";

public static final String OP_VIEW = "_operation0";
public static final String OP_DOWNLOAD = "_operation1";
public static final String OP_EDITING = "_operation2";
public static final String OP_NOTIFY = "_operation3";
public static final String OP_DYNAMIC = "_operation5";
public static final String OP_FEATURED = "_operation6";


}
Expand Up @@ -90,6 +90,12 @@ public class UserQueryInput {
searchParamToLuceneField.put(SearchParameter.THEMEKEY, LuceneIndexField.KEYWORD);
searchParamToLuceneField.put(SearchParameter.TOPICCATEGORY, LuceneIndexField.TOPIC_CATEGORY);
searchParamToLuceneField.put(SearchParameter.CATEGORY, LuceneIndexField.CAT);
searchParamToLuceneField.put(SearchParameter.OP_VIEW, LuceneIndexField._OP0);
searchParamToLuceneField.put(SearchParameter.OP_DOWNLOAD, LuceneIndexField._OP1);
searchParamToLuceneField.put(SearchParameter.OP_EDITING, LuceneIndexField._OP2);
searchParamToLuceneField.put(SearchParameter.OP_NOTIFY, LuceneIndexField._OP3);
searchParamToLuceneField.put(SearchParameter.OP_DYNAMIC, LuceneIndexField._OP5);
searchParamToLuceneField.put(SearchParameter.OP_FEATURED, LuceneIndexField._OP6);
}
private Map<String, Set<String>> searchCriteria = new HashMap<String, Set<String>>();
private Map<String, Set<String>> searchPrivilegeCriteria = new HashMap<String, Set<String>>();
Expand Down Expand Up @@ -161,9 +167,11 @@ public UserQueryInput(Element jdom) {
setSimilarity(jdom.getChildText(SearchParameter.SIMILARITY));
} else {
if (StringUtils.isNotBlank(nodeValue)) {

if (SECURITY_FIELDS.contains(nodeName)
|| nodeName.contains("_op")) {
// Handles operation parameters. These parameters are safe, because
// the fields have been sanitized before (in LuceneSearcher.java:713)
if (nodeName.startsWith("_operation")) {
addValues(searchCriteria, searchParamToLuceneField.get(nodeName), nodeValue);
} else if (SECURITY_FIELDS.contains(nodeName) || nodeName.contains("_op")) {
addValues(searchPrivilegeCriteria, nodeName, nodeValue);
} else if (RESERVED_FIELDS.contains(nodeName)) {
searchOption.put(nodeName, nodeValue);
Expand Down Expand Up @@ -310,4 +318,5 @@ public String getEditable() {
return editable;
}


}
@@ -1,6 +1,15 @@
package org.fao.geonet.kernel.search;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import jeeves.server.ServiceConfig;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.KeywordAnalyzer;
import org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper;
Expand All @@ -14,13 +23,6 @@
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static org.junit.Assert.assertEquals;

/**
* Unit test for building Lucene queries.
*
Expand Down Expand Up @@ -2246,4 +2248,71 @@ public void testDigitalAndPaper() {
// verify query
assertEquals("unexpected Lucene query", "+paper:true +_isTemplate:n", query.toString());
}

/**
* Tests passing operations (download and/or dynamic for instance) parameters criterias.
*
*/
@Test
public void testDownloadDynamicParameter() {
JDOMFactory factory = new DefaultJDOMFactory();
Element request = factory.element("request");
Element download = factory.element("_operation5").addContent("1 or 2 or 3");
Element dynamic = factory.element("_operation1").addContent("1 or 2 or 3");
request.addContent(download).addContent(dynamic);
LuceneQueryInput lQI = new LuceneQueryInput(request);
Query query = new LuceneQueryBuilder(_tokenizedFieldSet, _numericFieldSet, _analyzer, null).build(lQI);
assertTrue(query.toString().contains("+(_op5:1 _op5:2 _op5:3) +(_op1:1 _op1:2 _op1:3)"));
}
/**
* Same test as above, but only download.
*/
@Test
public void testDownloadParameter() {
JDOMFactory factory = new DefaultJDOMFactory();
Element request = factory.element("request");
Element download = factory.element("_operation1").addContent("1 or 2 or 3");
request.addContent(download);
LuceneQueryInput lQI = new LuceneQueryInput(request);
Query query = new LuceneQueryBuilder(_tokenizedFieldSet, _numericFieldSet, _analyzer, null).build(lQI);
assertTrue(query.toString().contains("+(_op1:1 _op1:2 _op1:3)"));
}
/**
* Only dynamic operation parameter.
*/
@Test
public void testDynamicParameter() {
JDOMFactory factory = new DefaultJDOMFactory();
Element request = factory.element("request");
Element dynamic = factory.element("_operation5").addContent("1 or 2 or 3");
request.addContent(dynamic);
LuceneQueryInput lQI = new LuceneQueryInput(request);
Query query = new LuceneQueryBuilder(_tokenizedFieldSet, _numericFieldSet, _analyzer, null).build(lQI);
assertTrue(query.toString().contains("+(_op5:1 _op5:2 _op5:3)"));
}
/**
* Only editing operation parameter.
*/
@Test
public void testEditingParameter() {
JDOMFactory factory = new DefaultJDOMFactory();
Element request = factory.element("request");
Element editing = factory.element("_operation2").addContent("1 or 2 or 3");
request.addContent(editing);
LuceneQueryInput lQI = new LuceneQueryInput(request);
Query query = new LuceneQueryBuilder(_tokenizedFieldSet, _numericFieldSet, _analyzer, null).build(lQI);
assertTrue(query.toString().contains("+(_op2:1 _op2:2 _op2:3)"));
}

/**
* No operation parameter.
*/
@Test
public void testWithoutOperationParameter() {
JDOMFactory factory = new DefaultJDOMFactory();
Element request = factory.element("request");
LuceneQueryInput lQI = new LuceneQueryInput(request);
Query query = new LuceneQueryBuilder(_tokenizedFieldSet, _numericFieldSet, _analyzer, null).build(lQI);
assertTrue(query.toString().equals("+_isTemplate:n"));
}
}

0 comments on commit aebda42

Please sign in to comment.