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

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

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

public class VnicProfileDaoDbFacadeImpl extends DefaultGenericDaoDbFacade<VnicProfile, Guid> implements VnicProfileDao {

    public VnicProfileDaoDbFacadeImpl() {
        super("VnicProfile");
    }

    @Override
    public List<VnicProfile> getAllForNetwork(Guid networkId) {
        return getCallsHandler().executeReadList("GetVnicProfilesByNetworkId",
                VnicProfileRowMapper.INSTANCE,
                getCustomMapSqlParameterSource().addValue("network_id", networkId));
    }

    @Override
    protected MapSqlParameterSource createFullParametersMapper(VnicProfile profile) {
        return createIdParameterMapper(profile.getId())
                .addValue("name", profile.getName())
                .addValue("network_id", profile.getNetworkId())
                .addValue("port_mirroring", profile.isPortMirroring())
                .addValue("description", profile.getDescription())
                .addValue("custom_properties",
                        SerializationFactory.getSerializer().serialize(profile.getCustomProperties()));
    }

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

    @Override
    protected RowMapper<VnicProfile> createEntityRowMapper() {
        return VnicProfileRowMapper.INSTANCE;
    }

    static class VnicProfileRowMapper implements RowMapper<VnicProfile> {

        public final static VnicProfileRowMapper INSTANCE = new VnicProfileRowMapper();

        @Override
        @SuppressWarnings("unchecked")
        public VnicProfile mapRow(ResultSet rs, int rowNum) throws SQLException {
            VnicProfile entity = new VnicProfile();
            entity.setId(Guid.createGuidFromString(rs.getString("id")));
            entity.setName(rs.getString("name"));
            entity.setNetworkId(Guid.createGuidFromString(rs.getString("network_id")));
            entity.setCustomProperties(SerializationFactory.getDeserializer()
                    .deserializeOrCreateNew(rs.getString("custom_properties"), LinkedHashMap.class));
            entity.setPortMirroring(rs.getBoolean("port_mirroring"));
            entity.setDescription(rs.getString("description"));
            return entity;
        }
    }
}

@@ -1870,16 +1870,25 @@
  static 
+abstract 
 class Vn
@@ -1909,30 +1909,23 @@
 pper
- implement
+Base%3CT extend
 s 
-RowMapper%3C
 Vnic
@@ -1937,98 +1937,33 @@
 le%3E 
-%7B%0A%0A        public final static VnicProfileRowMapper INSTANCE = new VnicProfileRowMapper();
+implements RowMapper%3CT%3E %7B
 %0A%0A  
@@ -2032,27 +2032,17 @@
  public 
-VnicProfile
+T
  mapRow(
@@ -2101,27 +2101,17 @@
         
-VnicProfile
+T
  entity 
@@ -2116,12 +2116,14 @@
 y = 
-new 
+create
 Vnic
@@ -2129,16 +2129,22 @@
 cProfile
+Entity
 ();%0A    
@@ -2168,54 +2168,26 @@
 tId(
-Guid.createGuidFromString(rs.getString(
+getGuid(rs, 
 %22id%22))
-)
 ;%0A  
@@ -2270,47 +2270,20 @@
 kId(
-Guid.createGuidFromString(rs.getString(
+getGuid(rs, 
 %22net
@@ -2292,17 +2292,16 @@
 rk_id%22))
-)
 ;%0A      
@@ -2644,16 +2644,388 @@
        %7D
+%0A%0A        abstract protected T createVnicProfileEntity();%0A    %7D%0A%0A    private static class VnicProfileRowMapper extends VnicProfileRowMapperBase%3CVnicProfile%3E %7B%0A%0A        public static final VnicProfileRowMapper INSTANCE = new VnicProfileRowMapper();%0A%0A        @Override%0A        protected VnicProfile createVnicProfileEntity() %7B%0A            return new VnicProfile();%0A        %7D
 %0A    %7D%0A%7D

Refactor VnicProfile row mapper