/* $This file is distributed under the terms of the license in /doc/license.txt$ */

package edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel;

public class SubclassTemplateModel extends BaseTemplateModel implements Comparable<SubclassTemplateModel> { 
    
    private final VClass vclass;
    private final List<ObjectPropertyStatementTemplateModel> statements;
    
    /* A dummy SubclassTemplateModel object is created for statements not belonging to any
     * real subclass. The template will recognize it because getName() returns an empty string.
     * This parallels the dummy PropertyGroupTemplateModel created for properties not
     * belonging to any real group.
     */ 
    SubclassTemplateModel(List<ObjectPropertyStatementTemplateModel> statements) {
        this(null, statements);
    }
    
    SubclassTemplateModel(VClass vclass, List<ObjectPropertyStatementTemplateModel> statements) {
        this.vclass = vclass;
        this.statements = statements;
    }

    @Override
    public int compareTo(SubclassTemplateModel other) {
        
        if (other == null) {
            return -1;
        }
        
        VClass vclassOther = other.getVClass();
        if (vclass == null) {
            return vclassOther == null ? 0 : 1;
        }
        if (vclassOther == null) {
            return -1;
        }
        
        int rank = vclass.getDisplayRank();
        int rankOther = vclassOther.getDisplayRank();
        
        int intCompare = 0;
        // Values < 1 are undefined and go at end, not beginning
        if (rank < 1) {
            intCompare = rankOther < 1 ? 0 : 1;
        } else if (rankOther < 1) {
            intCompare = -1;
        } else {           
            intCompare = ((Integer)rank).compareTo(rankOther);
        }

        if (intCompare != 0) {
            return intCompare;        
        }
        
        // If display ranks are equal, sort by name     
        String name = getName();
        String nameOther = vclassOther.getName();
               
        if (name == null) {
            return nameOther == null ? 0 : 1;
        } 
        if (nameOther == null) {
            return -1;
        }
        return name.compareToIgnoreCase(nameOther);

    }
    
    protected VClass getVClass() {
        return vclass;
    }
    
    /* Accessor methods for templates */
    
    public String getName() {
        return vclass == null ? "" : vclass.getName();
    }
    
    public List<ObjectPropertyStatementTemplateModel> getStatements() {
        return statements;
    }
    
}

@@ -657,19 +657,8 @@
     
-/* A dummy 
 Subc
@@ -678,269 +678,153 @@
 odel
- object is created for statements not belonging to any%0A     * real subclass. The template will recognize it because getName() returns an empty string.%0A     * This parallels the dummy PropertyGroupTemplateModel created for properties not%0A     *
+(VClass vclass, List%3CObjectPropertyStatementTemplateModel%3E statements) %7B%0A        // NB vclass may be null. If the statements don't
  belong
-ing
  to any 
 real
@@ -823,252 +823,157 @@
 any 
-real group.%0A     */ %0A    SubclassTemplateModel(List%3CObjectPropertyStatementTemplateModel%3E statements) %7B%0A        this(null, statements);%0A    %7D%0A    %0A    SubclassTemplateModel(VClass vclass, List%3CObjectPropertyStatementTemplateModel%3E statements) %7B
+subclass, a dummy SubclassTemplateModel%0A        // is created with a null vclass, so that the data can be presented in a uniform way to the template.
 %0A   
@@ -994,24 +994,25 @@
 ss = vclass;
+ 
 %0A        thi

Remove an unneeded constructor from SubclassTemplateModel.