Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

DS-1372: Change OAI2.0 DSpaceMetadataExistsFilter to check for one or

more fields

Change to org.dspace.xoai.filter.DSpaceMetadataExistsFilter to accept
multiple fields, where at least one is required to accept an item.
  • Loading branch information...
commit f75f901cde8a461ae8cde2fd1097798a0825055e 1 parent 459d0d1
@arieljlira arieljlira authored
View
58 dspace-oai/src/main/java/org/dspace/xoai/filter/DSpaceMetadataExistsFilter.java
@@ -8,16 +8,21 @@
package org.dspace.xoai.filter;
import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.dspace.core.Context;
import org.dspace.xoai.data.DSpaceItem;
import org.dspace.xoai.exceptions.InvalidMetadataFieldException;
+import org.dspace.xoai.filter.DSpaceFilter;
+import org.dspace.xoai.filter.DatabaseFilterResult;
+import org.dspace.xoai.filter.SolrFilterResult;
import org.dspace.xoai.util.MetadataFieldManager;
/**
- *
+ *
* @author Lyncode Development Team <dspace@lyncode.com>
*/
public class DSpaceMetadataExistsFilter extends DSpaceFilter
@@ -25,15 +30,20 @@
private static Logger log = LogManager
.getLogger(DSpaceMetadataExistsFilter.class);
- private String _field;
+ private List<String> _fields;
- private String getField()
+ private List<String> getFields()
{
- if (this._field == null)
+ if (this._fields == null)
{
- _field = super.getParameter("field");
+ this._fields = super.getParameters("fields");
+
+ //backwards compatibility check for field parameter
+ if (this._fields == null)
+ this._fields = super.getParameters("field");
+
}
- return _field;
+ return _fields;
}
@Override
@@ -41,9 +51,20 @@ public DatabaseFilterResult getWhere(Context context)
{
try
{
- return new DatabaseFilterResult(
- "EXISTS (SELECT tmp.* FROM metadatavalue tmp WHERE tmp.item_id=i.item_id AND tmp.metadata_field_id=?)",
- MetadataFieldManager.getFieldID(context, this.getField()));
+ List<String> fields = this.getFields();
+ StringBuilder where = new StringBuilder();
+ List<Object> args = new ArrayList<Object>(fields.size());
+ where.append("(");
+ for (int i = 0; i < fields.size(); i++) {
+ where.append("EXISTS (SELECT tmp.* FROM metadatavalue tmp WHERE tmp.item_id=i.item_id AND tmp.metadata_field_id=?)");
+ args.add(MetadataFieldManager.getFieldID(context, fields.get(i)));
+
+ if (i < fields.size() -1 )
+ where.append(" OR ") ;
+ }
+ where.append(")");
+
+ return new DatabaseFilterResult(where.toString(), args);
}
catch (InvalidMetadataFieldException e)
{
@@ -59,16 +80,27 @@ public DatabaseFilterResult getWhere(Context context)
@Override
public boolean isShown(DSpaceItem item)
{
- if (item.getMetadata(this.getField()).size() > 0)
- return true;
-
+ for (String field : this.getFields()) {
+ //do we have a match? if yes, our job is done
+ if (item.getMetadata(field).size() > 0)
+ return true;
+ }
return false;
}
@Override
public SolrFilterResult getQuery()
{
- return new SolrFilterResult("metadata." + this.getField() + ":[* TO *]");
+ StringBuilder cond = new StringBuilder("(");
+ List<String> fields = this.getFields();
+ for (int i = 0; i < fields.size(); i++) {
+ cond.append("metadata.").append(fields.get(i)).append(":[* TO *]");
+ if (i < fields.size() -1 )
+ cond.append(" OR ") ;
+ }
+ cond.append(")");
+
+ return new SolrFilterResult(cond.toString());
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.