-
Notifications
You must be signed in to change notification settings - Fork 2
Homework 4 #2
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
base: master
Are you sure you want to change the base?
Homework 4 #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package ru.geekbrains.servlet; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import ru.geekbrains.servlet.entity.Category; | ||
| import ru.geekbrains.servlet.entity.Category; | ||
| import ru.geekbrains.servlet.repository.CategoryRepository; | ||
|
|
||
| import javax.faces.bean.ManagedBean; | ||
| import javax.faces.bean.SessionScoped; | ||
| import javax.inject.Inject; | ||
| import javax.inject.Named; | ||
| import java.util.Collection; | ||
|
|
||
| @Named("categories") | ||
| @SessionScoped | ||
| public class CategoriesBean { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(CategoriesBean.class); | ||
|
|
||
| private Category category; | ||
|
|
||
| @Inject | ||
| private CategoryRepository categoryRepository; | ||
|
|
||
| public String getId() { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. На всякий случай хочу сказать, что есть еще один подход к построению managed бинов. Можно не создавать геттеры для всех полей сущности, а создать только один геттер для поля с текущей сущностью. Тогда можно будет обращаться к полям из представлений #{categories.category.id} |
||
| return category.getId(); | ||
| } | ||
|
|
||
| public void setId(String id) { | ||
| category.setId(id); | ||
| } | ||
|
|
||
| public String getName() { | ||
| return category.getName(); | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| category.setName(name); | ||
| } | ||
|
|
||
| public Collection<Category> getCategoryList() { | ||
| return categoryRepository.getAll(); | ||
| } | ||
|
|
||
| public void deleteAction(Category category) { | ||
| logger.info("Delete category with id {} and name {}", category.getId(), category.getName()); | ||
| categoryRepository.delete(category); | ||
| } | ||
|
|
||
| public String addAction() { | ||
| logger.info("Add category action"); | ||
|
|
||
| category = new Category(); | ||
| return "/category.xhtml?faces-redirect=true"; | ||
| } | ||
|
|
||
| public String editAction(Category category) { | ||
| logger.info("Edit category with id {} and name {}", category.getId(), category.getName()); | ||
|
|
||
| this.category = category; | ||
| return "/category.xhtml?faces-redirect=true"; | ||
| } | ||
|
|
||
| public String saveCategory() { | ||
| categoryRepository.merge(category); | ||
| return "/categories.xhtml?faces-redirect=true"; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package ru.geekbrains.servlet; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import ru.geekbrains.servlet.entity.Order; | ||
| import ru.geekbrains.servlet.repository.OrderRepository; | ||
|
|
||
| import javax.faces.bean.ManagedBean; | ||
| import javax.faces.bean.SessionScoped; | ||
| import javax.inject.Inject; | ||
| import javax.inject.Named; | ||
| import java.util.Collection; | ||
|
|
||
| @Named("orders") | ||
| @SessionScoped | ||
| public class OrdersBean { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(OrdersBean.class); | ||
|
|
||
| private Order order; | ||
|
|
||
| @Inject | ||
| private OrderRepository orderRepository; | ||
|
|
||
| public String getId() { | ||
| return order.getId(); | ||
| } | ||
|
|
||
| public void setId(String id) { | ||
| order.setId(id); | ||
| } | ||
|
|
||
| public Collection<Order> getOrderList() { | ||
| return orderRepository.getAll(); | ||
| } | ||
|
|
||
| public void deleteAction(Order order) { | ||
| logger.info("Delete order with id {} and name {}", order.getId()); | ||
| orderRepository.delete(order); | ||
| } | ||
|
|
||
| public String addAction() { | ||
| logger.info("Add order action"); | ||
|
|
||
| orderRepository.merge(new Order()); | ||
| return "/orders.xhtml?faces-redirect=true"; | ||
| } | ||
|
|
||
| public String editAction(Order order) { | ||
| logger.info("Edit order with id {} and name {}", order.getId()); | ||
|
|
||
| this.order = order; | ||
| return "/order.xhtml?faces-redirect=true"; | ||
| } | ||
|
|
||
| public String saveOrder() { | ||
| orderRepository.merge(order); | ||
| return "/orders.xhtml?faces-redirect=true"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package ru.geekbrains.servlet.entity; | ||
|
|
||
| import javax.validation.constraints.NotNull; | ||
|
|
||
| public class Category { | ||
|
|
||
| private String id; | ||
|
|
||
| @NotNull | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Валидация будет работать только в бинах, а этот файл бином не является. На следующем уроке мы преобразуем его в сущность и все заработатет. Пока же предлагаю использовать валидацию в xhtml. Компоненту inputText можно добавить свойство required. Кроме того есть еще семейство вот таких тегов для валидации https://www.tutorialspoint.com/jsf/jsf_validation_tags.htm |
||
| private String name; | ||
|
|
||
| public Category() {} | ||
|
|
||
| public Category(String id, String name) { | ||
| this.id = id; | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(String id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package ru.geekbrains.servlet.entity; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class Order { | ||
|
|
||
| private String id; | ||
| private Map<Product, Integer> orderMap; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А переопределен ли hashCode у продукта?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Мапа хранит для заказа пары продукт - количество. Хеш-функцию забыл. Сделаю по id. |
||
|
|
||
| public Order() {} | ||
|
|
||
| public Order(String id) { | ||
| this.id = id; | ||
| orderMap = new HashMap<>(); | ||
| } | ||
|
|
||
| public void addProduct(Product product, int count) { | ||
| if (orderMap.containsKey(product)) { | ||
| orderMap.compute(product, (good, amount) -> amount + count); | ||
| } else { | ||
| orderMap.put(product, count); | ||
| } | ||
| } | ||
|
|
||
| public void setProductCount(Product product, int count) { | ||
| orderMap.put(product, count); | ||
| } | ||
|
|
||
| public void removeProduct(Product product) { | ||
| orderMap.remove(product); | ||
| } | ||
|
|
||
| public String getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(String id) { | ||
| this.id = id; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package ru.geekbrains.servlet.repository; | ||
|
|
||
| import ru.geekbrains.servlet.entity.Category; | ||
|
|
||
| import javax.enterprise.context.ApplicationScoped; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| /** | ||
| * Класс-заглушка для репозитория | ||
| * через несколько уроков мы его заменим на | ||
| * полноценный JPA репозиторий | ||
| */ | ||
| @ApplicationScoped | ||
| public class CategoryRepository { | ||
|
|
||
| private AtomicInteger sequence = new AtomicInteger(); | ||
|
|
||
| private Map<String, Category> categoryMap = Collections.synchronizedMap(new LinkedHashMap<>()); | ||
|
|
||
| public CategoryRepository() { | ||
| this.merge(new Category("1", "Books")); | ||
| this.merge(new Category("2", "Planes")); | ||
| this.merge(new Category("3", "Fruits")); | ||
| this.merge(new Category("4", "Clothes")); | ||
| sequence.set(categoryMap.size()); | ||
| } | ||
|
|
||
| public Collection<Category> getAll() { | ||
| return categoryMap.values(); | ||
| } | ||
|
|
||
| public Category getById(String id) { | ||
| return categoryMap.get(id); | ||
| } | ||
|
|
||
| public void merge(Category category) { | ||
| if (category.getId() == null || !categoryMap.containsKey(category.getId())) { | ||
| category.setId(String.valueOf(sequence.incrementAndGet())); | ||
| } | ||
| categoryMap.put(category.getId(), category); | ||
| } | ||
|
|
||
| public void delete(Category category) { | ||
| categoryMap.remove(category.getId()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package ru.geekbrains.servlet.repository; | ||
|
|
||
| import ru.geekbrains.servlet.entity.Order; | ||
|
|
||
| import javax.enterprise.context.ApplicationScoped; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| /** | ||
| * Класс-заглушка для репозитория | ||
| * через несколько уроков мы его заменим на | ||
| * полноценный JPA репозиторий | ||
| */ | ||
| @ApplicationScoped | ||
| public class OrderRepository { | ||
|
|
||
| private AtomicInteger sequence = new AtomicInteger(); | ||
|
|
||
| private Map<String, Order> orderMap = Collections.synchronizedMap(new LinkedHashMap<>()); | ||
|
|
||
| public OrderRepository() { | ||
| this.merge(new Order("1")); | ||
| this.merge(new Order("2")); | ||
| this.merge(new Order("3")); | ||
| this.merge(new Order("4")); | ||
| sequence.set(orderMap.size()); | ||
| } | ||
|
|
||
| public Collection<Order> getAll() { | ||
| return orderMap.values(); | ||
| } | ||
|
|
||
| public Order getById(String id) { | ||
| return orderMap.get(id); | ||
| } | ||
|
|
||
| public void merge(Order order) { | ||
| if (order.getId() == null || !orderMap.containsKey(order.getId())) { | ||
| order.setId(String.valueOf(sequence.incrementAndGet())); | ||
| } | ||
| orderMap.put(order.getId(), order); | ||
| } | ||
|
|
||
| public void delete(Order order) { | ||
| orderMap.remove(order.getId()); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Насколько я зная возможность полностью отказаться от managed бинов появилась только в версии JSF 2.3. Но этот момент нужно уточнить. Речь идет о довольно новых вещах.