Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package cz.muni.fi.dndtroops.service; import cz.muni.fi.dndtroopsapi.exceptions.DnDTroopServiceException; import cz.muni.fi.dndtroops.dao.HeroDao; import cz.muni.fi.dndtroops.entity.Hero; import cz.muni.fi.dndtroops.entity.Troop; import cz.muni.fi.dndtroops.enums.RoleName; import cz.muni.fi.dndtroops.exception.ConnectionException; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.inject.Inject; import org.springframework.stereotype.Service; /** * * @author Karel Auf */ @Service public class HeroServiceImpl implements HeroService { final static Logger log = LoggerFactory.getLogger(HeroServiceImpl.class); @Inject private HeroDao heroDao; @Override public Hero createHero(Hero h) { log.debug("createHero({})", h); try { heroDao.createHero(h); return h; } catch (Exception ex) { throw new DnDTroopServiceException("Invalid parameters", ex); } } @Override public Hero findHeroById(Long id) { log.debug("findHeroById({})", id); try { return heroDao.findById(id); } catch (ConnectionException ex) { throw new DnDTroopServiceException("Invalid id", ex); } } @Override public Hero findHeroByName(String heroName) { log.debug("findHeroByName({})", heroName); try { return heroDao.findByName(heroName); } catch (Exception ex) { throw new DnDTroopServiceException("Invalid name", ex); } } @Override public List<Hero> findAllHeroes() { log.debug("findAllHeroes({})"); try { return heroDao.findAll(); } catch (ConnectionException ex) { throw new DnDTroopServiceException("Connection problem", ex); } } @Override public void addTroop(Hero hero, Troop troop) { log.debug("addTroop({})", hero, troop); if (hero.getTroop() != null) { throw new DnDTroopServiceException("Aready in troop"); } if (troop.getId() == null) { throw new DnDTroopServiceException("Troop not in DB"); } try { hero.setTroop(troop); List<Hero> list = troop.getMembers(); list.add(hero); troop.setMembers(list); } catch (Exception ex) { throw new DnDTroopServiceException("addTroop problem", ex); } } @Override public void removeTroop(Hero hero) { log.debug("removeTroop({})", hero); if (hero.getTroop() == null) { throw new DnDTroopServiceException("Hero not in troop."); } try { Troop t = hero.getTroop(); List<Hero> list = t.getMembers(); list.remove(hero); t.setMembers(list); hero.setTroop(null); } catch (Exception ex) { throw new DnDTroopServiceException("removeTroop problem", ex); } } @Override public void changeLevel(Hero hero, int lvl) { log.debug("changeLevel({})", hero, lvl); try { hero.setLevel(lvl); } catch (Exception ex) { throw new DnDTroopServiceException("Hero doesn't exist", ex); } } @Override public void changeRole(Hero hero, RoleName role) { log.debug("changeRole({})", hero, role); try { hero.setRole(role); } catch (Exception ex) { throw new DnDTroopServiceException("Hero doesn't exist", ex); } } @Override public void deleteHero(Hero h) { log.debug("deleteHero({})", h); try { heroDao.deleteHero(h); } catch (Exception ex) { throw new DnDTroopServiceException("Hero doesn't exist", ex); } } }