Source:
public ServiceResponse<List<PatientItem>> retrievePatientsFromQueryString(String patientSearchQuery) { ServiceResponse<List<PatientItem>> response = new ServiceResponse<>(); if (StringUtils.isNullOrWhiteSpace(patientSearchQuery)) { response.addError("", "bad parameters"); return response; } String[] words = patientSearchQuery.trim().split(" "); Integer id = null; String firstName = null; String lastName = null; String firstOrLastName = null; if (words.length == 0) { response.addError("", "query string empty"); return response; } else if (words.length == 1) { try { id = Integer.parseInt(words[0]); } catch (NumberFormatException ex) { firstName = words[0]; } } else if (words.length == 2) { firstName = words[0]; lastName = words[1]; } else { response.addError("", "too many words in query string"); return response; } List<? extends IPatient> patients; if (id != null) { patients = patientRepository.findPatientsById(id); } else { patients = patientRepository.findPatientsByName(firstName, lastName); } try { List<PatientItem> patientItems = new ArrayList<>(); for (IPatient patient : patients) { String pathToPhoto = null; Integer photoId = null; if (patient.getPhoto() != null) { pathToPhoto = patient.getPhoto().getFilePath(); photoId = patient.getPhoto().getId(); } patientItems.add(itemModelMapper.createPatientItem( patient.getId(), patient.getFirstName(), patient.getLastName(), patient.getCity(), patient.getAddress(), patient.getUserId(), patient.getAge(), patient.getSex(), null, null, null, null, pathToPhoto, photoId, null )); } response.setResponseObject(patientItems); } catch (Exception ex) { response.addError("", ex.getMessage()); } return response; }

Target:
public ServiceResponse<List<PatientItem>> retrievePatientsFromQueryString(String patientSearchQuery) { ServiceResponse<List<PatientItem>> response = new ServiceResponse<>(); if (StringUtils.isNullOrWhiteSpace(patientSearchQuery)) { response.addError("", "bad parameters"); return response; } String[] words = patientSearchQuery.trim().split(" "); Integer id = null; String firstName = null; String lastName = null; if (words.length == 0) { response.addError("", "query string empty"); return response; } else if (words.length == 1) { try { id = Integer.parseInt(words[0]); } catch (NumberFormatException ex) { firstName = words[0]; } } else if (words.length == 2) { firstName = words[0]; lastName = words[1]; } else { response.addError("", "too many words in query string"); return response; } List<? extends IPatient> patients; if (id != null) { patients = patientRepository.findPatientsById(id); } else { patients = patientRepository.findPatientsByName(firstName, lastName); } try { List<PatientItem> patientItems = new ArrayList<>(); for (IPatient patient : patients) { String pathToPhoto = null; Integer photoId = null; if (patient.getPhoto() != null) { pathToPhoto = patient.getPhoto().getFilePath(); photoId = patient.getPhoto().getId(); } patientItems.add(itemModelMapper.createPatientItem( patient.getId(), patient.getFirstName(), patient.getLastName(), patient.getCity(), patient.getAddress(), patient.getUserId(), patient.getAge(), patient.getSex(), null, null, null, null, pathToPhoto, photoId, null )); } response.setResponseObject(patientItems); } catch (Exception ex) { response.addError("", ex.getMessage()); } return response; }