MainPackage.Controllers.BookController.java Source code

Java tutorial

Introduction

Here is the source code for MainPackage.Controllers.BookController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MainPackage.Controllers;

import LibData.Business.Configs.BookConfigs;
import LibData.Models.Account;
import LibData.Models.Book;
import LibData.Models.Product;
import LibData.Providers.AccountProvider;
import LibData.Providers.BookProvider;
import LibData.Providers.InventoryProvider;
import LibData.Providers.ProductProvider;
import static LimitedSolution.Utilities.DateTimeHelper.getCurrentDateString;
import static LimitedSolution.Utilities.DateTimeHelper.getCurrentDateTimeString;
import static LimitedSolution.Utilities.DateTimeHelper.getCurrentTimeString;
import static LimitedSolution.Utilities.DateTimeHelper.getDateTimeString;
import static LimitedSolution.Utilities.JTableHelper.TableColumnAdjuster;
import MainPackage.Models.Book.AddBookModel;
import MainPackage.Models.Book.BookTableModel;
import MainPackage.Models.Book.BookTableModel.BookTableColumnModel;
import MainPackage.Models.Book.BookViewModel;
import MainPackage.Models.Book.UpdateBookModel;
import MainPackage.Views.Book.AddBookDialog;
import MainPackage.Views.Book.BooksFrame;
import MainPackage.Views.Book.UpdateBookDialog;
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
//import org.apache.poi.ss.usermodel.Workbook;

/**
 *
 * @author Limited
 */
public class BookController {

    private BookProvider _bookProvider = new BookProvider();
    private AccountProvider _accountProvider = new AccountProvider();
    private InventoryProvider _inventoryProvider = new InventoryProvider();

    public void ShowBooksTable(BooksFrame booksFrame) {
        BookTableModel model = new BookTableModel(_bookProvider.getAll());
        _inventoryProvider.addInventoryInformation(model);

        booksFrame.booksTable.setModel(model);
        TableColumnAdjuster(booksFrame.booksTable, 30);
    }

    public void AddBook(BooksFrame booksFrame, Account account) {
        AddBookDialog addBookDialog = new AddBookDialog(booksFrame, true, account);
        addBookDialog.show();
        ShowBooksTable(booksFrame);
    }

    public boolean AddBook(AddBookModel model) {
        if (!model.IsValidate()) {
            JOptionPane.showMessageDialog(null, model.MessageValidate(), "Thng tin khng hp l",
                    JOptionPane.WARNING_MESSAGE);

            return false;
        }

        Book book = model.getBook();
        book.setStatus(BookConfigs.BOOK_STATUS_ACTIVE);
        book.setCreatedBy(_accountProvider.getById(model.CreatedBy));

        if (_bookProvider.Insert(book)) {
            JOptionPane.showMessageDialog(null, "Thm sch " + model.Name + " thnh cng", "Thm sch",
                    JOptionPane.INFORMATION_MESSAGE);
            return true;
        }

        JOptionPane.showMessageDialog(null, "Thm sch " + model.Name + " tht bi", "Thm sch",
                JOptionPane.ERROR_MESSAGE);
        return false;
    }

    public void ViewBook(BooksFrame booksFrame, Book book, Account account) {
        UpdateBookDialog dialog = new UpdateBookDialog(booksFrame, true, new UpdateBookModel(book), account);
        dialog.show();
        ShowBooksTable(booksFrame);
    }

    public boolean UpdateBook(UpdateBookModel model) {
        if (!model.IsValidate()) {
            JOptionPane.showMessageDialog(null, model.MessageValidate(), "Thng tin khng hp l",
                    JOptionPane.WARNING_MESSAGE);

            return false;
        }

        Book book = model.getBook();

        if (_bookProvider.Update(book)) {
            JOptionPane.showMessageDialog(null, "Cp nht thng tin sch " + model.Name + " thnh cng",
                    "Cp nht thng tin sch", JOptionPane.INFORMATION_MESSAGE);
            return true;
        }

        JOptionPane.showMessageDialog(null, "Cp nht thng tin sch " + model.Name + " tht bi",
                "Cp nht thng tin sch", JOptionPane.ERROR_MESSAGE);
        return false;
    }

