how to modify the below code in java

Hi Experts,

can some one suggest me how to modify the below code in java

 public Map<String, List<NamedFiltersDTO>> getLlaNamedFilterInfo() {
        LOGGER.info("Lla types are {}", LlaTypeEnum.RESOLUTIONLla.getLlaType());
        List<Object> LlaNamedFilters = namedFiltersDao.getLlaFilterInfo();
        List<NamedFiltersDTO> namedFilters = Collections.emptyList();
        Map<String, List<NamedFiltersDTO>> namedFilter = new HashMap<>();
        if(!LlaNamedFilters.isEmpty()) {
            namedFilters = new ArrayList<NamedFiltersDTO>();
            for (Object object : LlaNamedFilters) {
                Object[] row = (Object[]) object;
                String responseType = (String) row[0];
                LOGGER.info("responseType is {}",responseType);
                LOGGER.info("type is {}",LlaTypeEnum.getEnumByName(responseType));
                if(!MWatchUtil.isNullOrEmpty(responseType)) {
                    if(LlaTypeEnum.getEnumByName(responseType).getId() == 1) {
                        NamedFiltersDTO namedFiltersDTO = new NamedFiltersDTO();
                        namedFiltersDTO.setPermissionName((String) row[1]);
                        namedFiltersDTO.setLlaParameters((String) row[2]);
                        namedFilters.add(namedFiltersDTO);
                        namedFilter.put(String.valueOf(LlaTypeEnum.getEnumByName(responseType).getId()), namedFilters);
                    }else if(LlaTypeEnum.getEnumByName(responseType).getId() == 2){
                        NamedFiltersDTO namedFiltersDTO = new NamedFiltersDTO();
                        namedFiltersDTO.setPermissionName((String) row[1]);
                        namedFiltersDTO.setLlaParameters((String) row[2]);
                        namedFilters.add(namedFiltersDTO);
                        namedFilter.put(String.valueOf(LlaTypeEnum.getEnumByName(responseType).getId()), namedFilters);                        
                    }
                }
            }            
        }
		return namedFilter;

Open in new window



return type Map<id,List<NamedFiltersDTO>>
type id 1 and 2 i am getting the same list of values


From the db i am getting 3 columns  typeid,permissionName,llaParameters

typeid:2:
=========
            
        "permissionName": "list.re6",        "llaParameters": "{"key":"30","value":"Expire within 30 mins","paused":"0","status":"abouttobreach","displayorder":"3"}"
        "permissionName": "list.re3",   "llaParameters": "{"key":"15","value":"Expire within 15 mins","paused":"0","status":"abouttobreach","displayorder":"2"}"
       
            
            
typeid:1:
=========

       "permissionName": "list.e1",     "llaParameters": "{"key":"60","value":"Expire within 1 hr","paused":"0","status":"abouttobreach","displayorder":"1"}"
       "permissionName": "list.e2",     "llaParameters": "{"key":"120","value":"Expire within 2 hrs","paused":"0","status":"abouttobreach","displayorder":"2"}"
     


Thanks,
LVL 2
srikoteshAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
where do I start?

1 - rename the method name to be more meaningful

2 - instead of doing:
if(LlaTypeEnum.getEnumByName(responseType).getId() == 1)

Open in new window


Can you not just compare the enum name?

3 - Object[] row = (Object[]) object
Dont you know at compile time that the list contains Object[] ?
Will get rid of the casting.

4 -Why not put the creation of NamedFiltersDTO into a factory class responsible for creating you the right object.

5 - unless my eyes are deceiving me.
if(LlaTypeEnum.getEnumByName(responseType).getId() == 1) and
else if(LlaTypeEnum.getEnumByName(responseType).getId() == 2)
are executing the same code. Why repeat it twice?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
srikoteshAuthor Commented:
Hi gurpbasi,

1. I will change the method name to meaningful name.
2. I will do comparison with enum name.
5th question

i have two type ids.
type id 1 having 2 records(type id also one of column in those two records),
type id 2 having 2 reocrds.
i am setting in a map
map<id,List<dtoObject>

here id will type id
and list object contains those two reocords(permission name and params)

From the above code i am getting all 4 records for type id 1 and type id 2.
for type id 1 only two records has to set, not all 4 similarily for type id 2 as well.

Please let me know if u require more clarity,
Thanks,
srikoteshAuthor Commented:
as u said on 4 step. Iam doing something wrong creation of a dto object.
please explain how do I do that.

thanks,
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

gurpsbassiCommented:
lets back track to point no 3 first. What are your thoughts?
srikoteshAuthor Commented:
from the db I am getting list object.
I am converting to object array and each record setting into map.
whatever I wrote that is correct only.
I didn't observed any mistake in that line.


I guess if I create another list for type I'd 2 it will work.
gurpsbassiCommented:
so why cant you write
List<Object> LlaNamedFilters = namedFiltersDao.getLlaFilterInfo();
 
as

List<Object[]> LlaNamedFilters = namedFiltersDao.getLlaFilterInfo();

?


OR EVEN

List<String[]> LlaNamedFilters = namedFiltersDao.getLlaFilterInfo();
srikoteshAuthor Commented:
yes u r correct
I will write it as List<Object[]>.
can you tell me what exactly wrong in my code.
I have created another list for type Id 2 now it is working fine.
gurpsbassiCommented:
 if(LlaTypeEnum.getEnumByName(responseType).getId() == 1) {
                        NamedFiltersDTO namedFiltersDTO = new NamedFiltersDTO();
                        namedFiltersDTO.setPermissionName((String) row[1]);
                        namedFiltersDTO.setLlaParameters((String) row[2]);
                        namedFilters.add(namedFiltersDTO);
                        namedFilter.put(String.valueOf(LlaTypeEnum.getEnumByName(responseType).getId()), namedFilters);
                    }else if(LlaTypeEnum.getEnumByName(responseType).getId() == 2){
                        NamedFiltersDTO namedFiltersDTO = new NamedFiltersDTO();
                        namedFiltersDTO.setPermissionName((String) row[1]);
                        namedFiltersDTO.setLlaParameters((String) row[2]);
                        namedFilters.add(namedFiltersDTO);
                        namedFilter.put(String.valueOf(LlaTypeEnum.getEnumByName(responseType).getId()), namedFilters);                        
                    }

Open in new window


The way I read this code:

if(LlaTypeEnum.getEnumByName(responseType).getId() == 1) {
  Do something
}
else if(LlaTypeEnum.getEnumByName(responseType).getId() == 2) {
  DO EXACTLY THE SAME THING !
}
gurpsbassiCommented:
And why go to all the trouble of creating an enum from a string and converting it back to a srting again.

Map<String, List<NamedFiltersDTO>> namedFilter = new HashMap<>();

perhaps could be
Map<LlaTypeEnum, List<NamedFiltersDTO>> namedFilter = new HashMap<>();

By the way LlaTypeEnum is a really bad choice of name!
srikoteshAuthor Commented:
see this code now
i will take care of naming conventions.

		public Map<String, List<NamedFiltersDTO>> getLlaNamedFilterInfo() {
	        LOGGER.info("Lla types are {}", LlaTypeEnum.RESOLUTIONLla.getLlaType());
	        List<Object> LlaNamedFilters = namedFiltersDao.getLlaFilterInfo();	        
	        Map<String, List<NamedFiltersDTO>> namedFilter = new HashMap<>();
	        if(!LlaNamedFilters.isEmpty()) {
	        	List<NamedFiltersDTO> namedFilters = new ArrayList<NamedFiltersDTO>();
	        	List<NamedFiltersDTO> namedFilters2 = new ArrayList<NamedFiltersDTO>();
	            for (Object object : LlaNamedFilters) {
	                Object[] row = (Object[]) object;
	                String responseType = (String) row[0];
	                LOGGER.info("responseType is {}",responseType);
	                LOGGER.info("type is {}",LlaTypeEnum.getEnumByName(responseType));
	                if(!MWatchUtil.isNullOrEmpty(responseType)) {
	                    if(LlaTypeEnum.RESOLUTION.getName.equals(responseType) {
	                        NamedFiltersDTO namedFiltersDTO = convertToNamedFiltersDto(row);
	                        namedFilters.add(namedFiltersDTO);
	                        namedFilter.put(String.valueOf(LlaTypeEnum.getEnumByName(responseType).getId()), namedFilters);
	                    }else if(LlaTypeEnum.RESPONSE.getName.equals(responseType){
	                    	NamedFiltersDTO namedFiltersDTO = convertToNamedFiltersDto(row);
	                        namedFilters2.add(namedFiltersDTO);
	                        namedFilter.put(String.valueOf(LlaTypeEnum.getEnumByName(responseType).getId()), namedFilters2);                        
	                    }
	                }
	            }            
	        }
			return namedFilter;

Open in new window

gurpsbassiCommented:
please make the changes as i suggested. I need the code to be clean before I can comment further.
srikoteshAuthor Commented:
verify now...
public Map<String, List<NamedFiltersDTO>> getTicketFilterInfo() {
	        LOGGER.info("Lla types are {}", TicketTypeEnum.RESOLUTIONLla.getLlaType());
	        List<Object[]> ticketFilters = namedFiltersDao.getTicketFilterInfo();	        
	        Map<String, List<NamedFiltersDTO>> namedFilter = new HashMap<>();
	        if(!ticketFilters.isEmpty()) {
	        	List<NamedFiltersDTO> namedFilters = new ArrayList<NamedFiltersDTO>();
	        	List<NamedFiltersDTO> namedFilters2 = new ArrayList<NamedFiltersDTO>();
	            for (Object row : ticketFilters) {  
	                String responseType = (String) row[0];
	                LOGGER.info("responseType is {}",responseType);
	                LOGGER.info("type is {}",TicketTypeEnum.getEnumByName(responseType));
	                if(!MWatchUtil.isNullOrEmpty(responseType)) {
	                    if(TicketTypeEnum.RESOLUTION.getName.equals(responseType) {
	                        NamedFiltersDTO namedFiltersDTO = convertToNamedFiltersDto(row);
	                        namedFilters.add(namedFiltersDTO);
	                        namedFilter.put(String.valueOf(TicketTypeEnum.getEnumByName(responseType).getId()), namedFilters);
	                    }else if(TicketTypeEnum.RESPONSE.getName.equals(responseType){
	                    	NamedFiltersDTO namedFiltersDTO = convertToNamedFiltersDto(row);
	                        namedFilters2.add(namedFiltersDTO);
	                        namedFilter.put(String.valueOf(TicketTypeEnum.getEnumByName(responseType).getId()), namedFilters2);                        
	                    }
	                }
	            }            
	        }
			return namedFilter;

Open in new window

gurpsbassiCommented:
namedFilters and namedFilters2 are not good names.
What do they represent?

resolution and response filters? please rename
gurpsbassiCommented:
also

if(TicketTypeEnum.RESOLUTION.getName.equals(responseType)

Open in new window


Is getName a method or another bad variable name?
Does this code actually compile?
gurpsbassiCommented:
I would also like to see the implementation of the method:

namedFiltersDao.getTicketFilterInfo()
srikoteshAuthor Commented:
Thanks for valuable suggestions

if(TicketTypeEnum.RESOLUTION.getName.equals(responseType)

i have type it manually it will be like this
if(TicketTypeEnum.RESOLUTION.getName().equals(responseType).

I will change enum property names as well.
gurpsbassiCommented:
hold on. Have you even tested if your code works?
There's no point asking experts on design decisions if the code doesn't compile.
srikoteshAuthor Commented:
yes my code is working as expected.
what the mistake I made earlier is
I haven't created another list for type I'd 2.
srikoteshAuthor Commented:
yes my code is working as expected.
what the mistake I made earlier is
I haven't created another list for type I'd 2.
gurpsbassiCommented:
you have awarded the points too soon.
I was just getting started.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.