8
8
9
9
import org .springframework .context .support .AbstractApplicationContext ;
10
10
import org .springframework .context .support .ClassPathXmlApplicationContext ;
11
+ import org .springframework .stereotype .Service ;
12
+ import org .springframework .transaction .annotation .Transactional ;
11
13
14
+ @ Service
15
+ @ Transactional
12
16
public class CakeBakingServiceImpl implements CakeBakingService {
13
17
14
18
private AbstractApplicationContext context ;
@@ -22,23 +26,31 @@ public void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException {
22
26
List <CakeToppingInfo > allToppings = getAllToppings ();
23
27
List <CakeToppingInfo > matchingToppings = allToppings .stream ()
24
28
.filter ((t ) -> t .name .equals (cakeInfo .cakeToppingInfo .name )).collect (Collectors .toList ());
25
- if (!matchingToppings .isEmpty ()) {
26
- // CakeToppingDao toppingBean = context.getBean(CakeToppingDao.class);
27
- // toppingBean.delete(matchingToppings.iterator().next().id.get());
28
- } else {
29
+ if (matchingToppings .isEmpty ()) {
29
30
throw new CakeBakingException (String .format ("Topping %s is not available" , cakeInfo .cakeToppingInfo .name ));
30
31
}
31
- List <CakeLayerInfo > allLayers = getAllLayers ();
32
+ List <CakeLayer > allLayers = getAllLayerEntities ();
33
+ List <CakeLayer > foundLayers = new ArrayList <>();
32
34
for (CakeLayerInfo info : cakeInfo .cakeLayerInfos ) {
33
- Optional <CakeLayerInfo > found = allLayers .stream ().filter ((layer ) -> layer .name .equals (info .name )).findFirst ();
34
- if (found .isPresent ()) {
35
- // CakeLayerDao layerBean = context.getBean(CakeLayerDao.class);
36
- // layerBean.delete(found.get().id.get());
37
- } else {
35
+ Optional <CakeLayer > found = allLayers .stream ().filter ((layer ) -> layer .getName ().equals (info .name )).findFirst ();
36
+ if (!found .isPresent ()) {
38
37
throw new CakeBakingException (String .format ("Layer %s is not available" , info .name ));
38
+ } else {
39
+ foundLayers .add (found .get ());
39
40
}
40
41
}
42
+ CakeToppingDao toppingBean = context .getBean (CakeToppingDao .class );
43
+ CakeTopping topping = toppingBean .findOne (matchingToppings .iterator ().next ().id .get ());
41
44
CakeDao cakeBean = context .getBean (CakeDao .class );
45
+ Cake cake = new Cake ();
46
+ cake = cakeBean .save (cake );
47
+ cake .setTopping (topping );
48
+ topping .setCake (cake );
49
+ cake .setLayers (foundLayers );
50
+ for (CakeLayer layer : foundLayers ) {
51
+ layer .setCake (cake );
52
+ }
53
+ cakeBean .save (cake );
42
54
}
43
55
44
56
@ Override
@@ -53,6 +65,16 @@ public void saveNewLayer(CakeLayerInfo layerInfo) {
53
65
bean .save (new CakeLayer (layerInfo .name , layerInfo .calories ));
54
66
}
55
67
68
+ private List <CakeTopping > getAllToppingEntities () {
69
+ CakeToppingDao bean = context .getBean (CakeToppingDao .class );
70
+ List <CakeTopping > result = new ArrayList <>();
71
+ Iterator <CakeTopping > iterator = bean .findAll ().iterator ();
72
+ while (iterator .hasNext ()) {
73
+ result .add (iterator .next ());
74
+ }
75
+ return result ;
76
+ }
77
+
56
78
@ Override
57
79
public List <CakeToppingInfo > getAllToppings () {
58
80
CakeToppingDao bean = context .getBean (CakeToppingDao .class );
@@ -65,6 +87,16 @@ public List<CakeToppingInfo> getAllToppings() {
65
87
return result ;
66
88
}
67
89
90
+ private List <CakeLayer > getAllLayerEntities () {
91
+ CakeLayerDao bean = context .getBean (CakeLayerDao .class );
92
+ List <CakeLayer > result = new ArrayList <>();
93
+ Iterator <CakeLayer > iterator = bean .findAll ().iterator ();
94
+ while (iterator .hasNext ()) {
95
+ result .add (iterator .next ());
96
+ }
97
+ return result ;
98
+ }
99
+
68
100
@ Override
69
101
public List <CakeLayerInfo > getAllLayers () {
70
102
CakeLayerDao bean = context .getBean (CakeLayerDao .class );
0 commit comments