Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jpa many to many #14

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
9d66441
Added Index Page and controller
springframeworkguru Nov 6, 2015
eecfc02
Adding .mvn to gitignore
springframeworkguru Nov 6, 2015
d806ca9
Added Index Page and controller
springframeworkguru Nov 6, 2015
ab33273
Added Web Jars
springframeworkguru Nov 6, 2015
56f9fd5
Added Product Listing
springframeworkguru Nov 7, 2015
3e4eebe
Added Display Product
springframeworkguru Nov 9, 2015
d75c97e
Added Create a Product
springframeworkguru Nov 9, 2015
b7b97e9
Added Create a Product
springframeworkguru Nov 9, 2015
8be594a
Added delete a Product
springframeworkguru Nov 14, 2015
c7f19c7
added customer
springframeworkguru Nov 16, 2015
af80230
added spring mvc test examples for index and crud (product only)
springframeworkguru Nov 18, 2015
0caaa61
added spring mvc test examples for index and crud (product only)
springframeworkguru Nov 18, 2015
29470dc
Merge branch 'master' into spring-mvc-test-crud-customer
springframeworkguru Nov 20, 2015
5f25fd9
fixed URL mapping issue for index
springframeworkguru Nov 20, 2015
ab7bb06
added dev tools
springframeworkguru Dec 1, 2015
94d8389
added jars for spring data jpa and h2
springframeworkguru Dec 9, 2015
171c8f4
Converted Product to JPA Entity
springframeworkguru Dec 9, 2015
6ad1014
Converted Product to JPA Entity
springframeworkguru Dec 9, 2015
5be187c
converted Customer Object to JPA Entity
springframeworkguru Dec 9, 2015
10c4cbc
Added JPA DAO serivce for products
springframeworkguru Dec 9, 2015
58751b1
fixed typo in class name
springframeworkguru Dec 9, 2015
3c7773c
adding classes for bootstrap
springframeworkguru Dec 10, 2015
22d6467
adding intgration tests
springframeworkguru Dec 14, 2015
ec6325e
Completed code assignemnt
springframeworkguru Dec 14, 2015
3e26e8a
Refactored services, added encryption service, created user domain ob…
springframeworkguru Dec 15, 2015
72c5eca
Refactored to use bidirectional one to one mapping for User/Customer
springframeworkguru Dec 15, 2015
433c246
added example to use ManytoOne JPA Mapping, plus entity mappings for …
springframeworkguru Dec 15, 2015
67dc233
Added Embedded JPA entity for address, refactored Customer to use Add…
springframeworkguru Dec 16, 2015
07db613
Completed coding assignment for JPA Entities
springframeworkguru Dec 18, 2015
37acb52
Fixing Order table name
springframeworkguru Dec 18, 2015
40b5c14
added Role Entity, and related service objects
springframeworkguru Dec 18, 2015
835a541
Fixed customer templates to get the correct data from the billing add…
Jan 17, 2017
75326b9
Merge pull request #11 from plorent/jpa-many-to-many
springframeworkguru Jan 17, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Thumbs.db
.project
.classpath
.idea
.mvn
*.iml
atlassian-ide-plugin.xml
target
38 changes: 37 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,44 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<!--WebJars-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.5</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.4</version>
</dependency>

<!--encryption lib-->
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
<version>1.9.2</version>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/guru/springframework/SpringmvcApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class SpringmvcApplication {

public static void main(String[] args) {
SpringApplication.run(SpringmvcApplication.class, args);
ApplicationContext ctx = SpringApplication.run(SpringmvcApplication.class, args);

// for (String name : ctx.getBeanDefinitionNames()){
// System.out.println(name);
// }
// System.out.println("******* Bean Count *******");
// System.out.println(ctx.getBeanDefinitionCount());
}
}
191 changes: 191 additions & 0 deletions src/main/java/guru/springframework/bootstrap/SpringJPABootstrap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package guru.springframework.bootstrap;

import guru.springframework.domain.*;
import guru.springframework.domain.security.Role;
import guru.springframework.enums.OrderStatus;
import guru.springframework.services.ProductService;
import guru.springframework.services.RoleService;
import guru.springframework.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
* Created by jt on 12/9/15.
*/
@Component
public class SpringJPABootstrap implements ApplicationListener<ContextRefreshedEvent>{

private ProductService productService;
private UserService userService;
private RoleService roleService;

@Autowired
public void setProductService(ProductService productService) {
this.productService = productService;
}

@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}

@Autowired
public void setRoleService(RoleService roleService) {
this.roleService = roleService;
}

@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
loadProducts();
loadUsersAndCustomers();
loadCarts();
loadOrderHistory();
loadRoles();
assignUsersToDefaultRole();

}

private void assignUsersToDefaultRole() {
List<Role> roles = (List<Role>) roleService.listAll();
List<User> users = (List<User>) userService.listAll();

roles.forEach(role ->{
if(role.getRole().equalsIgnoreCase("CUSTOMER")){
users.forEach(user -> {
user.addRole(role);
userService.saveOrUpdate(user);
});
}
});
}

private void loadRoles() {
Role role = new Role();
role.setRole("CUSTOMER");
roleService.saveOrUpdate(role);
}

private void loadOrderHistory() {
List<User> users = (List<User>) userService.listAll();
List<Product> products = (List<Product>) productService.listAll();

users.forEach(user ->{
Order order = new Order();
order.setCustomer(user.getCustomer());
order.setOrderStatus(OrderStatus.SHIPPED);

products.forEach(product -> {
OrderDetail orderDetail = new OrderDetail();
orderDetail.setProduct(product);
orderDetail.setQuantity(1);
order.addToOrderDetails(orderDetail);
});
});
}

