Java tutorial
/* * LICENSE * * "THE BEER-WARE LICENSE" (Revision 43): * "Sven Strittmatter" <weltraumschaf@googlemail.com> wrote this file. * As long as you retain this notice you can do whatever you want with * this stuff. If we meet some day, and you think this stuff is worth it, * you can buy me a non alcohol-free beer in return. * * Copyright (C) 2012 "Sven Strittmatter" <weltraumschaf@googlemail.com> */ package de.weltraumschaf.maconha.server.service; import de.weltraumschaf.commons.guava.Lists; import de.weltraumschaf.maconha.server.bean.NullAwareBeanUtilsBean; import de.weltraumschaf.maconha.server.dao.SongDao; import de.weltraumschaf.maconha.server.model.Song; import de.weltraumschaf.maconha.server.model.SongEntity; import java.util.List; import javax.ws.rs.core.Response; import de.weltraumschaf.maconha.server.errorhandling.AppException; import de.weltraumschaf.maconha.server.filters.AppConstants; import java.lang.reflect.InvocationTargetException; import org.apache.commons.beanutils.BeanUtilsBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; /** * * @author Sven Strittmatter <weltraumschaf@googlemail.com> */ public final class SongServiceDbAccess implements SongService { @Autowired SongDao songDao; @Override @Transactional public Long createSong(final Song song) throws AppException { validateInputForCreation(song); return songDao.createSong(new SongEntity(song)); } private void validateInputForCreation(final Song song) throws AppException { if (song.getTitle() == null) { throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(), 400, "Provided data not sufficient for insertion", "Please verify that the feed is properly generated/set", AppConstants.BLOG_POST_URL); } //etc... } @Override @Transactional public void createSongs(final List<Song> songs) throws AppException { for (Song podcast : songs) { createSong(podcast); } } @Override public List<Song> getSongs() throws AppException { final List<SongEntity> songs = songDao.getSongs(); return getPodcastsFromEntities(songs); } private List<Song> getPodcastsFromEntities(final List<SongEntity> podcastEntities) { final List<Song> response = Lists.newArrayList(); for (final SongEntity podcastEntity : podcastEntities) { response.add(new Song(podcastEntity)); } return response; } @Override public Song getSongById(final Long id) throws AppException { final SongEntity podcastById = songDao.getSongById(id); if (podcastById == null) { throw new AppException(Response.Status.NOT_FOUND.getStatusCode(), 404, "The song you requested with id " + id + " was not found in the database", "Verify the existence of the song with the id " + id + " in the database", AppConstants.BLOG_POST_URL); } return new Song(songDao.getSongById(id)); } @Override public void updateFullySong(final Song song) throws AppException { //do a validation to verify FULL update with PUT if (isFullUpdate(song)) { throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(), 400, "Please specify all properties for Full UPDATE", "required properties - id, title, feed, lnkOnPodcastpedia, description", AppConstants.BLOG_POST_URL); } final Song verifyPodcastExistenceById = verifySongExistenceById(song.getId()); if (verifyPodcastExistenceById == null) { throw new AppException(Response.Status.NOT_FOUND.getStatusCode(), 404, "The resource you are trying to update does not exist in the database", "Please verify existence of data in the database for the id - " + song.getId(), AppConstants.BLOG_POST_URL); } songDao.updateSong(new SongEntity(song)); } /** * Verifies the "completeness" of podcast resource sent over the wire * * @param song * @return */ private boolean isFullUpdate(Song song) { return song.getId() == null || song.getComment() == null || song.getGenre() == 0 || song.getTitle() == null || song.getTrack() == 0 || song.getYear() == 0; } @Override public Song verifySongExistenceById(final Long id) { SongEntity songById = songDao.getSongById(id); if (songById == null) { return null; } else { return new Song(songById); } } @Override public void updatePartiallySong(final Song song) throws AppException { //do a validation to verify existence of the resource final Song verifyPodcastExistenceById = verifySongExistenceById(song.getId()); if (verifyPodcastExistenceById == null) { throw new AppException(Response.Status.NOT_FOUND.getStatusCode(), 404, "The resource you are trying to update does not exist in the database", "Please verify existence of data in the database for the id - " + song.getId(), AppConstants.BLOG_POST_URL); } copyPartialProperties(verifyPodcastExistenceById, song); songDao.updateSong(new SongEntity(verifyPodcastExistenceById)); } private void copyPartialProperties(final Song verifyPodcastExistenceById, final Song podcast) { final BeanUtilsBean notNull = new NullAwareBeanUtilsBean(); try { notNull.copyProperties(verifyPodcastExistenceById, podcast); } catch (final IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (final InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void deleteSongById(final Long id) { songDao.deleteSongById(id); } @Override public void deletePodcasts() { songDao.deletePodcasts(); } }