Source:
<code>public Patient savePatient(Patient patient) throws APIException { if (patient.getPatientId() == null) { Context.requirePrivilege(PrivilegeConstants.ADD_PATIENTS); } else { Context.requirePrivilege(PrivilegeConstants.EDIT_PATIENTS); } if (patient.isVoided()) { Context.requirePrivilege(PrivilegeConstants.DELETE_PATIENTS); } if (patient.getIdentifiers().size() == 1) { patient.getPatientIdentifier().setPreferred(true); } <START> synchronized (PatientServiceImpl.class) { <END> if (!patient.isVoided()) { checkPatientIdentifiers(patient); } PatientIdentifier preferredIdentifier = null; PatientIdentifier possiblePreferredId = patient.getPatientIdentifier(); if (possiblePreferredId != null && possiblePreferredId.isPreferred() && !possiblePreferredId.isVoided()) { preferredIdentifier = possiblePreferredId; } for (PatientIdentifier id : patient.getIdentifiers()) { if (preferredIdentifier == null && !id.isVoided()) { id.setPreferred(true); preferredIdentifier = id; continue; } if (!id.equals(preferredIdentifier)) { id.setPreferred(false); } } PersonName preferredName = null; PersonName possiblePreferredName = patient.getPersonName(); if (possiblePreferredName != null && possiblePreferredName.isPreferred() && !possiblePreferredName.isVoided()) { preferredName = possiblePreferredName; } for (PersonName name : patient.getNames()) { if (preferredName == null && !name.isVoided()) { name.setPreferred(true); preferredName = name; continue; } if (!name.equals(preferredName)) { name.setPreferred(false); } } PersonAddress preferredAddress = null; PersonAddress possiblePreferredAddress = patient.getPersonAddress(); if (possiblePreferredAddress != null && possiblePreferredAddress.isPreferred() && !possiblePreferredAddress.isVoided()) { preferredAddress = possiblePreferredAddress; } for (PersonAddress address : patient.getAddresses()) { if (preferredAddress == null && !address.isVoided()) { address.setPreferred(true); preferredAddress = address; continue; } if (!address.equals(preferredAddress)) { address.setPreferred(false); } } return dao.savePatient(patient); } }</code><technical_language>I 'this' instead of class object, this is a spring bean is a singleton</technical_language>

Target:
public Patient savePatient(Patient patient) throws APIException { if (patient.getPatientId() == null) { Context.requirePrivilege(PrivilegeConstants.ADD_PATIENTS); } else { Context.requirePrivilege(PrivilegeConstants.EDIT_PATIENTS); } if (patient.isVoided()) { Context.requirePrivilege(PrivilegeConstants.DELETE_PATIENTS); } if (patient.getIdentifiers().size() == 1) { patient.getPatientIdentifier().setPreferred(true); } if (!patient.isVoided()) { checkPatientIdentifiers(patient); } PatientIdentifier preferredIdentifier = null; PatientIdentifier possiblePreferredId = patient.getPatientIdentifier(); if (possiblePreferredId != null && possiblePreferredId.isPreferred() && !possiblePreferredId.isVoided()) { preferredIdentifier = possiblePreferredId; } for (PatientIdentifier id : patient.getIdentifiers()) { if (preferredIdentifier == null && !id.isVoided()) { id.setPreferred(true); preferredIdentifier = id; continue; } if (!id.equals(preferredIdentifier)) { id.setPreferred(false); } } PersonName preferredName = null; PersonName possiblePreferredName = patient.getPersonName(); if (possiblePreferredName != null && possiblePreferredName.isPreferred() && !possiblePreferredName.isVoided()) { preferredName = possiblePreferredName; } for (PersonName name : patient.getNames()) { if (preferredName == null && !name.isVoided()) { name.setPreferred(true); preferredName = name; continue; } if (!name.equals(preferredName)) { name.setPreferred(false); } } PersonAddress preferredAddress = null; PersonAddress possiblePreferredAddress = patient.getPersonAddress(); if (possiblePreferredAddress != null && possiblePreferredAddress.isPreferred() && !possiblePreferredAddress.isVoided()) { preferredAddress = possiblePreferredAddress; } for (PersonAddress address : patient.getAddresses()) { if (preferredAddress == null && !address.isVoided()) { address.setPreferred(true); preferredAddress = address; continue; } if (!address.equals(preferredAddress)) { address.setPreferred(false); } } return dao.savePatient(patient); }