Search code examples
java-me

j2me - Filter results by two or more criteria


I'm trying to filter some records using the RecordFilter interface. In my app I have a couple of interfaces similar to this one, on which the user can enter an ID or Name (he/she could enter both or neither of them too)

enter image description here

Here's what I've done so far:

The Customer filter.

Here if the user didn't enter an ID, I pass 0 as a default value, that's why I evaluate customerID!=0

public class CustomerFilter implements RecordFilter {

    private String mName_Filter;
    private int mID_Filter;

    public CustomerFilter(String name_Filter, int id_Filter) {
        this.mName_Filter = name_Filter.toLowerCase();
        this.mID_Filter = id_Filter;
    }

    public boolean matches(byte[] candidate) {
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(candidate);
            DataInputStream dis = new DataInputStream(bis);
            int customerID = dis.readInt();
            String customerName = dis.readUTF().toLowerCase();
            if ((customerName != null && customerName.indexOf(mName_Filter) != -1) && (customerID != 0 && customerID == mID_Filter))                
                return true;
            if (customerName != null && customerName.indexOf(mName_Filter) != -1 && customerID == 0) 
                return true;
            if (customerName == null && (customerID != 0 && customerID == mID_Filter)) 
                return true;
            if (customerName == null && customerID == 0) 
                return true;

        } catch (IOException ex) {
            //What's the point in catching a exception here???
        }
        return false;
    }
}

The search method:

Note: This method is in a class that I call "RMSCustomer", in which I deal with everything related to RMS access. The search method receives two parameters (id and name) and uses them to instantiate the filter.

  public Customer[] search(int id, String name) throws RecordStoreException, IOException {
        RecordStore rs = null;
        RecordEnumeration recEnum = null;
        Customer[] customerList = null;
        try {
            rs = RecordStore.openRecordStore(mRecordStoreName, true);           
            if (rs.getNumRecords() > 0) {
                CustomerFilter filter = new CustomerFilter(name, id);            
                try {
                    recEnum = rs.enumerateRecords(filter, null, false);
                    if (recEnum.numRecords() > 0) {                  
                        customerList = new Customer[recEnum.numRecords()];
                        int counter = 0;
                        while (recEnum.hasNextElement()) {
                            Customer cust;
                            int idRecord = recEnum.nextRecordId();
                            byte[] filterRecord = rs.getRecord(idRecord);                      
                            cust = parseRecord(filterRecord);
                            cust.idRecord = idRecord;
                            customerList[counter] = cust;
                            counter++;
                        }
                    } 
                    else{
                        customerList = new Customer[0];
                        //How to send a message to the midlet from here
                        //saying something like "No Record Exists.Please select another filter"
                    }                   
                } finally {
                    recEnum.destroy();
                }
            }
            else{
                //How to send a message to the midlet from here
                //saying something like "No Record Exists.Please Add record"
            }
        } finally {
            rs.closeRecordStore();
        }
        return customerList;
    }

Even though, the code shown above works I still have some questions/problems:

In the Filter :

1) How can I improve the code that evaluates the possible values of the filters (name,id)? What if I had more filters?? Will I have to test all the possible combinations??

2) If the user doesn’t enter neither a ID nor a name, should I display all the records or should I display a message "Please enter a name or ID"?? What would you do in this case?

3) Why do I have to put a try-catch in the filter when I can't do anything there?? I can't show any alert from there or can I?

In the search method:

