Commit 9c0d44fc authored by xkorenk's avatar xkorenk
Browse files

milestone 2 service

parent 715e2c1b
Pipeline #131545 passed with stages
in 2 minutes and 5 seconds
package cz.muni.fi.pa165.winery.dto.wine;
import cz.muni.fi.pa165.winery.dto.PersistentDtoBase;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
// TODO
import javax.validation.constraints.NotBlank;
/**
* @author Michaela Korenkova
*/
@Getter
@Setter
@EqualsAndHashCode
public class GrapeDto extends PersistentDtoBase {
/**
* name of the grape
*/
@NotBlank
private String name;
}
package cz.muni.fi.pa165.winery.dto.wine;
import cz.muni.fi.pa165.winery.dto.PersistentDtoBase;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import javax.validation.constraints.PositiveOrZero;
import java.math.BigDecimal;
/**
* @author Michaela Korenkova
*/
@Getter
@Setter
@EqualsAndHashCode
public class HarvestDto extends PersistentDtoBase {
/**
* year of the harvest
*/
@NotNull
@Positive
private int harvestYear;
/**
* quality of wine
*/
@NotNull
@PositiveOrZero
private short grade;
/**
* quality of grape
*/
@NotNull
@PositiveOrZero
private BigDecimal yieldKilograms;
/**
* ID of harvested grape
*/
private long grapeId;
}
package cz.muni.fi.pa165.winery.services.wine;
import cz.muni.fi.pa165.winery.dto.wine.GrapeDto;
import cz.muni.fi.pa165.winery.services.PersistenceService;
/**
* @author Michaela Korenkova
*/
public interface GrapeService extends PersistenceService<GrapeDto> {
}
package cz.muni.fi.pa165.winery.services.wine;
import cz.muni.fi.pa165.winery.dto.wine.HarvestDto;
import cz.muni.fi.pa165.winery.services.PersistenceService;
import java.util.List;
/**
* @author Michaela Korenkova
*/
public interface HarvestService extends PersistenceService<HarvestDto> {
/**
* returns all harvests between given years
* @param fromYear from year
* @param toYear to year
* @return all harvests between these years
*/
List<HarvestDto> getAllWithinYears(int fromYear, int toYear);
}
......@@ -3,8 +3,12 @@ package cz.muni.fi.pa165.winery.persistence.dao.wine;
import cz.muni.fi.pa165.winery.persistence.dao.DaoBase;
import cz.muni.fi.pa165.winery.persistence.entities.Harvest;
import java.util.List;
/**
* @author Jakub Balga
*/
public interface HarvestDao extends DaoBase<Harvest> {
List<Harvest> getAllWithinYears(int fromYear, int toYear);
}
package cz.muni.fi.pa165.winery.persistence.dao.wine;
import cz.muni.fi.pa165.winery.exceptions.WineryDataAccessException;
import cz.muni.fi.pa165.winery.persistence.dao.DaoBaseImpl;
import cz.muni.fi.pa165.winery.persistence.entities.Harvest;
import cz.muni.fi.pa165.winery.persistence.entities.WineBottle;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author Jakub Balga
*/
......@@ -13,4 +17,16 @@ public class HarvestDaoImpl extends DaoBaseImpl<Harvest> implements HarvestDao {
public HarvestDaoImpl(){
super(Harvest.class);
}
@Override
public List<Harvest> getAllWithinYears(int fromYear, int toYear) {
try{
return entityManager.createQuery("select entity from Harvest entity "
+ "where entity.harvestYear >= " + fromYear + " and " +
"entity.harvestYear <= " + toYear, Harvest.class)
.getResultList();
} catch (Exception ex) {
throw new WineryDataAccessException(ex.getMessage(), ex);
}
}
}
package cz.muni.fi.pa165.winery.services.mappers.wine;
import cz.muni.fi.pa165.winery.dto.wine.GrapeDto;
import cz.muni.fi.pa165.winery.persistence.entities.Grape;
import cz.muni.fi.pa165.winery.services.mappers.EntityMapperImpl;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.stereotype.Service;
/**
* @author Michaela Korenkova
*/
@Service
public class GrapeEntityMapper extends EntityMapperImpl<GrapeDto, Grape> {
public GrapeEntityMapper() {
super(GrapeDto.class, Grape.class);
}
@Override
protected void configureModelMapper(ModelMapper mapper) {
super.configureModelMapper(mapper);
mapper.getConfiguration().setAmbiguityIgnored(true);
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
mapper.createTypeMap(Grape.class, GrapeDto.class);
}
}
package cz.muni.fi.pa165.winery.services.mappers.wine;
import cz.muni.fi.pa165.winery.dto.wine.HarvestDto;
import cz.muni.fi.pa165.winery.persistence.entities.Harvest;
import cz.muni.fi.pa165.winery.services.mappers.EntityMapperImpl;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.stereotype.Service;
/**
* @author Michaela Korenkova
*/
@Service
public class HarvestEntityMapper extends EntityMapperImpl<HarvestDto, Harvest> {
public HarvestEntityMapper() {
super(HarvestDto.class, Harvest.class);
}
@Override
protected void configureModelMapper(ModelMapper mapper) {
super.configureModelMapper(mapper);
mapper.getConfiguration().setAmbiguityIgnored(true);
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
mapper.createTypeMap(Harvest.class, HarvestDto.class)
.addMappings(m -> m.map(entity -> entity.getGrape().getId(), HarvestDto::setGrapeId));
}
}
\ No newline at end of file
package cz.muni.fi.pa165.winery.services.wine;
import cz.muni.fi.pa165.winery.dto.wine.GrapeDto;
import cz.muni.fi.pa165.winery.persistence.dao.wine.GrapeDao;
import cz.muni.fi.pa165.winery.persistence.entities.Grape;
import cz.muni.fi.pa165.winery.services.PersistenceServiceImpl;
import cz.muni.fi.pa165.winery.services.mappers.EntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author Michaela Korenkova
*/
@Service
public class GrapeServiceImpl extends PersistenceServiceImpl<GrapeDto, Grape> implements GrapeService{
@Autowired
public GrapeServiceImpl(GrapeDao grapeDao, EntityMapper<GrapeDto, Grape> mapper) {
super(GrapeDto.class, Grape.class, grapeDao, mapper);
}
}
package cz.muni.fi.pa165.winery.services.wine;
import cz.muni.fi.pa165.winery.dto.wine.HarvestDto;
import cz.muni.fi.pa165.winery.persistence.dao.wine.HarvestDao;
import cz.muni.fi.pa165.winery.persistence.entities.Harvest;
import cz.muni.fi.pa165.winery.services.PersistenceServiceImpl;
import cz.muni.fi.pa165.winery.services.mappers.EntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Michaela Korenkova
*/
@Service
public class HarvestServiceImpl extends PersistenceServiceImpl<HarvestDto, Harvest> implements HarvestService{
@Autowired
public HarvestServiceImpl(HarvestDao harvestDao, EntityMapper<HarvestDto, Harvest> mapper) {
super(HarvestDto.class, Harvest.class, harvestDao, mapper);
}
@Override
public List<HarvestDto> getAllWithinYears(int fromYear, int toYear) {
var entities = ((HarvestDao)getDao()).getAllWithinYears(fromYear, toYear);
return entities.stream().map(this::mapToDto).collect(Collectors.toList());
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment