package com.agilex.healthcare.schedulingmanager.mdws.translator;


import java.util.Iterator;

import com.agilex.healthcare.schedulingmanager.domain.AppointmentType;
import com.agilex.healthcare.schedulingmanager.domain.AppointmentTypes;
import com.agilex.healthcare.schedulingmanager.mdws.MdwsResponseException;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.AppointmentTypeArray;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.AppointmentTypeTO;

public class AppointmentTypeTranslator {

	public AppointmentTypes translate(AppointmentTypeArray arr) {
		AppointmentTypes retValue = new AppointmentTypes();
		if (arr == null)
			throw new MdwsResponseException(MdwsResponseException.UserMessages.INVALID_DATA_RETURNED, "AppointmentTypeArray was null");
		if (arr.getFault() != null)
			throw new MdwsResponseException(arr.getFault());
		if (arr.getAppointmentTypes() == null || arr.getAppointmentTypes().getAppointmentTypeTO() == null)
			return retValue;

		Iterator<AppointmentTypeTO> itAppointmentTypeTO = arr.getAppointmentTypes().getAppointmentTypeTO().iterator();
		while (itAppointmentTypeTO.hasNext()) {
			AppointmentTypeTO appointmentTypeTO = itAppointmentTypeTO.next();
			//Perform Error Checking
			if (appointmentTypeTO.getFault() != null)
				throw new MdwsResponseException(appointmentTypeTO.getFault());

			String id = appointmentTypeTO.getId();
			String name = appointmentTypeTO.getName();
			
			AppointmentType appointmentType = new AppointmentType();
			appointmentType.setId(id);
			appointmentType.setName(name);
			retValue.add(appointmentType);
		}

		return retValue;
	}
}
