// Copyright 2017 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.processing;

import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.searchdefinition.DocumentReference;
import com.yahoo.searchdefinition.DocumentReferences;
import com.yahoo.searchdefinition.RankProfileRegistry;
import com.yahoo.searchdefinition.Search;
import com.yahoo.searchdefinition.document.ImportedField;
import com.yahoo.searchdefinition.document.ImportedFields;
import com.yahoo.searchdefinition.document.SDField;
import com.yahoo.searchdefinition.document.TemporaryImportedField;
import com.yahoo.vespa.model.container.search.QueryProfiles;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Iterates all imported fields from SD-parsing and validates and resolves them into concrete fields from referenced document types.
 *
 * @author geirst
 */
public class ImportedFieldsResolver extends Processor {

    private final Map<String, ImportedField> importedFields = new LinkedHashMap<>();
    private final DocumentReferences references;

    public ImportedFieldsResolver(Search search, DeployLogger deployLogger, RankProfileRegistry rankProfileRegistry, QueryProfiles queryProfiles) {
        super(search, deployLogger, rankProfileRegistry, queryProfiles);
        references = search.getDocument().getDocumentReferences().get();
    }

    @Override
    public void process() {
        for (TemporaryImportedField importedField : search.temporaryImportedFields().get().fields().values()) {
            resolveImportedField(importedField);
        }
        search.setImportedFields(new ImportedFields(importedFields));
    }

    private void resolveImportedField(TemporaryImportedField importedField) {
        DocumentReference reference = validateDocumentReference(importedField);
        SDField referencedField = validateReferencedField(importedField, reference);
        importedFields.put(importedField.aliasFieldName(), new ImportedField(importedField.aliasFieldName(), reference, referencedField));
    }

    private DocumentReference validateDocumentReference(TemporaryImportedField importedField) {
        String documentReferenceFieldName = importedField.documentReferenceFieldName();
        DocumentReference reference = references.referenceMap().get(documentReferenceFieldName);
        if (reference == null) {
            fail(importedField.aliasFieldName(), "Document reference field '" + documentReferenceFieldName + "' not found");
        }
        return reference;
    }

    private SDField validateReferencedField(TemporaryImportedField importedField, DocumentReference reference) {
        String foreignFieldName = importedField.foreignFieldName();
        SDField referencedField = reference.search().getField(foreignFieldName);
        if (referencedField == null) {
            fail(importedField.aliasFieldName(), "Field '" + foreignFieldName + "' via document reference field '" + reference.documentReferenceField().getName() + "' not found");
        }
        return referencedField;
    }

    private void fail(String importedFieldName, String msg) {
        throw new IllegalArgumentException("For search '" + search.getName() + "', import field '" + importedFieldName + "': Imported field is not valid. " + msg);
    }

}

@@ -1474,52 +1474,8 @@
     
- for (TemporaryImportedField importedField :
  sea
@@ -1523,78 +1523,62 @@
 s().
-values()) %7B%0A            resolveImportedField(importedField);%0A        %7D
+forEach((name, field) -%3E resolveImportedField(field));
 %0A   

Use forEach() instead of for loop.