1) How can I show a proper message to the user from that method? something like "No records" (see the "ELSE" parts in my code

Sorry If I asked too many questions, it's just that there's any complete example of filters.

Thanks in advance


Solution

  • How can I improve the code that evaluates the possible values of the filters (name,id)?

    The ID is the first field in the record and the fastest one to search for. If the Id matches, It doesn't really matter what the customer name is. Normally you'll be looking for the records where the ID matches OR the customer name matches, so once the ID matches you can return true. This is my proposal for the CustomerFilter class:

    public class CustomerFilter implements RecordFilter {
    
        private String mName_Filter;
    
        //Use Integer instead of int. 
        //This way we can use null instead of zero if the user didn't type an ID.
        //This allows us to store IDs with values like 0, -1, etc.
        //It is a bit less memory efficient,
        //but you are not creating hundreds of filters, are you? (If you are, don't).
        private Integer mID_Filter; 
    
        public CustomerFilter(String name_Filter, Integer id_Filter) {
            this.mName_Filter = normalizeString(mName_Filter);
            this.mID_Filter = id_Filter;
        }
    
        //You should move this function to an StringUtils class and make it public.
        //Other filters might need it in the future.
        private static String normalizeString(final String s){
            if(s != null){      
                //Warning: you might want to replace accentuated chars as well.
                return s.toLowerCase();
            }
            return null;
        }
    
        public boolean matches(byte[] candidate) {
            ByteArrayInputStream bis = new ByteArrayInputStream(candidate);
            DataInputStream dis = new DataInputStream(bis); 
    
            try {               
                if(mID_Filter != null){
                    //If the ID is unique, and the search is ID OR other fields, this is fine
                    int customerID = dis.readInt();
    
                    if(mID_Filter.intValue == customerID){
                        return true;
                    } else {
                        return false; 
                    }
                }
    
                if(mName_Filter != null){
                    String customerName = normalizeString(dis.readUTF());
                    if(customerName != null && customerName.indexOf(mName_Filter) != -1){
                        return true;
                    }
                }
    
                if(mID_Filter == null && mName_Filter == null){
                    return true; // No filtering, every record matches.
                }
            } catch (IOException ex) {
                //Never swallow exceptions.
                //Even if you are using an underlying ByteArrayInputStream, an exception 
                //can still be thrown when reading from DataInputStream if you try to read
                //fields that do not exists.
    
                //But even if no exceptions were ever thrown, never swallow exceptions :)
                System.err.println(ex);
    
                //Optional: throw ex;
            } finally {
                //Always close streams.
                if(bis != null){
                    try {
                        bis.close();
                    } catch(IOException ioe){
                        System.err.println(ioe);
                    }
                }
    
                if(dis != null){
                    try {
                        dis.close();
                    } catch(IOException ioe){
                        System.err.println(ioe);
                    }
                }
            }
    
            return false;
        }
    }
    




    What if I had more filters?? Will I have to test all the possible combinations??

    It depends on your project. Usually the ID is unique and no two records exist with the same id. In this case you should explicitly design the screen so that the user understands that either he types an Id, or else he fills in the other fields. The condition would be like this:

    idMatches OR (field1Matches AND field2Matches AND ... fieldNMatches)
    

    If the user types nothing, then all records will be returned.

    But then again this is more a UX issue, I don't know if it is valid for your requirements.

    From the programming point of view, what is clear is that the more fields you add, the more messy your filter will became. To prevent this, you could use patterns like Decorator, Composite, and even Chain of responsibility. You'll probably have to trade good design for performance though.




    If the user doesn’t enter neither a ID nor a name, should I display all the records or should I display a message "Please enter a name or ID"?? What would you do in this case?

    It depends. Is there any other way to view all records? If so, then show the message.




    Why do I have to put a try-catch in the filter when I can't do anything there?? I can't show any alert from there or can I?

    You shouldn't. This class is only responsible of filtering, not of interacting with the user. You can still log the error from the catch clause, and then throw the exception again. That will propagate the exception up to RMSCustomer.search, so whatever client code is calling that function will handle the exception in the same way you are handling the other ones thrown by that method. But keep the finally clause to close the streams.




    How can I show a proper message to the user from that method? something like "No records" (see the "ELSE" parts in my code)

    You shouldn't do anything related to the GUI (like showing dialogs) from the RMSCustomer class. Even if you are not using the Model-View-Controller pattern, you still want to keep your class focused on a single responsibility (managing records). This is called the Single responsibility principle. Keeping your class isolated from the GUI will allow you to test it and reuse it in environments without GUI. The no records case should be handled by the screen when there are zero results. An array of lenght == 0 is fine here, and the screen will show the "No results" message. For other kinds of errors, you can extend the Exception class and throw your own custom exceptions, i.e: RecordParsingException, from the RMSCustomer.search method. The screen class will then map the different exceptions to the error message in the language of the user.