
package org.openmuc.framework.lib.rest1.rest.objects;

import org.openmuc.framework.config.DeviceConfig;
import org.openmuc.framework.config.IdCollisionException;
import org.openmuc.framework.lib.rest1.exceptions.RestConfigIsNotCorrectException;

public class RestDeviceConfigMapper {

    public static RestDeviceConfig getRestDeviceConfig(DeviceConfig dc) {

        RestDeviceConfig rdc = new RestDeviceConfig();
        rdc.setConnectRetryInterval(dc.getConnectRetryInterval());
        rdc.setDescription(dc.getDescription());
        rdc.setDeviceAddress(dc.getDeviceAddress());
        rdc.isDisabled(dc.isDisabled());
        rdc.setId(dc.getId());
        rdc.setSamplingTimeout(dc.getSamplingTimeout());
        rdc.setSettings(dc.getSettings());
        return rdc;
    }

    public static void setDeviceConfig(DeviceConfig dc, RestDeviceConfig rdc, String idFromUrl)
            throws IdCollisionException, RestConfigIsNotCorrectException {

        if (dc == null) {
            throw new RestConfigIsNotCorrectException("DriverConfig is null!");
        }
        else {

            if (rdc != null) {
                if (rdc.getId() != null && !rdc.getId().equals("") && !idFromUrl.equals(rdc.getId())) {
                    dc.setId(rdc.getId());
                }
                dc.setConnectRetryInterval(rdc.getConnectRetryInterval());
                dc.setDescription(rdc.getDescription());
                dc.setDeviceAddress(rdc.getDeviceAddress());
                dc.setDisabled(rdc.getDisabled());
                dc.setSamplingTimeout(rdc.getSamplingTimeout());
                dc.setSettings(rdc.getSettings());
            }
            else {
                throw new RestConfigIsNotCorrectException();
            }
        }

    }
}
