/*******************************************************************************
 * Copyright 2015 EMBL - European Bioinformatics Institute
 *
 * Licensed under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the
 * License.
 *******************************************************************************/

package org.mousephenotype.cda.solr.web.dto;


import org.apache.solr.client.solrj.SolrServerException;
import org.mousephenotype.cda.db.pojo.PhenotypeCallSummary;

import java.io.UnsupportedEncodingException;
import java.util.Map;


/**
 *
 * @author mrelac
 * This class encapsulates the code and data necessary to represent a row in the
 * Phenotype page's 'phenotypes' HTML table.
 */
public class PhenotypePageTableRow extends DataTableRow {

    public PhenotypePageTableRow() {
        super();
    }

    public PhenotypePageTableRow(PhenotypeCallSummaryDTO pcs, String baseUrl, Map<String, String> config, boolean hasImages) 
    throws UnsupportedEncodingException, SolrServerException {
        super(pcs, baseUrl, config, hasImages);
    }
    /**
     * Sort by:
     * <ul>
     * <li>p-value</li>
     * <li>allele</li>
     * <li>procedure</li>
     * <li>parameter</li>
     * <li>phenotyping center</li>
     * <li>source</li>
     * </ul>
     *
     * @param o operand to compare against
     * @return
     */
    @Override
    public int compareTo(DataTableRow o) {
        if (o.phenotypeTerm == null || this.phenotypeTerm == null) {
            return -1;
        }

	    if (allele == null || o.allele == null) {
		    return -1;
	    }

        // Phenotype Page sorting
        int pvalueOp = this.pValue.compareTo(o.pValue);
        if (pvalueOp == 0) {
            int alleleSymbolOp = this.allele.getSymbol().compareTo(o.allele.getSymbol());
            if (alleleSymbolOp == 0) {
                int procedureOp = this.procedure.getName().compareTo(o.procedure.getName());
                if (procedureOp == 0) {
                    int parameterOp = this.parameter.getName().compareTo(o.parameter.getName());
                    if (parameterOp == 0) {
                        int phenotypingCenterOp = this.phenotypingCenter.compareTo(o.phenotypingCenter);
                        if (phenotypingCenterOp == 0) {
                            return this.dataSourceName.compareTo(o.dataSourceName);
                        } else {
                            return phenotypingCenterOp;
                        }
                    } else {
                        return parameterOp;
                    }
                } else {
                    return procedureOp;
                }
            } else {
                return alleleSymbolOp;
            }
        } else {
            return pvalueOp;
        }
    }
    
    
    /**
     * @since 2016/05/05
     * @return Tabbed header to be used in table export. 
     * ! Keep in synch with order in getTabbedToString !
     */
    public static String getTabbedHeader(){
    	return "Gene\tAllele\tZygosity\tSex\tLife Stage\tPhenotype\tProcedure | Parameter\tPhenotyping Center | Source\tP Value\tGraph";
    }
    
    /**
     * @since 2016/05/05
     * @return Tabbed row for data export
     */
    public String toTabbedString(){
    	return getGene().getSymbol() + "\t"
                + getAllele().getSymbol() + "\t"
                + getZygosity() + "\t"
                + getSexes().get(0) + "\t"
                + getLifeStageName() + "\t"
                + getPhenotypeTerm().getName() + "\t"
                + getProcedure().getName() + " | " + getParameter().getName() + "\t"
                + getPhenotypingCenter() + " | " + getDataSourceName() + "\t"
                + getPrValueAsString() + "\t"
                + getEvidenceLink().getUrl();
    }
    
}

@@ -3513,19 +3513,18 @@
 rocedure
- %7C 
+%5Ct
 Paramete
@@ -3544,19 +3544,18 @@
 g Center
- %7C 
+%5Ct
 Source%5Ct
@@ -3750,28 +3750,24 @@
             
-    
 + getAllele(
@@ -3795,28 +3795,24 @@
             
-    
 + getZygosit
@@ -3830,28 +3830,24 @@
             
- 
-   
 + getSexes()
@@ -3853,36 +3853,32 @@
 ).get(0) + %22%5Ct%22%0A
-    
             + ge
@@ -3905,36 +3905,32 @@
 %5Ct%22%0A            
- 
-   
 + getPhenotypeTe
@@ -3959,28 +3959,24 @@
             
-    
 + getProcedu
@@ -3989,28 +3989,39 @@
 etName() + %22
- %7C %22
+%5Ct%22%0A           
  + getParame
@@ -4047,36 +4047,32 @@
 %5Ct%22%0A            
-    
 + getPhenotyping
@@ -4087,12 +4087,23 @@
  + %22
- %7C %22
+%5Ct%22%0A           
  + g
@@ -4120,36 +4120,32 @@
 ceName() + %22%5Ct%22%0A
-    
             + ge
@@ -4162,36 +4162,32 @@
 String() + %22%5Ct%22%0A
-    
             + ge

Split columns on phenotype page too.