Link to home
Start Free TrialLog in
Avatar of chaitu chaitu
chaitu chaituFlag for India

asked on

how to change this code being hardcoded in hashmap

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;
 }


Avatar of colr__
colr__

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__
Avatar of chaitu chaitu

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of colr__
colr__

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
mukundha_expert

i didnt get u
Avatar of Mayank S
You still need to maintain a list somewhere, though.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial