Я тоже обратила на это внимание, но откуда берутся два сервиса ума не дам.
вот собственно код ELibSession:
| Код | @Stateless public class ELibSession implements ELibSessionRemote, ELibSessionLocal { @PersistenceContext(unitName = "ELib-ejbPU") private EntityManager em; Worksession ws=null;
public void persist(Object object) { em.persist(object); }
public boolean registration(String login, String password)throws LibraryException { boolean register=false; if (!login.equals("")&&!password.equals("")){ Query query = em.createNamedQuery("Reader.findByLogin"); query.setParameter("login", login); if (query.getResultList().isEmpty()){ Reader reader=new Reader(login,password); em.persist(reader); register=true; } else throw new LibraryException("Пользователь с таким логином уже зарегистрирован"); } else throw new LibraryException("Поля логина и пароля не могут быть пустыми"); return register; }
public int authorization(String login, String password) throws LibraryException{ /*Author author1=new Author(0,"John","Vanduk"); Ebook book1=new Ebook(0,"Drupal 6",3,1); book1.setAuthor(author1); List<Location> locs=new LinkedList<Location>(); for (int i=0; i<book1.getCount(); i++){ Librarylocation ll=new Librarylocation(i); ll.setEbook(book1); locs.add(ll); em.persist(ll); book1.getLibrarylocationCollection().add(ll); } em.persist(author1); em.persist(book1);*/ int result=-1; Query query = em.createNamedQuery("Reader.findByFull"); query.setParameter("login", login); query.setParameter("password", password); List<Reader> readers=(List<Reader>)query.getResultList(); if (!readers.isEmpty()){ Reader r=readers.get(0); ws=new Worksession(r); em.persist(ws); result=ws.getId(); } else throw new LibraryException("Неверная пара логин-пароль"); return result; }
private List<Ebook> searchByTitle(String title) { List<Ebook> result=new LinkedList<Ebook>(); Query query = em.createNamedQuery("Ebook.findByTitle"); query.setParameter("title", title); result=query.getResultList(); return result; }
private List<Ebook> searchByAuthor(String firstName, String lastName) { List<Ebook> result=new LinkedList<Ebook>(); Query query = em.createNamedQuery("Ebook.findAll"); List<Ebook> books=query.getResultList(); for(Ebook book:books){ Author author=book.getAuthor(); if (author.getFirstname().equals(firstName)&&author.getLastname().equals(lastName)) result.add(book); } return result; }
public List<Ebook> search(String title, String firstName, String lastName) { List<Ebook> result=new LinkedList<Ebook>(); if (!title.equals("")&&firstName.equals("")&&lastName.equals("")){ result=searchByTitle(title); } if (title.equals("")&&!firstName.equals("")&&!lastName.equals("")){ result=searchByAuthor(firstName, lastName); } if (!title.equals("")&&!firstName.equals("")&&!lastName.equals("")){ List<Ebook> books=searchByTitle(title); for(Ebook book:books){ Author author=book.getAuthor(); if(author.getFirstname().equals(firstName)&&author.getLastname().equals(lastName)) result.add(book); } } return result; }
public boolean orderBook(Ebook ebook) throws LibraryException { boolean result=false; Reader reader=ws.getReader(); Userlocation ul=null; //проверяем была ли книга заказана пользователем ранее Query query=em.createNamedQuery("Userlocation.findAll"); List<Userlocation> ulocs=(List<Userlocation>)query.getResultList(); for(Userlocation uloc:ulocs){ if (uloc.getEbook().equals(ebook)&&uloc.getReader().equals(reader)) ul=uloc; } //если проверка выполнена, выполняем заказ if (ul==null){ Query query1=em.createNamedQuery("Librarylocation.findAll"); List<Librarylocation> liblocs=query1.getResultList(); for(Librarylocation libloc:liblocs){ if (libloc.getEbook().equals(ebook)&&!result){ em.remove(libloc); Userlocation uloc=new Userlocation(reader, ebook); em.persist(uloc); result=true; } } if(!result) throw new LibraryException("В библиотеке нет свободных экземпляров указанной книги"); } else{ throw new LibraryException("Эта книга уже была заказана Вами"); } return result; }
public boolean returnBook(Ebook ebook) { boolean result=false; Reader reader=ws.getReader(); Userlocation ul=null; //проверяем была ли книга заказана пользователем Query query=em.createNamedQuery("Userlocation.findAll"); List<Userlocation> ulocs=(List<Userlocation>)query.getResultList(); for(Userlocation uloc:ulocs){ if (uloc.getEbook().equals(ebook)&&uloc.getReader().equals(reader)) ul=uloc; } //если книга была заказана, осуществляем возврат if (ul!=null){ em.remove(ul); Librarylocation ll=new Librarylocation(ebook); em.persist(ll); result=true; } return result; }
public List<Ebook> showBooks() { Query query=em.createNamedQuery("Ebook.findAll"); List<Ebook> result=(List<Ebook>)query.getResultList(); return result; }
public List<Ebook> showReaderBook() { List<Ebook> result=new LinkedList<Ebook>(); Reader r=ws.getReader();
Query query = em.createNamedQuery("Userlocation.findAll"); List<Userlocation> ulocs=(List<Userlocation>)query.getResultList();
for(Userlocation ul:ulocs){ if (ul.getReader().equals(r)){ Query q=em.createNamedQuery("Ebook.findById"); q.setParameter("id", ul.getEbook().getId()); List<Ebook> books=(List<Ebook>)q.getResultList(); if (!books.isEmpty()) result.add(books.get(0)); } } return result; }
public int getWorkSession() { int result=-1; if (ws!=null){ result=ws.getId(); } return result; }
public List<Location> getEbookLocations(Ebook ebook) { List<Location> result=new LinkedList<Location>(); /*Collection<Librarylocation> liblocs=ebook.getLibrarylocationCollection(); Collection<Userlocation> userlocs=ebook.getUserlocationCollection(); if (liblocs!=null) result.addAll(liblocs); if (userlocs!=null) result.addAll(userlocs);*/ Query query=em.createNamedQuery("Librarylocation.findAll"); List<Librarylocation> liblocs=(List<Librarylocation>)query.getResultList(); for(Librarylocation libloc: liblocs){ if (libloc.getEbook().equals(ebook)){ result.add(libloc); } } Query query1=em.createNamedQuery("Userlocation.findAll"); List<Userlocation> ulocs=(List<Userlocation>)query1.getResultList(); for(Userlocation uloc: ulocs){ if (uloc.getEbook().equals(ebook)){ result.add(uloc); } } return result; }
// Add business logic below. (Right-click in editor and choose // "Insert Code > Add Business Method") }
|
здесь код интерфейса ELibSessionLocal
| Код | @Local public interface ELibSessionLocal {
boolean registration(String login, String password)throws LibraryException;
int authorization(String login, String password)throws LibraryException;
List<Ebook> search(String title, String firstName, String lastName);
boolean orderBook(Ebook ebook) throws LibraryException;
boolean returnBook(Ebook ebook);
List<Ebook> showBooks();
List<Ebook> showReaderBook();
int getWorkSession();
List<Location> getEbookLocations(Ebook ebook); }
|
а вот код самого сервиса ELibWS:
| Код | @WebService() @Stateless() public class ELibWS { @EJB private ELibSessionLocal ejbRef;// Add business logic below. (Right-click in editor and choose // "Insert Code > Add Web Service Operation")
@WebMethod(operationName = "registration") public boolean registration(@WebParam(name = "login") String login, @WebParam(name = "password") String password){ boolean result=false; try { result=ejbRef.registration(login, password); } catch (LibraryException ex) { Logger.getLogger(ELibWS.class.getName()).log(Level.SEVERE, null, ex); } return result; }
/** * Web service operation */ @WebMethod(operationName = "authorization") public int authorization(@WebParam(name = "login") String login, @WebParam(name = "password") String password){ int result=0; try { //TODO write your implementation code here: result=ejbRef.authorization(login, password); } catch (LibraryException ex) { Logger.getLogger(ELibWS.class.getName()).log(Level.SEVERE, null, ex); } return result; }
/** * Web service operation */ /*@WebMethod(operationName = "search") public List<Ebook> search(@WebParam(name = "title") String title, @WebParam(name = "firstName") String firstName, @WebParam(name = "lastName") String lastName) { //TODO write your implementation code here: return ejbRef.search(title, firstName, lastName); }*/
}
|
|