package org.intermine.web.results;

/*
 * Copyright (C) 2002-2005 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.ArrayList;
import java.util.List;
import java.util.Map;

import org.intermine.objectstore.query.Results;

import org.intermine.web.Constants;

import java.io.Serializable;

import org.apache.log4j.Logger;

/**
 * An inline table created by running a template.
 * @author Kim Rutherford
 */
public class InlineTemplateTable implements Serializable
{
    private Map webProperties;
    private int inlineSize = -1;
    private ArrayList inlineResults;
    private static final Logger LOG = Logger.getLogger(InlineTemplateTable.class);
    private final List columnNames;
    private int resultsSize = -1;
    private int maxInlineTableSize = 30;
    
    /**
     * Construct a new InlineTemplateTable
     * @param results the Results of running the template query
     * @param columnNames the names of each column in the Results object
     * @param webProperties the web properties from the session
     */
    public InlineTemplateTable(Results results, List columnNames, Map webProperties) {
        this.columnNames = columnNames;
        this.webProperties = webProperties;
        String maxInlineTableSizeString =
            (String) webProperties.get(Constants.INLINE_TABLE_SIZE);

        inlineSize = maxInlineTableSize;

        resultsSize = results.size();
            
        if (resultsSize < inlineSize) {
            inlineSize = resultsSize;
        }   

        try {
            maxInlineTableSize = Integer.parseInt(maxInlineTableSizeString);
        } catch (NumberFormatException e) {
            LOG.warn("Failed to parse " + Constants.INLINE_TABLE_SIZE + " property: "
                     + maxInlineTableSizeString);
        }

        inlineResults = new ArrayList(getInlineSize(results, maxInlineTableSize));

        for (int i = 0; i < getInlineSize(results, maxInlineTableSize); i++) {
            inlineResults.add(results.get(i));
        }
    }

    /**
     * Return headings for the columns
     * @return the column names
     */
    public List getColumnNames() {
        return columnNames;
    }

    /**
     * Return the part of the Results that should be display inline in the object details pages.
     * @return the first getSize() rows from the Results object that was passed to the constructor
     */
    public List getInlineResults() {
        return inlineResults;
    }
    
    /**
     * Return the number of table rows or the INLINE_TABLE_SIZE whichever is smaller.
     */
    private int getInlineSize(Results results, int maxInlineTableSize) {
        return inlineSize;
    }

    /**
     * Return the number of rows in the Results object that was passed to the constructor.
     * @return the row count
     */
    public int getResultsSize() {
        return resultsSize;
    }
}

@@ -988,9 +988,9 @@
 e = 
-3
+1
 0;%0A 
@@ -1420,24 +1420,71 @@
 Properties;%0A
+        resultsSize = results.size();%0A        %0A
         Stri
@@ -1587,193 +1587,8 @@
 );%0A%0A
-        inlineSize = maxInlineTableSize;%0A%0A        resultsSize = results.size();%0A            %0A        if (resultsSize %3C inlineSize) %7B%0A            inlineSize = resultsSize;%0A        %7D   %0A%0A
     
@@ -1846,32 +1846,32 @@
 bleSizeString);%0A
-
         %7D%0A%0A     
@@ -1859,24 +1859,166 @@
 );%0A        %7D
+%0A        %0A        inlineSize = maxInlineTableSize;%0A%0A        if (resultsSize %3C inlineSize) %7B%0A            inlineSize = resultsSize;%0A        %7D   
 %0A%0A        in

Make inline template tables the same length as collection tables. Fixes #739.