Skip to content

Commit ec6325e

Browse files
Completed code assignemnt
1 parent 22d6467 commit ec6325e

File tree

8 files changed

+142
-93
lines changed

8 files changed

+142
-93
lines changed

src/main/java/guru/springframework/bootstrap/SpringJPABootstrap.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package guru.springframework.bootstrap;
22

3+
import guru.springframework.domain.Customer;
34
import guru.springframework.domain.Product;
5+
import guru.springframework.services.CustomerService;
46
import guru.springframework.services.ProductService;
57
import org.springframework.beans.factory.annotation.Autowired;
68
import org.springframework.context.ApplicationListener;
@@ -16,16 +18,58 @@
1618
public class SpringJPABootstrap implements ApplicationListener<ContextRefreshedEvent>{
1719

1820
private ProductService productService;
21+
private CustomerService customerService;
1922

2023
@Autowired
2124
public void setProductService(ProductService productService) {
2225
this.productService = productService;
2326
}
2427

28+
@Autowired
29+
public void setCustomerService(CustomerService customerService) {
30+
this.customerService = customerService;
31+
}
32+
2533
@Override
2634
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
2735
loadProducts();
36+
loadCustomers();
37+
38+
}
39+
40+
public void loadCustomers() {
41+
Customer customer1 = new Customer();
42+
customer1.setFirstName("Micheal");
43+
customer1.setLastName("Weston");
44+
customer1.setAddressLine1("1 Main St");
45+
customer1.setCity("Miami");
46+
customer1.setState("Florida");
47+
customer1.setZipCode("33101");
48+
customer1.setEmail("[email protected]");
49+
customer1.setPhoneNumber("305.333.0101");
50+
customerService.saveOrUpdate(customer1);
51+
52+
Customer customer2 = new Customer();
53+
customer2.setFirstName("Fiona");
54+
customer2.setLastName("Glenanne");
55+
customer2.setAddressLine1("1 Key Biscane Ave");
56+
customer2.setCity("Miami");
57+
customer2.setState("Florida");
58+
customer2.setZipCode("33101");
59+
customer2.setEmail("[email protected]");
60+
customer2.setPhoneNumber("305.323.0233");
61+
customerService.saveOrUpdate(customer2);
2862

63+
Customer customer3 = new Customer();
64+
customer3.setFirstName("Sam");
65+
customer3.setLastName("Axe");
66+
customer3.setAddressLine1("1 Little Cuba Road");
67+
customer3.setCity("Miami");
68+
customer3.setState("Florida");
69+
customer3.setZipCode("33101");
70+
customer3.setEmail("[email protected]");
71+
customer3.setPhoneNumber("305.426.9832");
72+
customerService.saveOrUpdate(customer3);
2973
}
3074

