Java tutorial
/* * 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.Models.Account; import LibData.Models.Book; import LibData.Models.OrderLine; import LibData.Models.Orders; import LibData.Models.Product; import LibData.Providers.AccountProvider; import LibData.Providers.BookProvider; import LibData.Providers.InventoryProvider; import LibData.Providers.OrdersProvider; import LibData.Providers.ProductProvider; import static LimitedSolution.Utilities.CurrencyHelper.VNDToInt; import static LimitedSolution.Utilities.DateTimeHelper.getCurrentDateString; import static LimitedSolution.Utilities.DateTimeHelper.getCurrentTimeString; import static LimitedSolution.Utilities.JTableHelper.TableColumnAdjuster; import MainPackage.Models.Book.BookTableModel; import MainPackage.Views.Book.BooksFrame; import MainPackage.Views.Orders.NewOrderFrame; import java.util.List; import javax.swing.JOptionPane; import MainPackage.Models.Orders.NewOrderTableModel; import MainPackage.Models.Orders.NewOrderViewModel; import MainPackage.Models.Orders.OrdersTableModel; import MainPackage.Views.Orders.OrderInformationsFrame; import MainPackage.Views.Orders.OrdersFrame; import javax.persistence.criteria.Order; import static LimitedSolution.Utilities.JTableHelper.TableColumnAdjuster; import MainPackage.Models.Book.BookViewModel; import java.awt.Desktop; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; 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 static LimitedSolution.Utilities.DateTimeHelper.*; import static LimitedSolution.Utilities.CurrencyHelper.*; import java.awt.Frame; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc; /** * * @author Limited */ public class OrderController { private BookProvider _bookProvider = new BookProvider(); private InventoryProvider _inventoryProvider = new InventoryProvider(); private ProductProvider _productProvider = new ProductProvider(); private OrdersProvider _ordersProvider = new OrdersProvider(); private AccountProvider _accountProvider = new AccountProvider(); private static boolean CheckQuantityEnter(String quantity) { try { int Quantity = Integer.parseInt(quantity); return (Quantity > 0); } catch (Exception e) { return false; } } public void ShowBooksTable(NewOrderFrame frame) { BookTableModel model = new BookTableModel(_bookProvider.getAll()); _inventoryProvider.addInventoryInformation(model); frame.booksTable.setModel(model); TableColumnAdjuster(frame.booksTable, 30); } public void Find(NewOrderFrame frame, String text) { List<Book> list = _bookProvider.Find(text); BookTableModel model = new BookTableModel(list); _inventoryProvider.addInventoryInformation(model); frame.booksTable.setModel(model); TableColumnAdjuster(frame.booksTable, 30); JOptionPane.showMessageDialog(frame, "Tm thy " + list.size() + " kt qu", "Tm kim Sch", JOptionPane.INFORMATION_MESSAGE); } public void AddOrderLineToTable(NewOrderFrame frame, Product product, String quantity) { Integer Quantity = VNDToInt(quantity); if (!(Quantity != null && Quantity >= 1 && Quantity <= 100)) { JOptionPane.showMessageDialog(frame, "S lng khng hp l.", "Thm sn phm vo ha n tht bi", JOptionPane.WARNING_MESSAGE); return; } Integer InStock = _inventoryProvider.getInStockByProductId(product.getId()); Integer newQuantity = Quantity; NewOrderTableModel model = (NewOrderTableModel) frame.orderLinesTable.getModel(); if (model.contains(product.getIdCode())) { newQuantity = Quantity + model.getOrderLineByProductId(product.getId()).getQuantity(); } if (InStock.compareTo(newQuantity) < 0) { JOptionPane.showMessageDialog(frame, "Khng s lng hng yu cu." + '\n' + "Tng Yu cu: " + newQuantity + '\n' + "Tn kho: " + InStock, "Thm sn phm vo ha n tht bi", JOptionPane.WARNING_MESSAGE); return; } if (product.getPrice() == null) { JOptionPane.showMessageDialog(frame, "Sn phm cha c thit lp gi.", "Thm sn phm vo ha n tht bi", JOptionPane.WARNING_MESSAGE); return; } OrderLine line = new OrderLine(); line.setProductId(product); line.setQuantity(Quantity); line.setUnitPrice(product.getPrice().longValue()); line.setTotalPrice(line.getQuantity() * line.getUnitPrice()); line.setDiscountPrice(0); line.setVATPrice((long) (0.1 * line.getTotalPrice())); line.setPaidPrice(line.getTotalPrice()); model.fireTableDataChanged(line); TableColumnAdjuster(frame.orderLinesTable, 30); } public void UpdateOrderLineToOrderLinesTable(NewOrderFrame frame, OrderLine orderLine, String quantity) { Integer Quantity = VNDToInt(quantity); if (!(Quantity != null && Quantity >= 1 && Quantity <= 100)) { JOptionPane.showMessageDialog(frame, "S lng khng hp l.", "Cp nht ha n tht bi", JOptionPane.WARNING_MESSAGE); return; } Integer InStock = _inventoryProvider.getInStockByProductId(orderLine.getProductId().getId()); if (InStock.compareTo(Quantity) < 0) { JOptionPane.showMessageDialog(frame, "Khng s lng hng yu cu." + '\n' + "Tng Yu cu: " + Quantity + '\n' + "Tn kho: " + InStock, "Cp nht ha n tht bi", JOptionPane.WARNING_MESSAGE); return; } orderLine.setQuantity(Quantity); orderLine.setTotalPrice(orderLine.getQuantity() * orderLine.getUnitPrice()); orderLine.setDiscountPrice(0); orderLine.setVATPrice((long) (0.1 * orderLine.getTotalPrice())); ((NewOrderTableModel) frame.orderLinesTable.getModel()).fireTableDataChanged(); TableColumnAdjuster(frame.orderLinesTable, 30); } public void DeleteOrderLineFromOrderLinesTable(NewOrderFrame frame, OrderLine orderLine) { NewOrderTableModel model = (NewOrderTableModel) frame.orderLinesTable.getModel(); model.removeLine(orderLine); model.fireTableDataChanged(); TableColumnAdjuster(frame.orderLinesTable, 30); } public boolean MakeOrder(NewOrderFrame frame, NewOrderViewModel model, Account _account) { if (!model.IsValidate()) { JOptionPane.showMessageDialog(frame, model.MessageValidate(), "D liu khng hp l", JOptionPane.WARNING_MESSAGE); return false; } if (JOptionPane.showConfirmDialog(frame, "Bn chc chn mun lp ha n?" + '\n' + "Tng ti?n thanh ton: " + model.PaidPrice, "Lp ha n", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) { Orders order = model.getOrders(); order.setCreateBy(_account); if (_ordersProvider.Insert(order)) { JOptionPane.showMessageDialog(frame, "Lp ha n thnh cng", "Lp ha n", JOptionPane.INFORMATION_MESSAGE); OrderInformationsFrame orderInformationFrame = new OrderInformationsFrame( _ordersProvider.getById(order.getId()), _account); orderInformationFrame.show(); return true; } JOptionPane.showMessageDialog(frame, "Lp ha n tht bi", "Lp ha n", JOptionPane.ERROR_MESSAGE); return false; } return false; } public void ShowOrdersTable(OrdersFrame frame) { OrdersTableModel model = new OrdersTableModel(_ordersProvider.getAll()); frame.ordersTable.setModel(model); TableColumnAdjuster(frame.ordersTable, 30); } public void FindOrders(OrdersFrame frame, String text) { List<Orders> list = _ordersProvider.Find(text); OrdersTableModel model = new OrdersTableModel(list); frame.ordersTable.setModel(model); TableColumnAdjuster(frame.ordersTable, 30); JOptionPane.showMessageDialog(frame, "Tm thy " + list.size() + " kt qu", "Tm kim Ha n", JOptionPane.INFORMATION_MESSAGE); } public void Print(Frame frame, Orders order, Account _account) { try { XWPFDocument document = new XWPFDocument(); File file = new File("Ha n " + order.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, "CHI TIT HA ?N"); run.setFontSize(24); paragraph = createPrintInformation(document, _account); paragraph = createBookProductInformation(document, order); paragraph = createBookInformation(document, order); //create table XWPFTable table = document.createTable(); setTableAlignment(table, STJc.CENTER); table.setCellMargins(50, 50, 50, 50); table.setInsideHBorder(XWPFTable.XWPFBorderType.SINGLE, 10, 10, ""); table.setInsideVBorder(XWPFTable.XWPFBorderType.NONE, 20, 20, ""); //create first row XWPFTableRow row = table.getRow(0); row.setHeight(40); row.getCell(0).setText("STT"); row.addNewTableCell().setText("M Sn phm"); row.addNewTableCell().setText("Tn Sn phm"); row.addNewTableCell().setText("?n v"); row.addNewTableCell().setText("S lng"); row.addNewTableCell().setText("Gi ti?n"); row.addNewTableCell().setText("Thnh ti?n"); List<OrderLine> list = (List<OrderLine>) order.getOrderLineCollection(); for (int i = 0; i < list.size(); i++) { OrderLine line = list.get(i); row = table.createRow(); row.getCell(0).setText((i + 1) + ""); row.getCell(1).setText(line.getProductId().getIdCode()); row.getCell(2).setText(line.getProductId().getName()); row.getCell(3).setText("Quyn "); row.getCell(4).setText(line.getQuantity() + " "); row.getCell(5).setText(IntToVND(line.getUnitPrice()) + " "); row.getCell(6).setText(IntToVND(line.getTotalPrice()) + " "); } document.write(out); out.close(); if (Desktop.isDesktopSupported()) { Desktop.getDesktop().open(file); } JOptionPane .showMessageDialog(frame, "Xut file " + file.getName() + " thnh cng" + '\n' + "Ti v tr: " + file.getAbsolutePath(), "In thng tin Ha n", JOptionPane.INFORMATION_MESSAGE); } catch (Exception e) { System.out.println(e); JOptionPane.showMessageDialog(frame, "Xut file tht bi." + '\n' + "Vui lng ng ca s ang s dng file", "In thng tin Ha n", 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, Orders order) { XWPFParagraph paragraph = document.createParagraph(); paragraph.setAlignment(ParagraphAlignment.LEFT); XWPFRun run = createFieldRun(paragraph, "Thng tin Ha n: "); run.setFontSize(16); run.addCarriageReturn(); ArrayList<BookController.PrintRunField> productInformations = new ArrayList<BookController.PrintRunField>() { { add(new BookController.PrintRunField("M Ha n: ", order.getIdCode())); add(new BookController.PrintRunField("Ti khon lp: ", order.getCreateBy().getUsername())); add(new BookController.PrintRunField("Th?i gian lp: ", getDateTimeString(order.getCreateTime()))); add(new BookController.PrintRunField(" ", "")); add(new BookController.PrintRunField("Tng ti?n: ", IntToVND(order.getTotalPrice()))); add(new BookController.PrintRunField("Thu VAT: ", IntToVND(order.getVATPrice()))); add(new BookController.PrintRunField("Khuyn mi: ", IntToVND(order.getDiscount()))); add(new BookController.PrintRunField("Thanh ton: ", IntToVND(order.getPaidPrice()))); } }; for (int i = 0; i < productInformations.size(); i++) { run = createFieldRun(paragraph, productInformations.get(i).Field); run.addTab(); run = createValueRun(paragraph, productInformations.get(i).Value); run.addCarriageReturn(); } return paragraph; } private XWPFParagraph createBookInformation(XWPFDocument document, Orders order) { XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = createFieldRun(paragraph, "Thng tin Khch hng: "); run.setFontSize(16); run.addCarriageReturn(); ArrayList<BookController.PrintRunField> bookInformations = new ArrayList<BookController.PrintRunField>() { { add(new BookController.PrintRunField("Tn khch: ", order.getGuestName())); add(new BookController.PrintRunField("?a ch: ", order.getGuestPhone())); add(new BookController.PrintRunField("?in thoi: ", order.getGuestPhone())); add(new BookController.PrintRunField("Email: ", order.getGuestEmail())); add(new BookController.PrintRunField(" ", "")); add(new BookController.PrintRunField("Ghi ch: ", order.getDetails())); } }; for (BookController.PrintRunField bookInformation : bookInformations) { run = createFieldRun(paragraph, bookInformation.Field); run.addTab(); run = createValueRun(paragraph, bookInformation.Value); run.addCarriageReturn(); } return paragraph; } public void setTableAlignment(XWPFTable table, STJc.Enum justification) { CTTblPr tblPr = table.getCTTbl().getTblPr(); CTJc jc = (tblPr.isSetJc() ? tblPr.getJc() : tblPr.addNewJc()); jc.setVal(justification); } }