/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.solr.client.solrj.io.eval;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.solr.client.solrj.io.Tuple;
import org.apache.solr.client.solrj.io.stream.expr.Explanation;
import org.apache.solr.client.solrj.io.stream.expr.Explanation.ExpressionType;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParameter;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionValue;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;

public class FieldValueEvaluator extends SourceEvaluator {
  private static final long serialVersionUID = 1L;
  
  private String fieldName;
  private boolean literal;
  
  public FieldValueEvaluator(String fieldName) {
    if(fieldName.startsWith("\"") && fieldName.endsWith("\"") && fieldName.length() > 1){
      fieldName = fieldName.substring(1, fieldName.length() - 1);
      literal = true;
    }
    
    this.fieldName = fieldName;
  }
  
  @Override
  public Object evaluate(Tuple tuple) throws IOException {
    if(literal) {
      return fieldName;
    }

    Object value = tuple.get(fieldName);
    
    // This is somewhat radical.
    // Here, we allow for the use of the context to provide alternative values
    // when they are not available in the provided tuple. This means that all
    // evaluators can evaluate over both a stream's tuple and the context, and
    // can even evaluate over fields from both of them in the same evaluation
    if(null == value && null != getStreamContext()){
      value = getStreamContext().getLets().get(fieldName);
      
      // If what's contained in the context is itself an evaluator then
      // we need to evaluate it
      if(value instanceof StreamEvaluator){
        value = ((StreamEvaluator)value).evaluate(tuple);
      }
    }
    
    // if we have an array then convert to an ArrayList
    // if we have an iterable that is not a list then convert to ArrayList
    // lists are good to go
    if(null != value){
      if(value instanceof Object[]){
        Object[] array = (Object[])value;
        List<Object> list = new ArrayList<Object>(array.length);
        for(Object obj : array){
          list.add(obj);
        }
        return list;
      } else if(value instanceof Matrix) {
        return value;
      } else if(value instanceof VectorFunction) {
        return value;
      } else if(value instanceof Iterable && !(value instanceof List<?>)){
        Iterable<?> iter = (Iterable<?>)value;
        List<Object> list = new ArrayList<Object>();
        for(Object obj : iter){
          list.add(obj);
        }
        return list;
      }
    }

    return value;
  }
  
  @Override
  public StreamExpressionParameter toExpression(StreamFactory factory) throws IOException {
    return new StreamExpressionValue(fieldName);
  }

  @Override
  public Explanation toExplanation(StreamFactory factory) throws IOException {
    return new Explanation(nodeId.toString())
      .withExpressionType(ExpressionType.EVALUATOR)
      .withImplementingClass(getClass().getName())
      .withExpression(toExpression(factory).toString());
  }

}

@@ -1440,18 +1440,16 @@
 D = 1L;%0A
-  
 %0A  priva
@@ -1473,37 +1473,8 @@
 me;%0A
-  private boolean literal;%0A  
 %0A  p
@@ -1544,26 +1544,25 @@
 startsWith(%22
-%5C%22
+'
 %22) && fieldN
@@ -1579,10 +1579,9 @@
 th(%22
-%5C%22
+'
 %22) &
@@ -1677,40 +1677,14 @@
 1);%0A
-      literal = true;%0A
     %7D%0A
-    
 %0A   
@@ -1712,26 +1712,24 @@
 ldName;%0A  %7D%0A
-  
 %0A  @Override
@@ -1792,57 +1792,8 @@
 n %7B%0A
-    if(literal) %7B%0A      return fieldName;%0A    %7D%0A%0A
     
@@ -1829,20 +1829,16 @@
 dName);%0A
-    
 %0A    // 
@@ -2289,22 +2289,16 @@
 dName);%0A
-      
 %0A      /
@@ -2510,20 +2510,16 @@
 %7D%0A    %7D%0A
-    
 %0A    // 
@@ -3338,24 +3338,79 @@
    %7D%0A    %7D%0A%0A
+    if(value == null) %7B%0A      return fieldName;%0A    %7D%0A%0A
     return v
@@ -3419,18 +3419,16 @@
 ue;%0A  %7D%0A
-  
 %0A  @Over
@@ -3713,32 +3713,34 @@
 String())%0A      
+  
 .withExpressionT
@@ -3771,24 +3771,26 @@
 ATOR)%0A      
+  
 .withImpleme
@@ -3818,24 +3818,26 @@
 .getName())%0A
+  
       .withE
@@ -3887,9 +3887,8 @@
 ;%0A  %7D%0A%0A%7D
-%0A

Rollback inadvertent code change during documentation updates.