/*
 * $Id: ClusterTreeViewManager.java,v 1.5 2008/04/17 10:01:01 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ClusterTreeViewManager.java,v $
 * Revision 1.5  2008/04/17 10:01:01  marbobo
 * + Velocity-Templates angepasst
 *
 * Revision 1.4  2006/04/29 18:39:44  marbobo
 * + Refactoriengs und eine Ausgaben vereinheitlicht
 *
 * Revision 1.3  2005/10/13 11:30:17  lawern
 * Problem mit zu langen Kategorienamen des Cluster-Moduls behoben.
 *
 * Revision 1.2  2005/07/27 14:24:22  aram
 * Verzeichnis unclustered per default auf nicht aktiv gesetzt
 *
 * Revision 1.1  2005/06/23 00:58:34  aram
 * + Initiale Version
 *
 */
package vkc.util.treeview;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import vkc.clusterer.ClusteringDocument;
import vkc.clusterer.ClusteringItem;
import vkc.clusterer.TermEntry;
import vkc.util.StringUtils;
import vkc.util.VKCStringUtils;
import vkc.view.WebShare;

/**
 * This class manages <code>vkc.util.treeview.TreeView</code>
 */
public final class ClusterTreeViewManager {
	private ClusterTreeViewManager() {
	}

	/**
	 * Builds the global tree
	 */
	public static final TreeView getClusterTreeView(HashMap cluster) {
	    TreeView clusterTreeView = new TreeView();
		
	    ClusteringItem rootCluster = (ClusteringItem)cluster.get(new Integer(-1));
        
		if (rootCluster != null) {
		    TreeViewItem rootNode = new TreeViewItem( rootCluster.getClusterID().toString(), null, rootCluster.getRecursivDocCounts() + " - " + rootCluster.getName(), "javascript:void(0);", "CLUSTERTREEVIEW" );
	        clusterTreeView.setRootNode( rootNode );

		    ClusteringItem beginCluster = (ClusteringItem)cluster.get(new Integer(0));

		    if (beginCluster != null) {
		        Iterator it = beginCluster.getSubCluster().iterator();
		        
		        while (it.hasNext()) {
                    Integer id = (Integer) it.next();
                    
        		    ClusteringItem c = (ClusteringItem)cluster.get(id);
                    
        		    beginCluster.getDocumentIds().addAll(c.getDocumentIds());
        		    beginCluster.getDocuments().addAll(c.getDocuments());
        		    c.setParentID(null);
                }
		        
		        beginCluster.getSubCluster().clear();
		    }
		    
		    if (beginCluster != null && beginCluster.getDocuments().isEmpty()) {
		        rootCluster.getSubCluster().remove(beginCluster.getClusterID()); // enferne NodeId == 0 
		    }
		    
		    // Iterate over all categories
	        List tmpCatIds = new ArrayList();
	        List tmpNodes = new ArrayList();
	        tmpCatIds.add(new Integer(-1));
	        tmpNodes.add(rootNode);

	        Integer clusterId;
	        Comparator comp = new Comparator() {
                public int compare( Object o1, Object o2 ) {
                    TermEntry t1 = (TermEntry)o1;
                    TermEntry t2 = (TermEntry)o2;

                    return t2.compareTo(t1);
                }
            };

	        while ( !tmpCatIds.isEmpty() ) {
	            clusterId = (Integer) tmpCatIds.remove( 0 );
	            rootNode = (TreeViewItem) tmpNodes.remove( 0 );

	            // Subcategories
	            ClusteringItem c = (ClusteringItem)cluster.get(clusterId);
	            Iterator it = c.getSubCluster().iterator();

	            while (it.hasNext()) {
	                Integer subClusterId = (Integer)it.next();
	                ClusteringItem sc = (ClusteringItem)cluster.get(subClusterId);
	                String name = null;
	                
	                
	                if (sc.getName() != null) {
	                    name = sc.getName();
	                } else {
		                SortedSet s = new TreeSet(comp);
		                s.addAll(Arrays.asList(sc.getTerms().toArray(new TermEntry[0])));
		                //int i = 0;
		                
		                for (Iterator iter = s.iterator(); iter.hasNext();) {
	                        TermEntry te = (TermEntry) iter.next();
	                        
	                        if (name == null) {
	                            name = te.getTerm();
	                        } else {
                                if (name.length()+te.getTerm().length() < 200)
                                    name += " - " + te.getTerm();
                                else
                                    break;
	                        }
	                        
	                        //i++;
	                        
	                        //if (i == 5) {
	                            //break;
	                        //}
	                    }
		                
		                if (name != null && sc.getClusterID().intValue() !=  0) {
		                    sc.setName(name);
		                }
	                }
	                
	                String linkPath = WebShare.getContextPath() + "/clusterEditCategoryView.do?categoryId=" + sc.getClusterID();
	                
	                if (!StringUtils.isEmpty(sc.getName())) {
		                linkPath += "&name=" + VKCStringUtils.base64Encode(sc.getName());
	                } else {
		                linkPath += "&name=" + VKCStringUtils.base64Encode("");
	                }

	                if (!StringUtils.isEmpty(sc.getDescription())) {
		                linkPath += "&description=" + VKCStringUtils.base64Encode(sc.getDescription());
	                } else {
		                linkPath += "&description=" + VKCStringUtils.base64Encode("");
	                }

	                linkPath += "&action.decode.parameter=true";
	                
	                if (sc.isPlaneHierarchy()) {
		                linkPath += "&planeHierarchy=true";
	                }

	                TreeViewItem childNode = new TreeViewItem(sc.getClusterID().toString(), null, sc.getRecursivDocCounts() + (name == null ? "" : " - " + name), linkPath, "CLUSTEREDITCATEGORYVIEW");

	                if (sc.getClusterID().intValue() == 0) {
		                childNode.setChecked(false);
	                } else {
		                childNode.setChecked(true);
	                }
	                
	                childNode.setDrawCheckBox(true);
	                
	                // Add as child
	                tmpCatIds.add(subClusterId);
	                tmpNodes.add(childNode);
	                rootNode.addChild(childNode);
	            }

	            it = c.getDocuments().iterator();

	            while (it.hasNext()) {
	                ClusteringDocument d = (ClusteringDocument)it.next();

	                TreeViewItem childNode = new TreeViewItem("doc" + d.getDocumentId().toString(), "document.gif", d.getDocumentTitel(), WebShare.getContextPath() + "/clusterDocumentView.do?documentId=" + d.getDocumentId(), "CLUSTERDOCUMENTVIEW");

	                // Add as child
	                rootNode.addChild(childNode);
	            }
	        }
		}
        
        return clusterTreeView;
	}
}