package de.starwit.generator.generator;

import java.util.HashSet;
import java.util.Set;

import de.starwit.persistence.entity.Attribute;
import de.starwit.persistence.entity.DataType;
import de.starwit.persistence.entity.Domain;
import de.starwit.persistence.entity.Relationship;
import de.starwit.persistence.entity.RelationshipType;

/**
 *
 * @author anett
 *
 */
public class EntityImports {
    /**
     * Imports for Java-Entities Bean Validation and DataTypes for EntityGeneration.
     *
     * @param domain - definition of domain.
     * @return Set of Java imports
     */
    public static Set<String> gatherEntityImports(Domain domain) {
        Set<String> imports = new HashSet<>();
        if (domain.getAttributes() != null) {
            for (Attribute attr : domain.getAttributes()) {
                addImportsForAttribute(imports, attr);
            }
        }

        if (domain.getRelationships() != null) {
            for (Relationship relation : domain.getRelationships()) {
                addImportsForRelations(imports, relation);
            }
        }
        return imports;
    }

    private static void addImportsForAttribute(Set<String> imports, Attribute attr) {
        if (DataType.String.equals(attr.getDataType())) {
            if (attr.getMax() != null || attr.getMin() != null) {
                imports.add("import javax.validation.constraints.Size;");
            }
            if (!attr.isNullable()) {
                imports.add("import javax.validation.constraints.NotBlank;");
            }
        } else if (DataType.Date.equals(attr.getDataType()) || DataType.Time.equals(attr.getDataType())
                || DataType.Timestamp.equals(attr.getDataType())) {
            imports.add("import java.util.Date;");
            imports.add("import javax.persistence.Temporal;");
            imports.add("import javax.persistence.TemporalType;");
        } else if (DataType.Enum.equals(attr.getDataType())) {
            imports.add("import javax.persistence.EnumType;");
            imports.add("import javax.persistence.Enumerated;");
        } else if (DataType.BigDecimal.equals(attr.getDataType())) {
            imports.add("import java.math.BigDecimal;");
        }
        if (attr.getPattern() != null) {
            imports.add("import javax.validation.constraints.Pattern;");
        }

        if (!DataType.String.equals(attr.getDataType())) {
            if (!attr.isNullable()) {
                imports.add("import javax.validation.constraints.NotNull;");
            }
            if (attr.getMax() != null) {
                imports.add("import javax.validation.constraints.Max;");
            }
            if (attr.getMin() != null) {
                imports.add("import javax.validation.constraints.Min;");
            }
        }
    }

    private static void addImportsForRelations(Set<String> imports, Relationship relation) {
        imports.add("import javax.persistence." + relation.getRelationshipType() + ";");
        imports.add("import com.fasterxml.jackson.annotation.JsonFilter;");
        if (RelationshipType.ManyToMany.equals(relation.getRelationshipType()) ||
                RelationshipType.ManyToOne.equals(relation.getRelationshipType())) {
            imports.add("import javax.persistence.JoinColumn;");
        }
        if (RelationshipType.ManyToMany.equals(relation.getRelationshipType()) &&
                relation.isOwnerSide()) {
            imports.add("import javax.persistence.JoinTable;");
        }
        if (RelationshipType.OneToMany.equals(relation.getRelationshipType())
                || RelationshipType.ManyToMany.equals(relation.getRelationshipType())) {
            imports.add("import java.util.Set;");
        }
        if (RelationshipType.OneToOne.equals(relation.getRelationshipType()) &&
                relation.isOwnerSide()) {
            imports.add("import javax.persistence.CascadeType;");
        }
    }
}

@@ -51,20 +51,16 @@
 va.util.
-Hash
 Set;%0Aimp
@@ -69,24 +69,28 @@
 t java.util.
+Tree
 Set;%0A%0Aimport
@@ -683,12 +683,12 @@
 new 
-Hash
+Tree
 Set%3C
@@ -692,16 +692,54 @@
 et%3C%3E();%0A
+%0A        addGeneralImports(imports);%0A%0A
         
@@ -1110,32 +1110,32 @@
     %7D%0A        %7D%0A
-
         return i
@@ -1141,32 +1141,347 @@
 imports;%0A    %7D%0A%0A
+    private static void addGeneralImports(Set%3CString%3E imports) %7B%0A        imports.add(%22import javax.persistence.Column;%22);%0A        imports.add(%22import javax.persistence.Entity;%22);%0A        imports.add(%22import javax.persistence.Table;%22);%0A        imports.add(%22import javax.xml.bind.annotation.XmlRootElement;%22);%0A    %7D%0A%0A
     private stat
@@ -3412,24 +3412,88 @@
 nFilter;%22);%0A
+        // imports.add(%22import javax.persistence.FetchType;%22);%0A%0A
         if (

order imports