package org.ovirt.engine.core.dao;

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

import org.ovirt.engine.core.common.VdcObjectType;
import org.ovirt.engine.core.common.utils.EnumUtils;
import org.ovirt.engine.core.compat.Guid;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

/**
 * Implements the CRUD operations for job_subject_entity, a satellite table of Job.
 *
 */
public class JobSubjectEntityDaoFacadeImpl extends BaseDAODbFacade implements JobSubjectEntityDao {

    @Override
    public void save(Guid jobId, Guid entityId, VdcObjectType entityType) {
        MapSqlParameterSource parameterSource = getCustomMapSqlParameterSource()
                .addValue("job_id", jobId)
                .addValue("entity_id", entityId)
                .addValue("entity_type", EnumUtils.nameOrNull(entityType));

        getCallsHandler().executeModification("InsertJobSubjectEntity", parameterSource);
    }

    @Override
    public Map<Guid, VdcObjectType> getJobSubjectEntityByJobId(Guid jobId) {
        MapSqlParameterSource parameterSource = getCustomMapSqlParameterSource()
                .addValue("job_id", jobId);

        ParameterizedRowMapper<JobSubjectEntity> mapper = new ParameterizedRowMapper<JobSubjectEntity>() {

            @Override
            public JobSubjectEntity mapRow(ResultSet rs, int rowNum) throws SQLException {
                JobSubjectEntity entity = new JobSubjectEntity();
                entity.setEntityId(Guid.createGuidFromString(rs.getString("entity_id")));
                entity.setEntityType(VdcObjectType.valueOf(rs.getString("entity_type")));
                return entity;
            }
        };
        List<JobSubjectEntity> list =
                getCallsHandler().executeReadList("GetJobSubjectEntityByJobId", mapper, parameterSource);

        Map<Guid, VdcObjectType> entityMap = new HashMap<Guid, VdcObjectType>();
        for (JobSubjectEntity jobSubjectEntity : list) {
            entityMap.put(jobSubjectEntity.getEntityId(), jobSubjectEntity.getEntityType());
        }
        return entityMap;
    }

    @Override
    public List<Guid> getJobIdByEntityIdAndEntityType(Guid entityId) {
        MapSqlParameterSource parameterSource = getCustomMapSqlParameterSource()
                .addValue("entity_id", entityId);

        ParameterizedRowMapper<Guid> mapper = new ParameterizedRowMapper<Guid>() {

            @Override
            public Guid mapRow(ResultSet rs, int rowNum) throws SQLException {
                return Guid.createGuidFromString(rs.getString(1));
            }
        };

        return getCallsHandler().executeReadList("GetAllJobIdsByEntityIdAndEntityType", mapper, parameterSource);
    }

    private static class JobSubjectEntity {
        private Guid entityId;
        private VdcObjectType entityType;

        public void setEntityId(Guid entityId) {
            this.entityId = entityId;
        }

        public Guid getEntityId() {
            return entityId;
        }

        public void setEntityType(VdcObjectType entityType) {
            this.entityType = entityType;
        }

        public VdcObjectType getEntityType() {
            return entityType;
        }
    }

}

@@ -636,24 +636,204 @@
 ntityDao %7B%0A%0A
+    private static JobSubjectEntityRowMapper jobSubjectEntityRowMapper = new JobSubjectEntityRowMapper();%0A    private static JobIdRowMapper jobIdRowMapper = new JobIdRowMapper();%0A%0A
     @Overrid
@@ -1486,537 +1486,143 @@
     
-ParameterizedRowMapper%3CJobSubjectEntity%3E mapper = new ParameterizedRowMapper%3CJobSubjectEntity%3E() %7B%0A%0A            @Override%0A            public JobSubjectEntity mapRow(ResultSet rs, int rowNum) throws SQLException %7B%0A                JobSubjectEntity entity = new JobSubjectEntity();%0A                entity.setEntityId(Guid.createGuidFromString(rs.getString(%22entity_id%22)));%0A                entity.setEntityType(VdcObjectType.valueOf(rs.getString(%22entity_type%22)));%0A                return entity;%0A            %7D%0A        %7D;%0A        List%3CJ
+List%3CJobSubjectEntity%3E list =%0A                getCallsHandler().executeReadList(%22GetJobSubjectEntityByJobId%22,%0A                        j
 obSu
@@ -1628,32 +1628,34 @@
 ubjectEntity
-%3E list =
+RowMapper,
 %0A           
@@ -1663,79 +1663,15 @@
     
-getCallsHandler().executeReadList(%22GetJobSubjectEntityByJobId%22, mapper,
+       
  par
@@ -2181,286 +2181,8 @@
 );%0A%0A
-        ParameterizedRowMapper%3CGuid%3E mapper = new ParameterizedRowMapper%3CGuid%3E() %7B%0A%0A            @Override%0A            public Guid mapRow(ResultSet rs, int rowNum) throws SQLException %7B%0A                return Guid.createGuidFromString(rs.getString(1));%0A            %7D%0A        %7D;%0A%0A
     
@@ -2265,17 +2265,25 @@
 yType%22, 
-m
+jobIdRowM
 apper, p
@@ -2808,10 +2808,755 @@
 %0A    %7D%0A%0A
+    private static class JobSubjectEntityRowMapper implements ParameterizedRowMapper%3CJobSubjectEntity%3E %7B%0A%0A        @Override%0A        public JobSubjectEntity mapRow(ResultSet rs, int rowNum) throws SQLException %7B%0A            JobSubjectEntity entity = new JobSubjectEntity();%0A            entity.setEntityId(Guid.createGuidFromString(rs.getString(%22entity_id%22)));%0A            entity.setEntityType(VdcObjectType.valueOf(rs.getString(%22entity_type%22)));%0A            return entity;%0A        %7D%0A    %7D%0A%0A    private static class JobIdRowMapper implements ParameterizedRowMapper%3CGuid%3E %7B%0A%0A        @Override%0A        public Guid mapRow(ResultSet rs, int rowNum) throws SQLException %7B%0A            return Guid.createGuidFromString(rs.getString(1));%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Use single mapper instance by JobSubjectEntity