Wrote OfferWriter and following classes to save offers to persistence
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package de.rwu.easydrop.service.mapping;
|
||||
|
||||
import de.rwu.easydrop.api.dto.OfferDTO;
|
||||
|
||||
import de.rwu.easydrop.model.Offer;
|
||||
|
||||
/**
|
||||
* Maps between Offer, OfferDTO and OfferDTO.
|
||||
*
|
||||
* @since 0.2.0
|
||||
*
|
||||
* @see Offer
|
||||
* @see OfferDTO
|
||||
* @see OfferDTO
|
||||
*/
|
||||
public final class OfferMapper {
|
||||
|
||||
/**
|
||||
* Private constructor to prevent unwanted instantiation.
|
||||
*
|
||||
* @throws UnsupportedOperationException always
|
||||
*/
|
||||
private OfferMapper() throws UnsupportedOperationException {
|
||||
throw new UnsupportedOperationException("This is a mapping class, don't instantiate it.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Offer object from a corresponding DTO.
|
||||
*
|
||||
* @param dto Offer Data Transfer Object
|
||||
* @return Offer
|
||||
*/
|
||||
public static Offer mapOfferFromDTO(final OfferDTO dto) {
|
||||
Offer offer = new Offer();
|
||||
|
||||
return offer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an OfferDTO object from a corresponding offer.
|
||||
*
|
||||
* @param Offer offer
|
||||
* @return OfferDTO
|
||||
*/
|
||||
public static OfferDTO mapOfferToDTO(final Offer offer) {
|
||||
OfferDTO dto = new OfferDTO(offer.getOfferId());
|
||||
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package de.rwu.easydrop.service.validation;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import de.rwu.easydrop.exception.InvalidProductException;
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.model.Offer;
|
||||
|
||||
/**
|
||||
* Confirms validity of Offer data.
|
||||
*
|
||||
* @since 0.2.0
|
||||
*/
|
||||
public class OfferValidator {
|
||||
/**
|
||||
* Private constructor to prevent unwanted instantiation.
|
||||
*
|
||||
* @throws UnsupportedOperationException always
|
||||
*/
|
||||
private OfferValidator() throws UnsupportedOperationException {
|
||||
throw new UnsupportedOperationException("This is a validator class, don't instantiate it.");
|
||||
}
|
||||
/**
|
||||
* Makes sure an Offer does not contain invalid information.
|
||||
*
|
||||
* @param offer the Offer
|
||||
*/
|
||||
public static void validate(final Offer offer) {}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package de.rwu.easydrop.service.writer;
|
||||
|
||||
import de.rwu.easydrop.api.dto.OfferDTO;
|
||||
import de.rwu.easydrop.data.connector.AbstractOfferPersistence;
|
||||
import de.rwu.easydrop.model.Offer;
|
||||
import de.rwu.easydrop.service.mapping.OfferMapper;
|
||||
import de.rwu.easydrop.service.validation.OfferValidator;
|
||||
|
||||
public class OfferWriter {
|
||||
/**
|
||||
* Persistence.
|
||||
*/
|
||||
private AbstractOfferPersistence persistence;
|
||||
|
||||
/**
|
||||
* @param newPersistence the persistence to set
|
||||
*/
|
||||
public void setPersistence(final AbstractOfferPersistence newPersistence) {
|
||||
this.persistence = newPersistence;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates and saves product to persistence.
|
||||
*
|
||||
* @param product
|
||||
*/
|
||||
public void writeOfferToPersistence(final Offer offer) {
|
||||
OfferValidator.validate(offer);
|
||||
OfferDTO dto = OfferMapper.mapOfferToDTO(offer);
|
||||
|
||||
persistence.saveOffer(dto);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user