//
// This file is part of the OpenNMS(R) Application.
//
// OpenNMS(R) is Copyright (C) 2002-2003 Blast Internet Services, Inc.  All rights reserved.
// OpenNMS(R) is a derivative work, containing both original code, included code and modified
// code that was published under the GNU General Public License. Copyrights for modified 
// and included code are below.
//
// OpenNMS(R) is a registered trademark of Blast Internet Services, Inc.
//
// Copyright (C) 1999-2001 Oculan Corp.  All rights reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// For more information contact:
//      OpenNMS Licensing       <license@opennms.org>
//      http://www.opennms.org/
//      http://www.blast.com/
//

package org.opennms.web.event.filter;

import java.util.ArrayList;
import java.util.StringTokenizer;


public class LogMessageMatchesAnyFilter extends Object implements Filter 
{
    public static final String TYPE = "msgmatchany";
    protected String[] substrings;

    /**
     * @param stringList a space-delimited list of search substrings 
     */
    public LogMessageMatchesAnyFilter(String stringList) {
        if( stringList == null ) {
            throw new IllegalArgumentException("Cannot take null parameters.");            
        }
        
        StringTokenizer tokenizer = new StringTokenizer(stringList);
        ArrayList list = new ArrayList();
        
        while(tokenizer.hasMoreTokens()) {
            list.add(tokenizer.nextToken());
        }
            
        if(list.size() == 0) {
            throw new IllegalArgumentException("Cannot take a zero-length list of substrings");
        }
        
        this.substrings = (String[])list.toArray(new String[list.size()]);
    }    
    
    public LogMessageMatchesAnyFilter(String[] substrings) {
        if( substrings == null ) {
            throw new IllegalArgumentException("Cannot take null parameters.");
        }
        
        this.substrings = substrings;
    }
    
    public String getSql() {
        StringBuffer buffer = new StringBuffer(" (");
        
        buffer.append("UPPER(EVENTLOGMSG) LIKE '%");
        buffer.append(this.substrings[0].toUpperCase());
        buffer.append("%'");
        
        for(int i=1; i < this.substrings.length; i++ ) {
            buffer.append(" OR ");

            buffer.append("UPPER(EVENTLOGMSG) LIKE '%");
            buffer.append(this.substrings[i].toUpperCase());
            buffer.append("%'");
        }
        
        buffer.append(")");
        
        return buffer.toString();
    }
    
    public String getDescription() {
        return( TYPE + "=" + this.getQueryString() );
    }
    
    public String getTextDescription(){
        StringBuffer buffer = new StringBuffer("message containing \"");        
        buffer.append(this.substrings[0]);
        buffer.append("\"");

        for(int i=1; i < this.substrings.length; i++ ) {
            buffer.append(" or \"");
            buffer.append(this.substrings[i]);
            buffer.append("\"");            
        }
                
        return buffer.toString();
    }

    public String toString() {
        return( "<LogMessageMatchesAnyFilter: " + this.getDescription() + ">" );
    }

    public String[] getSubstrings() {
        return( this.substrings );
    }

    public boolean equals( Object obj ) {
        return( this.toString().equals( obj.toString() ));
    }
    
    public String getQueryString() {
        StringBuffer buffer = new StringBuffer();        
        buffer.append(this.substrings[0]);

        for(int i=1; i < this.substrings.length; i++ ) {
            buffer.append(" ");
            buffer.append(this.substrings[i]);
        }
                
        return( buffer.toString() );
    }
    
}


@@ -439,16 +439,107 @@
 Inc.%0A//%0A
+// Modifications:%0A//%0A// 2004 Feb 11: Change the search string logic from 'OR' to 'AND'.%0A//%0A
 // Copyr
@@ -1567,16 +1567,82 @@
 nizer;%0A%0A
+/**%0A * @author %3CA HREF=%22mailto:jamesz@blast.com%22%3EJames Zuo%3C/A%3E%0A */
 %0Apublic 
@@ -2972,275 +2972,24 @@
 end(
-this.substrings%5B0%5D.toUpperCase());%0A        buffer.append(%22%25'%22);%0A        %0A        for(int i=1; i %3C this.substrings.length; i++ ) %7B%0A            buffer.append(%22 OR %22);%0A%0A            buffer.append(%22UPPER(EVENTLOGMSG) LIKE '%25%22);%0A            buffer.append(this.subs
+getQueryS
 tring
-s%5Bi%5D
+()
 .toU
@@ -2997,28 +2997,24 @@
 perCase());%0A
-    
         buff
@@ -3030,35 +3030,16 @@
 (%22%25'%22);%0A
-        %7D%0A        %0A
         
@@ -3361,197 +3361,24 @@
 end(
-this.substrings%5B0%5D);%0A        buffer.append(%22%5C%22%22);%0A%0A        for(int i=1; i %3C this.substrings.length; i++ ) %7B%0A            buffer.append(%22 or %5C%22%22);%0A            buffer.append(this.substrings%5Bi%5D
+getQueryString()
 );%0A 
@@ -3376,36 +3376,32 @@
 ing());%0A        
-    
 buffer.append(%22%5C
@@ -3404,38 +3404,16 @@
 d(%22%5C%22%22);
-            %0A        %7D
 %0A       

Change search logic from 'OR' to 'AND'.