hari-p
8/6/2015 - 1:02 AM

RowMapper example

RowMapper example

package au.edu.educationau.tlf.econtent.dao.jdbc;

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

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import au.edu.educationau.tlf.econtent.dao.IntermediateGroupDao;
import au.edu.educationau.tlf.econtent.model.IntermediateGroup;
import au.edu.educationau.tlf.econtent.model.YearLevelStrand;

public class IntermediateGroupDaoJdbc extends SimpleJdbcDaoSupport implements IntermediateGroupDao{
	private static final String CREATE_INTERMEDIATE_GROUP_ENTRY = "INSERT INTO intermediate_group( "+
            "ac_uid, year_level_strand_id, group_title, group_description, group_type, linked_tables, acc_order) "+
            "VALUES (?, ?, ?, ?, ?, ?, ?) Returning internal_id;";
	
	private static final String GET_INTERMEDIATE_GROUP_BY_STRAND="SELECT internal_id, year_level_strand_id, ac_uid, group_title, group_description, "+
			"group_type, linked_tables, acc_order "+
			"FROM intermediate_group where year_level_strand_id =?;";
	
	private static final String DELETE_INTERMEDIATE_GROUP = "delete from intermediate_group;";

	@Override
	public int createIntermediateGroupEntry(IntermediateGroup intermediateGroup) {
		int idValue = getSimpleJdbcTemplate().queryForInt(CREATE_INTERMEDIATE_GROUP_ENTRY, StringUtils.substringAfterLast(intermediateGroup.getAcUid(), "/"),
				intermediateGroup.getYearLevelStrandId(), intermediateGroup.getGroupTitle(), intermediateGroup.getGroupDescription()
				, intermediateGroup.getGroupType(),intermediateGroup.getLinked_tables(), intermediateGroup.getAccOrder());
		return idValue;
	} 
	
	@Override
	public void deleteIntermediateGroup(){
		try{
			getSimpleJdbcTemplate().update(DELETE_INTERMEDIATE_GROUP);
		}catch(Exception e) {
			System.out.println("Error deleting has cd: " + e);
		}
	}

	@Override
	public List<IntermediateGroup> getIntermediateGroupByStrand(Integer strandId) {
		return getSimpleJdbcTemplate().query(GET_INTERMEDIATE_GROUP_BY_STRAND, new IntermediateGroupRowMapper(), strandId);
	}
	
	static class IntermediateGroupRowMapper implements
		ParameterizedRowMapper<IntermediateGroup> {
		public IntermediateGroup mapRow(ResultSet rs, int rowNumber) throws SQLException {
			IntermediateGroup intermediateGroup = new IntermediateGroup();
			intermediateGroup.setId(rs.getInt("internal_id"));
			intermediateGroup.setAcUid(rs.getString("ac_uid"));
			intermediateGroup.setAccOrder(rs.getInt("acc_order"));
			intermediateGroup.setGroupTitle(rs.getString("group_title"));
			intermediateGroup.setGroupType(rs.getString("group_type"));
			intermediateGroup.setGroupDescription(rs.getString("group_description"));
			intermediateGroup.setLinked_tables(rs.getString("linked_tables"));
			intermediateGroup.setYearLevelStrandId(rs.getInt("year_level_strand_id"));
			
			return intermediateGroup;
		}
	}

}
public List<String> getAllSubjectNameByLearningArea(String learningArea) {	
		 Object[] objs = new Object[]{learningArea};
		 return getSimpleJdbcTemplate().query(GET_ALL_SUBJECT_NAME_BY_LEARNING_AREA, new ParameterizedRowMapper<String>() {
           public String mapRow(ResultSet rs, int rowNum) throws SQLException {
               return rs.getString("subject_name");
           }
       },objs);
	}