package org.artgasmator.data.dao.impl;

import org.artgasmator.data.dao.ArtistDao;
import org.artgasmator.data.dao.ArtistLabelDao;
import org.artgasmator.model.Artist;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;

public class ArtistDaoImpl extends AbstractDao implements ArtistDao {

	private SimpleJdbcInsert createArtist;
	private ArtistLabelDao artistLabelDao;
	
	public ArtistLabelDao getArtistLabelDao() {
		return artistLabelDao;
	}

	public void setArtistLabelDao(ArtistLabelDao artistLabelDao) {
		this.artistLabelDao = artistLabelDao;
	}

	public void init() {
		this.createArtist = 
			new SimpleJdbcInsert(dataSource)
					.withTableName("artist")
					.usingColumns("artist_name", "artist_origin", "artist_years_active")
					.usingGeneratedKeyColumns("artist_id");
	}	
	
	public Integer createArtist(Artist artist) {
		
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("artist_origin", artist.getArtistOrigin())
			.addValue("artist_name", artist.getArtistName())
			.addValue("artist_years_active", artist.getArtistYearsActive());
		
		Integer createdArtistId = createArtist.executeAndReturnKey(params).intValue();
		
		artistLabelDao.createArtistLabels(createdArtistId, artist.getArtistLabels());
		
		return createdArtistId;
		
	}

}
