package de.tblsoft.solr.pipeline.bean;

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

/**
 * Created by tblsoft on 21.04.16.
 */
public class Document {

    private List<Field> fields = new ArrayList<Field>();


    public List<Field> getFields() {
        return fields;
    }


    public String getFieldValue(String name, String defaultValue) {
       String value = getFieldValue(name);
        if(value != null) {
            return value;
        }
        return defaultValue;
    }

    public String getFieldValue(String name) {
        List<String> fields = getFieldValues(name, new ArrayList<String>());
        if(fields.size()> 0) {
            return fields.get(0);
        }
        return null;
    }

    public List<String> getFieldValues(String name) {
        for(Field field:fields) {
            if(name.equals(field.getName())) {
                return field.getValues();
            }
        }
        return null;
    }

    public List<String> getFieldValues(String name, List<String> defaultValue) {
        List<String> fields = getFieldValues(name);
        if(fields != null) {
            return fields;
        }
        return defaultValue;
    }

    public void deleteField(String name) {
        List<Field> newFields = new ArrayList<Field>();
        for(Field field:fields) {
            if(!name.equals(field.getName())) {
                newFields.add(field);
            }
        }
        this.fields = newFields;
    }


    public void setField(String name, List<String> value) {
        Field field = new Field(name,value);
        deleteField(name);
        this.fields.add(field);
    }

    public void setField(String name, String value) {
        Field field = new Field(name,value);
        deleteField(name);
        this.fields.add(field);
    }

    public Field getField(String name) {
        for(Field field:fields) {
            if(name.equals(field.getName())) {
                return field;
            }
        }
        return null;
    }

    public void addField(String name, String value) {
        Field existingField = getField(name);
        if(existingField == null) {
            Field field = new Field(name,value);
            this.fields.add(field);
        } else {
            existingField.getValues().add(value);
        }


    }


    @Override
    public String toString() {
        return "Document{" +
                "fields=" + fields +
                '}';
    }
}

@@ -33,16 +33,56 @@
 .bean;%0A%0A
+import com.google.common.base.Strings;%0A%0A
 import j
@@ -2340,25 +2340,520 @@
 ;%0A        %7D%0A
-%0A
+    %7D%0A%0A    public void addField(String name, List%3CString%3E values) %7B%0A        Field existingField = getField(name);%0A        if(existingField == null) %7B%0A            Field field = new Field(name,values);%0A            this.fields.add(field);%0A        %7D else %7B%0A            existingField.getValues().addAll(values);%0A        %7D%0A    %7D%0A%0A    public void addFieldIfNotNullOrEmpty(String name, String value) %7B%0A        if(Strings.isNullOrEmpty(value)) %7B%0A            return;%0A        %7D%0A        addField(name,value);
 %0A    %7D%0A%0A%0A   

add method to Document to add a list of values