com.zhu.action.CarAction.java Source code

Java tutorial

Introduction

Here is the source code for com.zhu.action.CarAction.java

Source

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.zhu.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.zhu.entity.Car;
import com.zhu.entity.Orderinfo;
import com.zhu.form.CarForm;
import com.zhu.service.CarService;
import com.zhu.service.OrderService;
import com.zhu.util.UUID;

/**
 * MyEclipse Struts Creation date: 05-23-2015
 * 
 * XDoclet definition:
 * 
 * @struts.action parameter="m" scope="request" validate="true"
 */
public class CarAction extends DispatchAction {
    /*
     * Generated Methods
     */

    /**
     * Method execute
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) {
        HttpSession session = request.getSession();
        if (session.getAttribute("admin") == null) {
            return mapping.findForward("wrong");
        } else {
            CarService carService = new CarService();
            List<Car> car = carService.getCar();
            request.setAttribute("car", car);
            OrderService orderService = new OrderService();
            int back = orderService.getOrderCount();
            request.setAttribute("count", back);
            return mapping.findForward("list");
        }
    }

    public ActionForward addcar(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) {
        HttpSession session = request.getSession();
        if (session.getAttribute("admin") == null) {
            return mapping.findForward("wrong");
        } else {
            OrderService orderService = new OrderService();
            int back = orderService.getOrderCount();
            request.setAttribute("count", back);
            return mapping.findForward("add");
        }
    }

    public void getcarbyid(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        CarForm carForm = (CarForm) form;
        CarService carService = new CarService();
        PrintWriter out = response.getWriter();
        Car car = (Car) carService.getCarById(carForm.getId());
        String json = new JSONArray().fromObject(car).toString();
        log.info(json);
        out.print(json);
        out.flush();
        out.close();
    }

    // ?
    public void exportpeople(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) {
        CarForm carForm = (CarForm) form;

        // webbookExcel
        HSSFWorkbook wb = new HSSFWorkbook();
        // webbooksheet,Excelsheet
        HSSFSheet sheet = wb.createSheet("?");
        // sheet0,??poiExcel?short
        HSSFRow row = sheet.createRow((int) 0);
        // ? 
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ?
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("??");
        cell.setCellStyle(style);
        cell = row.createCell(1);
        cell.setCellValue("???");
        cell.setCellStyle(style);
        // ? ??
        OrderService orderService = new OrderService();
        List<Orderinfo> list = orderService.getOrderCarDetail(carForm.getId());
        System.out.println(list.size());
        for (int i = 0; i < list.size(); i++) {
            row = sheet.createRow(i + 1);
            Orderinfo order = (Orderinfo) list.get(i);
            // ?
            row.createCell(0).setCellValue(order.getN1());
            row.createCell(1).setCellValue(order.getI1());
            row = sheet.createRow(i + list.size() + 1);
            row.createCell(0).setCellValue(order.getN2());
            row.createCell(1).setCellValue(order.getI2());
            row = sheet.createRow(i + list.size() + list.size() + 1);
            row.createCell(0).setCellValue(order.getN3());
            row.createCell(1).setCellValue(order.getI3());
            row = sheet.createRow(i + list.size() + list.size() + list.size() + 1);
            row.createCell(0).setCellValue(order.getN4());
            row.createCell(1).setCellValue(order.getI4());
            row = sheet.createRow(i + list.size() + list.size() + list.size() + list.size() + 1);
            row.createCell(0).setCellValue(order.getN5());
            row.createCell(1).setCellValue(order.getI5());
            row = sheet.createRow(i + list.size() + list.size() + list.size() + list.size() + list.size() + 1);
            row.createCell(0).setCellValue(order.getN6());
            row.createCell(1).setCellValue(order.getI6());
            row = sheet.createRow(
                    i + list.size() + list.size() + list.size() + list.size() + list.size() + list.size() + 1);
            row.createCell(0).setCellValue(order.getN7());
            row.createCell(1).setCellValue(order.getI7());
            row = sheet.createRow(i + list.size() + list.size() + list.size() + list.size() + list.size()
                    + list.size() + list.size() + 1);
            row.createCell(0).setCellValue(order.getN8());
            row.createCell(1).setCellValue(order.getI8());
            row = sheet.createRow(i + list.size() + list.size() + list.size() + list.size() + list.size()
                    + list.size() + list.size() + list.size() + 1);
            row.createCell(0).setCellValue(order.getN9());
            row.createCell(1).setCellValue(order.getI9());
            row = sheet.createRow(i + list.size() + list.size() + list.size() + list.size() + list.size()
                    + list.size() + list.size() + list.size() + list.size() + 1);
            row.createCell(0).setCellValue(order.getN10());
            row.createCell(1).setCellValue(order.getI10());
        }
        // ?
        String filePath = "";
        Date dt = new Date();
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String date = df.format(dt).toString();
        filePath = "/Users/Nemo/Documents/carpeople" + date + ".xls";
        File file = new File(filePath);

        try {
            // FileOutputStream fout = new FileOutputStream(
            // "/Users/Nemo/Documents/carpeople.xls");
            // wb.write(fout);
            // fout.close();
            OutputStream out = new FileOutputStream(file);
            wb.write(out);
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        int key = 0;
        int MaxRowNum = 0, MaxCellNum = 0;
        try {
            FileInputStream in = new FileInputStream(filePath);
            POIFSFileSystem fs = new POIFSFileSystem(in);
            HSSFWorkbook workbook = new HSSFWorkbook(fs);
            FileOutputStream out = new FileOutputStream("/Users/Nemo/Documents/carpeople" + date + ".xls");
            int number = workbook.getNumberOfSheets();
            for (int i = 0; i < number; i++) { // ?sheet
                sheet = workbook.getSheetAt(i); // 14
                System.out.println("" + sheet.getSheetName() + " ? "
                        + (sheet.getLastRowNum() + 1));
                MaxRowNum = 0;
                for (int k = 0; k <= sheet.getLastRowNum(); k++) {
                    HSSFRow hRow = sheet.getRow(k);
                    // System.out.println((k + 1) + "");
                    if (isBlankRow(hRow)) // 
                    {
                        int m = 0;
                        for (m = k + 1; m <= sheet.getLastRowNum(); m++) {
                            HSSFRow nhRow = sheet.getRow(m);
                            if (!isBlankRow(nhRow)) {
                                // System.out.println("?" + (m + 1));
                                sheet.shiftRows(m, sheet.getLastRowNum(), k - m);
                                break;
                            }
                        }
                        if (m > sheet.getLastRowNum())
                            break; // ?
                    } else { // ?
                        MaxRowNum++;
                        if (MaxCellNum < hRow.getLastCellNum())
                            MaxCellNum = hRow.getLastCellNum();
                    }
                }
                workbook.setPrintArea(i, 0, MaxCellNum, 0, MaxRowNum);
                System.out
                        .println("?? " + sheet.getSheetName() + "  " + MaxRowNum);
            }
            workbook.write(out);
            in.close();
            out.close();
        } catch (IOException e) {
            System.out.println(key + " " + e.getMessage() + " ");
            e.printStackTrace();

        }

        System.out.println("??");

    }

    /**
     * excel 
     */
    public boolean isBlankRow(HSSFRow row) {
        if (row == null)
            return true;
        for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
            HSSFCell hcell = row.getCell(i);
            if (!isBlankCell(hcell))
                return false;
        }
        return true;
    }

    /**
     * excel ? /
     * 
     * @param hcell
     * @return
     */
    public boolean isBlankCell(HSSFCell hcell) {
        if (hcell == null)
            return true;
        hcell.setCellType(hcell.CELL_TYPE_STRING);
        String content = hcell.getStringCellValue().trim();
        if (content == null || "".equals(content)) // ?
        {
            return true;
        }
        return false;
    }

    // 
    public void export(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) {
        // webbookExcel
        HSSFWorkbook wb = new HSSFWorkbook();
        // webbooksheet,Excelsheet
        HSSFSheet sheet = wb.createSheet("?");
        // sheet0,??poiExcel?short
        HSSFRow row = sheet.createRow((int) 0);
        // ? 
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ?
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("id");
        cell.setCellStyle(style);
        cell = row.createCell(1);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell(2);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell(3);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell(4);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell(5);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell(6);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell(7);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell(8);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        // ? ??
        CarService carService = new CarService();
        List<Car> list = carService.getCar();

        for (int i = 0; i < list.size(); i++) {
            row = sheet.createRow((int) i + 1);
            Car car = (Car) list.get(i);
            // ?
            row.createCell(0).setCellValue(car.getId());
            row.createCell(1).setCellValue(car.getCarid());
            row.createCell(2).setCellValue(car.getLicense());
            row.createCell(3).setCellValue(car.getNumber());
            row.createCell(4).setCellValue(car.getPeople());
            row.createCell(5).setCellValue(car.getRoute());
            row.createCell(6).setCellValue(car.getStartdate());
            row.createCell(7).setCellValue(car.getEnddate());
            if (car.getStatus() == 0) {
                row.createCell(8).setCellValue("");
            } else if (car.getStatus() == 1) {
                row.createCell(8).setCellValue("");
            } else {
                row.createCell(8).setCellValue("");
            }
            // cell = row.createCell((short) 3);
        }
        // ?
        String filePath = "";
        Date dt = new Date();
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String date = df.format(dt).toString();
        filePath = "/Users/Nemo/Documents/car" + date + ".xls";
        File file = new File(filePath);
        try {
            FileOutputStream fout = new FileOutputStream(file);
            wb.write(fout);
            fout.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ActionForward getcardetail(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        if (session.getAttribute("admin") == null) {
            return mapping.findForward("wrong");
        } else {
            CarForm carForm = (CarForm) form;
            OrderService orderService = new OrderService();
            List<Orderinfo> list = orderService.getOrderCarDetail(carForm.getId());
            request.setAttribute("list", list);
            request.setAttribute("id", carForm.getId());
            int back = orderService.getOrderCount();
            request.setAttribute("count", back);
            return mapping.findForward("detail");
        }
    }

    public void addcarinfo(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        CarForm carForm = (CarForm) form;
        CarService carService = new CarService();
        PrintWriter out = response.getWriter();
        int back = 0;
        Car car = carService.getCarByCarId(carForm.getCarid());
        if (car == null) {
            car = carService.getCarByLicense(carForm.getLicense());
        }
        if (car != null) {
            if (car.getCarid().equals(carForm.getCarid())) {
                // id??
                back = 2;
                out.print(back);
                out.flush();
                out.close();
            } else if (car.getLicense().equals(carForm.getLicense())) {
                // ??
                back = 3;
                out.print(back);
                out.flush();
                out.close();
            }
        } else {
            car = new Car();
            UUID uuid = new UUID();
            car.setId(uuid.getUUID());
            car.setCarid(carForm.getCarid());
            car.setLicense(carForm.getLicense());
            car.setNumber(carForm.getNumber());
            car.setPeople(0);
            car.setRoute(carForm.getRoute());
            car.setStartdate(carForm.getStartdate());
            car.setEnddate(carForm.getEnddate());
            car.setStatus(0);
            back = carService.addCar(car);
            out.print(back);
            out.flush();
            out.close();
        }
    }

    public ActionForward updatecar(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) {
        HttpSession session = request.getSession();
        if (session.getAttribute("admin") == null) {
            return mapping.findForward("wrong");
        } else {
            CarForm carForm = (CarForm) form;
            CarService carService = new CarService();
            Car car = new Car();
            car = carService.getCarById(carForm.getId());
            request.setAttribute("car", car);
            return mapping.findForward("add");
        }
    }

    public void updatecarinfo(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        CarForm carForm = (CarForm) form;
        CarService carService = new CarService();
        PrintWriter out = response.getWriter();
        int back = 0;
        Car car = carService.getCarById(carForm.getId());
        car.setId(carForm.getId());
        car.setNumber(carForm.getNumber());
        car.setRoute(carForm.getRoute());
        car.setStartdate(carForm.getStartdate());
        car.setEnddate(carForm.getEnddate());
        car.setStatus(0);
        carService.updCar(car);
        out.print(back);
        out.flush();
        out.close();
    }

    public ActionForward delcarbyid(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) {
        HttpSession session = request.getSession();
        if (session.getAttribute("admin") == null) {
            return mapping.findForward("wrong");
        } else {
            CarForm carForm = (CarForm) form;
            CarService carService = new CarService();
            boolean back = carService.delCarById(carForm.getId());
            if (back = true) {
                request.setAttribute("message", "?");
            } else {
                request.setAttribute("message", "");
            }
            // List<Hotspot> hotspotlist = hotspotService.getHotSpot();
            // request.setAttribute("hotspot", hotspotlist);
            return init(mapping, form, request, response);

        }
    }

    public void depart(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        CarForm carForm = (CarForm) form;
        CarService carService = new CarService();
        Car car = carService.getCarById(carForm.getId());
        car.setStatus(1);
        carService.updCar(car);
        PrintWriter out = response.getWriter();
        out.print(1);
        out.flush();
        out.close();
    }

    public void finish(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        CarForm carForm = (CarForm) form;
        CarService carService = new CarService();
        Car car = carService.getCarById(carForm.getId());
        car.setStartdate("");
        car.setEnddate("");
        car.setPeople(0);
        car.setStatus(0);
        carService.updCar(car);
        PrintWriter out = response.getWriter();
        out.print(1);
        out.flush();
        out.close();
    }

    // 
    public void addpassenger(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        CarForm carForm = (CarForm) form;
        CarService carService = new CarService();
        OrderService orderService = new OrderService();
        PrintWriter out = response.getWriter();
        int back = 0;

        if (carForm.getFlag() == 1) {
            // 
            if (carForm.getId() != "") {
                // 
                Car car = carService.getCarById(carForm.getId());
                car.setPeople(car.getPeople() + carForm.getPeople());
                carService.updCar(car);
            }
            // ??
            Orderinfo order = orderService.getOrderById(carForm.getOrderid());
            order.setCarid(carForm.getId());
            order.setStatus(2);
            orderService.updOrder(order);
            out.print(back);
            out.flush();
            out.close();
        } else if (carForm.getFlag() == 2) {
            // 
            Orderinfo order = orderService.getOrderById(carForm.getOrderid());
            // ??,
            if (order.getCarid() != "" && order.getCarid() != null) {
                String id = order.getCarid();
                Car car = carService.getCarById(id);
                car.setPeople(car.getPeople() - carForm.getPeople());
                carService.updCar(car);
            }
            if (carForm.getId() != "") {
                // ?
                Car car = carService.getCarById(carForm.getId());
                car.setPeople(car.getPeople() + carForm.getPeople());
                carService.updCar(car);
            }
            // ??

            order.setCarid(carForm.getId());
            order.setStatus(2);
            orderService.updOrder(order);
            out.print(back);
            out.flush();
            out.close();
        }

    }
}