Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #128 from sedici/DS-1372

DS-1372: Change OAI2.0 DSpaceMetadataExistsFilter to check for one or more fields
  • Loading branch information...
commit d70faeb6d35d48def7beab923edd392628f32b8e 2 parents b49027b + eb9d43f
@lyncodev lyncodev authored
View
6 dspace-oai/pom.xml
@@ -111,5 +111,11 @@
<name>Ivan Másar</name>
<email>helix84@centrum.sk</email>
</developer>
+ <developer>
+ <name>Ariel J. Lira</name>
+ <email>arieljlira@gmail.com</email>
+ <organization>SeDiCI</organization>
+ <organizationUrl>http://sedici.unlp.edu.ar</organizationUrl>
+ </developer>
</developers>
</project>
View
64 dspace-oai/src/main/java/org/dspace/xoai/filter/DSpaceMetadataExistsFilter.java
@@ -8,16 +8,27 @@
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;
/**
- *
+ * This filter allows one to retrieve (from the data source) those items
+ * which contains at least one metadata field value defined, it allows
+ * one to define multiple metadata fields to check against.
+ *
+ * One line summary: At least one metadata field defined
+ *
+ * @author Ariel J. Lira <arieljlira@gmail.com>
* @author Lyncode Development Team <dspace@lyncode.com>
*/
public class DSpaceMetadataExistsFilter extends DSpaceFilter
@@ -25,15 +36,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 +57,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 +86,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.