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.dao; import cz.muni.fi.dndtroops.entity.Troop; import java.math.BigDecimal; import java.util.List; import javax.inject.Named; import javax.persistence.EntityManager; import javax.persistence.NoResultException; import javax.persistence.PersistenceContext; import javax.persistence.TypedQuery; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Repository; /** * * @author Karel Auf */ @Repository public class TroopDaoImpl implements TroopDao { final static Logger log = LoggerFactory.getLogger(TroopDaoImpl.class); @PersistenceContext private EntityManager em; @Override public void createTroop(Troop troop) throws IllegalArgumentException { log.debug("createTroop({})", troop); validate(troop); if (troop.getId() != null) { throw new IllegalArgumentException("troop id is already set"); } em.persist(troop); } @Override public void deleteTroop(Troop troop) throws IllegalArgumentException { log.debug("deleteTroop({})", troop); if (troop == null) { throw new IllegalArgumentException("troop pointer is null"); } em.remove(troop); //throws IllegalArgumentException if troop is not present or is not an entity } @Override public List<Troop> findAllTroops() { log.debug("findAllTroops({})"); return em.createQuery("select t from Troop t", Troop.class).getResultList(); } @Override public Troop findTroopById(Long id) throws IllegalArgumentException { log.debug("findTroopById({})", id); if (id == null) { throw new IllegalArgumentException("id is null"); } return em.find(Troop.class, id); } @Override public Troop findTroopByName(String name) throws IllegalArgumentException { log.debug("findTroopByName({})", name); if (name == null) { throw new IllegalArgumentException("name is null"); } if (name.length() == 0) { throw new IllegalArgumentException("name is empty string"); } try { TypedQuery<Troop> query = em.createQuery("SELECT t FROM Troop t WHERE t.name = :name", Troop.class); return query.setParameter("name", name).getSingleResult(); } catch (NoResultException nre) { return null; } } @Override public void updateGoldForTroop(Long id, BigDecimal gold) throws IllegalArgumentException { log.debug("updateRewardForMission({})", id, gold); Troop t1 = findTroopById(id); //checks if mission is in DB if (t1 == null) { throw new IllegalArgumentException("troop is not in DB"); } t1.setMoney(gold); } @Override public void assignMissionToTroop(Long troopId, String mission) throws IllegalArgumentException { log.debug("assignMissionToTroop({})", troopId, mission); Troop t1 = findTroopById(troopId); //checks if mission is in DB if (t1 == null) { throw new IllegalArgumentException("troop is not in DB"); } t1.setMission(mission); } @Override public void assignMembersToTroop(Long troopId, List members) throws IllegalArgumentException { log.debug("assignMembersToTroop({})", troopId, members); Troop t1 = findTroopById(troopId); //checks if mission is in DB if (t1 == null) { throw new IllegalArgumentException("troop is not in DB"); } t1.setMembers(members); } private static void validate(Troop troop) { if (troop == null) { throw new IllegalArgumentException("troop pointer is null"); } if (troop.getName() == null) { throw new IllegalArgumentException("troop name is null"); } } }