diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Master/Daten- und Systemintegration/Praktikum/Projekte/DSI-Praktikum-1/ausgang/DSI-ejb/src/java/beans/artikelManager | |
| download | Studium-master.tar.gz Studium-master.tar.bz2 | |
Diffstat (limited to 'Master/Daten- und Systemintegration/Praktikum/Projekte/DSI-Praktikum-1/ausgang/DSI-ejb/src/java/beans/artikelManager')
2 files changed, 152 insertions, 0 deletions
diff --git a/Master/Daten- und Systemintegration/Praktikum/Projekte/DSI-Praktikum-1/ausgang/DSI-ejb/src/java/beans/artikelManager/ArtikelManagerBean.java b/Master/Daten- und Systemintegration/Praktikum/Projekte/DSI-Praktikum-1/ausgang/DSI-ejb/src/java/beans/artikelManager/ArtikelManagerBean.java new file mode 100644 index 0000000..71b10ae --- /dev/null +++ b/Master/Daten- und Systemintegration/Praktikum/Projekte/DSI-Praktikum-1/ausgang/DSI-ejb/src/java/beans/artikelManager/ArtikelManagerBean.java @@ -0,0 +1,123 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package beans.artikelManager; + +import beans.*; +import entities.Artikel; +import exceptions.IdBereitsVergebenException; +import exceptions.UnbekanntesEntityException; +import java.util.List; +import javax.ejb.Stateless; +import javax.jws.WebMethod; +import javax.jws.WebParam; +import javax.jws.WebResult; +import javax.jws.WebService; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +/** + * + * @author Jan + */ +@WebService +@Stateless(mappedName="ArtikelManagerBean") +public class ArtikelManagerBean implements ArtikelManagerRemote { + @PersistenceContext + private EntityManager em; + + + /** + * erstellt einen Artikel und liefert die Artikel-Id zurueck + * @param artikel + * @return Artikel-Id + */ + @WebMethod + public @WebResult(name="newArtikelId") long createArtikel( + @WebParam(name="artikel") Artikel artikel) throws IdBereitsVergebenException { + try { + em.persist(artikel); + return artikel.getId(); + } + catch(Exception e){ + throw new IdBereitsVergebenException("Artikel-Id bereits vergeben: [id="+ artikel.getId()+"]"); + } + } + + + /** + * Gibt einen Artikel anhand seiner Id zurueck + * @param artikelId + * @return + */ + @WebMethod + public @WebResult(name="artikel") Artikel readArtikel( + @WebParam(name="artikelId") long artikelId) throws UnbekanntesEntityException{ + Artikel artikel = em.find(Artikel.class, artikelId ); + if (artikel == null) + throw new UnbekanntesEntityException("Artikel", artikelId); + return artikel; + } + + + /** + * gibt alle Artikel zurueck + * @return + */ + @WebMethod + public @WebResult(name="allArtikel") List<Artikel> readAllArtikel(){ + return (List<Artikel>)em.createQuery("SELECT a FROM Artikel a ORDER BY a.id").getResultList(); + } + + /** + * Schreibt die Änderungen eines Artikels in die Datenbank + * @param artikel + */ + @WebMethod + public void updateArtikel( + @WebParam(name="artikel") Artikel artikel){ + em.merge(artikel); + } + + /** + * Entfernt einen Artikel mit der angegebenen Artikel-Id + * @param artikelId + */ + @WebMethod + public void deleteArtikel( + @WebParam(name="artikelId") long artikelId){ + Artikel artikel = em.find(Artikel.class, artikelId ); + em.remove(artikel); + } + + + /** + * entfernt alle Artikel + */ + @WebMethod + public void deleteAllArtikel(){ + for(Artikel artikel : readAllArtikel()){ + deleteArtikel( artikel.getId() ); + } + } + + + /** + * Prüft ob es einen Artikel mit angegebener Artikel-Id gibt + * @param artikelId + * @return + */ + @WebMethod + public @WebResult(name="existiert") boolean existsArtikel( + @WebParam(name="artikelId") long artikelId){ + Artikel artikel = em.find(Artikel.class, artikelId ); + if(artikel != null) + return true; + + return false; + } + + +} diff --git a/Master/Daten- und Systemintegration/Praktikum/Projekte/DSI-Praktikum-1/ausgang/DSI-ejb/src/java/beans/artikelManager/ArtikelManagerRemote.java b/Master/Daten- und Systemintegration/Praktikum/Projekte/DSI-Praktikum-1/ausgang/DSI-ejb/src/java/beans/artikelManager/ArtikelManagerRemote.java new file mode 100644 index 0000000..dba3832 --- /dev/null +++ b/Master/Daten- und Systemintegration/Praktikum/Projekte/DSI-Praktikum-1/ausgang/DSI-ejb/src/java/beans/artikelManager/ArtikelManagerRemote.java @@ -0,0 +1,29 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package beans.artikelManager; + +import entities.Artikel; +import exceptions.IdBereitsVergebenException; +import exceptions.UnbekanntesEntityException; +import java.util.List; +import javax.ejb.Remote; + +/** + * + * @author Jan + */ +@Remote +public interface ArtikelManagerRemote { + + public long createArtikel(Artikel artikel) throws IdBereitsVergebenException; + public Artikel readArtikel(long artikelId) throws UnbekanntesEntityException; + public List<Artikel> readAllArtikel(); + public void updateArtikel(Artikel artikel); + public void deleteArtikel(long artikelId); + public void deleteAllArtikel(); + public boolean existsArtikel(long artikelId); + +} |
