Hi Lance!

On Wed, 2010-07-28 at 02:31 +0200, Lance Norskog wrote:
> Should this go into the trunk, or does it only solve problems unique
> to your use case?

The solution is generic but is an extension of XPathEntityProcessor
because I didn't want to touch the solr.war. This way I can deploy the
extension into SOLR_HOME/lib.
The problem that it solves is not one with XPathEntityProcessor but more
general. What it does:

It adds an attribute to the entity that I called "skipIfEmpty" which
takes the variable (it could even take more variables seperated by
whitespace).
On entityProcessor.init() which is called for sub-entities per row of
root entity (:= before every new request to the data source), the value
of the attribute is resolved and if it is null or empty (after
trimming), the entity is not further processed.
This attribute is only allowed on sub-entities.

It would probably be nicer to put that somewhere higher up in the class
hierarchy so that all entity processors could make use of it.
But I don't know how common the use case is - all examples I found where
more or less "joins" on primary keys.

Cheers,
Chantal

Here comes the code==================================

import static
org.apache.solr.handler.dataimport.DataImportHandlerException.SEVERE;

import java.util.Map;
import java.util.logging.Logger;

import org.apache.solr.handler.dataimport.Context;
import org.apache.solr.handler.dataimport.DataImportHandlerException;
import org.apache.solr.handler.dataimport.XPathEntityProcessor;

public class OptionalXPathEntityProcessor extends XPathEntityProcessor {
        private Logger log =
Logger.getLogger(OptionalXPathEntityProcessor.class.getName());
        private static final String SKIP_IF_EMPTY = "skipIfEmpty";
        private boolean skip = false;

        @Override
        protected void firstInit(Context context) {
                if (context.isRootEntity()) {
                        throw new DataImportHandlerException(SEVERE,
"OptionalXPathEntityProcessor not allowed for root entities.");
                }
                super.firstInit(context);
        }

        @Override
        public void init(Context context) {
                String value = 
context.getResolvedEntityAttribute(SKIP_IF_EMPTY);
                if (value == null || value.trim().isEmpty()) {
                        skip = true;
                } else {
                        super.init(context);
                        skip = false;
                }
        }

        @Override
        public Map<String, Object> nextRow() {
                if (skip) return null;
                return super.nextRow();
        }
}


Reply via email to