#84 Removed Layers stuff accidentally pushed to master
This commit is contained in:
parent
913b4fa30c
commit
ea524ee2c0
@ -1,36 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<project
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.5.0</version>
|
||||
</parent>
|
||||
<groupId>com.iluwatar.layers</groupId>
|
||||
<artifactId>layers</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-entitymanager</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-dbcp</groupId>
|
||||
<artifactId>commons-dbcp</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -1,27 +0,0 @@
|
||||
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("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("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();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
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;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
@Entity
|
||||
public class Cake {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
private CakeTopping topping;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
private List<CakeLayer> layers;
|
||||
|
||||
public Cake() {
|
||||
setLayers(new ArrayList<>());
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public CakeTopping getTopping() {
|
||||
return topping;
|
||||
}
|
||||
|
||||
public void setTopping(CakeTopping topping) {
|
||||
this.topping = topping;
|
||||
}
|
||||
|
||||
public List<CakeLayer> getLayers() {
|
||||
return layers;
|
||||
}
|
||||
|
||||
public void setLayers(List<CakeLayer> layers) {
|
||||
this.layers = layers;
|
||||
}
|
||||
|
||||
public void addLayer(CakeLayer layer) {
|
||||
this.layers.add(layer);
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
public class CakeBakingException extends Exception {
|
||||
|
||||
public CakeBakingException() {
|
||||
}
|
||||
|
||||
public CakeBakingException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CakeBakingService {
|
||||
|
||||
void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException;
|
||||
|
||||
void saveNewTopping(CakeToppingInfo toppingInfo);
|
||||
|
||||
List<CakeToppingInfo> getAllToppings();
|
||||
|
||||
void saveNewLayer(CakeLayerInfo layerInfo);
|
||||
|
||||
List<CakeLayerInfo> getAllLayers();
|
||||
}
|
@ -1,111 +0,0 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
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;
|
||||
|
||||
public CakeBakingServiceImpl() {
|
||||
this.context = new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException {
|
||||
List<CakeToppingInfo> allToppings = getAllToppings();
|
||||
List<CakeToppingInfo> matchingToppings = allToppings.stream()
|
||||
.filter((t) -> t.name.equals(cakeInfo.cakeToppingInfo.name)).collect(Collectors.toList());
|
||||
if (matchingToppings.isEmpty()) {
|
||||
throw new CakeBakingException(String.format("Topping %s is not available", cakeInfo.cakeToppingInfo.name));
|
||||
}
|
||||
List<CakeLayer> allLayers = getAllLayerEntities();
|
||||
List<CakeLayer> foundLayers = new ArrayList<>();
|
||||
for (CakeLayerInfo info: cakeInfo.cakeLayerInfos) {
|
||||
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
|
||||
public void saveNewTopping(CakeToppingInfo toppingInfo) {
|
||||
CakeToppingDao bean = context.getBean(CakeToppingDao.class);
|
||||
bean.save(new CakeTopping(toppingInfo.name, toppingInfo.calories));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveNewLayer(CakeLayerInfo layerInfo) {
|
||||
CakeLayerDao bean = context.getBean(CakeLayerDao.class);
|
||||
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);
|
||||
List<CakeToppingInfo> result = new ArrayList<>();
|
||||
Iterator<CakeTopping> iterator = bean.findAll().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
CakeTopping next = iterator.next();
|
||||
result.add(new CakeToppingInfo(next.getId(), next.getName(), next.getCalories()));
|
||||
}
|
||||
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);
|
||||
List<CakeLayerInfo> result = new ArrayList<>();
|
||||
Iterator<CakeLayer> iterator = bean.findAll().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
CakeLayer next = iterator.next();
|
||||
result.add(new CakeLayerInfo(next.getId(), next.getName(), next.getCalories()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface CakeDao extends CrudRepository<Cake, Long> {
|
||||
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class CakeInfo {
|
||||
|
||||
public final Optional<Long> id;
|
||||
public final CakeToppingInfo cakeToppingInfo;
|
||||
public final List<CakeLayerInfo> cakeLayerInfos;
|
||||
|
||||
public CakeInfo(Long id, CakeToppingInfo cakeToppingInfo, List<CakeLayerInfo> cakeLayerInfos) {
|
||||
this.id = Optional.of(id);
|
||||
this.cakeToppingInfo = cakeToppingInfo;
|
||||
this.cakeLayerInfos = cakeLayerInfos;
|
||||
}
|
||||
|
||||
public CakeInfo(CakeToppingInfo cakeToppingInfo, List<CakeLayerInfo> cakeLayerInfos) {
|
||||
this.id = Optional.empty();
|
||||
this.cakeToppingInfo = cakeToppingInfo;
|
||||
this.cakeLayerInfos = cakeLayerInfos;
|
||||
}
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
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 {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
private int calories;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.ALL)
|
||||
private Cake cake;
|
||||
|
||||
public CakeLayer() {
|
||||
}
|
||||
|
||||
public CakeLayer(String name, int calories) {
|
||||
this.setName(name);
|
||||
this.setCalories(calories);
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getCalories() {
|
||||
return calories;
|
||||
}
|
||||
|
||||
public void setCalories(int calories) {
|
||||
this.calories = calories;
|
||||
}
|
||||
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface CakeLayerDao extends CrudRepository<CakeLayer, Long> {
|
||||
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class CakeLayerInfo {
|
||||
|
||||
public final Optional<Long> id;
|
||||
public final String name;
|
||||
public final int calories;
|
||||
|
||||
public CakeLayerInfo(Long id, String name, int calories) {
|
||||
this.id = Optional.of(id);
|
||||
this.name = name;
|
||||
this.calories = calories;
|
||||
}
|
||||
|
||||
public CakeLayerInfo(String name, int calories) {
|
||||
this.id = Optional.empty();
|
||||
this.name = name;
|
||||
this.calories = calories;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("CakeLayerInfo name: %s calories: %d", name, calories);
|
||||
}
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
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 {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
private int calories;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
private Cake cake;
|
||||
|
||||
public CakeTopping() {
|
||||
}
|
||||
|
||||
public CakeTopping(String name, int calories) {
|
||||
this.setName(name);
|
||||
this.setCalories(calories);
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getCalories() {
|
||||
return calories;
|
||||
}
|
||||
|
||||
public void setCalories(int calories) {
|
||||
this.calories = calories;
|
||||
}
|
||||
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface CakeToppingDao extends CrudRepository<CakeTopping, Long> {
|
||||
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class CakeToppingInfo {
|
||||
|
||||
public final Optional<Long> id;
|
||||
public final String name;
|
||||
public final int calories;
|
||||
|
||||
public CakeToppingInfo(Long id, String name, int calories) {
|
||||
this.id = Optional.of(id);
|
||||
this.name = name;
|
||||
this.calories = calories;
|
||||
}
|
||||
|
||||
public CakeToppingInfo(String name, int calories) {
|
||||
this.id = Optional.empty();
|
||||
this.name = name;
|
||||
this.calories = calories;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("CakeToppingInfo name: %s calories: %d", name, calories);
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence version="1.0"
|
||||
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
|
||||
|
||||
<persistence-unit name="jpaData" />
|
||||
|
||||
</persistence>
|
@ -1,42 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:security="http://www.springframework.org/schema/security"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
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/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>
|
||||
|
||||
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
|
||||
destroy-method="close">
|
||||
<property name="driverClassName" value="org.h2.Driver" />
|
||||
<property name="url" value="jdbc:h2:~/databases/person" />
|
||||
<property name="username" value="sa" />
|
||||
<property name="password" value="sa" />
|
||||
</bean>
|
||||
|
||||
<bean id="entityManagerFactory"
|
||||
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
<property name="packagesToScan" value="com.iluwatar" />
|
||||
<property name="persistenceProvider">
|
||||
<bean class="org.hibernate.ejb.HibernatePersistence" />
|
||||
</property>
|
||||
<property name="jpaProperties">
|
||||
<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>
|
||||
</beans>
|
@ -1,12 +0,0 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user