?
Solved

how to change this code being hardcoded in hashmap

Posted on 2006-06-05
10
Medium Priority
?
446 Views
Last Modified: 2012-06-21
from servlet i am calling this method where i am validating certain things;

 i dont want to hardcoded in validateCharges method in the key value pair;if i want to validate 100 fields then its very difficult to write hardcoded.

 how to change this code?
 
        HashMap map = Utility.validateCharges(chargesForm);

           ActionMessages errors=new ActionMessages();

          if(map.containsKey("ori"))
        {

            errors.add("Invalid", new Message(("Invalid Data :"),false));
               if(map.get("ori")!= null)
                     errors.add("ori", new Message((String)map.get("ori"),false));
               if(map.get("destination")!= null)
                     errors.add("destination", new Message((String)map.get("dest"),false));
                 if(map.get("commodityCode")!= null)
                     errors.add("commodityCode", new Message((String)map.get("Code"),false));
               if(map.get("Level")!= null)
                     errors.add("Level", new Message((String)map.get("Level"),false));
                 if(map.get("serviceType")!= null)
                     errors.add("serviceType", new Message((String)map.get("serviceType"),false));
            
        }

 
 public HashMap  validateCharges(ChargesDOB chargesForm)
 {
   String ori chargesForm.getOri();
   String oriType = chargesForm.getOriType();
   
   String destination=chargesForm.getDest();
   String destinationType=chargesForm.getDestType();
   
   String Code = chargesForm.getCode();
   
   String Level = chargesForm.getLevel();
   String serviceType = chargesForm.getType();
   
   HashMap      userInfo      = new HashMap();
   String message = "";
    if(ori != null && !ori.equals(""))
    {
    if(oriType != null && oriType.equalsIgnoreCase("Sta"))
    message =validateOrigin(ori);
    if(message!="")
        userInfo.put("ori",message);
    }
     if(destination != null && !destination.equals(""))
    {
    if(destinationType != null && destinationType.equalsIgnoreCase("Dest"))
    message =validateDestination(destination);
    if(message!="")
        userInfo.put("dest",message);
    }
     if(Code != null && !Code.equals(""))
    {
     message =validateCommodity(Code);
    if(message!="")
        userInfo.put("Code",message);
    }
      if(Level != null && !Level.equals(""))
    {
     message =validateServiceLevel(Level);
    if(message!="")
        userInfo.put("Level",message);
    }
      if(serviceType != null && !serviceType.equals(""))
    {
     message =validateServiceType(serviceType);
    if(message!="")
        userInfo.put("serviceType",message);
    }
 
     return userInfo;
 }


0
Comment
Question by:chaitu chaitu
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 8

Expert Comment

by:colr__
ID: 16833063
Get an iterator object form the map, and iterate through each element ensuign that it isnt empty.

Any other checks would be dependant on the value itself, so these would *have* to be done manually.

colr__
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 16833167
IF there are 100 fields are there in UI;so i want to validate each field entered in UI against database value;if user entered 4 to 5 fields wrong values;then i want to show the user that u entered these many fields wrong data;how will u approach this?
0
 
LVL 8

Accepted Solution

by:
colr__ earned 1000 total points
ID: 16833499
You will have to maintain a list of the input elements in the UI, in something like an array.

Then when you are validating, progress through each element in that array and check it has sufficient input values. If you dont maintain a list, tyhen you cant access them in this manner, and you'd have to explitly wright validation code for each separet one. With them all in an array, you can do the following:

for (int i=0; i<theArray.length; i++){
  if (!theArray[i].getText().equals(""))
    // this is ok - a value has been set
  else
    // this input doesnt have anything set
}

colr__
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:mukundha_expert
ID: 16835610
I think you can create a Bean class with getters and setters for all the input  fields.

you can make use of <jsp:useBean>, <jsp:setProperty> to create the bean with all the fields.

Write a stored procedure in DB, to validate the input fields and return  number of mismatches or type of error.

call the SP from ur JSP or servlet and do what ever u wanted.
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 16841913
mukundha_expert

i didnt get u
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16842300
You still need to maintain a list somewhere, though.
0
 
LVL 10

Assisted Solution

by:mukundha_expert
mukundha_expert earned 1000 total points
ID: 16849471


Hi

For eg. Take a CustomerBean with ID,name,age.

What you can do in the bean class is

import java.util.ArrayList;
import java.util.Hashtable;

public class CustomerBean {
            String ID;
            String name;
            String age;

            String[] names=  {"ID","name","age"};

            Hashtable table = new Hashtable () ;

            public String getAge() {

                        return age;

            }

            public void setAge(String age) {

                        if(Validate.validateAge ( age )) {

                        this.age = age;

                        table.put ( new Integer(3),age) ;

                        }

            }

            public String getID() {

                        return ID;

            }

            public void setID(String id) {

                        if (Validate.validateID ( ID ) ) {

                        this.ID = id;

                        table.put (new Integer(1),ID );

                        }

            }

            public String getName() {

                        return name;

            }

            public void setName(String name) {

                        if (Validate.validateName ( name )){

                        this.name = name;

                        table.put ( new Integer ( 2),name ) ;

                        }

            }

           

            public ArrayList checkForNulls ()

            {

                       

                        ArrayList error = new ArrayList ();

                        for ( int i =0 ;i < names.length ; i ++ )

                        {

                                    Object obj = table.get(new Integer ( i ) ) ;

                                    if ( obj == null ){

                                                error.add(names[i]);

                                    }

                        }

                        return error;

            }

}

Youc can create an instance of this bean in the JSP using the jsp:useBean and set the properties using jsp:setProperty

later you can call the checkForNulls () to get the list of fields that are violating ur conditions.
the key to the field names in the HashTable is corresponding to what you give in the names[];
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question