Issue
I'm new to Java and object oriented programming. I've tried to find the solution myself, but couldn't manage to solve this problem.
I have two duplicated pieces of code which I need to remove. As I understand it, I have to create a super class for it. How can I create a super class or method in order to remove the duplicated code?:
public class PartnerOneService {
public static void updatePartner(Handle handle, Long mandant, Long rID, Integer roleCd, Long lebId, String user) throws ResourceNotFoundException {
// 1: Check if partner already exists
Role roleInDb = roleSql.readActive(mandant, rID, roleCd);
if (roleInDb == null) {
// 2: Insert new partner
Process process = processCheck.get(0);
Role role = createRole(mandant, roleCd, rID, process.getProcessId(), process.getProcessSubId(), lebId);
roleVersioning.insert(role, TecTaskCd.MAN_REGULATION.getValue(), user, SfDate.getTimestampMilli(), ActualizationStatus.FINAL, false);
} else {
// 3: Update partner
roleInDb.setPartnerId(lebId);
roleVersioning.insert(roleInDb, TecTaskCd.MAN_REGULATION.getValue(), user, SfDate.getTimestampMilli(), ActualizationStatus.FINAL, false);
}
}
}
public class PartnerTwoService {
public static void updatePartner(Handle handle, Long mandant, Long rID, Integer roleCd, Long lebId, String user) throws ResourceNotFoundException {
// 1: Check if partner already exists
Role roleInDb = roleSql.readActive(mandant, rID, roleCd);
if (roleInDb == null) {
// 2: Insert new partner
Process process = processCheck.get(0);
Role role = createRole(mandant, roleCd, rID, process.getProcessId(), process.getProcessSubId(), lebId);
roleVersioning.insert(role, TecTaskCd.MAN_REGULATION.getValue(), user, SfDate.getTimestampMilli(), ActualizationStatus.FINAL, false);
} else {
// 3: Update partner
roleInDb.setPartnerId(lebId);
roleVersioning.insert(roleInDb, TecTaskCd.MAN_REGULATION.getValue(), user, SfDate.getTimestampMilli(), ActualizationStatus.FINAL, false);
}
}
}
Solution
You can create third service like RolePartnerService and put this code in where. Then you need to set roles partner you can call it like
rolePartnerService.setRole(mandant, rID, roleCd); //and whatever arguments you need
Answered By - Ekaterina
Answer Checked By - Mildred Charles (JavaFixing Admin)