package org.ovirt.engine.core.dao.network;

import org.ovirt.engine.core.common.businessentities.network.NetworkQoS;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.dao.DefaultGenericDaoDbFacade;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;

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

public class NetworkQoSDaoFacadeImpl extends DefaultGenericDaoDbFacade<NetworkQoS, Guid> implements NetworkQoSDao {

    protected final RowMapper<NetworkQoS> mapper = createEntityRowMapper();

    public NetworkQoSDaoFacadeImpl(){
        super("NetworkQos");
    }

    private static Integer getIntegerOrNull(ResultSet rs, String columnName) throws SQLException {
        String string = rs.getString(columnName);
        return string == null ? null : Integer.parseInt(string);
    }

    @Override
    protected MapSqlParameterSource createIdParameterMapper(Guid guid) {
        return getCustomMapSqlParameterSource()
                .addValue("id", guid);
    }

    @Override
    protected RowMapper<NetworkQoS> createEntityRowMapper() {
        return new RowMapper<NetworkQoS>() {
            @Override
            public NetworkQoS mapRow(ResultSet rs, int rowNum)
                    throws SQLException {
                NetworkQoS entity = new NetworkQoS();
                entity.setId(Guid.createGuidFromString(rs.getString("id")));
                entity.setName(rs.getString("name"));
                entity.setStoragePoolId(Guid.createGuidFromString(rs.getString("storage_pool_id")));
                entity.setInboundAverage(getIntegerOrNull(rs, "inbound_average"));
                entity.setInboundPeak(getIntegerOrNull(rs, "inbound_peak"));
                entity.setInboundBurst(getIntegerOrNull(rs, "inbound_burst"));
                entity.setOutboundAverage(getIntegerOrNull(rs, "outbound_average"));
                entity.setOutboundPeak(getIntegerOrNull(rs, "outbound_peak"));
                entity.setOutboundBurst(getIntegerOrNull(rs, "outbound_burst"));
                return entity;
            }
        };
    }

    @Override
    public List<NetworkQoS> getAllForStoragePoolId(Guid storagePoolId) {
        MapSqlParameterSource parameterSource = getCustomMapSqlParameterSource()
                .addValue("storage_pool_id", storagePoolId.getUuid());
        return getCallsHandler().executeReadList("GetAllNetworkQosForStoragePool", mapper, parameterSource);
    }

    @Override
    protected MapSqlParameterSource createFullParametersMapper(NetworkQoS networkQoS) {
        return getCustomMapSqlParameterSource()
                .addValue("id", networkQoS.getId())
                .addValue("name", networkQoS.getName())
                .addValue("storage_pool_id", networkQoS.getStoragePoolId())
                .addValue("inbound_average", networkQoS.getInboundAverage())
                .addValue("inbound_peak", networkQoS.getInboundPeak())
                .addValue("inbound_burst", networkQoS.getInboundBurst())
                .addValue("outbound_average", networkQoS.getOutboundAverage())
                .addValue("outbound_peak", networkQoS.getOutboundPeak())
                .addValue("outbound_burst", networkQoS.getOutboundBurst());

    }
}

@@ -792,21 +792,13 @@
     
-String string
+int i
  = r
@@ -802,22 +802,19 @@
 = rs.get
-String
+Int
 (columnN
@@ -838,22 +838,20 @@
 urn 
-string == n
+rs.wasN
 ull
+()
  ? n
@@ -860,32 +860,9 @@
 l : 
-Integer.parseInt(string)
+i
 ;%0A  
@@ -1382,54 +1382,26 @@
 tId(
-Guid.createGuidFromString(rs.getString(
+getGuid(rs, 
 %22id%22))
-)
 ;%0A  
@@ -1496,47 +1496,20 @@
 lId(
-Guid.createGuidFromString(rs.getString(
+getGuid(rs, 
 %22sto
@@ -1523,17 +1523,16 @@
 ol_id%22))
-)
 ;%0A      

Fix optimization of NetworkQoSDaoFacadeImpl