package it.unibz.inf.ontop.dbschema.impl.json;

import com.fasterxml.jackson.annotation.*;
import it.unibz.inf.ontop.dbschema.*;
import it.unibz.inf.ontop.dbschema.impl.DatabaseTableDefinition;
import it.unibz.inf.ontop.exception.MetadataExtractionException;

import java.util.List;
import java.util.stream.Stream;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "name",
        "from",
        "to"
})
public class JsonForeignKey extends JsonOpenObject  {
    public final String name;
    public final Part from, to;

    @JsonCreator
    public JsonForeignKey(@JsonProperty("name") String name,
                          @JsonProperty("from") Part from,
                          @JsonProperty("to") Part to) {
        this.name = name;
        this.from = from;
        this.to = to;
    }

    public JsonForeignKey(ForeignKeyConstraint fk) {
        this.name = fk.getName();
        this.from = new Part(fk.getRelation(), fk.getComponents().stream()
                .map(ForeignKeyConstraint.Component::getAttribute));
        this.to = new Part(fk.getReferencedRelation(), fk.getComponents().stream()
                .map(ForeignKeyConstraint.Component::getReferencedAttribute));
    }

    public void insert(NamedRelationDefinition relation, MetadataLookup lookup) throws MetadataExtractionException {

        ForeignKeyConstraint.Builder builder = ForeignKeyConstraint.builder(name, relation,
                lookup.getRelation(JsonMetadata.deserializeRelationID(lookup.getQuotedIDFactory(), to.relation)));

        try {
            for (int i = 0; i < from.columns.size(); i++)
                builder.add(lookup.getQuotedIDFactory().createAttributeID(from.columns.get(i)),
                        lookup.getQuotedIDFactory().createAttributeID(to.columns.get(i)));
        }
        catch (AttributeNotFoundException e) {
            throw new MetadataExtractionException(e);
        }

        builder.build();
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonPropertyOrder({
            "relation",
            "columns"
    })
    public static class Part extends JsonOpenObject {
        public final List<String> relation;
        public final List<String> columns;

        @JsonCreator
        public Part(@JsonProperty("relation") List<String> relation,
                    @JsonProperty("columns") List<String> columns) {
            this.relation = relation;
            this.columns = columns;
        }

        public Part(NamedRelationDefinition relation, Stream<Attribute> attributes) {
            this.relation = JsonMetadata.serializeRelationID(relation.getID());
            this.columns = JsonMetadata.serializeAttributeList(attributes);
        }
    }
}

@@ -152,110 +152,102 @@
 top.
-dbschema.impl.DatabaseTableDefinition;%0Aimport it.unibz.inf.ontop.exception.MetadataExtractionException
+exception.MetadataExtractionException;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
 ;%0A%0Ai
@@ -530,16 +530,105 @@
 m, to;%0A%0A
+    private static final Logger LOGGER = LoggerFactory.getLogger(JsonForeignKey.class);%0A%0A
     @Jso
@@ -1449,16 +1449,51 @@
 Builder 
+builder;%0A        try %7B%0A            
 builder 
@@ -1531,32 +1531,36 @@
 name, relation,%0A
+    
                 
@@ -1657,16 +1657,183 @@
 tion)));
+%0A        %7D%0A        catch (MetadataExtractionException e) %7B%0A            LOGGER.warn(%22Cannot find table %7B%7D for FK %7B%7D%22, to.relation, name);%0A            return ;%0A        %7D
 %0A%0A      

stop throwing an exception when target rel not reachable.