Ich möchte eine Warenkorb programmieren. Es gibt die Bereiche ProduktKategorie und Artikel. Das Bearbeiten von Kategorien funktioniert soweit. Ich kann Kategorien löschen oder speichern.
Ich habe im Bereich Artikel ein Drop Down Menu das dem Artikel eine Kategorie zuweist. Die Aktualisierung dieses Drop Down Menüs funktioniert nur teilweise. Speichere ich zusätzlich eine neue Kategorie, aktualisiert sich das Drop Down Menu. Lösche ich eine Kategorie funktioniert das nicht (Die entsprechende Kategorie wird aus dem Drop Down Menü nicht entfernt).
manageArticles.xhtml:
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<h:outputStylesheet
library="css"
name="manageCategories.css" />
</h:head>
<h:body class="background">
<div>
<h1>Adminbereich fWarenkorb 4.6</h1>
<h:form>
<h:panelGroup layout="block" styleClass="panel">
<h1>Artikel speichern</h1><br/>
<h:dataTable value="#{manageArticlesBean.getAllArticles()}" var="article" style="font-size : 90%" styleClass="dataTable" rowClasses="odd,even">
<h:column>
<f:facet name="header">Id
</f:facet>
#{article.id}
</h:column>
<h:column>
<f:facet name="header">Artikel
</f:facet>
#{article.articleName}
</h:column>
<h:column>
<f:facet name="header">EP
</f:facet>
#{article.EP}
</h:column>
<h:column>
<f:facet name="header">Löschen
</f:facet>
<h:commandButton value="Löschen"
action="#{manageArticlesBean.deleteArticleEM(article.id)}" />
</h:column>
</h:dataTable>
Artikel:<br />
<h:inputText value="#{manageArticlesBean.article.articleName}" id="article2"/>
<h:messages for="article2" styleClass="errorMessage" /><br />
EP:<br />
<h:inputText value="#{manageArticlesBean.article.EP}" id="eP2"/>
<h:messages for="eP2" styleClass="errorMessage" /><br />
Kategorie:<br />
<h:selectOneMenu value="#{manageArticlesBean.allCategoriesValue}">
<f:selectItems value="#{manageArticlesBean.allCategoriesValue}" />
</h:selectOneMenu><br />
<h:commandButton value="Artikel speichern"
action="#{manageArticlesBean.saveArticle}" />
</h:panelGroup>
</h:form>
</div>
</h:body>
</html>
manageArticlesBean.java:
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;
import javax.transaction.NotSupportedException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import repository.ArticleRepository;
import repository.CategorieRepository;
import entities.Article;
import entities.Categorie;
@SessionScoped
@Named
public class manageArticlesBean implements Serializable {
@Inject
private ArticleRepository articleRepository;
private Article article = new Article();
List<Article> allArticles = new ArrayList<Article>();
boolean boolArticleExists = false;
@Inject
private manageCategoriesBean manageCategoriesBean;
Map<String, Object> allCategoriesMap = new LinkedHashMap();
public manageArticlesBean(){
}
public void saveArticle(){
try{
articleRepository.saveArticle(article);
article = new Article();
System.out.println("Artikel unique: "+boolArticleExists);
}
catch(Exception ex){
ex.printStackTrace();
}
}
public boolean articleExists(){
for(int a=0;a<allArticles.size(); a++){
Article articleRepository = allArticles.get(a);
System.out.println("artikel2saveArtikel: "+article.getArticleName()+" "+allArticles.get(a).getArticleName());
if(article.getArticleName().equals(articleRepository.getArticleName())){
boolArticleExists = true;
break;
}
else{
boolArticleExists = false;
}
}
return boolArticleExists;
}
public Article getArticle(){
return this.article;
}
public void setArticle(Article aArticle){
this.article = aArticle;
}
public List<Article> getAllArticles(){
this.allArticles = articleRepository.getEntriesEM();
return this.allArticles;
}
public void deleteArticleEM(long aArticleId){
try {
articleRepository.deleteArticleEM(aArticleId);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (NotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String navigateToArticles(){
return Pages.ARTICLES;
}
public Map<String, Object> getAllCategoriesValue(){
List<Categorie> allCategories = manageCategoriesBean.getAllCategories();
for(int a=0;a<allCategories.size();a++){
allCategoriesMap.put(allCategories.get(a).getCategorieName(), allCategories.get(a).getCategorieName());
}
return allCategoriesMap;
}
}
manageCategoriesBean.java:
package beans;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.TimeZone;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;
import javax.transaction.NotSupportedException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import repository.CategorieRepository;
import entities.Categorie;
@SessionScoped
@Named
public class manageCategoriesBean implements Serializable {
@Inject
private CategorieRepository categorieRepository;
private Categorie categorie = new Categorie();
List<Categorie> allCategories = new ArrayList<Categorie>();
boolean boolCategorieExists = false;
public manageCategoriesBean(){
}
public void saveCategorie(){
try{
categorieRepository.saveCategorie(categorie);
categorie = new Categorie();
}
catch(Exception ex){
ex.printStackTrace();
}
}
public boolean categorieExists(){
for(int a=0;a<allCategories.size(); a++){
Categorie categorieRepository = allCategories.get(a);
if(categorie.getCategorieName().equals(categorieRepository.getCategorieName())){
boolCategorieExists = true;
break;
}
else{
boolCategorieExists = false;
}
}
return boolCategorieExists;
}
public Categorie getCategorie(){
return this.categorie;
}
public void setCategorie(Categorie aCategorie){
this.categorie = aCategorie;
}
public List<Categorie> getAllCategories(){
this.allCategories = categorieRepository.getEntriesEM();
return this.allCategories;
}
public void deleteCategorieEM(long aCategorieId){
try {
categorieRepository.deleteCategorieEM(aCategorieId);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (NotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String navigateToCategories(){
return Pages.CATEGORIES;
}
}
ArticleRepository:
package repository;
import java.io.Serializable;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Qualifier;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.NotSupportedException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import entities.Article;
import entities.Categorie;
@SessionScoped
@Named
public class ArticleRepository implements Serializable {
@Inject
@qualifier.DataRepository
private EntityManager entityManager;
@Resource
private UserTransaction utx;
public void saveArticle(Article e)throws NotSupportedException, SystemException {
utx.begin();
try {
entityManager.merge(e);
flushEM();
utx.commit();
} catch (Exception ex) {
utx.rollback();
ex.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public List<Article> getEntries() {
return entityManager.createQuery("select e from Article e")
.getResultList();
}
@SuppressWarnings("unchecked")
public void deleteArticleEM(long aArtikelId) throws IllegalStateException, SecurityException, SystemException, NotSupportedException {
utx.begin();
try {
Article categorie = entityManager.find(Article.class, aArtikelId);
entityManager.merge(categorie);
entityManager.remove(categorie);
flushEM();
utx.commit();
} catch (Exception ex) {
utx.rollback();
ex.printStackTrace();
}
}
public List<Article> getEntriesEM(){
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Article> criteria = cb.createQuery(Article.class);
Root<Article> entry = criteria.from(Article.class);
criteria.select(entry).orderBy(cb.asc(entry.get("articleName")));
return entityManager.createQuery(criteria).getResultList();
}
public void flushEM(){
entityManager.flush();
}
}