package cz.muni.fi.pa165.library.backend;
import cz.muni.fi.pa165.library.api.BookTO;
import cz.muni.fi.pa165.library.api.BorrowTO;
import cz.muni.fi.pa165.library.api.ReaderTO;
import cz.muni.fi.pa165.library.api.ReservationTO;
import cz.muni.fi.pa165.library.api.UserTO;
import java.util.ArrayList;
import java.util.List;
/**
* Class contains static methods for purpose of converting TO to entity and
* entity to TO.
*
* @author Marian Labuda
*/
public class DTOConvertor {
public static BookTO convertBookEntityToTO(Book book) {
if (book == null) {
return null;
}
BookTO bookTO = new BookTO(book.getTitle(), book.getAuthor(), book.getGenre(), book.getPublicationYear(), book.getPublicationPlace(), book.getPublisher(), book.getISBN(), book.getState(), book.getAvailability());
bookTO.setId(book.getId());
return bookTO;
}
public static Book convertBookTOToEntity(BookTO bookTO) {
if (bookTO == null) {
return null;
}
Book book = new Book(bookTO.getTitle(), bookTO.getAuthor(), bookTO.getGenre(), bookTO.getPublicationYear(), bookTO.getPublicationPlace(), bookTO.getPublisher(), bookTO.getISBN(), bookTO.getStatus(), bookTO.getAvailability());
book.setId(bookTO.getId());
return book;
}
public static List<BookTO> convertBookEntityListToTOList(List<Book> bookList) {
if (bookList == null) {
return null;
}
List<BookTO> bookTOList = new ArrayList<BookTO>();
for (Book book : bookList) {
bookTOList.add(DTOConvertor.convertBookEntityToTO(book));
}
return bookTOList;
}
public static List<Book> convertBookTOListToEntityList(List<BookTO> bookTOList) {
if (bookTOList == null) {
return null;
}
List<Book> bookList = new ArrayList<Book>();
for (BookTO bookTO : bookTOList) {
bookList.add(DTOConvertor.convertBookTOToEntity(bookTO));
}
return bookList;
}
public static BorrowTO convertBorrowEntityToTO(Borrow borrow) {
if (borrow == null) {
return null;
}
List<BookTO> titleTOList = new ArrayList<BookTO>();
if (borrow.getTitles() != null) {
for (Book book : borrow.getTitles()) {
titleTOList.add(DTOConvertor.convertBookEntityToTO(book));
}
}
BorrowTO borrowTO = new BorrowTO(DTOConvertor.convertReaderEntityToTO(borrow.getReader()),
titleTOList, borrow.getBorrowDate(), borrow.getExpirationDate());
borrowTO.setBorrowID(borrow.getBorrowID());
return borrowTO;
}
public static Borrow convertBorrowTOToEntity(BorrowTO borrowTO) {
if (borrowTO == null) {
return null;
}
List<Book> titleList = new ArrayList<Book>();
if (borrowTO.getTitlesTO() != null) {
for (BookTO bookTO : borrowTO.getTitlesTO()) {
titleList.add(DTOConvertor.convertBookTOToEntity(bookTO));
}
}
Borrow borrow;
borrow = new Borrow(DTOConvertor.convertReaderTOToEntity(borrowTO.getReaderTO()),
titleList, borrowTO.getBorrowDate(), borrowTO.getExpirationDate());
borrow.setBorrowID(borrowTO.getBorrowID());
return borrow;
}
public static List<BorrowTO> convertBorrowEntityListToTOList(List<Borrow> borrowList) {
if (borrowList == null) {
return null;
}
List<BorrowTO> borrowTOList = new ArrayList<BorrowTO>();
for (Borrow borrow : borrowList) {
borrowTOList.add(DTOConvertor.convertBorrowEntityToTO(borrow));
}
return borrowTOList;
}
public static List<Borrow> convertBorrowTOListToEntityList(List<BorrowTO> borrowTOList) {
if (borrowTOList == null) {
return null;
}
List<Borrow> borrowList = new ArrayList<Borrow>();
for (BorrowTO borrowTO : borrowTOList) {
borrowList.add(DTOConvertor.convertBorrowTOToEntity(borrowTO));
}
return borrowList;
}
public static ReaderTO convertReaderEntityToTO(Reader reader) {
if (reader == null) {
return null;
}
ReaderTO readerTO = new ReaderTO(reader.getFirstName(), reader.getSurname(), reader.getBirthNumber(),
reader.getAddress(), reader.getEmail(), reader.getTelephoneNumber(), reader.getPassword());
readerTO.setId(reader.getId());
return readerTO;
}
public static Reader convertReaderTOToEntity(ReaderTO readerTO) {
if (readerTO == null) {
return null;
}
Reader reader = new Reader(readerTO.getFirstName(), readerTO.getSurname(), readerTO.getBirthNumber(),
readerTO.getAddress(), readerTO.getEmail(), readerTO.getTelephoneNumber(), readerTO.getPassword());
reader.setId(readerTO.getId());
return reader;
}
public static List<ReaderTO> convertReaderEntityListToTOList(List<Reader> readers) {
if (readers == null) {
return null;
}
List<ReaderTO> result = new ArrayList<ReaderTO>();
for (Reader reader : readers) {
result.add(DTOConvertor.convertReaderEntityToTO(reader));
}
return result;
}
public static ReservationTO convertReservationEntityToTO(Reservation reservation) {
if (reservation == null) {
return null;
}
ReservationTO reservationTO = new ReservationTO(DTOConvertor.convertReaderEntityToTO(reservation.getReader()),
DTOConvertor.convertBookEntityToTO(reservation.getBook()), reservation.getReservationDate());
reservationTO.setReservationID(reservation.getReservationID());
return reservationTO;
}
public static Reservation convertReservationTOToEntity(ReservationTO reservationTO) {
if (reservationTO == null) {
return null;
}
Reservation reservation = new Reservation(DTOConvertor.convertReaderTOToEntity(reservationTO.getReaderTO()),
DTOConvertor.convertBookTOToEntity(reservationTO.getBookTO()), reservationTO.getReservationDate());
reservation.setID(reservationTO.getReservationID());
return reservation;
}
public static List<ReservationTO> convertReservationEntityListToTOList(List<Reservation> reservationList) {
if (reservationList == null) {
return null;
}
List<ReservationTO> reservationTOList = new ArrayList<ReservationTO>();
for (Reservation reservation : reservationList) {
reservationTOList.add(DTOConvertor.convertReservationEntityToTO(reservation));
}
return reservationTOList;
}
public static List<Reservation> convertReservationTOListToEntityList(List<ReservationTO> reservationTOList) {
if (reservationTOList == null) {
return null;
}
List<Reservation> reservationList = new ArrayList<Reservation>();
for (ReservationTO reservationTO : reservationTOList) {
reservationList.add(DTOConvertor.convertReservationTOToEntity(reservationTO));
}
return reservationList;
}
public static UserTO convertUserEntityToTO(User user) {
if (user == null) {
return null;
}
UserTO userTO = new UserTO(user.getEmail(), user.getPassword(), user.getFirstName(), user.getLastName());
userTO.setUserID(user.getUserID());
return userTO;
}
public static User convertUserTOToEntity(UserTO userTO) {
if (userTO == null) {
return null;
}
User user = new User(userTO.getEmail(), userTO.getPassword(), userTO.getFirstName(), userTO.getLastName());
user.setUserID(userTO.getUserID());
return user;
}
public static List<UserTO> convertUserEntityListToTOList(List<User> userList) {
if (userList == null) {
return null;
}
List<UserTO> userTOList = new ArrayList<UserTO>();
for (User user : userList) {
userTOList.add(DTOConvertor.convertUserEntityToTO(user));
}
return userTOList;
}
public static List<User> convertUserTOListToEntityList(List<UserTO> userTOList) {
if (userTOList == null) {
return null;
}
List<User> userList = new ArrayList<User>();
for (UserTO userTO : userTOList) {
userList.add(DTOConvertor.convertUserTOToEntity(userTO));
}
return userList;
}
}