// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.indexinglanguage;

import com.yahoo.document.*;
import com.yahoo.document.fieldpathupdate.FieldPathUpdate;
import com.yahoo.document.update.FieldUpdate;
import com.yahoo.document.update.ValueUpdate;
import com.yahoo.vespa.indexinglanguage.expressions.Expression;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Simon Thoresen Hult
 */
@SuppressWarnings("rawtypes")
public class SimpleAdapterFactory implements AdapterFactory {
    public static class SelectExpression {
        public Expression selectExpression(DocumentType documentType, String fieldName) {
            return null;
        }
    }
    private final SelectExpression expressionSelector;

    public SimpleAdapterFactory() {
        this(new SelectExpression());
    }
    public SimpleAdapterFactory(SelectExpression expressionSelector) {
        this.expressionSelector = expressionSelector;
    }

    @Override
    public DocumentAdapter newDocumentAdapter(Document doc) {
        return newDocumentAdapter(doc, false);
    }

    public DocumentAdapter newDocumentAdapter(Document doc, boolean isUpdate) {
        if (isUpdate) {
            return new SimpleDocumentAdapter(doc);
        }
        return new SimpleDocumentAdapter(doc, doc);
    }

    @Override
    public List<UpdateAdapter> newUpdateAdapterList(DocumentUpdate upd) {
        List<UpdateAdapter> ret = new ArrayList<>();
        DocumentType docType = upd.getDocumentType();
        DocumentId docId = upd.getId();
        Document complete = new Document(docType, upd.getId());
        for (FieldPathUpdate fieldUpd : upd) {
            if (FieldPathUpdateHelper.isComplete(fieldUpd)) {
                // A 'complete' field path update is basically a regular top-level field update
                // in wolf's clothing. Convert it to a regular field update to be friendlier
                // towards the search core backend.
                FieldPathUpdateHelper.applyUpdate(fieldUpd, complete);
            } else {
                ret.add(new IdentityFieldPathUpdateAdapter(fieldUpd, newDocumentAdapter(complete, true)));
            }
        }
        for (FieldUpdate fieldUpd : upd.getFieldUpdates()) {
            Field field = fieldUpd.getField();
            for (ValueUpdate valueUpd : fieldUpd.getValueUpdates()) {
                if (FieldUpdateHelper.isComplete(field, valueUpd)) {
                    FieldUpdateHelper.applyUpdate(field, valueUpd, complete);
                } else {
                    Document partial = FieldUpdateHelper.newPartialDocument(docType, docId, field, valueUpd);
                    ret.add(FieldUpdateAdapter.fromPartialUpdate(expressionSelector.selectExpression(docType, field.getName()),newDocumentAdapter(partial, true), valueUpd));
                }
            }
        }
        ret.add(FieldUpdateAdapter.fromCompleteUpdate(newDocumentAdapter(complete, true)));
        return ret;
    }
}

@@ -1717,32 +1717,54 @@
 eldUpd : upd) %7B%0A
+            try %7B%0A    
             if (
@@ -1825,16 +1825,20 @@
         
+   
+ 
 // A 'co
@@ -1925,16 +1925,20 @@
         
+    
 // in wo
@@ -2022,16 +2022,20 @@
         
+   
+ 
 // towar
@@ -2062,16 +2062,20 @@
 ackend.%0A
+    
         
@@ -2129,32 +2129,36 @@
 Upd, complete);%0A
+    
             %7D el
@@ -2170,32 +2170,36 @@
                 
+    
 ret.add(new Iden
@@ -2265,32 +2265,253 @@
 plete, true)));%0A
+                %7D%0A            %7D catch (NullPointerException e) %7B%0A                throw new IllegalArgumentException(%22Exception during handling of update '%22 + fieldUpd + %22' to field '%22 + fieldUpd.getFieldPath() + %22'%22, e);%0A
             %7D%0A  
@@ -2688,32 +2688,58 @@
 lueUpdates()) %7B%0A
+                try %7B%0A    
                 
@@ -2807,24 +2807,28 @@
             
+   
+ 
 FieldUpdateH
@@ -2881,32 +2881,36 @@
                 
+    
 %7D else %7B%0A       
@@ -2894,32 +2894,36 @@
        %7D else %7B%0A
+    
                 
@@ -3028,32 +3028,36 @@
                 
+    
 ret.add(FieldUpd
@@ -3147,16 +3147,17 @@
 Name()),
+ 
 newDocum
@@ -3191,24 +3191,239 @@
 valueUpd));%0A
+                    %7D%0A                %7D catch (NullPointerException e) %7B%0A                    throw new IllegalArgumentException(%22Exception during handling of update '%22 + valueUpd + %22' to field '%22 + field + %22'%22, e);%0A
             

Add more information to a NullPointerException.