gt: 					if (link.getSourceId() == id) {
context: package zeno2.db;
  
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.HashSet;
  import java.util.Hashtable;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Set;
  
  import zeno2.kernel.Article;
  import zeno2.kernel.ArticleCollection;
  import zeno2.kernel.Link;
  import zeno2.kernel.OutlineNode;
  import zeno2.kernel.Topic;
  import zeno2.kernel.ZenoCollection;
  import zeno2.kernel.ZenoResource;
  import zeno2.kernel.ZenoException;
  
  
  public class OutlineGenerator {
  	FactoryImpl factory;
  	ZenoCollection collection;
  	int id = 0;
  	boolean extended = true;
  	Comparator comparator;
  	
  	public OutlineGenerator(FactoryImpl factory, ZenoCollection collection, boolean extended) {
  		this.factory = factory;
  		this.collection = collection;
  		this.id = collection.getId();
  		this.extended = extended;
  	}
  	
  
  		/** Returns an ordered list of OutlineNodes, with the article of the given id as the
  	 source of the link in each node. At each level of the tree, the links are sorted by
  	 label and source alias.  Since journals are arbitrary graphs, not trees, duplicates
  	 or cycles are broken by expanding only a single occurrence of each node. The
  	 other occurrences appear in the tree, but are not expanded.
  	 The linkLabel parameter can be used to include articles which can be reached only via
  	 links with the given label,
  	 where each child node in the tree must be the source of a link
  	 targeting the parent node.  If the linkLabel parameter is null or the
  	 empty string, all links are used to generate the tree. */
  	 
  	 
  	 
  	 public List getOutline(List articles, String label, int direction)
  			throws ZenoException {
  		
  		Hashtable mainLinkTable = new Hashtable();
  		collectLinks(mainLinkTable, label, direction);
  		List outline; 
  		if (extended)
  			outline = getXOutline(mainLinkTable, articles);
  		else
  		 	outline = getOutline(mainLinkTable, articles);
  		 return outline;
  	}
  	
  	public List getFullOutline(List artCollections, String label, int direction)
  			throws ZenoException {
  		
  		extended = true;
  		Hashtable mainLinkTable = new Hashtable();
  		collectLinks(mainLinkTable, label, direction);
  		List articles = ArticleCollection.addAll(artCollections);
  		return getFullOutline(mainLinkTable, articles);
  	}
  	 
  	 public OutlineNode getOutline (int resourceId, String linkLabel) 
  			throws ZenoException
  	{	
  		List articles = new ArrayList();
  		Article art = (Article)factory.loadResource(resourceId);	
  		articles.add(art);
  		List nodes = getOutline(articles, linkLabel, -1);
  		return (OutlineNode)nodes.get(0);
  	}
  	
  	
  
  	/** collects all inlinks or outlinks of the journal in linkTable
  	if direction is < 0 or > 0 respectively */
  	
  	
  	public void collectLinks(Hashtable linkTable, String linkLabel, int direction) 
  			throws ZenoException {
  		
  		StringBuffer buf = new StringBuffer();
  		buf.append("select label, source_alias, target_alias, source, target, flag from link, resource");
  		if (direction < 0)
  			buf.append(" where target=id");
  		else
  			buf.append(" where source=id");
  		if (collection instanceof Topic)
  			buf.append(" and part=");
  		else
  			buf.append(" and parent=");
  		buf.append(DBClient.format(this.id));
  		
  		if (!"".equals(linkLabel)) {
  			buf.append(" and label = ");
  			buf.append(DBClient.format(linkLabel));
  		}
  		buf.append(" and source_mark='false' and target_mark='false'");
  		List allLinks = LinkImpl.getLinksWhere(factory, buf.toString());
  		Iterator it = allLinks.iterator();
  		while(it.hasNext()) {
  			LinkImpl link = (LinkImpl)it.next();
  			Integer keyId = 
  				new Integer((direction < 0) ? link.getTargetId() : link.getSourceId());
  			List links = (List)linkTable.get(keyId);
  			if (links == null) {
  				links = new ArrayList();
  				linkTable.put(keyId, links);
  			}
  			links.add(link);
  		}
  	}
  
  
  	protected void collectLinksBut(Hashtable linkTable, String linkLabel) 
  			throws ZenoException {
  	
  		StringBuffer buf = new StringBuffer();
  		buf.append("select distinct label, source_alias, target_alias, source,target, flag from link, resource");
  		buf.append(" where (target=id or source =id) ");
  		if (collection instanceof Topic)
  			buf.append(" and part=");
  		else
  			buf.append(" and parent=");
  		buf.append(DBClient.format(this.id));
  		if (linkLabel != null && !linkLabel.equals("")) {
  			buf.append(" and label<>");
  			buf.append(DBClient.format(linkLabel));
  		}
  		//heg
  		buf.append(" and source_mark='false' and target_mark='false'");
  		List alllinks = LinkImpl.getLinksWhere(factory, buf.toString());
  		Iterator it = alllinks.iterator();
  		while(it.hasNext()) {
  			LinkImpl link = (LinkImpl)it.next();
  			Integer targetId = new Integer(link.getTargetId());
  			List links = (List)linkTable.get(targetId);
  			if (links == null) {
  				links = new ArrayList();
  				linkTable.put(targetId, links);
  			}
  			links.add(link);
  			Integer sourceId = new Integer(link.getSourceId());
  			links = (List)linkTable.get(sourceId);
  			if (links == null) {
  				links = new ArrayList();
  				linkTable.put(sourceId, links);
  			}
  			links.add(link);
  		}
  	} 
  	
  	public List getOutline(Hashtable mainLinkTable, List articles)
  			throws ZenoException {
  		
  		comparator = new NodeComparator();
  		List result = new ArrayList();
  		Iterator artit = articles.iterator();
  		List outlineLinks = new ArrayList();
  		Hashtable visited = new Hashtable();
  		int nr = 1;
  		while (artit.hasNext()) {
  			ArticleImpl art = (ArticleImpl)artit.next();
  			OutlineNode artNode =  
  				new OutlineNode("", Integer.toString(art.getId()), art.getAlias());
  			artNode.setPosition(Integer.toString(nr++));
  			artNode.setResource(art);
  			result.add(artNode);
  		}
  		Iterator resit = result.iterator();
  		while (resit.hasNext()) {
  			OutlineNode artNode = (OutlineNode)resit.next();
  			genChildren(mainLinkTable, artNode, visited, null);
  		}
  		return result;	
  	}
  	
  	
  	protected List getXOutline(Hashtable mainLinkTable, List articles)
  			throws ZenoException {
  		
  		comparator = new NodeComparator2();
  		List result = new ArrayList();
  		Iterator artit = articles.iterator();
  		List outlineLinks = new ArrayList();
  		Hashtable resTable = new Hashtable();
  		Hashtable visited = new Hashtable();
  		int nr = 1;
  		while (artit.hasNext()) {
  			ArticleImpl art = (ArticleImpl)artit.next();
  			OutlineNode artNode = 
  				new OutlineNode("", Integer.toString(art.getId()), art.getAlias());
  			artNode.setPosition(Integer.toString(nr++));
  			artNode.setResource(art);
  			collectOutlineLinks(mainLinkTable, art.id, outlineLinks);
  			resTable.put(new Integer(art.getId()), art);
  			result.add(artNode);
  		}
  		LinkImpl.fillLinks(factory, outlineLinks, resTable, false);
  		Iterator resit = result.iterator();
  		while (resit.hasNext()) {
  			OutlineNode artNode = (OutlineNode)resit.next();
  			Article root = (Article)artNode.getResource();
  			genChildren(mainLinkTable, artNode, visited, root);
  		}
  		return result;	
  	}
  	
  	public static Hashtable genResTable(List resList) {
  		Hashtable table = new Hashtable();
  		Iterator lit = resList.iterator();
  		while(lit.hasNext()) {
  			ArticleImpl art = (ArticleImpl)lit.next();
  			table.put(new Integer(art.getId()), art);
  		}
  		return table;
  	}
  	
  	public List getFullOutline(Hashtable mainLinkTable, List articles)
  			throws ZenoException {
  		
  		comparator = new NodeComparator2();
  		List result = new ArrayList();
  		Iterator artit = articles.iterator();
  		Hashtable resTable = genResTable(articles);
  		Hashtable visited = new Hashtable();
  		int nr = 1;
  		while (artit.hasNext()) {
  			ArticleImpl art = (ArticleImpl)artit.next();
  			if (art instanceof Topic || 
  					visited.get(new Integer(art.getId())) == null) {
  				OutlineNode artNode = 
  					new OutlineNode("", Integer.toString(art.getId()), art.getAlias());
  				artNode.setPosition(Integer.toString(nr++));
  				artNode.setResource(art);
  				result.add(artNode);
  				List outlineLinks = new ArrayList();
  				collectOutlineLinks(mainLinkTable, art.id, outlineLinks);
  				LinkImpl.fillLinks(factory, outlineLinks, resTable, false);
  				genChildren(mainLinkTable, artNode, visited, art);
  			}
  		}
  		return result;	
  	}
  	
  	
  	protected static void collectOutlineLinks
  		(Hashtable linkTable, int id, List outlineLinks) {
  	
  		List links = (List)linkTable.get(new Integer(id));
  		if (links != null) {
  			Iterator it = links.iterator();
  			while (it.hasNext()) {
  				LinkImpl link = (LinkImpl) it.next();
  				if (!outlineLinks.contains(link)) {
  					outlineLinks.add(link);
  					int otherId;
  