    public boolean DeleteBook(BooksFrame booksFrame, Book book, Account _account) {
        if (JOptionPane.showConfirmDialog(booksFrame,
                "Bn chc chn mun xa sch" + '\n' + book.getName() + "?", "Xa sch",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {

            if (_bookProvider.Delete(book.getId())) {

                JOptionPane.showMessageDialog(booksFrame, "Xa sch " + book.getName() + " thnh cng",
                        "Xa sch", JOptionPane.INFORMATION_MESSAGE);
                return true;

            }

            JOptionPane.showMessageDialog(booksFrame, "Xa sch " + book.getName() + " tht bi",
                    "Xa sch", JOptionPane.ERROR_MESSAGE);
            return false;

        }

        return true;
    }

    public void Find(BooksFrame booksFrame, String text) {
        List<Book> list = _bookProvider.Find(text);
        BookTableModel model = new BookTableModel(list);
        _inventoryProvider.addInventoryInformation(model);

        booksFrame.booksTable.setModel(model);
        TableColumnAdjuster(booksFrame.booksTable, 30);
        JOptionPane.showMessageDialog(booksFrame, "Tm thy " + list.size() + " kt qu",
                "Tm kim Sch", JOptionPane.INFORMATION_MESSAGE);
    }

    public BookViewModel GetBookInformations(Book selectedBook) {
        BookViewModel model = new BookViewModel(selectedBook);

        Integer inStock = _inventoryProvider.getInStockByProductId(selectedBook.getProductId());
        Integer in = _inventoryProvider.getInQuantitiesByProductId(selectedBook.getProductId());
        Integer out = _inventoryProvider.getOutQuantitiesByProductId(selectedBook.getProductId());

        model.InventoryInStock = inStock != null ? inStock + "" : "0";
        model.InventoryIn = in != null ? in + "" : "0";
        model.InventoryOut = out != null ? out + "" : "0";

        return model;
    }

    public static class PrintRunField {

        public String Field;
        public String Value;

        public PrintRunField(String Field, String Value) {
            this.Field = Field;
            this.Value = Value;
        }

    }

    public void PrintBook(BooksFrame booksFrame, Book book, Account _account) {
        try {
            XWPFDocument document = new XWPFDocument();
            File file = new File("Sch " + book.getIdCode() + ".doc");
            if (file.exists()) {
                file.createNewFile();
            }

            FileOutputStream out = new FileOutputStream(file);

            XWPFParagraph paragraph = document.createParagraph();
            paragraph.setAlignment(ParagraphAlignment.CENTER);

            XWPFRun run;
            BookViewModel bookView = new BookViewModel(book);

            /////////////////
            run = createFieldRun(paragraph, "THNG TIN S?CH");
            run.setFontSize(24);

            paragraph = createPrintInformation(document, _account);
            paragraph = createBookProductInformation(document, bookView);
            paragraph = createBookInformation(document, bookView);

            document.write(out);
            out.close();

            if (Desktop.isDesktopSupported()) {
                Desktop.getDesktop().open(file);
            }

            JOptionPane
                    .showMessageDialog(booksFrame,
                            "Xut file " + file.getName() + " thnh cng" + '\n' + "Ti v tr: "
                                    + file.getAbsolutePath(),
                            "In thng tin Sch", JOptionPane.INFORMATION_MESSAGE);

        } catch (Exception e) {
            System.out.println(e);

            JOptionPane.showMessageDialog(booksFrame,
                    "Xut file tht bi." + '\n' + "Vui lng ng ca s ang s dng file",
                    "In thng tin Sch", JOptionPane.WARNING_MESSAGE);
        }

    }

    private XWPFRun createValueRun(XWPFParagraph paragraph, String text) {
        XWPFRun run = createRun(paragraph, text);
        run.setBold(false);
        return run;
    }

    private XWPFRun createFieldRun(XWPFParagraph paragraph, String text) {
        XWPFRun run = createRun(paragraph, text);
        run.setBold(true);
        return run;
    }

    private XWPFRun createRun(XWPFParagraph paragraph, String text) {
        XWPFRun run;
        run = paragraph.createRun();
        run.setText(text);
        run.addTab();
        return run;
    }

    private XWPFParagraph createPrintInformation(XWPFDocument document, Account _account) {
        XWPFParagraph paragraph = document.createParagraph();

        paragraph.setAlignment(ParagraphAlignment.LEFT);
        XWPFRun run = createFieldRun(paragraph, "Ngy in: ");
        run.setFontSize(10);
        run = createValueRun(paragraph, getCurrentDateString());
        run.setFontSize(10);
        run.addCarriageReturn();
        run = createFieldRun(paragraph, "Th?i gian in: ");
        run.setFontSize(10);
        run = createValueRun(paragraph, getCurrentTimeString());
        run.setFontSize(10);
        run.addCarriageReturn();
        run = createFieldRun(paragraph, "Ti khon in: ");
        run.setFontSize(10);
        run = createValueRun(paragraph, _accountProvider.getById(_account.getId()).getUsername());
        run.setFontSize(10);
        run.addCarriageReturn();

        return paragraph;
    }

    private XWPFParagraph createBookProductInformation(XWPFDocument document, BookViewModel bookView) {
        XWPFParagraph paragraph = document.createParagraph();

        paragraph.setAlignment(ParagraphAlignment.LEFT);
        XWPFRun run = createFieldRun(paragraph, "Thng tin Sn phm: ");
        run.setFontSize(16);
        run.addCarriageReturn();

        ArrayList<PrintRunField> productInformations = new ArrayList<PrintRunField>() {
            {
                add(new PrintRunField("M sch: ", bookView.IdCode));
                add(new PrintRunField("M ISBN: ", bookView.ISBN));
                add(new PrintRunField("Ti khon to: ", bookView.CreatedBy));
                add(new PrintRunField("Th?i gian to: ", bookView.CreateTime));
                add(new PrintRunField("Gi sch: ", bookView.Price));
                add(new PrintRunField("Tnh trng: ", bookView.Status));
            }
        };

        for (int i = 0; i < productInformations.size(); i++) {

            run = createFieldRun(paragraph, productInformations.get(i).Field);
            run.addTab();
            run = createValueRun(paragraph, productInformations.get(i).Value);
            if (i % 2 == 0) {
                run.addTab();
            } else {
                run.addCarriageReturn();
            }
        }

        return paragraph;
    }

    private XWPFParagraph createBookInformation(XWPFDocument document, BookViewModel bookView) {
        XWPFParagraph paragraph = document.createParagraph();

        XWPFRun run = createFieldRun(paragraph, "Thng tin Sch: ");
        run.setFontSize(16);
        run.addCarriageReturn();

        ArrayList<PrintRunField> bookInformations = new ArrayList<PrintRunField>() {
            {
                add(new PrintRunField("Tn sch: ", bookView.Name));
                add(new PrintRunField("Tc gi: ", bookView.Author));
                add(new PrintRunField("Nh xut bn: ", bookView.Publisher));
                add(new PrintRunField("Th loi: ", bookView.Type));
                add(new PrintRunField("Pht hnh: ", bookView.PublishMonth + "/" + bookView.PublishYear));
                add(new PrintRunField("M t:      ", bookView.Details));
            }
        };

        for (PrintRunField bookInformation : bookInformations) {
            run = createFieldRun(paragraph, bookInformation.Field);
            run.addTab();
            run = createValueRun(paragraph, bookInformation.Value);
            run.addCarriageReturn();
        }

        return paragraph;
    }
}