package org.ovirt.engine.core.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.core.common.businessentities.VmGuestAgentInterface;
import org.ovirt.engine.core.compat.Guid;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

public class VmGuestAgentInterfaceDaoDbFacadeImpl extends BaseDAODbFacade implements VmGuestAgentInterfaceDao{

    private static final String DELIMITER = ",";

    public List<VmGuestAgentInterface> getAllForVm(Guid vmId) {
        return getAllForVm(vmId, null, false);
    }

    @Override
    public List<VmGuestAgentInterface> getAllForVm(Guid vmId, Guid userId, boolean filtered) {
        return getCallsHandler().executeReadList("GetVmGuestAgentInterfacesByVmId",
                VmGuestAgentInterfaceRowMapper.instance,
                getCustomMapSqlParameterSource().addValue("vm_id", vmId)
                        .addValue("user_id", userId)
                        .addValue("filtered", filtered));
    }

    public void save(VmGuestAgentInterface vmGuestAgentInterface) {
        getCallsHandler().executeModification("InsertVmGuestAgentInterface",
                createFullParametersMapper(vmGuestAgentInterface));
    }

    public void removeAllForVm(Guid vmId) {
        getCallsHandler().executeModification("DeleteVmGuestAgentInterfacesByVmId",
                getCustomMapSqlParameterSource().addValue("vm_id", vmId));
    }

    protected MapSqlParameterSource createFullParametersMapper(VmGuestAgentInterface entity) {
        return getCustomMapSqlParameterSource()
                .addValue("vm_id", entity.getVmId())
                .addValue("interface_name", entity.getInterfaceName())
                .addValue("mac_address", entity.getMacAddress())
                .addValue("ipv4_addresses", getIpAddressesAsString(entity.getIpv4Addresses()))
                .addValue("ipv6_addresses", getIpAddressesAsString(entity.getIpv6Addresses()));
    }

    private String getIpAddressesAsString(List<String> ipAddresses) {
        return StringUtils.join(ipAddresses, DELIMITER);
    }

    protected final static class VmGuestAgentInterfaceRowMapper implements ParameterizedRowMapper<VmGuestAgentInterface> {
        public static VmGuestAgentInterfaceRowMapper instance = new VmGuestAgentInterfaceRowMapper();

        @Override
        public VmGuestAgentInterface mapRow(ResultSet rs, int rowNum)
                throws SQLException {
            VmGuestAgentInterface vmGuestAgentInterface = new VmGuestAgentInterface();
            vmGuestAgentInterface.setVmId(Guid.createGuidFromString(rs.getString("vm_id")));
            vmGuestAgentInterface.setInterfaceName(rs.getString("interface_name"));
            vmGuestAgentInterface.setMacAddress(rs.getString("mac_address"));
            vmGuestAgentInterface.setIpv4Addresses(getListOfIpAddresses(rs.getString("ipv4_addresses")));
            vmGuestAgentInterface.setIpv6Addresses(getListOfIpAddresses(rs.getString("ipv6_addresses")));
            return vmGuestAgentInterface;
        }

        private List<String> getListOfIpAddresses(String ipAddressesAsString) {
            return ipAddressesAsString == null ? null
                    : Arrays.asList(StringUtils.split(ipAddressesAsString, DELIMITER));
        }
    }
}

@@ -599,16 +599,30 @@
 = %22,%22;%0A%0A
+    @Override%0A
     publ
@@ -669,32 +669,32 @@
 Vm(Guid vmId) %7B%0A
-
         return g
@@ -1164,32 +1164,46 @@
 tered));%0A    %7D%0A%0A
+    @Override%0A
     public void 
@@ -1402,24 +1402,38 @@
 e));%0A    %7D%0A%0A
+    @Override%0A
     public v
@@ -2172,16 +2172,23 @@
 private 
+static 
 String g
@@ -2446,16 +2446,22 @@
  static 
+final 
 VmGuestA
@@ -3262,24 +3262,24 @@
 %0A        %7D%0A%0A
-
         priv
@@ -3282,16 +3282,23 @@
 private 
+static 
 List%3CStr

Fix modifiers in VmGuestAgentInterfaceDao