package org.intermine.web.results;

/*
 * Copyright (C) 2002-2004 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.util.Iterator;
import java.util.Set;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.Collection;

import org.intermine.metadata.ClassDescriptor;
import org.intermine.metadata.Model;

/**
 * Class to represent a collection field of an object for the webapp
 * @author Mark Woodbridge
 */
public class DisplayCollection
{
    ClassDescriptor cld;
    int size;
    Map classes = new LinkedHashMap();
    boolean verbose = false;

    /**
     * Constructor
     * @param c the actual collection
     * @param cld the type of this collection
     * @param model the metadata for the collection
     * @throws Exception if an error occurs
     */
    public DisplayCollection(Collection c, ClassDescriptor cld, Model model) throws Exception {
        this.cld = cld;
        size = c.size();
        for (Iterator i = c.iterator(); i.hasNext();) {
            Set clds = ObjectViewController.getLeafClds(i.next().getClass(), model);
            if (clds.size() == 1 && clds.iterator().next().equals(cld)) {
                break;
            }
            if (classes.containsKey(clds)) {
                classes.put(clds, new Integer(((Integer) classes.get(clds)).intValue() + 1));
            } else {
                classes.put(clds, new Integer(1));
            }
        }
    }

    /**
     * Get the class descriptor for this collection
     * @return the class descriptor
     */
    public ClassDescriptor getCld() {
        return cld;
    }

    /**
     * Get the size of this collection
     * @return the size
     */
    public int getSize() {
        return size;
    }

    /**
     * Get the map of type of objects in this collection
     * @return the classes
     */
    public Map getClasses() {
        return classes;
    }
    
    /**
     * Set the verbosity level for display of this reference
     * @param verbose the verbosity level
     */
    public void setVerbose(boolean verbose) {
        this.verbose = verbose;
    }

    /**
     * Get the verbosity level for display of this reference
     * @return the verbosity level
     */
    public boolean isVerbose() {
        return verbose;
    }
}
@@ -331,57 +331,8 @@
 */%0A%0A
-import java.util.Iterator;%0Aimport java.util.Set;%0A
 impo
@@ -1097,24 +1097,27 @@
 = c.size();%0A
+// 
         for 
@@ -1156,24 +1156,27 @@
 sNext();) %7B%0A
+// 
             
@@ -1248,16 +1248,19 @@
 model);%0A
+// 
         
@@ -1321,24 +1321,27 @@
 als(cld)) %7B%0A
+// 
             
@@ -1351,16 +1351,19 @@
  break;%0A
+// 
         
@@ -1360,32 +1360,34 @@
 /             %7D%0A
+//
             if (
@@ -1378,24 +1378,25 @@
             
+ 
 if (classes.
@@ -1412,24 +1412,27 @@
 ey(clds)) %7B%0A
+// 
             
@@ -1509,24 +1509,27 @@
 ue() + 1));%0A
+// 
             
@@ -1537,16 +1537,19 @@
  else %7B%0A
+// 
         
@@ -1591,16 +1591,19 @@
 er(1));%0A
+// 
         
@@ -1604,24 +1604,27 @@
           %7D%0A
+// 
         %7D%0A  

Disable collection summaries until we can avoid iterating through the whole collection