Example case 3 - Updating individual details

This example shows the most complex case. In this case, the individual shift details are changed.

Before

private void updateRefIdForOrgShiftDetails(List<Long> relievingShiftIds, List<Long> originalShiftSumIds) {
    List<EsShiftCDO> originalShifts = 
        shiftService.getEsShiftCDOsForSummaryIds(originalShiftSumIds);
    List<EsShiftDetail> updateOriginalShiftDtls = new ArrayList<>();
    for (EsShiftCDO orgShift : originalShifts) {
        for (EsShiftDetail orgDtl : orgShift.getShiftDetails()) {
            if (relievingShiftIds.contains(orgDtl.getShiftsumRefId())) {
                orgDtl.setShiftsumRefId(null);
                // Only some of the shift details are changed
                updateOriginalShiftDtls.add(orgDtl);
            }
        }
    }
    shiftService.updateShiftDetails(updateOriginalShiftDtls);
}

In this case you need to find a way to get complete the EsShiftCDO objects before you can save changes to the details. If there is no obvious way to bring EsShiftCDOs into the existing method that updates details, then the safest approach is to reload the details from DB by shiftSummaryIds.

After

private void updateRefIdForOrgShiftDetails(List<Long> relievingShiftIds, List<Long> originalShiftSumIds) {
    List<EsShiftCDO> originalShifts =
        shiftService.getEsShiftCDOsForSummaryIds(originalShiftSumIds);
    List<EsShiftCDO> updateOriginalShifts = new ArrayList<>();
    for (EsShiftCDO orgShift : originalShifts) {
        for (EsShiftDetail orgDtl : orgShift.getShiftDetails()) {
            if (relievingShiftIds.contains(orgDtl.getShiftsumRefId())) {
                orgDtl.setShiftsumRefId(null);
                // Only some of the shift details are changed
                updateOriginalShifts.add(orgShift);
            }
        }
    }
    flatShiftService.insertOrUpdateShiftsBatch(updateOriginalShifts);
}

For example, if a collection of changed EsShiftDetails is all you have, then load all the related shifts and merge the related shifts together before calling FlatShiftService API.