#84 Work on cake baking
This commit is contained in:
parent
8f5b1b0ed3
commit
913b4fa30c
@ -14,7 +14,7 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-jpa</artifactId>
|
||||
</dependency>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-entitymanager</artifactId>
|
||||
|
@ -1,16 +1,27 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
CakeBakingService service = new CakeBakingServiceImpl();
|
||||
service.saveNewLayer(new CakeLayerInfo("foo", 1));
|
||||
service.saveNewLayer(new CakeLayerInfo("bar", 2));
|
||||
service.saveNewLayer(new CakeLayerInfo("chocolate", 1200));
|
||||
service.saveNewLayer(new CakeLayerInfo("banana", 900));
|
||||
service.saveNewLayer(new CakeLayerInfo("strawberry", 950));
|
||||
service.getAllLayers().stream().forEach((layer) -> System.out.println(layer));
|
||||
|
||||
service.saveNewTopping(new CakeToppingInfo("hoi", 11));
|
||||
service.saveNewTopping(new CakeToppingInfo("candies", 350));
|
||||
service.getAllToppings().stream().forEach((topping) -> System.out.println(topping));
|
||||
|
||||
CakeInfo cakeInfo = new CakeInfo(new CakeToppingInfo("candies", 0),
|
||||
Arrays.asList(new CakeLayerInfo("chocolate", 0), new CakeLayerInfo("chocolate", 0),
|
||||
new CakeLayerInfo("chocolate", 0)));
|
||||
try {
|
||||
service.bakeNewCake(cakeInfo);
|
||||
} catch (CakeBakingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package com.iluwatar.layers;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
@ -16,10 +17,10 @@ public class Cake {
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@OneToOne
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
private CakeTopping topping;
|
||||
|
||||
@OneToMany
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
private List<CakeLayer> layers;
|
||||
|
||||
public Cake() {
|
||||
@ -49,4 +50,8 @@ public class Cake {
|
||||
public void setLayers(List<CakeLayer> layers) {
|
||||
this.layers = layers;
|
||||
}
|
||||
|
||||
public void addLayer(CakeLayer layer) {
|
||||
this.layers.add(layer);
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,11 @@ import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.context.support.AbstractApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class CakeBakingServiceImpl implements CakeBakingService {
|
||||
|
||||
private AbstractApplicationContext context;
|
||||
@ -22,23 +26,31 @@ public class CakeBakingServiceImpl implements CakeBakingService {
|
||||
List<CakeToppingInfo> allToppings = getAllToppings();
|
||||
List<CakeToppingInfo> matchingToppings = allToppings.stream()
|
||||
.filter((t) -> t.name.equals(cakeInfo.cakeToppingInfo.name)).collect(Collectors.toList());
|
||||
if (!matchingToppings.isEmpty()) {
|
||||
// CakeToppingDao toppingBean = context.getBean(CakeToppingDao.class);
|
||||
// toppingBean.delete(matchingToppings.iterator().next().id.get());
|
||||
} else {
|
||||
if (matchingToppings.isEmpty()) {
|
||||
throw new CakeBakingException(String.format("Topping %s is not available", cakeInfo.cakeToppingInfo.name));
|
||||
}
|
||||
List<CakeLayerInfo> allLayers = getAllLayers();
|
||||
List<CakeLayer> allLayers = getAllLayerEntities();
|
||||
List<CakeLayer> foundLayers = new ArrayList<>();
|
||||
for (CakeLayerInfo info: cakeInfo.cakeLayerInfos) {
|
||||
Optional<CakeLayerInfo> found = allLayers.stream().filter((layer) -> layer.name.equals(info.name)).findFirst();
|
||||
if (found.isPresent()) {
|
||||
// CakeLayerDao layerBean = context.getBean(CakeLayerDao.class);
|
||||
// layerBean.delete(found.get().id.get());
|
||||
} else {
|
||||
Optional<CakeLayer> found = allLayers.stream().filter((layer) -> layer.getName().equals(info.name)).findFirst();
|
||||
if (!found.isPresent()) {
|
||||
throw new CakeBakingException(String.format("Layer %s is not available", info.name));
|
||||
} else {
|
||||
foundLayers.add(found.get());
|
||||
}
|
||||
}
|
||||
CakeToppingDao toppingBean = context.getBean(CakeToppingDao.class);
|
||||
CakeTopping topping = toppingBean.findOne(matchingToppings.iterator().next().id.get());
|
||||
CakeDao cakeBean = context.getBean(CakeDao.class);
|
||||
Cake cake = new Cake();
|
||||
cake = cakeBean.save(cake);
|
||||
cake.setTopping(topping);
|
||||
topping.setCake(cake);
|
||||
cake.setLayers(foundLayers);
|
||||
for (CakeLayer layer: foundLayers) {
|
||||
layer.setCake(cake);
|
||||
}
|
||||
cakeBean.save(cake);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -53,6 +65,16 @@ public class CakeBakingServiceImpl implements CakeBakingService {
|
||||
bean.save(new CakeLayer(layerInfo.name, layerInfo.calories));
|
||||
}
|
||||
|
||||
private List<CakeTopping> getAllToppingEntities() {
|
||||
CakeToppingDao bean = context.getBean(CakeToppingDao.class);
|
||||
List<CakeTopping> result = new ArrayList<>();
|
||||
Iterator<CakeTopping> iterator = bean.findAll().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
result.add(iterator.next());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CakeToppingInfo> getAllToppings() {
|
||||
CakeToppingDao bean = context.getBean(CakeToppingDao.class);
|
||||
@ -65,6 +87,16 @@ public class CakeBakingServiceImpl implements CakeBakingService {
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<CakeLayer> getAllLayerEntities() {
|
||||
CakeLayerDao bean = context.getBean(CakeLayerDao.class);
|
||||
List<CakeLayer> result = new ArrayList<>();
|
||||
Iterator<CakeLayer> iterator = bean.findAll().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
result.add(iterator.next());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CakeLayerInfo> getAllLayers() {
|
||||
CakeLayerDao bean = context.getBean(CakeLayerDao.class);
|
||||
|
@ -1,8 +1,10 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class CakeLayer {
|
||||
@ -14,6 +16,9 @@ public class CakeLayer {
|
||||
private String name;
|
||||
|
||||
private int calories;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.ALL)
|
||||
private Cake cake;
|
||||
|
||||
public CakeLayer() {
|
||||
}
|
||||
@ -51,4 +56,12 @@ public class CakeLayer {
|
||||
public String toString() {
|
||||
return String.format("name: %s calories: %d", name, calories);
|
||||
}
|
||||
|
||||
public Cake getCake() {
|
||||
return cake;
|
||||
}
|
||||
|
||||
public void setCake(Cake cake) {
|
||||
this.cake = cake;
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +22,6 @@ public class CakeLayerInfo {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("name: %s calories: %d", name, calories);
|
||||
return String.format("CakeLayerInfo name: %s calories: %d", name, calories);
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,10 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
@Entity
|
||||
public class CakeTopping {
|
||||
@ -15,6 +17,9 @@ public class CakeTopping {
|
||||
|
||||
private int calories;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
private Cake cake;
|
||||
|
||||
public CakeTopping() {
|
||||
}
|
||||
|
||||
@ -51,4 +56,12 @@ public class CakeTopping {
|
||||
public String toString() {
|
||||
return String.format("name: %s calories: %d", name, calories);
|
||||
}
|
||||
|
||||
public Cake getCake() {
|
||||
return cake;
|
||||
}
|
||||
|
||||
public void setCake(Cake cake) {
|
||||
this.cake = cake;
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +22,6 @@ public class CakeToppingInfo {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("name: %s calories: %d", name, calories);
|
||||
return String.format("CakeToppingInfo name: %s calories: %d", name, calories);
|
||||
}
|
||||
}
|
||||
|
@ -6,10 +6,12 @@
|
||||
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
|
||||
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd">
|
||||
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
|
||||
|
||||
<jpa:repositories base-package="com.iluwatar" />
|
||||
|
||||
<tx:annotation-driven transaction-manager="transactionManager" />
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
|
||||
<property name="entityManagerFactory" ref="entityManagerFactory" />
|
||||
</bean>
|
||||
@ -33,6 +35,7 @@
|
||||
<map>
|
||||
<entry key="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
|
||||
<entry key="hibernate.hbm2ddl.auto" value="create-drop" />
|
||||
<entry key="hibernate.show_sql" value="true" />
|
||||
</map>
|
||||
</property>
|
||||
</bean>
|
||||
|
Loading…
x
Reference in New Issue
Block a user