SearchListIterator.java :  » Content-Management-System » webman » com » teamkonzept » webman » mainint » db » Java Open Source

Java Open Source » Content Management System » webman 
webman » com » teamkonzept » webman » mainint » db » SearchListIterator.java
package com.teamkonzept.webman.mainint.db;

import com.teamkonzept.lib.TKListIterator;
import com.teamkonzept.db.TKUnprepQuery;
import com.teamkonzept.db.TKDBManager;
import com.teamkonzept.db.QueryConstants;
import com.teamkonzept.lib.TKTemplate;


/**
 * ein Listiterator fr die Klassen : TKDBGetOneNodeContentSearch,TKDBGetWholetreeContentSearch
 * Dieser Listiterator representiert die Template-Variable 
 * SEARCH_STRING; es wird pro Element &lt;TK:SEARCH&gt; und &lt;TK:CONN&gt; ausgegeben <br>
 *
 * Note that two formats of &lt;TK:SEARCH&gt; tag exist. <br>
 * <ul>
 * <li>Sybase (non-Oracle): '%searchpattern%'</li>
 * <li>Oracle: searchpattern (without enclosing "%" and "'", respectively)</li>
 * </ul>
 *
 * @author $Author: sebastian $
 * @version $Revision: 1.4 $
 */
public class SearchListIterator implements TKListIterator
{
  private java.util.Vector data;
  private String listname;
  private TKUnprepQuery query;
    
  /* konvertiert den suchenparameter nach sql-Format */
  
  private static String konvSearchParameter(String search) 
  {
    if (search == null)
      return(null);
    if (search.length() == 0)
      return(search);
    
    StringBuffer sb = new StringBuffer(search);
    
    int i = sb.length() - 1;
    while (i >= 0) 
    {
      if ((sb.charAt(i) == '%') || (sb.charAt(i) == '_') || (sb.charAt(i) == '\\'))
        sb.insert(i,'\\');
      
      i--;  
    } 
    return( '%' + sb.toString() + '%');
  }
  
  /* fr Suche : bastelt aus dem bergebenen Suchstring 
     einen Vektor, der die Suchwerte mit den dazugehrigen
     Verknpfungen enthhlt (OR,AND)
    */ 
  
  private static java.util.Vector getSearchParameter(String search)
  {
    if (search == null)
      return null;
    
    java.util.Vector back = new java.util.Vector();
    String[] stringarray = new String[2];
    int pre = 0;
    int i;
    for (i = 0;i < search.length();i++)
    {
      if (search.startsWith(" OR ",i) || search.startsWith(" AND ",i))
      {
        stringarray[0] =  search.substring(pre, i );
        if (search.startsWith(" OR ",i))
        {
          pre = i + 4;
          stringarray[1] = "OR";
        }  
        else
        {
          pre = i + 5;
          stringarray[1] = "AND";  
        }  
        if (stringarray[0] != null)
        {
          if (stringarray[0].length() != 0)
            back.add(new String[] {stringarray[0],stringarray[1]});
        }  
      }
    }
    if (pre != i)
      back.add(new String[] {search.substring(pre, i), ""});
      
    if (back.isEmpty())
      return(null);
    else  
      return(back);
  } 
    
  public SearchListIterator(String data, String listname, TKUnprepQuery query) 
  {
    this.data = getSearchParameter( data );
    this.listname = listname;
    this.query = query;
  }

  public boolean apply(TKTemplate t, int count, String type) 
  {
    if ( this.listname.equalsIgnoreCase( type ) )
    {
      if (count >= data.size() )
        return(false);
        
      String[] help = (String[])this.data.elementAt( count );
      String h = "";
      try
      {
        h = query.convert( "SEARCH_UNC", new String(help[0]));
        t.set( "SEARCH_UNC", h);
        h = query.convert( "SEARCH", new String(konvSearchParameter(help[0])));
        t.set( "SEARCH", h);
      }
      catch(Exception e)
      {
        h = ""; // fr JTest
      }  
      t.set("CONN",new String(help[1]));
      return(true);
    }
    return(false);
  }
}  
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.