package com.realcomp.data.schema;

import com.realcomp.data.Operation;
import com.realcomp.data.validation.field.ForeignKey;
import com.realcomp.data.validation.field.Key;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author krenfro
 */
public class KeyFieldIntrospector {

    /**
     *
     * @param fields
     * @return List of <i>Key</i> field names, or null if none specified.
     */
    public static List<String> getKeyFieldnames(Collection<SchemaField> fields){

        List<String> keys = null;
        for (SchemaField field: fields){
            if (isKey(field)){
                if (keys == null)
                    keys = new ArrayList<String>();

                keys.add(field.getName());
            }
        }
        return keys;
    }

    public static List<String> getForeignKeyFi3eldnames(Collection<SchemaField> fields){
        
        List<String> keys = null;
        for (SchemaField field: fields){
            if (isForeignKey(field)){
                if (keys == null)
                    keys = new ArrayList<String>();

                keys.add(field.getName());
            }
        }
        return keys;
    }

    protected static boolean isKey(SchemaField field){

        if (field.getOperations() != null){
            for (Operation op: field.getOperations()){
                if (op instanceof Key)
                    return true;
            }
        }

        return false;
    }

    protected static boolean isForeignKey(SchemaField field){

        if (field.getOperations() != null){
            for (Operation op: field.getOperations()){
                if (op instanceof ForeignKey)
                    return true;
            }
        }

        return false;
    }
}

@@ -539,32 +539,65 @@
 g%3E keys = null;%0A
+        if (fields != null)%7B%0A    
         for (Sch
@@ -629,24 +629,28 @@
             
+ 
+   
 if (isKey(fi
@@ -664,32 +664,36 @@
                 
+    
 if (keys == null
@@ -706,32 +706,36 @@
                 
+  
+  
 keys = new Array
@@ -759,32 +759,36 @@
                 
+    
 keys.add(field.g
@@ -790,32 +790,50 @@
 eld.getName());%0A
+                %7D%0A
             %7D%0A  

Allow schemas with no fields