/*
 * $Id: DocumentTypeSortFactory.java,v 1.3 2004/07/20 08:41:36 jonyp Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: DocumentTypeSortFactory.java,v $
 * Revision 1.3  2004/07/20 08:41:36  jonyp
 * added copyright info + cvs log
 *
 *
 */

package vkc.util.sort;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import vkc.db.entities.VKCDocumentType;

public class DocumentTypeSortFactory {

    public static final int SORT_BY_ID_UP          = 1;
    public static final int SORT_BY_ID_DOWN        = 2;
    public static final int SORT_BY_EXTENSION_UP   = 3;
    public static final int SORT_BY_EXTENSION_DOWN = 4;
    
    private DocumentTypeSortFactory() {
    }
    
    public final static void sort( List l, int mode ){

        if ( l==null ) {
            return;
        }

        switch( mode ) {        

            case SORT_BY_ID_DOWN:
                Collections.sort( l, COMP_ID_DOWN );
                break;
            
            case SORT_BY_ID_UP:
                Collections.sort( l, COMP_ID_UP );
                break;

            case SORT_BY_EXTENSION_DOWN:
                Collections.sort( l, COMP_EXTENSION_DOWN );
                break;
            
            case SORT_BY_EXTENSION_UP:
                Collections.sort( l, COMP_EXTENSION_UP );
                break;
        }
    }

    private static final Comparator COMP_ID_DOWN = new Comparator(  ) {

        public int compare( Object o1, Object o2 )
        {
            Integer i1 = ((VKCDocumentType)o1).getDocTypeId();
            Integer i2 = ((VKCDocumentType)o2).getDocTypeId();
            
            return -i1.compareTo( i2 );
        }
    };

    private static final Comparator COMP_ID_UP = new Comparator(  ) {

        public int compare( Object o1, Object o2 )
        {
            Integer i1 = ((VKCDocumentType)o1).getDocTypeId();
            Integer i2 = ((VKCDocumentType)o2).getDocTypeId();
            
            return i1.compareTo( i2 );
        }
    };
    
    private static final Comparator COMP_EXTENSION_DOWN = new Comparator(  ) {

        public int compare( Object o1, Object o2 )
        {
            String s1 = ((VKCDocumentType)o1).getExtension();
            String s2 = ((VKCDocumentType)o2).getExtension();
            
            if ( s1.equals( "?" ) ) {
                return 1; // Der Typ "unbekannt" immer an letzter Stelle
            }

            return -s1.compareToIgnoreCase( s2 );
        }
    };

    private static final Comparator COMP_EXTENSION_UP = new Comparator(  ) {

        public int compare( Object o1, Object o2 )
        {
            String s1 = ((VKCDocumentType)o1).getExtension();
            String s2 = ((VKCDocumentType)o2).getExtension();

            if ( s1.equals( "?" ) ) {
                return -1; // Der Typ "unbekannt" immer an erster Stelle
            }

            return s1.compareToIgnoreCase( s2 );
        }
    };
    
}