private void loadCarts() {
List<User> users = (List<User>) userService.listAll();
List<Product> products = (List<Product>) productService.listAll();

users.forEach(user -> {
user.setCart(new Cart());
CartDetail cartDetail = new CartDetail();
cartDetail.setProduct(products.get(0));
cartDetail.setQuantity(2);
user.getCart().addCartDetail(cartDetail);
userService.saveOrUpdate(user);
});
}

public void loadUsersAndCustomers() {
User user1 = new User();
user1.setUsername("mweston");
user1.setPassword("password");

Customer customer1 = new Customer();
customer1.setFirstName("Micheal");
customer1.setLastName("Weston");
customer1.setBillingAddress(new Address());
customer1.getBillingAddress().setAddressLine1("1 Main St");
customer1.getBillingAddress().setCity("Miami");
customer1.getBillingAddress().setState("Florida");
customer1.getBillingAddress().setZipCode("33101");
customer1.setEmail("[email protected]");
customer1.setPhoneNumber("305.333.0101");
user1.setCustomer(customer1);
userService.saveOrUpdate(user1);

User user2 = new User();
user2.setUsername("fglenanne");
user2.setPassword("password");

Customer customer2 = new Customer();
customer2.setFirstName("Fiona");
customer2.setLastName("Glenanne");
customer2.setBillingAddress(new Address());
customer2.getBillingAddress().setAddressLine1("1 Key Biscane Ave");
customer2.getBillingAddress().setCity("Miami");
customer2.getBillingAddress().setState("Florida");
customer2.getBillingAddress().setZipCode("33101");
customer2.setEmail("[email protected]");
customer2.setPhoneNumber("305.323.0233");
user2.setCustomer(customer2);
userService.saveOrUpdate(user2);

User user3 = new User();
user3.setUsername("saxe");
user3.setPassword("password");
Customer customer3 = new Customer();
customer3.setFirstName("Sam");
customer3.setLastName("Axe");
customer3.setBillingAddress(new Address());
customer3.getBillingAddress().setAddressLine1("1 Little Cuba Road");
customer3.getBillingAddress().setCity("Miami");
customer3.getBillingAddress().setState("Florida");
customer3.getBillingAddress().setZipCode("33101");
customer3.setEmail("[email protected]");
customer3.setPhoneNumber("305.426.9832");

user3.setCustomer(customer3);
userService.saveOrUpdate(user3);
}

public void loadProducts(){

Product product1 = new Product();
product1.setDescription("Product 1");
product1.setPrice(new BigDecimal("12.99"));
product1.setImageUrl("http://example.com/product1");
productService.saveOrUpdate(product1);

Product product2 = new Product();
product2.setDescription("Product 2");
product2.setPrice(new BigDecimal("14.99"));
product2.setImageUrl("http://example.com/product2");
productService.saveOrUpdate(product2);

Product product3 = new Product();
product3.setDescription("Product 3");
product3.setPrice(new BigDecimal("34.99"));
product3.setImageUrl("http://example.com/product3");
productService.saveOrUpdate(product3);

Product product4 = new Product();
product4.setDescription("Product 4");
product4.setPrice(new BigDecimal("44.99"));
product4.setImageUrl("http://example.com/product4");
productService.saveOrUpdate(product4);

Product product5 = new Product();
product5.setDescription("Product 5");
product5.setPrice(new BigDecimal("25.99"));
product5.setImageUrl("http://example.com/product5");
productService.saveOrUpdate(product5);

}
}
18 changes: 18 additions & 0 deletions src/main/java/guru/springframework/config/CommonBeanConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package guru.springframework.config;

import org.jasypt.util.password.StrongPasswordEncryptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* Created by jt on 12/14/15.
*/
@Configuration
public class CommonBeanConfig {

@Bean
public StrongPasswordEncryptor strongEncryptor(){
StrongPasswordEncryptor encryptor = new StrongPasswordEncryptor();
return encryptor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package guru.springframework.controllers;

import guru.springframework.domain.Customer;
import guru.springframework.services.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
* Created by jt on 11/15/15.
*/
@RequestMapping("/customer")
@Controller
public class CustomerController {

private CustomerService customerService;

@Autowired
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}

@RequestMapping({"/list", "/"})
public String listCustomers(Model model){
model.addAttribute("customers", customerService.listAll());
return "customer/list";
}

@RequestMapping("/show/{id}")
public String showCustomer(@PathVariable Integer id, Model model){
model.addAttribute("customer", customerService.getById(id));
return "customer/show";
}

@RequestMapping("/edit/{id}")
public String edit(@PathVariable Integer id, Model model){
model.addAttribute("customer", customerService.getById(id));
return "customer/customerform";
}

@RequestMapping("/new")
public String newCustomer(Model model){
model.addAttribute("customer", new Customer());
return "customer/customerform";
}

@RequestMapping(method = RequestMethod.POST)
public String saveOrUpdate(Customer customer){
Customer newCustomer = customerService.saveOrUpdate(customer);
return "redirect:customer/show/" + newCustomer.getId();
}

@RequestMapping("/delete/{id}")
public String delete(@PathVariable Integer id){
customerService.delete(id);
return "redirect:/customer/list";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package guru.springframework.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
* Created by jt on 11/6/15.
*/
@Controller
public class IndexController {

@RequestMapping({"/", ""})
public String index(){
return "index";
}
}
Loading