3175
public void loadProducts(){

src/main/java/guru/springframework/services/AbstractMapService.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public abstract class AbstractMapService {
1212

1313
public AbstractMapService() {
1414
domainMap = new HashMap<>();
15-
loadDomainObjects();
1615
}
1716

1817
public List<DomainObject> listAll() {
@@ -45,6 +44,4 @@ private Integer getNextKey(){
4544
return Collections.max(domainMap.keySet()) + 1;
4645
}
4746

48-
protected abstract void loadDomainObjects();
49-
5047
}

src/main/java/guru/springframework/services/CustomerServiceImpl.java

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
import guru.springframework.domain.Customer;
44
import guru.springframework.domain.DomainObject;
5+
import org.springframework.context.annotation.Profile;
56
import org.springframework.stereotype.Service;
67

7-
import java.util.HashMap;
88
import java.util.List;
99

1010
/**
1111
* Created by jt on 11/14/15.
1212
*/
1313
@Service
14+
@Profile("map")
1415
public class CustomerServiceImpl extends AbstractMapService implements CustomerService {
1516

1617
@Override
@@ -33,45 +34,4 @@ public void delete(Integer id) {
3334
super.delete(id);
3435
}
3536

36-
@Override
37-
protected void loadDomainObjects() {
38-
domainMap = new HashMap<>();
39-
40-
Customer customer1 = new Customer();
41-
customer1.setId(1);
42-
customer1.setFirstName("Micheal");
43-
customer1.setLastName("Weston");
44-
customer1.setAddressLine1("1 Main St");
45-
customer1.setCity("Miami");
46-
customer1.setState("Florida");
47-
customer1.setZipCode("33101");
48-
customer1.setEmail("[email protected]");
49-
customer1.setPhoneNumber("305.333.0101");
50-
51-
Customer customer2 = new Customer();
52-
customer2.setId(2);
53-
customer2.setFirstName("Fiona");
54-
customer2.setLastName("Glenanne");
55-
customer2.setAddressLine1("1 Key Biscane Ave");
56-
customer2.setCity("Miami");
57-
customer2.setState("Florida");
58-
customer2.setZipCode("33101");
59-
customer2.setEmail("[email protected]");
60-
customer2.setPhoneNumber("305.323.0233");
61-
62-
Customer customer3 = new Customer();
63-
customer3.setId(3);
64-
customer3.setFirstName("Sam");
65-
customer3.setLastName("Axe");
66-
customer3.setAddressLine1("1 Little Cuba Road");
67-
customer3.setCity("Miami");
68-
customer3.setState("Florida");
69-
customer3.setZipCode("33101");
70-
customer3.setEmail("[email protected]");
71-
customer3.setPhoneNumber("305.426.9832");
72-
73-
domainMap.put(1, customer1);
74-
domainMap.put(2, customer2);
75-
domainMap.put(3, customer3);
76-
}
7737
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package guru.springframework.services;
2+
3+
import guru.springframework.domain.Customer;
4+
import org.springframework.context.annotation.Profile;
5+
import org.springframework.stereotype.Service;
6+
7+
import javax.persistence.EntityManager;
8+
import javax.persistence.EntityManagerFactory;
9+
import javax.persistence.PersistenceUnit;
10+
import java.util.List;
11+
12+
/**
13+
* Created by jt on 12/14/15.
14+
*/
15+
@Service
16+
@Profile("jpadao")
17+
public class CustomerServiceJPADaoImpl implements CustomerService{
18+
private EntityManagerFactory emf;
19+
20+
@PersistenceUnit
21+
public void setEmf(EntityManagerFactory emf) {
22+
this.emf = emf;
23+
}
24+
25+
@Override
26+
public List<Customer> listAll() {
27+
EntityManager em = emf.createEntityManager();
28+
29+
return em.createQuery("from Customer", Customer.class).getResultList();
30+
}
31+
32+
@Override
33+
public Customer getById(Integer id) {
34+
EntityManager em = emf.createEntityManager();
35+
36+
return em.find(Customer.class, id);
37+
}
38+
39+
@Override
40+
public Customer saveOrUpdate(Customer domainObject) {
41+
EntityManager em = emf.createEntityManager();
42+
43+
em.getTransaction().begin();
44+
Customer savedCustomer = em.merge(domainObject);
45+
em.getTransaction().commit();
46+
47+
return savedCustomer;
48+
}
49+
50+
@Override
51+
public void delete(Integer id) {
52+
EntityManager em = emf.createEntityManager();
53+
54+
em.getTransaction().begin();
55+
em.remove(em.find(Customer.class, id));
56+
em.getTransaction().commit();
57+
}
58+
}

src/main/java/guru/springframework/services/ProductServiceImpl.java

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import org.springframework.context.annotation.Profile;
66
import org.springframework.stereotype.Service;
77

8-
import java.math.BigDecimal;
9-
import java.util.HashMap;
108
import java.util.List;
119

1210
/**
@@ -36,47 +34,4 @@ public void delete(Integer id) {
3634
super.delete(id);
3735
}
3836

39-
protected void loadDomainObjects(){
40-
domainMap = new HashMap<>();
41-
42-
Product product1 = new Product();
43-
product1.setId(1);
44-
product1.setDescription("Product 1");
45-
product1.setPrice(new BigDecimal("12.99"));
46-
product1.setImageUrl("http://example.com/product1");
47-
48-
domainMap.put(1, product1);
49-
50-
Product product2 = new Product();
51-
product2.setId(2);
52-
product2.setDescription("Product 2");
53-
product2.setPrice(new BigDecimal("14.99"));
54-
product2.setImageUrl("http://example.com/product2");
55-
56-
domainMap.put(2, product2);
57-
58-
Product product3 = new Product();
59-
product3.setId(3);
60-
product3.setDescription("Product 3");
61-
product3.setPrice(new BigDecimal("34.99"));
62-
product3.setImageUrl("http://example.com/product3");
63-
64-
domainMap.put(3, product3);
65-
66-
Product product4 = new Product();
67-
product4.setId(4);
68-
product4.setDescription("Product 4");
69-
product4.setPrice(new BigDecimal("44.99"));
70-
product4.setImageUrl("http://example.com/product4");
71-
72-
domainMap.put(4, product4);
73-
74-
Product product5 = new Product();
75-
product5.setId(5);
76-
product5.setDescription("Product 5");
77-
product5.setPrice(new BigDecimal("25.99"));
78-
product5.setImageUrl("http://example.com/product5");
79-
80-
domainMap.put(5, product5);
81-
}
82-
}
37+
}

src/main/java/guru/springframework/services/ProductServiceJpaDaoImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,5 @@ public void delete(Integer id) {
5555
em.getTransaction().begin();
5656
em.remove(em.find(Product.class, id));
5757
em.getTransaction().commit();
58-
5958
}
6059
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package guru.springframework.services;
2+
3+
import guru.springframework.config.JpaIntegrationConfig;
4+
import guru.springframework.domain.Customer;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.SpringApplicationConfiguration;
9+
import org.springframework.test.context.ActiveProfiles;
10+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11+
12+
import java.util.List;
13+
14+
/**
15+
* Created by jt on 12/14/15.
16+
*/
17+
@RunWith(SpringJUnit4ClassRunner.class)
18+
@SpringApplicationConfiguration(JpaIntegrationConfig.class)
19+
@ActiveProfiles("jpadao")
20+
public class CustomerServiceJpaDaoImplTest {
21+
22+
private CustomerService customerService;
23+
24+
@Autowired
25+
public void setCustomerService(CustomerService customerService) {
26+
this.customerService = customerService;
27+
}
28+
29+
@Test
30+
public void testList() throws Exception {
31+
List<Customer> customers = (List<Customer>) customerService.listAll();
32+
33+
assert customers.size() == 3;
34+
35+
}
36+
}

src/test/java/guru/springframework/services/ProductServiceJapDaoImplTest.java renamed to src/test/java/guru/springframework/services/ProductServiceJpaDaoImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
@RunWith(SpringJUnit4ClassRunner.class)
1818
@SpringApplicationConfiguration(JpaIntegrationConfig.class)
1919
@ActiveProfiles("jpadao")
20-
public class ProductServiceJapDaoImplTest {
20+
public class ProductServiceJpaDaoImplTest {
2121

2222
private ProductService productService;
2323

0 commit comments

Comments
 (0)