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.TroopDao; import cz.muni.fi.dndtroops.entity.Hero; import cz.muni.fi.dndtroops.entity.Troop; import java.math.BigDecimal; import java.util.List; import javax.inject.Inject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; /** * * @author Karel Auf */ @Service public class TroopServiceImpl implements TroopService { final static Logger log = LoggerFactory.getLogger(TroopServiceImpl.class); @Inject private TroopDao troopDao; @Override public Troop createTroop(Troop t) { log.debug("createTroop({})", t); try { troopDao.createTroop(t); return t; } catch (Exception e) { throw new DnDTroopServiceException("Troop creation problem", e); } } @Override public Troop findTroopById(Long id) { log.debug("findTroopById({})", id); try { return troopDao.findTroopById(id); } catch (Exception e) { throw new DnDTroopServiceException("findTroopById problem", e); } } @Override public Troop findTroopByName(String troopName) { log.debug("findTroopByName({})", troopName); try { return troopDao.findTroopByName(troopName); } catch (Exception e) { throw new DnDTroopServiceException("findTroopByName problem", e); } } @Override public List<Troop> findAllTroops() { log.debug("findAllTroops({})"); try { return troopDao.findAllTroops(); } catch (Exception e) { throw new DnDTroopServiceException("findAllTroops problem", e); } } @Override public void addHeroToTroop(Troop troop, Hero hero) { log.debug("addHeroToTroop({})", troop, hero); if (hero.getTroop() != null) { throw new DnDTroopServiceException("troop for Hero already set"); } if (troop.getMembers().contains(hero) == true) { throw new DnDTroopServiceException("hero is already in this troop"); } try { List<Hero> list = troop.getMembers(); list.add(hero); troop.setMembers(list); hero.setTroop(troop); } catch (Exception e) { throw new DnDTroopServiceException("addHeroToTroop problem", e); } } @Override public void removeHeroFromTroop(Troop troop, Hero hero) { log.debug("removeHeroToTroop({})", troop, hero); if (hero.getTroop() == null) { throw new DnDTroopServiceException("hero is not in troop"); } if (hero.getTroop() != troop) { throw new DnDTroopServiceException("hero is in different troop"); } if (troop.getMembers().contains(hero) == false) { throw new DnDTroopServiceException("troop doesn't contain this hero"); } try { List<Hero> list = troop.getMembers(); list.remove(hero); troop.setMembers(list); hero.setTroop(null); } catch (Exception e) { throw new DnDTroopServiceException("removeHeroFromTroop problem", e); } } @Override public void changeMoney(Troop troop, BigDecimal newMoney) { log.debug("changeMoney({})", troop, newMoney); try { troop.setMoney(newMoney); } catch (Exception e) { throw new DnDTroopServiceException("changeMoney problem", e); } } @Override public void changeMission(Troop troop, String mission) { log.debug("changeMission({})", troop, mission); try { troop.setMission(mission); } catch (Exception e) { throw new DnDTroopServiceException("changeMission problem", e); } } @Override public void deleteTroop(Troop t) { log.debug("deleteTroop({})", t); try { troopDao.deleteTroop(t); } catch (Exception e) { throw new DnDTroopServiceException("changeMission problem", e); } } }