package uk.gov.hscic.appointment.slot;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections4.Transformer;

import uk.gov.hscic.model.appointment.SlotDetail;
import uk.gov.hscic.organization.OrganizationEntity;

public class SlotDetailToSlotEntityTransformer implements Transformer<SlotDetail, SlotEntity> {

    @Override
    public SlotEntity transform(SlotDetail item) {
        SlotEntity slotEntity = new SlotEntity();
        slotEntity.setId(item.getId());

//        if (item.getAppointmentId() != null) {
//            AppointmentEntity appointment = new AppointmentEntity();
//            appointment.setId(item.getAppointmentId());
//            slotEntity.setAppointmentId(appointment);
//        }
        
        List<OrganizationEntity> bookableOrganizations = item.getOrganizationIds().stream().map(id -> createOrganizationEntityWithId(id)).collect(Collectors.toList());

        slotEntity.setTypeCode(item.getTypeCode());
        slotEntity.setTypeDisply(item.getTypeDisply());
        slotEntity.setScheduleReference(item.getScheduleReference());
        slotEntity.setFreeBusyType(item.getFreeBusyType());
        slotEntity.setStartDateTime(item.getStartDateTime());
        slotEntity.setEndDateTime(item.getEndDateTime());
        slotEntity.setLastUpdated(item.getLastUpdated());
        slotEntity.setGpConnectBookable(item.isGpConnectBookable());
        slotEntity.setBookableOrgTypes(item.getOrganizationTypes());
        slotEntity.setBookableOrganizations(bookableOrganizations);
        return slotEntity;
    }
    
    private OrganizationEntity createOrganizationEntityWithId(Long id) {
    	OrganizationEntity orgEnt = new OrganizationEntity();
    	orgEnt.setId(id);
    	return orgEnt;
    }
}

@@ -752,185 +752,8 @@
    %7D
-%0A        %0A        List%3COrganizationEntity%3E bookableOrganizations = item.getOrganizationIds().stream().map(id -%3E createOrganizationEntityWithId(id)).collect(Collectors.toList());
 %0A%0A  
@@ -1304,32 +1304,250 @@
 pes());%0A        
+if(item.getOrganizationIds() != null) %7B%0A        %09List%3COrganizationEntity%3E bookableOrganizations = item.getOrganizationIds().stream().map(id -%3E createOrganizationEntityWithId(id)).collect(Collectors.toList());%0A        %09
 slotEntity.setBo
@@ -1590,16 +1590,35 @@
 tions);%0A
+        %7D%0A        %0A
         

Fix NPE