1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/*
* Created on 25.11.2004 by HS
*
*/
package de.laliluna.library.bl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
import de.laliluna.library.Book;
import de.laliluna.library.Customer;
import de.laliluna.library.HibernateSessionFactory;
/**
* @author HS
*
*
*/
public class LibraryManager {
/**
* get all books from the database
*
* @return Array of BookValue
*/
public Book[] getAllBooks() {
/* will hold the books we are going to return later */
List books = new ArrayList();
/* a Hibernate session */
Session session = null;
/* we always need a transaction */
Transaction tx = null;
/* get session of the current thread */
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
List tmpBooks = session.createQuery(
"select b from Book as b order by b.author, b.title").list();
for (Iterator iter = tmpBooks.iterator(); iter.hasNext();) {
books.add((Book) iter.next());
}
tx.commit();
return (Book[]) books.toArray(new Book[0]);
}
/**
* get book by primary key
*
* @param primaryKey
* @return a Book or null
*/
public Book getBookByPrimaryKey(Integer primaryKey) {
/* holds our return value */
Book book = null;
/* a Hibernate session */
Session session = null;
/* we always need a transaction */
Transaction tx = null;
/* get session of the current thread */
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
book = (Book) session.get(Book.class, primaryKey);
tx.commit();
return book;
}
/**
* sets the book as borrowed to the user specified in the database
*
* @param primaryKey
* @param userPrimaryKey
*/
public void borrowBook(Integer primaryKey, Integer customerPrimaryKey) {
/* a Hibernate session */
Session session = null;
/* we always need a transaction */
Transaction tx = null;
/* get session of the current thread */
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
Book book = (Book) session.get(Book.class, primaryKey);
// only if borrowing of the book is allowed
if (book.getBorrowallowed()) {
Customer customer = (Customer) session.get(Customer.class,
customerPrimaryKey);
if (book != null && customer != null)
book.setCustomer(customer);
}
tx.commit();
}
/**
* customer returns a book, relation in the db between customer and book is
* deleted
*
* @param primaryKey
*/
public void returnBook(Integer primaryKey) {
/* a Hibernate session */
Session session = null;
/* we always need a transaction */
Transaction tx = null;
/* get session of the current thread */
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
Book book = (Book) session.get(Book.class, primaryKey);
if (book != null) {
// session.get returns null when no entry is found
Customer customer = book.getCustomer();
if (customer != null) {
customer.getBooks().remove(book);
book.setCustomer(null);
}
}
tx.commit();
}
/**
* updates/creates a book
*
* @param bookValue
*/
public void saveBook(Book bookValue) {
/* a Hibernate session */
Session session = null;
/* we always need a transaction */
Transaction tx = null;
/* get session of the current thread */
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
Book book;
if (bookValue.getId() != null && bookValue.getId().intValue() != 0) { // [laliluna]
// 04.12.2004
// load
// book
// from
// DB
book = (Book) session.get(Book.class, bookValue.getId());
if (book != null) {
book.setAuthor(bookValue.getAuthor());
book.setTitle(bookValue.getTitle());
book.setBorrowallowed(bookValue.getBorrowallowed());
session.update(book);
}
} else // [laliluna] 04.12.2004 create new book
{
book = new Book();
book.setAuthor(bookValue.getAuthor());
book.setTitle(bookValue.getTitle());
book.setBorrowallowed(bookValue.getBorrowallowed());
session.save(book);
}
tx.commit();
}
/**
* deletes a book
*
* @param primaryKey
*/
public void removeBookByPrimaryKey(Integer primaryKey) {
/* a Hibernate session */
Session session = null;
/* we always need a transaction */
Transaction tx = null;
/* get session of the current thread */
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
Book book = (Book) session.get(Book.class, primaryKey);
if (book != null)
if (book.getCustomer() != null)
book.getCustomer().getBooks().remove(book);
session.delete(book);
tx.commit();
}
/**
* returns all customers from the db
*
* @return
*/
public Customer[] getAllCustomers() {
/* will hold the books we are going to return later */
List customers = new ArrayList();
/* a Hibernate session */
Session session = null;
/* we always need a transaction */
Transaction tx = null;
/* get session of the current thread */
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
List tmpCustomer = session.createQuery(
"select c from Customer as c order by c.lastname").list();
for (Iterator iter = tmpCustomer.iterator(); iter.hasNext();) {
customers.add((Customer) iter.next());
}
tx.commit();
return (Customer[]) customers.toArray(new Customer[0]);
}
/**
* gets a customer from the db
*
* @param primaryKey
* @return the customer class or null, when no customer is found
*/
public Customer getCustomerByPrimaryKey(Integer primaryKey) {
/* holds our return value */
Customer customer = null;
/* a Hibernate session */
Session session = null;
/* we always need a transaction */
Transaction tx = null;
/* get session of the current thread */
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
customer = (Customer) session.get(Customer.class, primaryKey);
tx.commit();
return customer;
}
/**
* saves the customers to the db
*
* @param customer
*/
public void saveCustomer(Customer customer) {
/* a Hibernate session */
Session session = null;
/* we always need a transaction */
Transaction tx = null;
/* get session of the current thread */
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
if (customer.getId() == null || customer.getId().intValue() == 0) // [laliluna]
// 06.12.2004
// create
// customer
session.save(customer);
else {
Customer toBeUpdated = (Customer) session.get(Customer.class,
customer.getId());
toBeUpdated.setAge(customer.getAge());
toBeUpdated.setLastname(customer.getLastname());
toBeUpdated.setFirstname(customer.getFirstname());
session.update(toBeUpdated);
}
tx.commit();
}
/**
* deletes a customer from the database
*
* @param primaryKey
*/
public void removeCustomerByPrimaryKey(Integer primaryKey) {
/* a Hibernate session */
Session session = null;
/* we always need a transaction */
Transaction tx = null;
/* get session of the current thread */
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
Customer customer = (Customer) session.get(Customer.class, primaryKey);
if (customer != null) {
Set books = customer.getBooks();
for (Iterator iter = books.iterator(); iter.hasNext();) {
Book element = (Book) iter.next();
element.setCustomer(null);
}
session.delete(customer);
}
tx.commit();
}
} |
Partager