data.services.CarPropertyService.java Source code

Java tutorial

Introduction

Here is the source code for data.services.CarPropertyService.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 data.services;

import data.dao.CarPropertyDao;
import data.entity.CarProperty;
import data.services.parent.PrimService;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
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.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import support.StringAdapter;

/**
 *
 * @author bezdatiuzer
 */
@Service
@Transactional
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class CarPropertyService extends PrimService {

    @Autowired
    private CarPropertyDao carPropertyDao;
    @Autowired
    private BaseParamService baseParamService;

    public void create(CarProperty cp) throws Exception {
        CarProperty supcp = new CarProperty();
        supcp.setOldId(cp.getOldId());
        if (carPropertyDao.find(supcp).isEmpty()) {
            if (validate(cp)) {
                carPropertyDao.save(cp);
            }
        } else {
            addError(" ?    quto  ??");
        }
    }

    public List<CarProperty> getAllProps() {
        List<CarProperty> pl = carPropertyDao.getAllAsc("uid");
        List<CarProperty> res = new ArrayList();
        for (CarProperty cp : pl) {
            if (cp.getUid() != null && !cp.getUid().equals("") && baseParamService.checkUid(cp.getUid())) {
                res.add(cp);
            }
        }
        return res;
    }

    public List<CarProperty> getAllPropsForShow() {
        List<CarProperty> pl = carPropertyDao.getAllAsc("uid");
        List<CarProperty> res = new ArrayList();
        for (CarProperty cp : pl) {
            if (cp.getUid() != null && !cp.getUid().equals("") && baseParamService.checkUid(cp.getUid())) {
                cp.setUid(baseParamService.uidSpaceFiltr(cp.getUid()));
                res.add(cp);
            }
        }
        return res;
    }

    public void update(Long cpId, String uid, String paramValue, Long percentValue, String radical) {
        if (baseParamService.checkUid(uid) || uid.equals("")) {
            CarProperty cp = carPropertyDao.find(cpId);
            if (cp != null) {
                cp.setUid(uid);
                cp.setParamValue(StringAdapter.toDouble(paramValue));
                cp.setPercentValue(percentValue);
                cp.setRadical(radical);
                if (validate(cp)) {
                    carPropertyDao.update(cp);
                }
            } else {
                addError("? ?  : " + cpId);
            }
        } else {
            addError("     ? " + uid);
        }
    }

    public CarProperty findByOldId(Long oldId) {
        CarProperty supccg = new CarProperty();
        supccg.setOldId(oldId);
        List<CarProperty> list = carPropertyDao.find(supccg);
        if (!list.isEmpty()) {
            if (list.size() == 1) {
                return list.get(0);
            } else {
                addError("       quto");
            }
        } else {
            addError("  ");
        }
        return null;
    }

    public CarProperty find(Long propId) {
        return carPropertyDao.find(propId);
    }

    public HSSFWorkbook getXls() {
        Logger log = Logger.getLogger(this.getClass());
        try {
            HSSFWorkbook workbook = new HSSFWorkbook();
            List<CarProperty> oblist = carPropertyDao.getAllAsc();
            HSSFSheet sheet = workbook.createSheet("FirsSheet");
            HSSFRow rowhead = sheet.createRow((short) 0);
            rowhead.createCell(0).setCellValue("ID");
            rowhead.createCell(1).setCellValue("TITLE");
            rowhead.createCell(2).setCellValue("");
            rowhead.createCell(3).setCellValue("UID");
            rowhead.createCell(4).setCellValue("");
            rowhead.createCell(5).setCellValue("");
            rowhead.createCell(6).setCellValue("");
            int n = 1;
            if (!oblist.isEmpty()) {
                for (CarProperty ob : oblist) {
                    HSSFRow rowbody = sheet.createRow((short) n);
                    rowbody.createCell(0).setCellValue(StringAdapter.getString(ob.getCpId()));
                    rowbody.createCell(1).setCellValue(StringAdapter.getString(ob.getTitle()));
                    rowbody.createCell(2).setCellValue(StringAdapter.getString(ob.getCPG().getTitle()));
                    rowbody.createCell(3).setCellValue(StringAdapter.getString(ob.getUid()));
                    rowbody.createCell(4)
                            .setCellValue(StringAdapter.getString(ob.getParamValue()).replace(".", ","));
                    rowbody.createCell(5).setCellValue(StringAdapter.getString(ob.getPercentValue()));
                    rowbody.createCell(6).setCellValue(StringAdapter.getString(ob.getRadical()));
                    n++;
                }
                ;
            }
            return workbook;
        } catch (Exception e) {
            log.warn("HSSFWorkbook.getXls()", e);
        }
        return null;
    }

    public void updateFromXml(File fl) {
        try {
            FileInputStream fi = new FileInputStream(fl);
            try {
                HSSFWorkbook workbook = new HSSFWorkbook(fi);
                HSSFSheet sheet = workbook.getSheetAt(0);
                Iterator<Row> it = sheet.iterator();
                while (it.hasNext()) {
                    Row row = it.next();
                    Long id = StringAdapter.toLong(StringAdapter.HSSFSellValue(row.getCell(0)));
                    Cell cuid = row.getCell(3);
                    String uid = "";
                    if (cuid != null) {
                        uid = StringAdapter.HSSFSellValue(cuid);
                    }
                    Cell valc = row.getCell(4);
                    Double val = (double) 0;
                    if (valc != null) {
                        String valstr = StringAdapter.HSSFSellValue(valc).replace(",", ".").trim();
                        val = StringAdapter.toDouble(valstr);
                    }

                    Cell percc = row.getCell(5);
                    Long perc = (long) 0;
                    if (percc != null) {
                        String percstr = StringAdapter.HSSFSellValue(percc);
                        if (percstr.contains(".")) {
                            int point = percstr.indexOf(".");
                            percstr = percstr.substring(0, point);
                        }
                        if (!percstr.equals("")) {
                            perc = StringAdapter.toLong(percstr);
                        }
                    }

                    Cell crad = row.getCell(6);
                    String rad = "";
                    if (crad != null) {
                        rad = StringAdapter.HSSFSellValue(crad);
                        if (rad.contains(".")) {
                            int point = rad.indexOf(".");
                            rad = rad.substring(0, point);
                        }
                        if (rad.trim().equals("0")) {
                            rad = "";
                        } else {
                            rad = rad.replace(" ", "");
                        }
                    }

                    /*String val=StringAdapter.HSSFSellValue(row.getCell(4));
                     String pers=StringAdapter.HSSFSellValue(row.getCell(5));
                     String rad=StringAdapter.HSSFSellValue(row.getCell(6));*/
                    CarProperty cl = carPropertyDao.find(id);
                    if (cl != null) {
                        cl.setUid(uid);
                        cl.setParamValue(val);
                        cl.setPercentValue(perc);
                        cl.setRadical(rad);
                        if (validate(cl)) {
                            carPropertyDao.update(cl);
                        }
                    }
                }
                workbook.close();
            } catch (Exception e) {
                addError(StringAdapter.getStackTraceException(e));
            }
            fi.close();
        } catch (Exception e) {
            addError("  xml");
            addError(StringAdapter.getStackTraceException(e));
        }
    }
}